Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 0e9c4fc

Browse files
author
rhc54
committed
Merge pull request #1002 from igor-ivanov/pr/v1.x/issue-1336
v1.10: Move Memory Allocation Hooks usage from openib
2 parents 1276ddc + 9baa314 commit 0e9c4fc

File tree

14 files changed

+262
-145
lines changed

14 files changed

+262
-145
lines changed

ompi/mca/btl/openib/btl_openib.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,6 @@ struct mca_btl_openib_component_t {
310310
#if BTL_OPENIB_FAILOVER_ENABLED
311311
int verbose_failover;
312312
#endif
313-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
314-
int use_memalign;
315-
size_t memalign_threshold;
316-
void* (*previous_malloc_hook)(size_t __size, const void*);
317-
#endif
318313
#if OPAL_CUDA_SUPPORT
319314
bool cuda_async_send;
320315
bool cuda_async_recv;

ompi/mca/btl/openib/btl_openib_component.c

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
* Copyright (c) 2004-2005 The Regents of the University of California.
1212
* All rights reserved.
1313
* Copyright (c) 2006-2013 Cisco Systems, Inc. All rights reserved.
14-
* Copyright (c) 2006-2009 Mellanox Technologies. All rights reserved.
14+
* Copyright (c) 2006-2016 Mellanox Technologies. All rights reserved.
1515
* Copyright (c) 2006-2013 Los Alamos National Security, LLC. All rights
1616
* reserved.
1717
* Copyright (c) 2006-2007 Voltaire All rights reserved.
1818
* Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
1919
* Copyright (c) 2011-2014 NVIDIA Corporation. All rights reserved.
2020
* Copyright (c) 2012 Oak Ridge National Laboratory. All rights reserved
2121
* Copyright (c) 2013 Intel, Inc. All rights reserved
22-
* Copyright (c) 2014-2015 Research Organization for Information Science
22+
* Copyright (c) 2014-2016 Research Organization for Information Science
2323
* and Technology (RIST). All rights reserved.
2424
* Copyright (c) 2014 Bull SAS. All rights reserved.
2525
* $COPYRIGHT$
@@ -42,22 +42,8 @@
4242
#include <fcntl.h>
4343
#include <stdlib.h>
4444
#include <stddef.h>
45-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
46-
/*
47-
* The include of malloc.h below breaks abstractions in OMPI (by
48-
* directly including a header file from another component), but has
49-
* been ruled "ok" because the openib component is only supported on
50-
* Linux.
51-
*
52-
* The malloc hooks in newer glibc were deprecated, including stock
53-
* malloc.h causes compilation warnings. Instead, we use the internal
54-
* linux component malloc.h which does not cause these warnings.
55-
* Internally, OMPI uses the built-in ptmalloc from the linux memory
56-
* component anyway.
57-
*/
58-
#include "opal/mca/memory/linux/malloc.h"
59-
#endif
6045

46+
#include "opal/mca/memory/memory.h"
6147
#include "opal/mca/event/event.h"
6248
#include "opal/align.h"
6349
#include "opal/util/output.h"
@@ -129,7 +115,6 @@ static void btl_openib_handle_incoming_completion(mca_btl_base_module_t* btl,
129115
* Local variables
130116
*/
131117
static mca_btl_openib_device_t *receive_queues_device = NULL;
132-
static bool malloc_hook_set = false;
133118
static int num_devices_intentionally_ignored = 0;
134119

135120
mca_btl_openib_component_t mca_btl_openib_component = {
@@ -159,30 +144,6 @@ mca_btl_openib_component_t mca_btl_openib_component = {
159144
}
160145
};
161146

162-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
163-
/* This is a memory allocator hook. The purpose of this is to make
164-
* every malloc aligned since this speeds up IB HCA work.
165-
* There two basic cases here:
166-
*
167-
* 1. Memory manager for Open MPI is enabled. Then memalign below will
168-
* be overridden by __memalign_hook which is set to
169-
* opal_memory_linux_memalign_hook. Thus, _malloc_hook is going to
170-
* use opal_memory_linux_memalign_hook.
171-
*
172-
* 2. No memory manager support. The memalign below is just regular glibc
173-
* memalign which will be called through __malloc_hook instead of malloc.
174-
*/
175-
static void *btl_openib_malloc_hook(size_t sz, const void* caller)
176-
{
177-
if (sz < mca_btl_openib_component.memalign_threshold &&
178-
malloc_hook_set) {
179-
return mca_btl_openib_component.previous_malloc_hook(sz, caller);
180-
} else {
181-
return memalign(mca_btl_openib_component.use_memalign, sz);
182-
}
183-
}
184-
#endif
185-
186147
static int btl_openib_component_register(void)
187148
{
188149
int ret;
@@ -287,16 +248,6 @@ static int btl_openib_component_close(void)
287248
if (NULL != mca_btl_openib_component.default_recv_qps) {
288249
free(mca_btl_openib_component.default_recv_qps);
289250
}
290-
291-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
292-
/* Must check to see whether the malloc hook was set before
293-
assigning it back because ompi_info will call _register() and
294-
then _close() (which won't set the hook) */
295-
if (malloc_hook_set) {
296-
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
297-
malloc_hook_set = false;
298-
}
299-
#endif
300251

301252
/* close memory registration debugging output */
302253
opal_output_close (mca_btl_openib_component.memory_registration_verbose);
@@ -2461,19 +2412,8 @@ btl_openib_component_init(int *num_btl_modules,
24612412
*num_btl_modules = 0;
24622413
num_devs = 0;
24632414

2464-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
2465-
/* If we got this far, then setup the memory alloc hook (because
2466-
we're most likely going to be using this component). The hook
2467-
is to be set up as early as possible in this function since we
2468-
want most of the allocated resources be aligned.*/
2469-
if (mca_btl_openib_component.use_memalign > 0 &&
2470-
(opal_mem_hooks_support_level() &
2471-
(OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_CHUNK_SUPPORT)) != 0) {
2472-
mca_btl_openib_component.previous_malloc_hook = __malloc_hook;
2473-
__malloc_hook = btl_openib_malloc_hook;
2474-
malloc_hook_set = true;
2475-
}
2476-
#endif
2415+
opal_memory->memoryc_set_alignment(32, mca_btl_openib_module.super.btl_eager_limit);
2416+
24772417
/* Currently refuse to run if MPI_THREAD_MULTIPLE is enabled */
24782418
if (ompi_mpi_thread_multiple && !mca_btl_base_thread_multiple_override) {
24792419
opal_output_verbose(5, ompi_btl_base_framework.framework_output,
@@ -2894,13 +2834,6 @@ btl_openib_component_init(int *num_btl_modules,
28942834

28952835
mca_btl_openib_component.ib_num_btls = 0;
28962836
btl_openib_modex_send();
2897-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
2898-
/*Unset malloc hook since the component won't start*/
2899-
if (malloc_hook_set) {
2900-
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
2901-
malloc_hook_set = false;
2902-
}
2903-
#endif
29042837
return NULL;
29052838
}
29062839

ompi/mca/btl/openib/btl_openib_mca.c

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -703,26 +703,18 @@ int btl_openib_register_mca_params(void)
703703
0, &mca_btl_openib_component.gid_index,
704704
REGINT_GE_ZERO));
705705

706-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
707-
CHECK(reg_int("memalign", NULL,
708-
"[64 | 32 | 0] - Enable (64bit or 32bit)/Disable(0) memory"
709-
"alignment for all malloc calls if btl openib is used.",
710-
32, &mca_btl_openib_component.use_memalign,
711-
REGINT_GE_ZERO));
712-
713-
mca_btl_openib_component.memalign_threshold =
714-
mca_btl_openib_module.super.btl_eager_limit;
715-
tmp = mca_base_component_var_register(&mca_btl_openib_component.super.btl_version,
716-
"memalign_threshold",
717-
"Allocating memory more than btl_openib_memalign_threshhold"
718-
"bytes will automatically be aligned to the value of btl_openib_memalign bytes."
719-
"memalign_threshhold defaults to the same value as mca_btl_openib_eager_limit.",
720-
MCA_BASE_VAR_TYPE_SIZE_T, NULL, 0, 0,
721-
OPAL_INFO_LVL_9,
722-
MCA_BASE_VAR_SCOPE_READONLY,
723-
&mca_btl_openib_component.memalign_threshold);
724-
if (0 > tmp) ret = tmp;
725-
#endif
706+
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
707+
tmp = mca_base_var_find ("opal", "memory", "linux", "memalign");
708+
if (0 <= tmp) {
709+
(void) mca_base_var_register_synonym(tmp, "ompi", "btl", "openib", "memalign",
710+
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
711+
}
712+
tmp = mca_base_var_find ("opal", "memory", "linux", "memalign_threshold");
713+
if (0 <= tmp) {
714+
(void) mca_base_var_register_synonym(tmp, "opal", "btl", "openib", "memalign_threshold",
715+
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
716+
}
717+
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
726718

727719
/* Register any MCA params for the connect pseudo-components */
728720
if (OMPI_SUCCESS == ret) {
@@ -816,16 +808,5 @@ int btl_openib_verify_mca_params (void)
816808
}
817809
#endif
818810

819-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
820-
if (mca_btl_openib_component.use_memalign != 32
821-
&& mca_btl_openib_component.use_memalign != 64
822-
&& mca_btl_openib_component.use_memalign != 0){
823-
opal_show_help("help-mpi-btl-openib.txt", "invalid mca param value",
824-
true, "Wrong btl_openib_memalign parameter value. Allowed values: 64, 32, 0.",
825-
"btl_openib_memalign is reset to 32");
826-
mca_btl_openib_component.use_memalign = 32;
827-
}
828-
#endif
829-
830811
return OMPI_SUCCESS;
831812
}

ompi/mca/btl/openib/configure.m4

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,6 @@ AC_DEFUN([MCA_ompi_btl_openib_CONFIG],[
129129
[enable openib BTL failover])
130130
AM_CONDITIONAL([MCA_btl_openib_enable_failover], [test "x$btl_openib_failover_enabled" = "x1"])
131131

132-
133-
# Check for __malloc_hook availability
134-
AC_ARG_ENABLE(btl-openib-malloc-alignment,
135-
AC_HELP_STRING([--enable-btl-openib-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))
136-
137-
btl_openib_malloc_hooks_enabled=0
138-
AS_IF([test "$enable_btl_openib_malloc_alignment" != "no"],
139-
[AC_CHECK_HEADER([malloc.h],
140-
[AC_CHECK_FUNC([__malloc_hook],
141-
[AC_CHECK_FUNC([__realloc_hook],
142-
[AC_CHECK_FUNC([__free_hook],
143-
[btl_openib_malloc_hooks_enabled=1])])])])])
144-
145-
AS_IF([test "$enable_btl_openib_malloc_alignment" = "yes" -a "$btl_openib_malloc_hooks_enabled" = "0"],
146-
[AC_MSG_ERROR([openib malloc alignment is requested but __malloc_hook is not available])])
147-
AC_MSG_CHECKING([whether the openib BTL will use malloc hooks])
148-
AS_IF([test "$btl_openib_malloc_hooks_enabled" = "0"],
149-
[AC_MSG_RESULT([no])],
150-
[AC_MSG_RESULT([yes])])
151-
152-
AC_DEFINE_UNQUOTED(BTL_OPENIB_MALLOC_HOOKS_ENABLED, [$btl_openib_malloc_hooks_enabled],
153-
[Whether the openib BTL malloc hooks are enabled])
154-
155132
# make sure that CUDA-aware checks have been done
156133
AC_REQUIRE([OPAL_CHECK_CUDA])
157134

ompi/runtime/ompi_mpi_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
from here, since any MPI code is going to call MPI_Init... */
104104
OPAL_DECLSPEC void (*__malloc_initialize_hook) (void) =
105105
opal_memory_linux_malloc_init_hook;
106-
#endif
106+
#endif /* defined(MEMORY_LINUX_PTMALLOC2) && MEMORY_LINUX_PTMALLOC2 */
107107

108108
/* This is required for the boundaries of the hash tables used to store
109109
* the F90 types returned by the MPI_Type_create_f90_XXX functions.

opal/mca/memory/base/empty.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
2-
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
2+
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
3+
* Copyright (c) 2016 Research Organization for Information Science
4+
* and Technology (RIST). All rights reserved.
35
* $COPYRIGHT$
46
*
57
* Additional copyrights may follow
@@ -51,6 +53,15 @@ OPAL_DECLSPEC int opal_memory_base_component_deregister_empty(void *start,
5153
size_t len,
5254
uint64_t cookie);
5355

56+
/**
57+
* Default (empty) implementation of the memoryc_set_alignment function
58+
*
59+
* See opal/mca/memory/memory.h for a description of the parameters.
60+
*/
61+
OPAL_DECLSPEC void opal_memory_base_component_set_alignment_empty(int use_memalign,
62+
size_t memalign_threshold);
63+
64+
5465
END_C_DECLS
5566

5667
#endif

opal/mca/memory/base/memory_base_empty.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* University of Stuttgart. All rights reserved.
1010
* Copyright (c) 2004-2005 The Regents of the University of California.
1111
* All rights reserved.
12+
* Copyright (c) 2016 Research Organization for Information Science
13+
* and Technology (RIST). All rights reserved.
1214
* $COPYRIGHT$
1315
*
1416
* Additional copyrights may follow
@@ -34,3 +36,9 @@ int opal_memory_base_component_deregister_empty(void *base, size_t len,
3436
{
3537
return OPAL_SUCCESS;
3638
}
39+
40+
void opal_memory_base_component_set_alignment_empty(int use_memalign,
41+
size_t memalign_threshold)
42+
{
43+
}
44+

opal/mca/memory/base/memory_base_open.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Copyright (c) 2004-2005 The Regents of the University of California.
1212
* All rights reserved.
1313
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
14+
* Copyright (c) 2016 Research Organization for Information Science
15+
* and Technology (RIST). All rights reserved.
1416
* $COPYRIGHT$
1517
*
1618
* Additional copyrights may follow
@@ -54,6 +56,7 @@ static opal_memory_base_component_2_0_0_t empty_component = {
5456
empty_process,
5557
opal_memory_base_component_register_empty,
5658
opal_memory_base_component_deregister_empty,
59+
opal_memory_base_component_set_alignment_empty,
5760
};
5861

5962

opal/mca/memory/linux/configure.m4

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ AC_DEFUN([MCA_opal_memory_linux_CONFIG],[
6161
[memory_linux_ptmalloc2_happy=no
6262
memory_linux_ummu_happy=no])])
6363

64+
65+
######################################################################
66+
# if memory hook available
67+
######################################################################
68+
memory_hook_found=1
69+
AS_IF([test "$memory_hook_found" -eq 1],
70+
[memory_hook_found=0 AC_CHECK_HEADER([malloc.h],
71+
[AC_CHECK_FUNC([__malloc_initialize_hook],
72+
[AC_CHECK_FUNC([__malloc_hook],
73+
[AC_CHECK_FUNC([__realloc_hook],
74+
[AC_CHECK_FUNC([__free_hook],
75+
[memory_hook_found=1])])])])])])
76+
AC_MSG_CHECKING([whether the system can use malloc hooks])
77+
AS_IF([test "$memory_hook_found" = "0"],
78+
[AC_MSG_RESULT([no])],
79+
[AC_MSG_RESULT([yes])])
80+
AC_DEFINE_UNQUOTED([MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT], [$memory_hook_found],
81+
[Whether the system has Memory Allocation Hooks])
82+
83+
AC_ARG_ENABLE(memory-linux-malloc-alignment,
84+
AC_HELP_STRING([--enable-memory-linux-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))
85+
86+
malloc_align_enabled=0
87+
AS_IF([test "$enable_memory_linux_malloc_alignment" != "no"],
88+
[malloc_align_enabled=$memory_hook_found])
89+
90+
AS_IF([test "$enable_memory_linux_malloc_alignment" = "yes" && test "$malloc_align_enabled" = "0"],
91+
[AC_MSG_ERROR([memory linux malloc alignment is requested but __malloc_hook is not available])])
92+
AC_MSG_CHECKING([whether the memory linux will use malloc alignment])
93+
AS_IF([test "$malloc_align_enabled" = "0"],
94+
[AC_MSG_RESULT([no])],
95+
[AC_MSG_RESULT([yes])])
96+
97+
AC_DEFINE_UNQUOTED(MEMORY_LINUX_MALLOC_ALIGN_ENABLED, [$malloc_align_enabled],
98+
[Whether the memory linux malloc alignment is enabled])
99+
64100
######################################################################
65101
# ptmalloc2
66102
######################################################################

opal/mca/memory/linux/hooks.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "opal/mca/memory/memory.h"
3535
#include "opal/util/show_help.h"
3636
#include "opal/constants.h"
37+
#include "opal/memoryhooks/memory.h"
3738

3839
#include "opal/mca/memory/linux/memory_linux.h"
3940

@@ -735,7 +736,10 @@ static check_result_t check(const char *name)
735736
}
736737
}
737738

738-
/* OMPI's init function */
739+
740+
/* This function is called on loading libmpi in case system has Memory Allocation Hooks
741+
* (see ompi/runtime/ompi_mpi_init.c for details)
742+
*/
739743
void opal_memory_linux_malloc_init_hook(void)
740744
{
741745
check_result_t r1, lp, lpp;

0 commit comments

Comments
 (0)