Skip to content

Commit

Permalink
Replace posix_memalign with std::aligned_alloc (BlueBrain/CoreNeuron#859
Browse files Browse the repository at this point in the history
)

* Replace posix_memalign with std::aligned_alloc

Unfortunately aligned_alloc does not allow aligning buffers that are not
a multiple of the alignment. To help with this, we allocate slightly
larger buffers (up to the next multiple of the alignment) where necessary.

CoreNEURON Repo SHA: BlueBrain/CoreNeuron@414479d
  • Loading branch information
Omar Awile authored Sep 19, 2022
1 parent 8ef874f commit 55a15bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/coreneuron/utils/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ class MemoryManaged {
// does nothing by default
};

#include <stdlib.h>
#include <cstdlib>

inline void alloc_memory(void*& pointer, size_t num_bytes, size_t alignment) {
#if defined(MINGW)
nrn_assert((pointer = _aligned_malloc(num_bytes, alignment)) != nullptr);
#else
nrn_assert(posix_memalign(&pointer, alignment, num_bytes) == 0);
#endif
size_t fill = 0;
if (num_bytes % alignment != 0) {
size_t multiple = num_bytes / alignment;
fill = alignment * (multiple + 1) - num_bytes;
}
nrn_assert((pointer = std::aligned_alloc(alignment, num_bytes + fill)) != nullptr);
}

inline void calloc_memory(void*& pointer, size_t num_bytes, size_t alignment) {
Expand Down
9 changes: 1 addition & 8 deletions src/coreneuron/utils/nrnoc_aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,7 @@ void* erealloc(void* ptr, size_t size) {
}

void* nrn_cacheline_alloc(void** memptr, size_t size) {
#if HAVE_MEMALIGN
if (posix_memalign(memptr, 64, size) != 0) {
fprintf(stderr, "posix_memalign not working\n");
assert(0);
}
#else
*memptr = emalloc(size);
#endif
alloc_memory(*memptr, size, 64);
return *memptr;
}

Expand Down

0 comments on commit 55a15bf

Please sign in to comment.