(git:5a3db73)
Loading...
Searching...
No Matches
offload_mempool.c File Reference
#include "offload_mempool.h"
#include "../mpiwrap/cp_mpi.h"
#include "offload_library.h"
#include "offload_runtime.h"
#include "../base/cp_assert.h"
#include <inttypes.h>
#include <omp.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  offload_memchunk
 Private struct for storing a chunk of memory. More...
struct  offload_mempool
 Private struct for storing a memory pool. More...

Macros

#define OFFLOAD_MEMPOOL_PRINT(FN, MSG, OUTPUT_UNIT)
#define OFFLOAD_MEMPOOL_OMPALLOC   1
#define OFFLOAD_MEMPOOL_SLACK   4 /* 1/4 */

Typedefs

typedef struct offload_memchunk offload_memchunk_t
 Private struct for storing a chunk of memory.
typedef struct offload_mempool offload_mempool_t
 Private struct for storing a memory pool.

Enumerations

enum  { MEMPOOL_INITIAL_BUCKETS = 64 }

Functions

static uint64_t imax (uint64_t x, uint64_t y)
 Returns the larger of two given integer (missing from the C standard).
static void * actual_malloc (const size_t size, const bool on_device)
 Private routine for actually allocating system memory.
static void actual_free (void *memory, const bool on_device)
 Private routine for actually freeing system memory.
static size_t mempool_bucket (const void *mem, size_t nbuckets)
 Bucket for an aligned host or device address (power-of-two table).
static void mempool_grow_table (offload_mempool_t *pool)
 Grow the address index. Chaining still works if resizing fails.
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 pool use offload_mempool_modify.
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 parent pointers or recursion.
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 the first one reused or trimmed.
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_memchunk_t * mempool_take_fit (offload_mempool_t *pool, size_t size)
 Take the smallest idle chunk within the existing reuse tolerance.
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 capacity between the idle and live sets.
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 * 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.
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 void internal_mempool_free (offload_mempool_t *pool, const void *mem)
 Private routine for releasing memory back to the pool.
void offload_mempool_host_free (const void *memory)
 Internal routine for releasing memory back to the pool.
void offload_mempool_device_free (const void *memory)
 Internal routine for releasing memory back to the pool.
static void internal_mempool_clear (offload_mempool_t *pool, const bool on_device)
 Private routine for freeing all memory in the pool.
void offload_mempool_clear (void)
 Internal routine for freeing all memory in the pool.
void offload_mempool_stats_get (offload_mempool_stats_t *memstats)
 Internal routine to query statistics.
void offload_mempool_stats_print (int fortran_comm, void(*print_func)(const char *, int, int), int output_unit)
 Print allocation statistics..

Variables

static offload_mempool_t mempool_host
 Private pools for host and device memory.
static offload_mempool_t mempool_device

Macro Definition Documentation

◆ OFFLOAD_MEMPOOL_PRINT

#define OFFLOAD_MEMPOOL_PRINT ( FN,
MSG,
OUTPUT_UNIT )
Value:
((FN)(MSG, (int)strlen(MSG), OUTPUT_UNIT))

Definition at line 24 of file offload_mempool.c.

◆ OFFLOAD_MEMPOOL_OMPALLOC

#define OFFLOAD_MEMPOOL_OMPALLOC   1

Definition at line 26 of file offload_mempool.c.

◆ OFFLOAD_MEMPOOL_SLACK

#define OFFLOAD_MEMPOOL_SLACK   4 /* 1/4 */

Definition at line 31 of file offload_mempool.c.

Typedef Documentation

◆ offload_memchunk_t

Private struct for storing a chunk of memory.

Author
Ole Schuett

◆ offload_mempool_t

Private struct for storing a memory pool.

Author
Ole Schuett

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MEMPOOL_INITIAL_BUCKETS 

Definition at line 32 of file offload_mempool.c.

Function Documentation

◆ imax()

uint64_t imax ( uint64_t x,
uint64_t y )
inlinestatic

Returns the larger of two given integer (missing from the C standard).

Author
Ole Schuett

Definition at line 79 of file offload_mempool.c.

Here is the caller graph for this function:

◆ actual_malloc()

void * actual_malloc ( const size_t size,
const bool on_device )
static

Private routine for actually allocating system memory.

Author
Ole Schuett

Definition at line 85 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ actual_free()

void actual_free ( void * memory,
const bool on_device )
static

Private routine for actually freeing system memory.

Author
Ole Schuett

Definition at line 131 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mempool_bucket()

size_t mempool_bucket ( const void * mem,
size_t nbuckets )
static

Bucket for an aligned host or device address (power-of-two table).

Definition at line 173 of file offload_mempool.c.

Here is the caller graph for this function:

◆ mempool_grow_table()

void mempool_grow_table ( offload_mempool_t * pool)
static

Grow the address index. Chaining still works if resizing fails.

Definition at line 183 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mempool_register()

void mempool_register ( offload_mempool_t * pool,
offload_memchunk_t * chunk,
size_t requested )
static

Register a loan and account for the requested and actual chunk sizes. All index operations on the pool use offload_mempool_modify.

Definition at line 210 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mempool_splay()

offload_memchunk_t * mempool_splay ( offload_memchunk_t * root,
size_t size )
static

Bring an idle capacity (or the last node on its search path) to the root. Top-down splaying needs no parent pointers or recursion.

Definition at line 229 of file offload_mempool.c.

Here is the caller graph for this function:

◆ mempool_cache()

void mempool_cache ( offload_mempool_t * pool,
offload_memchunk_t * chunk )
static

Insert a returned chunk by its actual capacity. Within a size, the most recently returned chunk is the first one reused or trimmed.

Definition at line 275 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mempool_take_root()

offload_memchunk_t * mempool_take_root ( offload_mempool_t * pool)
static

Take a chunk from the size currently at the root of the idle tree.

Definition at line 305 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mempool_take_fit()

offload_memchunk_t * mempool_take_fit ( offload_mempool_t * pool,
size_t size )
static

Take the smallest idle chunk within the existing reuse tolerance.

Definition at line 328 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_mempool_trim()

offload_memchunk_t * internal_mempool_trim ( offload_mempool_t * pool)
static

Trim the largest idle chunks after a backend allocation. A cache hit or return only transfers capacity between the idle and live sets.

Definition at line 356 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_mempool_release()

void internal_mempool_release ( offload_memchunk_t * release,
const bool on_device )
static

Private routine for freeing the chunks taken out of the pool.

Definition at line 375 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_mempool_malloc()

void * internal_mempool_malloc ( offload_mempool_t * pool,
const size_t size,
const bool on_device )
static

Private routine for allocating host or device memory from the pool.

Author
Ole Schuett and Hans Pabst

Definition at line 389 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offload_mempool_host_malloc()

void * offload_mempool_host_malloc ( const size_t size)

Internal routine for allocating host memory from the pool.

Author
Ole Schuett

Definition at line 433 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offload_mempool_device_malloc()

void * offload_mempool_device_malloc ( const size_t size)

Internal routine for allocating device memory from the pool.

Author
Ole Schuett

Definition at line 441 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_mempool_free()

void internal_mempool_free ( offload_mempool_t * pool,
const void * mem )
static

Private routine for releasing memory back to the pool.

Author
Ole Schuett

Definition at line 449 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offload_mempool_host_free()

void offload_mempool_host_free ( const void * memory)

Internal routine for releasing memory back to the pool.

Author
Ole Schuett

Definition at line 475 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offload_mempool_device_free()

void offload_mempool_device_free ( const void * memory)

Internal routine for releasing memory back to the pool.

Author
Ole Schuett

Definition at line 483 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_mempool_clear()

void internal_mempool_clear ( offload_mempool_t * pool,
const bool on_device )
static

Private routine for freeing all memory in the pool.

Author
Ole Schuett

Definition at line 491 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offload_mempool_clear()

void offload_mempool_clear ( void )

Internal routine for freeing all memory in the pool.

Author
Ole Schuett and Hans Pabst

Definition at line 523 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ offload_mempool_stats_get()

void offload_mempool_stats_get ( offload_mempool_stats_t * memstats)

Internal routine to query statistics.

Author
Hans Pabst

Definition at line 532 of file offload_mempool.c.

Here is the caller graph for this function:

◆ offload_mempool_stats_print()

void offload_mempool_stats_print ( int fortran_comm,
void(* print_func )(const char *, int, int),
int output_unit )

Print allocation statistics..

Print allocation statistics.

Author
Hans Pabst

Definition at line 554 of file offload_mempool.c.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ mempool_host

offload_mempool_t mempool_host
static
Initial value:
= {.allocated =
mempool_host.initial_buckets,
@ MEMPOOL_INITIAL_BUCKETS
static offload_mempool_t mempool_host
Private pools for host and device memory.

Private pools for host and device memory.

Author
Ole Schuett

Definition at line 68 of file offload_mempool.c.

◆ mempool_device

offload_mempool_t mempool_device
static
Initial value:
= {.allocated =
mempool_device.initial_buckets,
static offload_mempool_t mempool_device

Definition at line 71 of file offload_mempool.c.