From c0c56fe30a2e22c61e0e196cb13b5de4aec7c295 Mon Sep 17 00:00:00 2001 From: Jeff Hammond Date: Tue, 12 Oct 2021 11:57:34 +0300 Subject: [PATCH] fix shmem_calloc implementation 1. shmem_calloc needs to allocate count*size not size. resolves https://github.com/pmodels/oshmpi/issues/122 2. push calloc impl down into OSHMPI layer. this is a performance optimization. previously, there was a theoretical possibility that some PEs could return before other PEs were done allocating. we could have fixed this by adding a second barrier, but instead we pushed calloc down to OSHMPI layer. a side benefit is that debugging output now exposes calloc calls directly. 3. early return if count or size is zero, as required by OpenSHMEM 1.5 section 9.3.3, which is noted in source code comments. Signed-off-by: Jeff Hammond --- src/include/oshmpi_impl.h | 1 + src/internal/mem_impl.c | 14 ++++++++++++++ src/shmem/mem.c | 10 ++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/include/oshmpi_impl.h b/src/include/oshmpi_impl.h index 8693c5bf..8ad614b9 100644 --- a/src/include/oshmpi_impl.h +++ b/src/include/oshmpi_impl.h @@ -357,6 +357,7 @@ void *OSHMPI_malloc(size_t size); void OSHMPI_free(void *ptr); void *OSHMPI_realloc(void *ptr, size_t size); void *OSHMPI_align(size_t alignment, size_t size); +void *OSHMPI_calloc(size_t count, size_t size); void OSHMPI_strided_initialize(void); void OSHMPI_strided_finalize(void); diff --git a/src/internal/mem_impl.c b/src/internal/mem_impl.c index ad40dda4..6a651c63 100644 --- a/src/internal/mem_impl.c +++ b/src/internal/mem_impl.c @@ -70,3 +70,17 @@ void *OSHMPI_align(size_t alignment, size_t size) OSHMPI_barrier_all(); return ptr; } + +void *OSHMPI_calloc(size_t count, size_t size) +{ + void *ptr = NULL; + + OSHMPI_THREAD_ENTER_CS(&OSHMPI_global.symm_heap_mspace_cs); + ptr = mspace_calloc(OSHMPI_global.symm_heap_mspace, count, size); + OSHMPI_THREAD_EXIT_CS(&OSHMPI_global.symm_heap_mspace_cs); + + OSHMPI_DBGMSG("count %ld, size %ld, ptr %p, disp 0x%lx\n", count, size, ptr, + (MPI_Aint) ptr - (MPI_Aint) OSHMPI_global.symm_heap_attr.base); + OSHMPI_barrier_all(); + return ptr; +} diff --git a/src/shmem/mem.c b/src/shmem/mem.c index 661e706b..d556215e 100644 --- a/src/shmem/mem.c +++ b/src/shmem/mem.c @@ -47,9 +47,15 @@ void *shmem_calloc(size_t count, size_t size) { void *ptr = NULL; + /* OpenSHMEM 1.5 Section 9.3.3: + * When count or size is 0, the shmem_calloc routine returns without performing a barrier. + * Otherwise, this routine calls a procedure that is semantically equivalent to shmem_barrier_all on exit. */ + if ((count == 0) || (size == 0)) { + return ptr; + } + OSHMPI_NOINLINE_RECURSIVE() - ptr = OSHMPI_malloc(size); - memset(ptr, 0, count * size); + ptr = OSHMPI_calloc(count, size); return ptr; }