Skip to content

Commit cd17fee

Browse files
committed
performance fix: openib use memalign for malloc
This commit was SVN r26409.
1 parent 3928a1e commit cd17fee

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

ompi/mca/btl/openib/btl_openib.h

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ struct mca_btl_openib_component_t {
297297
#if BTL_OPENIB_FAILOVER_ENABLED
298298
int verbose_failover;
299299
#endif
300+
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
301+
int use_memalign;
302+
size_t memalign_threshold;
303+
void* (*previous_malloc_hook)(size_t __size, const void*);
304+
#endif
300305
}; typedef struct mca_btl_openib_component_t mca_btl_openib_component_t;
301306

302307
OMPI_MODULE_DECLSPEC extern mca_btl_openib_component_t mca_btl_openib_component;

ompi/mca/btl/openib/btl_openib_component.c

+37
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const char *ibv_get_sysfs_path(void);
4747
#include <sys/stat.h>
4848
#include <stdlib.h>
4949
#include <stddef.h>
50+
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
51+
#include <malloc.h>
52+
#endif
5053

5154
#include "opal/mca/event/event.h"
5255
#include "opal/align.h"
@@ -136,6 +139,26 @@ mca_btl_openib_component_t mca_btl_openib_component = {
136139
}
137140
};
138141

142+
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
143+
/* This is a memory allocator hook. The purpose of this is to make
144+
* every malloc aligned since this speeds up IB HCA work.
145+
* There two basic cases here:
146+
* 1. Memory manager for Open MPI is enabled. Then memalign below will be
147+
* overridden by __memalign_hook which is set to opal_memory_linux_memalign_hook.
148+
* Thus, _malloc_hook is going to use opal_memory_linux_memalign_hook.
149+
* 2. No memory manager support. The memalign below is just regular glibc
150+
* memalign which will be called through __malloc_hook instead of malloc.
151+
*/
152+
static void *btl_openib_malloc_hook(size_t sz, const void* caller)
153+
{
154+
if (sz < mca_btl_openib_component.memalign_threshold) {
155+
return mca_btl_openib_component.previous_malloc_hook(sz, caller);
156+
} else {
157+
return memalign(mca_btl_openib_component.use_memalign, sz);
158+
}
159+
}
160+
#endif
161+
139162
static int btl_openib_component_register(void)
140163
{
141164
int ret;
@@ -162,6 +185,13 @@ static int btl_openib_component_register(void)
162185
return OMPI_ERR_NOT_AVAILABLE;
163186
}
164187

188+
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
189+
mca_btl_openib_component.previous_malloc_hook = __malloc_hook;
190+
if (mca_btl_openib_component.use_memalign > 0) {
191+
__malloc_hook = btl_openib_malloc_hook;
192+
}
193+
#endif
194+
165195
return OMPI_SUCCESS;
166196
}
167197

@@ -178,6 +208,7 @@ static int btl_openib_component_open(void)
178208
OBJ_CONSTRUCT(lock, opal_mutex_t);
179209
OBJ_CONSTRUCT(srq_addr_table, opal_hash_table_t);
180210
#endif
211+
181212
/* initialize state */
182213
mca_btl_openib_component.ib_num_btls = 0;
183214
mca_btl_openib_component.openib_btls = NULL;
@@ -238,6 +269,11 @@ static int btl_openib_component_close(void)
238269
if (NULL != mca_btl_openib_component.default_recv_qps) {
239270
free(mca_btl_openib_component.default_recv_qps);
240271
}
272+
273+
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
274+
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
275+
#endif
276+
241277
return rc;
242278
}
243279

@@ -3829,3 +3865,4 @@ int mca_btl_openib_post_srr(mca_btl_openib_module_t* openib_btl, const int qp)
38293865
OPAL_THREAD_UNLOCK(&openib_btl->ib_lock);
38303866
return OMPI_ERROR;
38313867
}
3868+

ompi/mca/btl/openib/btl_openib_mca.c

+30
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,36 @@ int btl_openib_register_mca_params(void)
608608
0, &mca_btl_openib_component.gid_index,
609609
REGINT_GE_ZERO));
610610

611+
#if OPENIB_MALLOC_HOOKS_ENABLED
612+
CHECK(reg_int("memalign", NULL,
613+
"[64 | 32 | 0] - Enable (64bit or 32bit)/Disable(0) memory"
614+
"alignment for all malloc calls if btl openib is used.",
615+
32, &mca_btl_openib_component.use_memalign,
616+
REGINT_GE_ZERO));
617+
618+
if (mca_btl_openib_component.use_memalign != 32
619+
&& mca_btl_openib_component.use_memalign != 64
620+
&& mca_btl_openib_component.use_memalign != 0){
621+
orte_show_help("help-mpi-btl-openib.txt", "invalid mca param value",
622+
true, "Wrong btl_openib_memalign parameter value. Allowed values: 64, 32, 0.",
623+
"btl_openib_memalign is reset to 32");
624+
mca_btl_openib_component.use_memalign = 32;
625+
}
626+
reg_int("memalign_threshold", NULL,
627+
"Allocating memory more than btl_openib_memalign_threshhold"
628+
"bytes will automatically be algined to the value of btl_openib_memalign bytes."
629+
"memalign_threshhold defaults to the same value as mca_btl_openib_eager_limit.",
630+
mca_btl_openib_component.eager_limit,
631+
&ival,
632+
REGINT_GE_ZERO);
633+
if (ival < 0){
634+
orte_show_help("help-mpi-btl-openib.txt", "invalid mca param value",
635+
true, "btl_openib_memalign_threshold must be positive",
636+
"btl_openib_memalign_threshold is reset to btl_openib_eager_limit");
637+
ival = mca_btl_openib_component.eager_limit;
638+
}
639+
mca_btl_openib_component.memalign_threshold = (size_t)ival;
640+
#endif
611641
/* Register any MCA params for the connect pseudo-components */
612642
if (OMPI_SUCCESS == ret) {
613643
ret = ompi_btl_openib_connect_base_register();

ompi/mca/btl/openib/configure.m4

+24
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ AC_DEFUN([MCA_ompi_btl_openib_CONFIG],[
8181
AC_MSG_RESULT([$cpcs])])
8282

8383
# Enable openib device failover. It is disabled by default.
84+
AC_MSG_CHECKING([whether openib failover is enabled])
8485
AC_ARG_ENABLE([btl-openib-failover],
8586
[AC_HELP_STRING([--enable-btl-openib-failover],
8687
[enable openib BTL failover (default: disabled)])])
@@ -95,6 +96,29 @@ AC_DEFUN([MCA_ompi_btl_openib_CONFIG],[
9596
[enable openib BTL failover])
9697
AM_CONDITIONAL([MCA_btl_openib_enable_failover], [test "x$btl_openib_failover_enabled" = "x1"])
9798

99+
100+
# Check for __malloc_hook availability
101+
AC_ARG_ENABLE(btl-openib-malloc-alignment,
102+
AC_HELP_STRING([--enable-btl-openib-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))
103+
104+
btl_openib_malloc_hooks_enabled=0
105+
AS_IF([test "$enable_btl_openib_malloc_alignment" != "no"],
106+
[AC_CHECK_HEADER([malloc.h],
107+
[AC_CHECK_FUNC([__malloc_hook],
108+
[AC_CHECK_FUNC([__realloc_hook],
109+
[AC_CHECK_FUNC([__free_hook],
110+
[btl_openib_malloc_hooks_enabled=1])])])])])
111+
112+
AS_IF([test "$enable_btl_openib_malloc_alignment" = "yes" -a "$btl_openib_malloc_hooks_enabled" = "0"],
113+
[AC_MSG_ERROR([openib malloc alignment is requested but __malloc_hook is not available])])
114+
AC_MSG_CHECKING([whether the openib BTL will use malloc hooks])
115+
AS_IF([test "$btl_openib_malloc_hooks_enabled" = "0"],
116+
[AC_MSG_RESULT([no])],
117+
[AC_MSG_RESULT([yes])])
118+
119+
AC_DEFINE_UNQUOTED(BTL_OPENIB_MALLOC_HOOKS_ENABLED, [$btl_openib_malloc_hooks_enabled],
120+
[Whether the openib BTL malloc hooks are enabled])
121+
98122
# substitute in the things needed to build openib
99123
AC_SUBST([btl_openib_CFLAGS])
100124
AC_SUBST([btl_openib_CPPFLAGS])

0 commit comments

Comments
 (0)