20#if defined(__parallel)
24#define OFFLOAD_MEMPOOL_PRINT(FN, MSG, OUTPUT_UNIT) \
25 ((FN)(MSG, (int)strlen(MSG), OUTPUT_UNIT))
26#define OFFLOAD_MEMPOOL_OMPALLOC 1
31#define OFFLOAD_MEMPOOL_SLACK 4
79static inline uint64_t
imax(uint64_t x, uint64_t y) {
return (x > y ? x : y); }
94#if defined(__OFFLOAD) && defined(__LIBXSTREAM)
97 OFFLOAD_CHECK(libxstream_mem_dev_allocate_hint(&memory, size,
98 LIBXSTREAM_MALLOC_CP2K));
100 OFFLOAD_CHECK(libxstream_mem_host_allocate_hint(&memory, size,
101 LIBXSTREAM_MALLOC_CP2K));
103#elif defined(__OFFLOAD)
106 offloadMalloc(&memory, size);
109 offloadMallocHost(&memory, size);
111#elif OFFLOAD_MEMPOOL_OMPALLOC && (201811 <= _OPENMP)
112 memory = omp_alloc(size, omp_null_allocator);
113#elif defined(__parallel) && !OFFLOAD_MEMPOOL_OMPALLOC
114 if (MPI_SUCCESS != MPI_Alloc_mem((MPI_Aint)size, MPI_INFO_NULL, &memory)) {
115 fprintf(stderr,
"ERROR: MPI_Alloc_mem failed at %s:%i\n", name, __FILE__,
117 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
120 memory = malloc(size);
132 if (NULL == memory) {
137#if defined(__OFFLOAD) && defined(__LIBXSTREAM)
141 libxstream_mem_dev_deallocate_hint(memory, LIBXSTREAM_MALLOC_CP2K));
144 libxstream_mem_host_deallocate_hint(memory, LIBXSTREAM_MALLOC_CP2K));
146#elif defined(__OFFLOAD)
152 offloadFreeHost(memory);
154#elif OFFLOAD_MEMPOOL_OMPALLOC && (201811 <= _OPENMP)
156 omp_free(memory, omp_null_allocator);
157#elif defined(__parallel) && !OFFLOAD_MEMPOOL_OMPALLOC
159 if (MPI_SUCCESS != MPI_Free_mem(memory)) {
160 fprintf(stderr,
"ERROR: MPI_Free_mem failed at %s:%i\n", name, __FILE__,
162 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
174 uint64_t key = (uintptr_t)mem;
176 key *= UINT64_C(11400714819323198485);
177 return (key ^ (key >> 32)) & (nbuckets - 1);
184 const size_t n = 2 * pool->
nbuckets;
186 if (buckets == NULL) {
191 while (entry != NULL) {
194 entry->
next = buckets[bucket];
195 buckets[bucket] = entry;
219 chunk->
used = requested;
234 if (size < root->size && root->
left != NULL) {
236 if (size < child->size) {
240 if (root->
left == NULL) {
244 greater->
left = root;
247 }
else if (size > root->
size && root->
right != NULL) {
249 if (size > child->
size) {
253 if (root->
right == NULL) {
277 if (root != NULL && root->
size != chunk->
size) {
280 if (root != NULL && root->
size == chunk->
size) {
312 if (root->
left == NULL) {
314 }
else if (root->
right == NULL) {
334 if (root->
size != size) {
336 if (root->
size < size && root->
right != NULL) {
341 successor->
left = root;
365 total -= chunk->
size;
366 chunk->
next = release;
376 const bool on_device) {
377 while (release != NULL) {
379 release = chunk->
next;
390 const bool on_device) {
396#pragma omp critical(offload_mempool_modify)
405 chunk = malloc(
sizeof(*chunk));
410 if (chunk->
mem == NULL) {
416#pragma omp critical(offload_mempool_modify)
453#pragma omp critical(offload_mempool_modify)
458 while (*link != NULL && (*link)->
mem != mem) {
459 link = &(*link)->
next;
492 const bool on_device) {
494#pragma omp critical(offload_mempool_modify)
509 while (available != NULL) {
510 if (available->
right != NULL) {
534#pragma omp critical(offload_mempool_modify)
570 " ----------------------------------------------------------------"
581 " - OFFLOAD MEMPOOL STATISTICS "
591 " ----------------------------------------------------------------"
595 " Memory consumption "
596 " Number of allocations Used [MiB] Size [MiB]\n",
599#if defined(__OFFLOAD)
602 snprintf(buffer,
sizeof(buffer),
604 " %20" PRIuPTR
" %10" PRIuPTR
" %10" PRIuPTR
"\n",
606 (uintptr_t)((memstats.
device_used + (512U << 10)) >> 20),
607 (uintptr_t)((memstats.
device_peak + (512U << 10)) >> 20));
613 snprintf(buffer,
sizeof(buffer),
615 " %20" PRIuPTR
" %10" PRIuPTR
" %10" PRIuPTR
"\n",
617 (uintptr_t)((memstats.
host_used + (512U << 10)) >> 20),
618 (uintptr_t)((memstats.
host_peak + (512U << 10)) >> 20));
624 " ----------------------------------------------------------------"
#define cp_assert(condition)
void cp_mpi_max_uint64(uint64_t *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_UINT64_T.
cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm)
Wrapper around MPI_Comm_f2c.
static void print_func(const char *msg, int msglen, int output_unit)
Wrapper for printf, passed to dbm_library_print_stats.
static void const int const int i
void offload_activate_chosen_device(void)
Activates the device selected via offload_set_chosen_device().
static void internal_mempool_free(offload_mempool_t *pool, const void *mem)
Private routine for releasing memory back to the pool.
static void mempool_grow_table(offload_mempool_t *pool)
Grow the address index. Chaining still works if resizing fails.
struct offload_memchunk offload_memchunk_t
Private struct for storing a chunk of memory.
static offload_mempool_t mempool_device
static void actual_free(void *memory, const bool on_device)
Private routine for actually freeing system memory.
void offload_mempool_host_free(const void *memory)
Internal routine for releasing memory back to the pool.
void offload_mempool_clear(void)
Internal routine for freeing all memory in the pool.
void offload_mempool_device_free(const void *memory)
Internal routine for releasing memory back to the pool.
static void * internal_mempool_malloc(offload_mempool_t *pool, const size_t size, const bool on_device)
Private routine for allocating host or device memory from the pool.
#define OFFLOAD_MEMPOOL_PRINT(FN, MSG, OUTPUT_UNIT)
void offload_mempool_stats_get(offload_mempool_stats_t *memstats)
Internal routine to query statistics.
static offload_memchunk_t * mempool_take_fit(offload_mempool_t *pool, size_t size)
Take the smallest idle chunk within the existing reuse tolerance.
static size_t mempool_bucket(const void *mem, size_t nbuckets)
Bucket for an aligned host or device address (power-of-two table).
#define OFFLOAD_MEMPOOL_SLACK
static void * actual_malloc(const size_t size, const bool on_device)
Private routine for actually allocating system memory.
static void internal_mempool_clear(offload_mempool_t *pool, const bool on_device)
Private routine for freeing all memory in the pool.
static uint64_t imax(uint64_t x, uint64_t y)
Returns the larger of two given integer (missing from the C standard).
struct offload_mempool offload_mempool_t
Private struct for storing a memory pool.
@ MEMPOOL_INITIAL_BUCKETS
static void mempool_cache(offload_mempool_t *pool, offload_memchunk_t *chunk)
Insert a returned chunk by its actual capacity. Within a size, the most recently returned chunk is th...
static offload_memchunk_t * mempool_splay(offload_memchunk_t *root, size_t size)
Bring an idle capacity (or the last node on its search path) to the root. Top-down splaying needs no ...
void offload_mempool_stats_print(int fortran_comm, void(*print_func)(const char *, int, int), int output_unit)
Print allocation statistics..
void * offload_mempool_host_malloc(const size_t size)
Internal routine for allocating host memory from the pool.
void * offload_mempool_device_malloc(const size_t size)
Internal routine for allocating device memory from the pool.
static offload_memchunk_t * mempool_take_root(offload_mempool_t *pool)
Take a chunk from the size currently at the root of the idle tree.
static offload_mempool_t mempool_host
Private pools for host and device memory.
static offload_memchunk_t * internal_mempool_trim(offload_mempool_t *pool)
Trim the largest idle chunks after a backend allocation. A cache hit or return only transfers capacit...
static void internal_mempool_release(offload_memchunk_t *release, const bool on_device)
Private routine for freeing the chunks taken out of the pool.
static void mempool_register(offload_mempool_t *pool, offload_memchunk_t *chunk, size_t requested)
Register a loan and account for the requested and actual chunk sizes. All index operations on the poo...
Private struct for storing a chunk of memory.
struct offload_memchunk * next
struct offload_memchunk * left
struct offload_memchunk * right
Internal struct for pool statistics.
Private struct for storing a memory pool.
offload_memchunk_t * available
offload_memchunk_t ** allocated
offload_memchunk_t * initial_buckets[MEMPOOL_INITIAL_BUCKETS]