Skip to content

Commit

Permalink
fix shmem_calloc implementation
Browse files Browse the repository at this point in the history
1. shmem_calloc needs to allocate count*size not size.
   resolves #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 <[email protected]>
  • Loading branch information
jeffhammond authored and yfguo committed Jun 10, 2022
1 parent 8c112d8 commit c0c56fe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/include/oshmpi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/internal/mem_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 8 additions & 2 deletions src/shmem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c0c56fe

Please sign in to comment.