LCOV - code coverage report
Current view: top level - src/offload - offload_mempool.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 99.2 % 254 252
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 21 21

            Line data    Source code
       1              : /*----------------------------------------------------------------------------*/
       2              : /*  CP2K: A general program to perform molecular dynamics simulations         */
       3              : /*  Copyright 2000-2026 CP2K developers group <https://cp2k.org>              */
       4              : /*                                                                            */
       5              : /*  SPDX-License-Identifier: BSD-3-Clause                                     */
       6              : /*----------------------------------------------------------------------------*/
       7              : #include "offload_mempool.h"
       8              : #include "../mpiwrap/cp_mpi.h"
       9              : #include "offload_library.h"
      10              : #include "offload_runtime.h"
      11              : 
      12              : #include "../base/cp_assert.h"
      13              : #include <inttypes.h>
      14              : #include <omp.h>
      15              : #include <stdbool.h>
      16              : #include <stdio.h>
      17              : #include <stdlib.h>
      18              : #include <string.h>
      19              : 
      20              : #if defined(__parallel)
      21              : #include <mpi.h>
      22              : #endif
      23              : 
      24              : #define OFFLOAD_MEMPOOL_PRINT(FN, MSG, OUTPUT_UNIT)                            \
      25              :   ((FN)(MSG, (int)strlen(MSG), OUTPUT_UNIT))
      26              : #define OFFLOAD_MEMPOOL_OMPALLOC 1
      27              : // A reused chunk exceeds the requested size by at most this fraction, and the
      28              : // pool holds at most the most ever requested at once plus this fraction of it:
      29              : // what a workload needs again stays cached, whereas sizes that do not fit
      30              : // cannot let the pool grow without bound (the bound covers the reuse waste).
      31              : #define OFFLOAD_MEMPOOL_SLACK 4 /* 1/4 */
      32              : enum { MEMPOOL_INITIAL_BUCKETS = 64 };
      33              : 
      34              : /*******************************************************************************
      35              :  * \brief Private struct for storing a chunk of memory.
      36              :  * \author Ole Schuett
      37              :  ******************************************************************************/
      38              : typedef struct offload_memchunk {
      39              :   void *mem; // first: allows to cast memchunk into mem-ptr...
      40              :   struct offload_memchunk *next; // hash chain or idle stack of one size
      41              :   struct offload_memchunk *left;
      42              :   union {
      43              :     struct offload_memchunk *right; // idle size tree
      44              :     size_t used;                    // requested bytes while on loan
      45              :   };
      46              :   size_t size;
      47              : } offload_memchunk_t;
      48              : 
      49              : /*******************************************************************************
      50              :  * \brief Private struct for storing a memory pool.
      51              :  * \author Ole Schuett
      52              :  ******************************************************************************/
      53              : typedef struct offload_mempool {
      54              :   offload_memchunk_t *available;  // one tree node per distinct idle capacity
      55              :   offload_memchunk_t **allocated; // allocated chunks indexed by address
      56              :   offload_memchunk_t *initial_buckets[MEMPOOL_INITIAL_BUCKETS];
      57              :   size_t nbuckets, nallocated;
      58              :   uint64_t mallocs; // backend allocations, retained across clear
      59              :   uint64_t available_size, allocated_size;
      60              :   uint64_t allocated_used, used_peak; // requested sizes in use, most ever
      61              :   uint64_t peak_size;                 // peak total capacity before trimming
      62              : } offload_mempool_t;
      63              : 
      64              : /*******************************************************************************
      65              :  * \brief Private pools for host and device memory.
      66              :  * \author Ole Schuett
      67              :  ******************************************************************************/
      68              : static offload_mempool_t mempool_host = {.allocated =
      69              :                                              mempool_host.initial_buckets,
      70              :                                          .nbuckets = MEMPOOL_INITIAL_BUCKETS};
      71              : static offload_mempool_t mempool_device = {.allocated =
      72              :                                                mempool_device.initial_buckets,
      73              :                                            .nbuckets = MEMPOOL_INITIAL_BUCKETS};
      74              : 
      75              : /*******************************************************************************
      76              :  * \brief Returns the larger of two given integer (missing from the C standard)
      77              :  * \author Ole Schuett
      78              :  ******************************************************************************/
      79      5821016 : static inline uint64_t imax(uint64_t x, uint64_t y) { return (x > y ? x : y); }
      80              : 
      81              : /*******************************************************************************
      82              :  * \brief Private routine for actually allocating system memory.
      83              :  * \author Ole Schuett
      84              :  ******************************************************************************/
      85       114292 : static void *actual_malloc(const size_t size, const bool on_device) {
      86       114292 :   if (size == 0) {
      87              :     return NULL;
      88              :   }
      89              : 
      90       114292 :   void *memory = NULL;
      91       114292 :   (void)on_device; // used only by offload backends
      92              : 
      93              : // This pool must not sit on top of LIBXSTREAM's pool (double caching).
      94              : #if defined(__OFFLOAD) && defined(__LIBXSTREAM)
      95              :   offload_activate_chosen_device();
      96              :   if (on_device) {
      97              :     OFFLOAD_CHECK(libxstream_mem_dev_allocate_hint(&memory, size,
      98              :                                                    LIBXSTREAM_MALLOC_CP2K));
      99              :   } else {
     100              :     OFFLOAD_CHECK(libxstream_mem_host_allocate_hint(&memory, size,
     101              :                                                     LIBXSTREAM_MALLOC_CP2K));
     102              :   }
     103              : #elif defined(__OFFLOAD)
     104              :   if (on_device) {
     105              :     offload_activate_chosen_device();
     106              :     offloadMalloc(&memory, size);
     107              :   } else {
     108              :     offload_activate_chosen_device();
     109              :     offloadMallocHost(&memory, size);
     110              :   }
     111              : #elif OFFLOAD_MEMPOOL_OMPALLOC && (201811 /*v5.0*/ <= _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__,
     116              :             __LINE__);
     117              :     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
     118              :   }
     119              : #else
     120       114292 :   memory = malloc(size);
     121              : #endif
     122              : 
     123       114292 :   cp_assert(memory != NULL);
     124              :   return memory;
     125              : }
     126              : 
     127              : /*******************************************************************************
     128              :  * \brief Private routine for actually freeing system memory.
     129              :  * \author Ole Schuett
     130              :  ******************************************************************************/
     131       114172 : static void actual_free(void *memory, const bool on_device) {
     132       114172 :   if (NULL == memory) {
     133              :     return;
     134              :   }
     135              : 
     136              : // This pool must not sit on top of LIBXSTREAM's pool (double caching).
     137              : #if defined(__OFFLOAD) && defined(__LIBXSTREAM)
     138              :   offload_activate_chosen_device();
     139              :   if (on_device) {
     140              :     OFFLOAD_CHECK(
     141              :         libxstream_mem_dev_deallocate_hint(memory, LIBXSTREAM_MALLOC_CP2K));
     142              :   } else {
     143              :     OFFLOAD_CHECK(
     144              :         libxstream_mem_host_deallocate_hint(memory, LIBXSTREAM_MALLOC_CP2K));
     145              :   }
     146              : #elif defined(__OFFLOAD)
     147              :   if (on_device) {
     148              :     offload_activate_chosen_device();
     149              :     offloadFree(memory);
     150              :   } else {
     151              :     offload_activate_chosen_device();
     152              :     offloadFreeHost(memory);
     153              :   }
     154              : #elif OFFLOAD_MEMPOOL_OMPALLOC && (201811 /*v5.0*/ <= _OPENMP)
     155              :   (void)on_device; // mark used
     156              :   omp_free(memory, omp_null_allocator);
     157              : #elif defined(__parallel) && !OFFLOAD_MEMPOOL_OMPALLOC
     158              :   (void)on_device; // mark used
     159              :   if (MPI_SUCCESS != MPI_Free_mem(memory)) {
     160              :     fprintf(stderr, "ERROR: MPI_Free_mem failed at %s:%i\n", name, __FILE__,
     161              :             __LINE__);
     162              :     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
     163              :   }
     164              : #else
     165       114172 :   (void)on_device; // mark used
     166       114172 :   free(memory);
     167              : #endif
     168              : }
     169              : 
     170              : /*******************************************************************************
     171              :  * \brief Bucket for an aligned host or device address (power-of-two table).
     172              :  ******************************************************************************/
     173     11418760 : static size_t mempool_bucket(const void *mem, size_t nbuckets) {
     174     11418760 :   uint64_t key = (uintptr_t)mem;
     175     11418760 :   key ^= key >> 32;
     176     11418760 :   key *= UINT64_C(11400714819323198485);
     177     11418760 :   return (key ^ (key >> 32)) & (nbuckets - 1);
     178              : }
     179              : 
     180              : /*******************************************************************************
     181              :  * \brief Grow the address index. Chaining still works if resizing fails.
     182              :  ******************************************************************************/
     183           35 : static void mempool_grow_table(offload_mempool_t *pool) {
     184           35 :   const size_t n = 2 * pool->nbuckets;
     185           35 :   offload_memchunk_t **buckets = calloc(n, sizeof(*buckets));
     186           35 :   if (buckets == NULL) {
     187              :     return;
     188              :   }
     189         5347 :   for (size_t i = 0; i < pool->nbuckets; ++i) {
     190         5312 :     offload_memchunk_t *entry = pool->allocated[i];
     191        10624 :     while (entry != NULL) {
     192         5312 :       offload_memchunk_t *next = entry->next;
     193         5312 :       const size_t bucket = mempool_bucket(entry->mem, n);
     194         5312 :       entry->next = buckets[bucket];
     195         5312 :       buckets[bucket] = entry;
     196         5312 :       entry = next;
     197              :     }
     198              :   }
     199           35 :   if (pool->allocated != pool->initial_buckets) {
     200           18 :     free(pool->allocated);
     201              :   }
     202           35 :   pool->allocated = buckets;
     203           35 :   pool->nbuckets = n;
     204              : }
     205              : 
     206              : /*******************************************************************************
     207              :  * \brief Register a loan and account for the requested and actual chunk sizes.
     208              :  *        All index operations on the pool use offload_mempool_modify.
     209              :  ******************************************************************************/
     210      5706724 : static void mempool_register(offload_mempool_t *pool, offload_memchunk_t *chunk,
     211              :                              size_t requested) {
     212      5706724 :   if (pool->nallocated >= pool->nbuckets) {
     213           35 :     mempool_grow_table(pool);
     214              :   }
     215      5706724 :   const size_t bucket = mempool_bucket(chunk->mem, pool->nbuckets);
     216      5706724 :   chunk->next = pool->allocated[bucket];
     217      5706724 :   pool->allocated[bucket] = chunk;
     218      5706724 :   ++pool->nallocated;
     219      5706724 :   chunk->used = requested;
     220      5706724 :   pool->allocated_size += chunk->size;
     221      5706724 :   pool->allocated_used += requested;
     222      5706724 :   pool->used_peak = imax(pool->used_peak, pool->allocated_used);
     223      5706724 : }
     224              : 
     225              : /*******************************************************************************
     226              :  * \brief Bring an idle capacity (or the last node on its search path) to the
     227              :  *        root. Top-down splaying needs no parent pointers or recursion.
     228              :  ******************************************************************************/
     229     12185255 : static offload_memchunk_t *mempool_splay(offload_memchunk_t *root,
     230              :                                          size_t size) {
     231     12185255 :   offload_memchunk_t head, *less = &head, *greater = &head;
     232     12185255 :   head.left = head.right = NULL;
     233     36156423 :   for (;;) {
     234     36156423 :     if (size < root->size && root->left != NULL) {
     235     12846502 :       offload_memchunk_t *child = root->left;
     236     12846502 :       if (size < child->size) {
     237      7603903 :         root->left = child->right;
     238      7603903 :         child->right = root;
     239      7603903 :         root = child;
     240      7603903 :         if (root->left == NULL) {
     241              :           break;
     242              :         }
     243              :       }
     244     11938738 :       greater->left = root;
     245     11938738 :       greater = root;
     246     11938738 :       root = root->left;
     247     23309921 :     } else if (size > root->size && root->right != NULL) {
     248     13055333 :       offload_memchunk_t *child = root->right;
     249     13055333 :       if (size > child->size) {
     250      7712084 :         root->right = child->left;
     251      7712084 :         child->left = root;
     252      7712084 :         root = child;
     253      7712084 :         if (root->right == NULL) {
     254              :           break;
     255              :         }
     256              :       }
     257     12032430 :       less->right = root;
     258     12032430 :       less = root;
     259     12032430 :       root = root->right;
     260              :     } else {
     261              :       break;
     262              :     }
     263              :   }
     264     12185255 :   less->right = root->left;
     265     12185255 :   greater->left = root->right;
     266     12185255 :   root->left = head.right;
     267     12185255 :   root->right = head.left;
     268     12185255 :   return root;
     269              : }
     270              : 
     271              : /*******************************************************************************
     272              :  * \brief Insert a returned chunk by its actual capacity. Within a size, the
     273              :  *        most recently returned chunk is the first one reused or trimmed.
     274              :  ******************************************************************************/
     275      5706724 : static void mempool_cache(offload_mempool_t *pool, offload_memchunk_t *chunk) {
     276      5706724 :   offload_memchunk_t *root = pool->available;
     277      5706724 :   if (root != NULL && root->size != chunk->size) {
     278      4757993 :     root = mempool_splay(root, chunk->size);
     279              :   }
     280      5656699 :   if (root != NULL && root->size == chunk->size) {
     281      3216341 :     chunk->next = root->next;
     282      3216341 :     root->next = chunk;
     283      3216341 :     pool->available = root;
     284              :   } else {
     285      2490383 :     chunk->next = chunk->left = chunk->right = NULL;
     286      2490383 :     if (root != NULL) {
     287      2440358 :       if (chunk->size < root->size) {
     288      1527211 :         chunk->left = root->left;
     289      1527211 :         chunk->right = root;
     290      1527211 :         root->left = NULL;
     291              :       } else {
     292       913147 :         chunk->right = root->right;
     293       913147 :         chunk->left = root;
     294       913147 :         root->right = NULL;
     295              :       }
     296              :     }
     297      2490383 :     pool->available = chunk;
     298              :   }
     299      5706724 :   pool->available_size += chunk->size;
     300      5706724 : }
     301              : 
     302              : /*******************************************************************************
     303              :  * \brief Take a chunk from the size currently at the root of the idle tree.
     304              :  ******************************************************************************/
     305      5652487 : static offload_memchunk_t *mempool_take_root(offload_mempool_t *pool) {
     306      5652487 :   offload_memchunk_t *root = pool->available;
     307      5652487 :   offload_memchunk_t *chunk = root->next;
     308      5652487 :   if (chunk != NULL) {
     309      3190782 :     root->next = chunk->next;
     310              :   } else {
     311      2461705 :     chunk = root;
     312      2461705 :     if (root->left == NULL) {
     313       122881 :       pool->available = root->right;
     314      2338824 :     } else if (root->right == NULL) {
     315       375211 :       pool->available = root->left;
     316              :     } else {
     317      1963613 :       pool->available = mempool_splay(root->left, root->size);
     318      1963613 :       pool->available->right = root->right;
     319              :     }
     320              :   }
     321      5652487 :   pool->available_size -= chunk->size;
     322      5652487 :   return chunk;
     323              : }
     324              : 
     325              : /*******************************************************************************
     326              :  * \brief Take the smallest idle chunk within the existing reuse tolerance.
     327              :  ******************************************************************************/
     328      5706724 : static offload_memchunk_t *mempool_take_fit(offload_mempool_t *pool,
     329              :                                             size_t size) {
     330      5706724 :   offload_memchunk_t *root = pool->available;
     331      5706724 :   if (root == NULL) {
     332              :     return NULL;
     333              :   }
     334      5688459 :   if (root->size != size) {
     335      4755049 :     root = mempool_splay(root, size);
     336      4755049 :     if (root->size < size && root->right != NULL) {
     337              :       // The search ended at the predecessor. Promote its successor.
     338       679392 :       offload_memchunk_t *successor = mempool_splay(root->right, size);
     339       679392 :       cp_assert(successor->left == NULL);
     340       679392 :       root->right = NULL;
     341       679392 :       successor->left = root;
     342       679392 :       root = successor;
     343              :     }
     344      4755049 :     pool->available = root;
     345              :   }
     346      5688459 :   if (root->size < size || root->size - size > size / OFFLOAD_MEMPOOL_SLACK) {
     347              :     return NULL;
     348              :   }
     349      5592432 :   return mempool_take_root(pool);
     350              : }
     351              : 
     352              : /*******************************************************************************
     353              :  * \brief Trim the largest idle chunks after a backend allocation. A cache hit
     354              :  *        or return only transfers capacity between the idle and live sets.
     355              :  ******************************************************************************/
     356       114292 : static offload_memchunk_t *internal_mempool_trim(offload_mempool_t *pool) {
     357       114292 :   offload_memchunk_t *release = NULL;
     358       114292 :   uint64_t total = pool->available_size + pool->allocated_size;
     359       174347 :   while (pool->available != NULL && total > pool->used_peak &&
     360       148052 :          total - pool->used_peak > pool->used_peak / OFFLOAD_MEMPOOL_SLACK) {
     361        60055 :     if (pool->available->right != NULL) {
     362        15440 :       pool->available = mempool_splay(pool->available, SIZE_MAX);
     363              :     }
     364        60055 :     offload_memchunk_t *chunk = mempool_take_root(pool);
     365        60055 :     total -= chunk->size;
     366        60055 :     chunk->next = release;
     367        60055 :     release = chunk;
     368              :   }
     369       114292 :   return release;
     370              : }
     371              : 
     372              : /*******************************************************************************
     373              :  * \brief Private routine for freeing the chunks taken out of the pool.
     374              :  ******************************************************************************/
     375       142910 : static void internal_mempool_release(offload_memchunk_t *release,
     376              :                                      const bool on_device) {
     377       257082 :   while (release != NULL) {
     378       114172 :     offload_memchunk_t *const chunk = release;
     379       114172 :     release = chunk->next;
     380       114172 :     actual_free(chunk->mem, on_device);
     381       114172 :     free(chunk);
     382              :   }
     383       142910 : }
     384              : 
     385              : /*******************************************************************************
     386              :  * \brief Private routine for allocating host or device memory from the pool.
     387              :  * \author Ole Schuett and Hans Pabst
     388              :  ******************************************************************************/
     389      5712940 : static void *internal_mempool_malloc(offload_mempool_t *pool, const size_t size,
     390              :                                      const bool on_device) {
     391      5712940 :   if (size == 0) {
     392              :     return NULL;
     393              :   }
     394              : 
     395      5706724 :   offload_memchunk_t *chunk;
     396     11413448 : #pragma omp critical(offload_mempool_modify)
     397              :   {
     398      5706724 :     chunk = mempool_take_fit(pool, size);
     399      5706724 :     if (chunk != NULL) {
     400      5592432 :       mempool_register(pool, chunk, size);
     401              :     }
     402              :   }
     403              : 
     404      5706724 :   if (chunk == NULL) {
     405       114292 :     chunk = malloc(sizeof(*chunk));
     406       114292 :     if (chunk == NULL) {
     407              :       return NULL;
     408              :     }
     409       114292 :     chunk->mem = actual_malloc(size, on_device);
     410       114292 :     if (chunk->mem == NULL) {
     411            0 :       free(chunk);
     412            0 :       return NULL;
     413              :     }
     414       114292 :     chunk->size = size;
     415       114292 :     offload_memchunk_t *release;
     416       228584 : #pragma omp critical(offload_mempool_modify)
     417              :     {
     418       114292 :       mempool_register(pool, chunk, size);
     419       114292 :       ++pool->mallocs;
     420       114292 :       pool->peak_size =
     421       114292 :           imax(pool->peak_size, pool->available_size + pool->allocated_size);
     422       114292 :       release = internal_mempool_trim(pool);
     423              :     }
     424       114292 :     internal_mempool_release(release, on_device);
     425              :   }
     426      5706724 :   return chunk->mem;
     427              : }
     428              : 
     429              : /*******************************************************************************
     430              :  * \brief Internal routine for allocating host memory from the pool.
     431              :  * \author Ole Schuett
     432              :  ******************************************************************************/
     433      5391659 : void *offload_mempool_host_malloc(const size_t size) {
     434      5391659 :   return internal_mempool_malloc(&mempool_host, size, false);
     435              : }
     436              : 
     437              : /*******************************************************************************
     438              :  * \brief Internal routine for allocating device memory from the pool
     439              :  * \author Ole Schuett
     440              :  ******************************************************************************/
     441       321281 : void *offload_mempool_device_malloc(const size_t size) {
     442       321281 :   return internal_mempool_malloc(&mempool_device, size, true);
     443              : }
     444              : 
     445              : /*******************************************************************************
     446              :  * \brief Private routine for releasing memory back to the pool.
     447              :  * \author Ole Schuett
     448              :  ******************************************************************************/
     449      7513772 : static void internal_mempool_free(offload_mempool_t *pool, const void *mem) {
     450      7513772 :   if (mem == NULL) {
     451              :     return;
     452              :   }
     453      5706724 : #pragma omp critical(offload_mempool_modify)
     454              :   {
     455      5706724 :     cp_assert(pool->nallocated > 0);
     456      5706724 :     const size_t bucket = mempool_bucket(mem, pool->nbuckets);
     457      5706724 :     offload_memchunk_t **link = &pool->allocated[bucket];
     458      5829494 :     while (*link != NULL && (*link)->mem != mem) {
     459       122770 :       link = &(*link)->next;
     460              :     }
     461      5706724 :     offload_memchunk_t *chunk = *link;
     462      5706724 :     cp_assert(chunk != NULL);
     463      5706724 :     *link = chunk->next;
     464      5706724 :     --pool->nallocated;
     465      5706724 :     pool->allocated_size -= chunk->size;
     466      5706724 :     pool->allocated_used -= chunk->used;
     467      5706724 :     mempool_cache(pool, chunk);
     468              :   }
     469              : }
     470              : 
     471              : /*******************************************************************************
     472              :  * \brief Internal routine for releasing memory back to the pool.
     473              :  * \author Ole Schuett
     474              :  ******************************************************************************/
     475      7192491 : void offload_mempool_host_free(const void *memory) {
     476      7192491 :   internal_mempool_free(&mempool_host, memory);
     477      7192491 : }
     478              : 
     479              : /*******************************************************************************
     480              :  * \brief Internal routine for releasing memory back to the pool.
     481              :  * \author Ole Schuett
     482              :  ******************************************************************************/
     483       321281 : void offload_mempool_device_free(const void *memory) {
     484       321281 :   internal_mempool_free(&mempool_device, memory);
     485       321281 : }
     486              : 
     487              : /*******************************************************************************
     488              :  * \brief Private routine for freeing all memory in the pool.
     489              :  * \author Ole Schuett
     490              :  ******************************************************************************/
     491         2992 : static void internal_mempool_clear(offload_mempool_t *pool,
     492              :                                    const bool on_device) {
     493         2992 :   offload_memchunk_t *available, **buckets = NULL;
     494         5984 : #pragma omp critical(offload_mempool_modify)
     495              :   {
     496         2992 :     cp_assert(pool->nallocated == 0);
     497         2992 :     if (pool->nallocated == 0 && pool->allocated != pool->initial_buckets) {
     498           17 :       buckets = pool->allocated;
     499           17 :       memset(pool->initial_buckets, 0, sizeof(pool->initial_buckets));
     500           17 :       pool->allocated = pool->initial_buckets;
     501           17 :       pool->nbuckets = MEMPOOL_INITIAL_BUCKETS;
     502              :     }
     503         2992 :     available = pool->available;
     504         2992 :     pool->available = NULL;
     505         2992 :     pool->available_size = 0;
     506         2992 :     pool->used_peak = pool->allocated_used;
     507              :   }
     508         2992 :   free(buckets);
     509        31610 :   while (available != NULL) {
     510        28618 :     if (available->right != NULL) {
     511        13768 :       available = mempool_splay(available, SIZE_MAX);
     512              :     }
     513        28618 :     offload_memchunk_t *next = available->left;
     514        28618 :     internal_mempool_release(available, on_device);
     515        28618 :     available = next;
     516              :   }
     517         2992 : }
     518              : 
     519              : /*******************************************************************************
     520              :  * \brief Internal routine for freeing all memory in the pool.
     521              :  * \author Ole Schuett and Hans Pabst
     522              :  ******************************************************************************/
     523         1496 : void offload_mempool_clear(void) {
     524         1496 :   internal_mempool_clear(&mempool_host, false);
     525         1496 :   internal_mempool_clear(&mempool_device, true);
     526         1496 : }
     527              : 
     528              : /*******************************************************************************
     529              :  * \brief Internal routine to query statistics.
     530              :  * \author Hans Pabst
     531              :  ******************************************************************************/
     532        11422 : void offload_mempool_stats_get(offload_mempool_stats_t *memstats) {
     533        11422 :   cp_assert(NULL != memstats);
     534        11422 : #pragma omp critical(offload_mempool_modify)
     535              :   {
     536        11422 :     memstats->host_mallocs = mempool_host.mallocs;
     537        11422 :     memstats->host_used = mempool_host.used_peak;
     538        11422 :     memstats->host_size =
     539        11422 :         mempool_host.available_size + mempool_host.allocated_size;
     540        11422 :     memstats->host_peak = mempool_host.peak_size;
     541              : 
     542        11422 :     memstats->device_mallocs = mempool_device.mallocs;
     543        11422 :     memstats->device_used = mempool_device.used_peak;
     544        11422 :     memstats->device_size =
     545        11422 :         mempool_device.available_size + mempool_device.allocated_size;
     546        11422 :     memstats->device_peak = mempool_device.peak_size;
     547              :   }
     548        11422 : }
     549              : 
     550              : /*******************************************************************************
     551              :  * \brief Print allocation statistics..
     552              :  * \author Hans Pabst
     553              :  ******************************************************************************/
     554        11422 : void offload_mempool_stats_print(int fortran_comm,
     555              :                                  void (*print_func)(const char *, int, int),
     556              :                                  int output_unit) {
     557        11422 :   cp_assert(omp_get_num_threads() == 1);
     558              : 
     559        11422 :   char buffer[100];
     560        11422 :   const cp_mpi_comm_t comm = cp_mpi_comm_f2c(fortran_comm);
     561        11422 :   offload_mempool_stats_t memstats;
     562        11422 :   offload_mempool_stats_get(&memstats);
     563        11422 :   cp_mpi_max_uint64(&memstats.device_mallocs, 1, comm);
     564        11422 :   cp_mpi_max_uint64(&memstats.host_mallocs, 1, comm);
     565              : 
     566        11422 :   if (0 != memstats.device_mallocs || 0 != memstats.host_mallocs) {
     567        10412 :     OFFLOAD_MEMPOOL_PRINT(print_func, "\n", output_unit);
     568        10412 :     OFFLOAD_MEMPOOL_PRINT(
     569              :         print_func,
     570              :         " ----------------------------------------------------------------"
     571              :         "---------------\n",
     572              :         output_unit);
     573        10412 :     OFFLOAD_MEMPOOL_PRINT(
     574              :         print_func,
     575              :         " -                                                               "
     576              :         "              -\n",
     577              :         output_unit);
     578              : 
     579        10412 :     OFFLOAD_MEMPOOL_PRINT(
     580              :         print_func,
     581              :         " -                          OFFLOAD MEMPOOL STATISTICS           "
     582              :         "              -\n",
     583              :         output_unit);
     584        10412 :     OFFLOAD_MEMPOOL_PRINT(
     585              :         print_func,
     586              :         " -                                                               "
     587              :         "              -\n",
     588              :         output_unit);
     589        10412 :     OFFLOAD_MEMPOOL_PRINT(
     590              :         print_func,
     591              :         " ----------------------------------------------------------------"
     592              :         "---------------\n",
     593              :         output_unit);
     594        10412 :     OFFLOAD_MEMPOOL_PRINT(print_func,
     595              :                           " Memory consumption               "
     596              :                           " Number of allocations  Used [MiB]  Size [MiB]\n",
     597              :                           output_unit);
     598              :   }
     599              : #if defined(__OFFLOAD)
     600              :   if (0 < memstats.device_mallocs) {
     601              :     cp_mpi_max_uint64(&memstats.device_peak, 1, comm);
     602              :     snprintf(buffer, sizeof(buffer),
     603              :              " Device                            "
     604              :              " %20" PRIuPTR "  %10" PRIuPTR "  %10" PRIuPTR "\n",
     605              :              (uintptr_t)memstats.device_mallocs,
     606              :              (uintptr_t)((memstats.device_used + (512U << 10)) >> 20),
     607              :              (uintptr_t)((memstats.device_peak + (512U << 10)) >> 20));
     608              :     OFFLOAD_MEMPOOL_PRINT(print_func, buffer, output_unit);
     609              :   }
     610              : #endif
     611        11422 :   if (0 < memstats.host_mallocs) {
     612        10412 :     cp_mpi_max_uint64(&memstats.host_peak, 1, comm);
     613        10412 :     snprintf(buffer, sizeof(buffer),
     614              :              " Host                              "
     615              :              " %20" PRIuPTR "  %10" PRIuPTR "  %10" PRIuPTR "\n",
     616        10412 :              (uintptr_t)memstats.host_mallocs,
     617        10412 :              (uintptr_t)((memstats.host_used + (512U << 10)) >> 20),
     618        10412 :              (uintptr_t)((memstats.host_peak + (512U << 10)) >> 20));
     619        10412 :     OFFLOAD_MEMPOOL_PRINT(print_func, buffer, output_unit);
     620              :   }
     621        11422 :   if (0 < memstats.device_mallocs || 0 < memstats.host_mallocs) {
     622        10412 :     OFFLOAD_MEMPOOL_PRINT(
     623              :         print_func,
     624              :         " ----------------------------------------------------------------"
     625              :         "---------------\n",
     626              :         output_unit);
     627              :   }
     628        11422 : }
     629              : 
     630              : // EOF
        

Generated by: LCOV version 2.0-1