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

Commit

Permalink
Merge pull request #1002 from igor-ivanov/pr/v1.x/issue-1336
Browse files Browse the repository at this point in the history
v1.10: Move Memory Allocation Hooks usage from openib
  • Loading branch information
rhc54 committed Mar 22, 2016
2 parents 1276ddc + 9baa314 commit 0e9c4fc
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 145 deletions.
5 changes: 0 additions & 5 deletions ompi/mca/btl/openib/btl_openib.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,6 @@ struct mca_btl_openib_component_t {
#if BTL_OPENIB_FAILOVER_ENABLED
int verbose_failover;
#endif
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
int use_memalign;
size_t memalign_threshold;
void* (*previous_malloc_hook)(size_t __size, const void*);
#endif
#if OPAL_CUDA_SUPPORT
bool cuda_async_send;
bool cuda_async_recv;
Expand Down
77 changes: 5 additions & 72 deletions ompi/mca/btl/openib/btl_openib_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2006-2013 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2006-2009 Mellanox Technologies. All rights reserved.
* Copyright (c) 2006-2016 Mellanox Technologies. All rights reserved.
* Copyright (c) 2006-2013 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2006-2007 Voltaire All rights reserved.
* Copyright (c) 2009-2012 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011-2014 NVIDIA Corporation. All rights reserved.
* Copyright (c) 2012 Oak Ridge National Laboratory. All rights reserved
* Copyright (c) 2013 Intel, Inc. All rights reserved
* Copyright (c) 2014-2015 Research Organization for Information Science
* Copyright (c) 2014-2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2014 Bull SAS. All rights reserved.
* $COPYRIGHT$
Expand All @@ -42,22 +42,8 @@
#include <fcntl.h>
#include <stdlib.h>
#include <stddef.h>
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/*
* The include of malloc.h below breaks abstractions in OMPI (by
* directly including a header file from another component), but has
* been ruled "ok" because the openib component is only supported on
* Linux.
*
* The malloc hooks in newer glibc were deprecated, including stock
* malloc.h causes compilation warnings. Instead, we use the internal
* linux component malloc.h which does not cause these warnings.
* Internally, OMPI uses the built-in ptmalloc from the linux memory
* component anyway.
*/
#include "opal/mca/memory/linux/malloc.h"
#endif

#include "opal/mca/memory/memory.h"
#include "opal/mca/event/event.h"
#include "opal/align.h"
#include "opal/util/output.h"
Expand Down Expand Up @@ -129,7 +115,6 @@ static void btl_openib_handle_incoming_completion(mca_btl_base_module_t* btl,
* Local variables
*/
static mca_btl_openib_device_t *receive_queues_device = NULL;
static bool malloc_hook_set = false;
static int num_devices_intentionally_ignored = 0;

mca_btl_openib_component_t mca_btl_openib_component = {
Expand Down Expand Up @@ -159,30 +144,6 @@ mca_btl_openib_component_t mca_btl_openib_component = {
}
};

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/* This is a memory allocator hook. The purpose of this is to make
* every malloc aligned since this speeds up IB HCA work.
* There two basic cases here:
*
* 1. Memory manager for Open MPI is enabled. Then memalign below will
* be overridden by __memalign_hook which is set to
* opal_memory_linux_memalign_hook. Thus, _malloc_hook is going to
* use opal_memory_linux_memalign_hook.
*
* 2. No memory manager support. The memalign below is just regular glibc
* memalign which will be called through __malloc_hook instead of malloc.
*/
static void *btl_openib_malloc_hook(size_t sz, const void* caller)
{
if (sz < mca_btl_openib_component.memalign_threshold &&
malloc_hook_set) {
return mca_btl_openib_component.previous_malloc_hook(sz, caller);
} else {
return memalign(mca_btl_openib_component.use_memalign, sz);
}
}
#endif

static int btl_openib_component_register(void)
{
int ret;
Expand Down Expand Up @@ -287,16 +248,6 @@ static int btl_openib_component_close(void)
if (NULL != mca_btl_openib_component.default_recv_qps) {
free(mca_btl_openib_component.default_recv_qps);
}

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/* Must check to see whether the malloc hook was set before
assigning it back because ompi_info will call _register() and
then _close() (which won't set the hook) */
if (malloc_hook_set) {
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
malloc_hook_set = false;
}
#endif

/* close memory registration debugging output */
opal_output_close (mca_btl_openib_component.memory_registration_verbose);
Expand Down Expand Up @@ -2461,19 +2412,8 @@ btl_openib_component_init(int *num_btl_modules,
*num_btl_modules = 0;
num_devs = 0;

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/* If we got this far, then setup the memory alloc hook (because
we're most likely going to be using this component). The hook
is to be set up as early as possible in this function since we
want most of the allocated resources be aligned.*/
if (mca_btl_openib_component.use_memalign > 0 &&
(opal_mem_hooks_support_level() &
(OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_CHUNK_SUPPORT)) != 0) {
mca_btl_openib_component.previous_malloc_hook = __malloc_hook;
__malloc_hook = btl_openib_malloc_hook;
malloc_hook_set = true;
}
#endif
opal_memory->memoryc_set_alignment(32, mca_btl_openib_module.super.btl_eager_limit);

/* Currently refuse to run if MPI_THREAD_MULTIPLE is enabled */
if (ompi_mpi_thread_multiple && !mca_btl_base_thread_multiple_override) {
opal_output_verbose(5, ompi_btl_base_framework.framework_output,
Expand Down Expand Up @@ -2894,13 +2834,6 @@ btl_openib_component_init(int *num_btl_modules,

mca_btl_openib_component.ib_num_btls = 0;
btl_openib_modex_send();
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/*Unset malloc hook since the component won't start*/
if (malloc_hook_set) {
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
malloc_hook_set = false;
}
#endif
return NULL;
}

Expand Down
43 changes: 12 additions & 31 deletions ompi/mca/btl/openib/btl_openib_mca.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,26 +703,18 @@ int btl_openib_register_mca_params(void)
0, &mca_btl_openib_component.gid_index,
REGINT_GE_ZERO));

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
CHECK(reg_int("memalign", NULL,
"[64 | 32 | 0] - Enable (64bit or 32bit)/Disable(0) memory"
"alignment for all malloc calls if btl openib is used.",
32, &mca_btl_openib_component.use_memalign,
REGINT_GE_ZERO));

mca_btl_openib_component.memalign_threshold =
mca_btl_openib_module.super.btl_eager_limit;
tmp = mca_base_component_var_register(&mca_btl_openib_component.super.btl_version,
"memalign_threshold",
"Allocating memory more than btl_openib_memalign_threshhold"
"bytes will automatically be aligned to the value of btl_openib_memalign bytes."
"memalign_threshhold defaults to the same value as mca_btl_openib_eager_limit.",
MCA_BASE_VAR_TYPE_SIZE_T, NULL, 0, 0,
OPAL_INFO_LVL_9,
MCA_BASE_VAR_SCOPE_READONLY,
&mca_btl_openib_component.memalign_threshold);
if (0 > tmp) ret = tmp;
#endif
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
tmp = mca_base_var_find ("opal", "memory", "linux", "memalign");
if (0 <= tmp) {
(void) mca_base_var_register_synonym(tmp, "ompi", "btl", "openib", "memalign",
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
}
tmp = mca_base_var_find ("opal", "memory", "linux", "memalign_threshold");
if (0 <= tmp) {
(void) mca_base_var_register_synonym(tmp, "opal", "btl", "openib", "memalign_threshold",
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
}
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */

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

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
if (mca_btl_openib_component.use_memalign != 32
&& mca_btl_openib_component.use_memalign != 64
&& mca_btl_openib_component.use_memalign != 0){
opal_show_help("help-mpi-btl-openib.txt", "invalid mca param value",
true, "Wrong btl_openib_memalign parameter value. Allowed values: 64, 32, 0.",
"btl_openib_memalign is reset to 32");
mca_btl_openib_component.use_memalign = 32;
}
#endif

return OMPI_SUCCESS;
}
23 changes: 0 additions & 23 deletions ompi/mca/btl/openib/configure.m4
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,6 @@ AC_DEFUN([MCA_ompi_btl_openib_CONFIG],[
[enable openib BTL failover])
AM_CONDITIONAL([MCA_btl_openib_enable_failover], [test "x$btl_openib_failover_enabled" = "x1"])


# Check for __malloc_hook availability
AC_ARG_ENABLE(btl-openib-malloc-alignment,
AC_HELP_STRING([--enable-btl-openib-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))

btl_openib_malloc_hooks_enabled=0
AS_IF([test "$enable_btl_openib_malloc_alignment" != "no"],
[AC_CHECK_HEADER([malloc.h],
[AC_CHECK_FUNC([__malloc_hook],
[AC_CHECK_FUNC([__realloc_hook],
[AC_CHECK_FUNC([__free_hook],
[btl_openib_malloc_hooks_enabled=1])])])])])

AS_IF([test "$enable_btl_openib_malloc_alignment" = "yes" -a "$btl_openib_malloc_hooks_enabled" = "0"],
[AC_MSG_ERROR([openib malloc alignment is requested but __malloc_hook is not available])])
AC_MSG_CHECKING([whether the openib BTL will use malloc hooks])
AS_IF([test "$btl_openib_malloc_hooks_enabled" = "0"],
[AC_MSG_RESULT([no])],
[AC_MSG_RESULT([yes])])

AC_DEFINE_UNQUOTED(BTL_OPENIB_MALLOC_HOOKS_ENABLED, [$btl_openib_malloc_hooks_enabled],
[Whether the openib BTL malloc hooks are enabled])

# make sure that CUDA-aware checks have been done
AC_REQUIRE([OPAL_CHECK_CUDA])

Expand Down
2 changes: 1 addition & 1 deletion ompi/runtime/ompi_mpi_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
from here, since any MPI code is going to call MPI_Init... */
OPAL_DECLSPEC void (*__malloc_initialize_hook) (void) =
opal_memory_linux_malloc_init_hook;
#endif
#endif /* defined(MEMORY_LINUX_PTMALLOC2) && MEMORY_LINUX_PTMALLOC2 */

/* This is required for the boundaries of the hash tables used to store
* the F90 types returned by the MPI_Type_create_f90_XXX functions.
Expand Down
13 changes: 12 additions & 1 deletion opal/mca/memory/base/empty.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand Down Expand Up @@ -51,6 +53,15 @@ OPAL_DECLSPEC int opal_memory_base_component_deregister_empty(void *start,
size_t len,
uint64_t cookie);

/**
* Default (empty) implementation of the memoryc_set_alignment function
*
* See opal/mca/memory/memory.h for a description of the parameters.
*/
OPAL_DECLSPEC void opal_memory_base_component_set_alignment_empty(int use_memalign,
size_t memalign_threshold);


END_C_DECLS

#endif
8 changes: 8 additions & 0 deletions opal/mca/memory/base/memory_base_empty.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand All @@ -34,3 +36,9 @@ int opal_memory_base_component_deregister_empty(void *base, size_t len,
{
return OPAL_SUCCESS;
}

void opal_memory_base_component_set_alignment_empty(int use_memalign,
size_t memalign_threshold)
{
}

3 changes: 3 additions & 0 deletions opal/mca/memory/base/memory_base_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2016 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
Expand Down Expand Up @@ -54,6 +56,7 @@ static opal_memory_base_component_2_0_0_t empty_component = {
empty_process,
opal_memory_base_component_register_empty,
opal_memory_base_component_deregister_empty,
opal_memory_base_component_set_alignment_empty,
};


Expand Down
36 changes: 36 additions & 0 deletions opal/mca/memory/linux/configure.m4
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ AC_DEFUN([MCA_opal_memory_linux_CONFIG],[
[memory_linux_ptmalloc2_happy=no
memory_linux_ummu_happy=no])])


######################################################################
# if memory hook available
######################################################################
memory_hook_found=1
AS_IF([test "$memory_hook_found" -eq 1],
[memory_hook_found=0 AC_CHECK_HEADER([malloc.h],
[AC_CHECK_FUNC([__malloc_initialize_hook],
[AC_CHECK_FUNC([__malloc_hook],
[AC_CHECK_FUNC([__realloc_hook],
[AC_CHECK_FUNC([__free_hook],
[memory_hook_found=1])])])])])])
AC_MSG_CHECKING([whether the system can use malloc hooks])
AS_IF([test "$memory_hook_found" = "0"],
[AC_MSG_RESULT([no])],
[AC_MSG_RESULT([yes])])
AC_DEFINE_UNQUOTED([MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT], [$memory_hook_found],
[Whether the system has Memory Allocation Hooks])

AC_ARG_ENABLE(memory-linux-malloc-alignment,
AC_HELP_STRING([--enable-memory-linux-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))

malloc_align_enabled=0
AS_IF([test "$enable_memory_linux_malloc_alignment" != "no"],
[malloc_align_enabled=$memory_hook_found])

AS_IF([test "$enable_memory_linux_malloc_alignment" = "yes" && test "$malloc_align_enabled" = "0"],
[AC_MSG_ERROR([memory linux malloc alignment is requested but __malloc_hook is not available])])
AC_MSG_CHECKING([whether the memory linux will use malloc alignment])
AS_IF([test "$malloc_align_enabled" = "0"],
[AC_MSG_RESULT([no])],
[AC_MSG_RESULT([yes])])

AC_DEFINE_UNQUOTED(MEMORY_LINUX_MALLOC_ALIGN_ENABLED, [$malloc_align_enabled],
[Whether the memory linux malloc alignment is enabled])

######################################################################
# ptmalloc2
######################################################################
Expand Down
6 changes: 5 additions & 1 deletion opal/mca/memory/linux/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "opal/mca/memory/memory.h"
#include "opal/util/show_help.h"
#include "opal/constants.h"
#include "opal/memoryhooks/memory.h"

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

Expand Down Expand Up @@ -735,7 +736,10 @@ static check_result_t check(const char *name)
}
}

/* OMPI's init function */

/* This function is called on loading libmpi in case system has Memory Allocation Hooks
* (see ompi/runtime/ompi_mpi_init.c for details)
*/
void opal_memory_linux_malloc_init_hook(void)
{
check_result_t r1, lp, lpp;
Expand Down
14 changes: 13 additions & 1 deletion opal/mca/memory/linux/memory_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ typedef struct opal_memory_linux_component_t {
int ummunotify_fd;
#endif

#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
int use_memalign;
size_t memalign_threshold;
#endif

#if MEMORY_LINUX_PTMALLOC2
/* Ptmalloc2-specific data. Note that these variables are all marked as volatile.
* This is needed because of what may be a buggy optimization in the GCC 4.9.2
Expand Down Expand Up @@ -64,13 +69,20 @@ int opal_memory_linux_ummunotify_close(void);
/* memory_linux_ptmalloc2.c */
int opal_memory_linux_ptmalloc2_open(void);
int opal_memory_linux_ptmalloc2_close(void);
OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);

/* memory_linux_munmap.c */
OPAL_DECLSPEC int opal_memory_linux_free_ptmalloc2_munmap(void *start, size_t length, int from_alloc);
OPAL_DECLSPEC int munmap(void* addr, size_t len);
#endif /* !MEMORY_LINUX_PTMALLOC2 */

#if MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT
OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);
#endif /* MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT */

#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
OPAL_DECLSPEC void opal_memory_linux_malloc_set_alignment(int use_memalign, size_t memalign_threshold);
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */

END_C_DECLS

#endif
Loading

0 comments on commit 0e9c4fc

Please sign in to comment.