Skip to content

Commit aa8be9c

Browse files
authored
Merge pull request #6284 from devreal/ompi-rdma-memalign
Ensure proper alignment of memory provided by MPI
2 parents ecd990a + c385c92 commit aa8be9c

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

ompi/mca/osc/rdma/osc_rdma_component.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ static int allocate_state_single (ompi_osc_rdma_module_t *module, void **base, s
430430
* registration handles needed to access this data. */
431431
total_size = local_rank_array_size + module->region_size +
432432
module->state_size + leader_peer_data_size;
433+
total_size += OPAL_ALIGN_PAD_AMOUNT(total_size, OPAL_ALIGN_MIN);
433434

434435
if (MPI_WIN_FLAVOR_ALLOCATE == module->flavor) {
435436
total_size += size;
@@ -572,6 +573,12 @@ static int allocate_state_shared (ompi_osc_rdma_module_t *module, void **base, s
572573
module->state_offset = state_base = local_rank_array_size + module->region_size;
573574
data_base = state_base + leader_peer_data_size + module->state_size * local_size;
574575

576+
/* ensure proper alignment */
577+
data_base += OPAL_ALIGN_PAD_AMOUNT(data_base, OPAL_ALIGN_MIN);
578+
if (MPI_WIN_FLAVOR_ALLOCATE == module->flavor) {
579+
size += OPAL_ALIGN_PAD_AMOUNT(size, OPAL_ALIGN_MIN);
580+
}
581+
575582
do {
576583
temp = calloc (local_size, sizeof (temp[0]));
577584
if (NULL == temp) {

opal/include/opal/align.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@
2828
#define OPAL_ALIGN_PTR(x,a,t) ((t)OPAL_ALIGN((uintptr_t)x, a, uintptr_t))
2929
#define OPAL_ALIGN_PAD_AMOUNT(x,s) ((~((uintptr_t)(x))+1) & ((uintptr_t)(s)-1))
3030

31+
#if __STDC_VERSION__ >= 201101L
32+
#include <stddef.h>
33+
#define OPAL_ALIGN_MIN (_Alignof(max_align_t))
34+
#else
35+
#define OPAL_ALIGN_MIN (sizeof(long double))
36+
#endif /* __STDC_VERSION__ >= 201101L */
37+
3138
#endif /* OPAL_ALIGN_H */

opal/mca/mpool/base/mpool_base_alloc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mpool_base_tree.h"
3232
#include "opal/threads/mutex.h"
3333
#include "opal/util/info.h"
34+
#include "opal/align.h"
3435

3536

3637
static void unregister_tree_item(mca_mpool_base_tree_item_t *mpool_tree_item)
@@ -76,7 +77,7 @@ void *mca_mpool_base_alloc(size_t size, opal_info_t *info, const char *hints)
7677

7778
mpool = mca_mpool_base_module_lookup (hints);
7879
if (NULL != mpool) {
79-
mem = mpool->mpool_alloc (mpool, size, sizeof(void *), 0);
80+
mem = mpool->mpool_alloc (mpool, size, OPAL_ALIGN_MIN, 0);
8081
}
8182

8283
if (NULL == mem) {

opal/mca/shmem/shmem_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define OPAL_SHMEM_TYPES_H
3333

3434
#include "opal_config.h"
35+
#include "opal/align.h"
3536

3637
#include <stddef.h>
3738
#include <string.h>
@@ -103,7 +104,7 @@ struct opal_shmem_seg_hdr_t {
103104
opal_atomic_lock_t lock;
104105
/* pid of the segment creator */
105106
pid_t cpid;
106-
};
107+
} __opal_attribute_aligned__(OPAL_ALIGN_MIN);
107108
typedef struct opal_shmem_seg_hdr_t opal_shmem_seg_hdr_t;
108109

109110
struct opal_shmem_ds_t {

test/mpool/mpool_memkind.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "opal_config.h"
3030
#ifdef HAVE_MEMKIND_H
3131
#include "opal/constants.h"
32+
#include "opal/align.h"
3233
#include "opal/mca/mpool/mpool.h"
3334
#include "opal/include/opal/frameworks.h"
3435
#include "opal/runtime/opal.h"
@@ -99,6 +100,11 @@ int main (int argc, char* argv[])
99100
goto error;
100101
}
101102

103+
if (0 != ((uintptr_t)ptr % OPAL_ALIGN_MIN)) {
104+
error = "improper memory alignment detected";
105+
goto error;
106+
}
107+
102108
/*
103109
* now try policies
104110
*/
@@ -123,6 +129,11 @@ int main (int argc, char* argv[])
123129
error = "mca_mpool_base_free() failed";
124130
goto error;
125131
}
132+
133+
if (0 != ((uintptr_t)ptr % OPAL_ALIGN_MIN)) {
134+
error = "improper memory alignment detected";
135+
goto error;
136+
}
126137
mk_ptr++;
127138
}
128139
mt_ptr++;

0 commit comments

Comments
 (0)