5#include "plumbing/lattice.h"
7#include "plumbing/backend_gpu/defs.h"
12#if defined(GPU_MEMORY_POOL) && !defined(HILAPP)
15#define gpuMallocDirect(a, b) GPU_CHECK(hipMalloc(a, b))
16#define gpuFreeDirect(a) GPU_CHECK(hipFree(a))
18#define gpuMallocDirect(a, b) GPU_CHECK(cudaMalloc(a, b))
19#define gpuFreeDirect(a) GPU_CHECK(cudaFree(a))
21static_assert(0 &&
"HIP or CUDA must be defined");
28#define MIN_ALLOC_SIZE 128
35static size_t total_size = 0;
36static size_t n_allocs = 0;
37static size_t n_true_allocs = 0;
38static double free_list_avg_size = 0;
39static double free_list_avg_search = 0;
41static std::list<allocation> free_list = {};
42static std::list<allocation> in_use_list = {};
44void gpu_memory_pool_alloc(
void **p,
size_t req_size) {
46 if (req_size < MIN_ALLOC_SIZE) {
47 req_size = MIN_ALLOC_SIZE;
51 free_list_avg_size += free_list.size();
54 bool found_match =
false;
55 auto ptr = free_list.begin();
57 for (
auto it = free_list.begin(); it != free_list.end(); it++) {
59 if (it->size == req_size) {
66 if (it->size > req_size && it->size < 2 * req_size) {
67 if (!found_match || ptr->size > it->size) {
74 free_list_avg_search += steps;
79 in_use_list.splice(in_use_list.begin(), free_list, ptr);
82 hila::out <<
"GPU MEMORY: request " << req_size <<
" gave block " << ptr->size
83 <<
" current total " << total_size <<
'\n';
91 gpuMallocDirect(&(a.ptr), req_size);
93 in_use_list.push_front(a);
97 total_size += req_size;
100 hila::out <<
"GPU MEMORY: request " << req_size <<
" NEW allocation, current total "
101 << total_size <<
'\n';
106void gpu_memory_pool_free(
void *ptr) {
109 for (
auto it = in_use_list.begin(); it != in_use_list.end(); it++) {
110 if (it->ptr == ptr) {
113 free_list.splice(free_list.begin(), in_use_list, it);
116 hila::out <<
"GPU MEMORY: FREE block of size " << it->size <<
", current total "
117 << total_size <<
'\n';
125 hila::out <<
"Memory free error - unknown pointer " << ptr <<
'\n';
130void gpu_memory_pool_purge() {
132 for (
auto it = free_list.begin(); it != free_list.end(); it++) {
133 gpuFreeDirect(it->ptr);
135 total_size -= it->size;
138 hila::out <<
"GPU MEMORY: Purging " << it->size <<
", bytes, total size " << total_size
146void gpu_memory_pool_report() {
148 hila::out <<
"\nGPU Memory pool statistics from node 0:\n";
149 hila::out <<
" Total pool size " << ((double)total_size) / (1024 * 1024) <<
" MB\n";
150 hila::out <<
" # of allocations " << n_allocs <<
" real allocs " << std::setprecision(2)
151 << ((double)n_true_allocs) / n_allocs * 100 <<
"%\n";
152 hila::out <<
" Average free list search " << free_list_avg_search / n_allocs
154 hila::out <<
" Average free list size " << free_list_avg_size / n_allocs <<
" items\n\n";
This file defines all includes for HILA.
This files containts definitions for the Field class and the classes required to define it such as fi...
int myrank()
rank of this node
std::ostream out
this is our default output file stream
void terminate(int status)