16 inline bool use_pool() {
return get_yakl_instance().pool_enabled; }
24 inline void set_device_alloc_free(std::function<
void *(
size_t )> &alloc , std::function<
void (
void * )> &dealloc) {
25 #if defined(YAKL_ARCH_CUDA)
26 #if defined (YAKL_MANAGED_MEMORY)
27 alloc = [] (
size_t bytes ) ->
void* {
28 if (bytes == 0)
return nullptr;
30 cudaMallocManaged(&ptr,bytes);
31 if (ptr ==
nullptr)
yakl_throw(
"ERROR: cudaMallocManaged returned nullptr. You have likely run out of memory");
34 omp_target_associate_ptr(ptr,ptr,bytes,0,0);
38 acc_map_data(ptr,ptr,bytes);
43 dealloc = [] (
void *ptr ) {
48 alloc = [] (
size_t bytes ) ->
void* {
49 if (bytes == 0)
return nullptr;
51 cudaMalloc(&ptr,bytes);
52 if (ptr ==
nullptr)
yakl_throw(
"ERROR: cudaMalloc returned nullptr. You have likely run out of memory");
56 dealloc = [] (
void *ptr ) {
61 #elif defined(YAKL_ARCH_HIP)
62 #if defined (YAKL_MANAGED_MEMORY)
63 alloc = [] (
size_t bytes ) ->
void* {
64 if (bytes == 0)
return nullptr;
66 hipMallocManaged(&ptr,bytes);
67 if (ptr ==
nullptr)
yakl_throw(
"ERROR: hipMallocManaged returned nullptr. You have likely run out of memory");
70 omp_target_associate_ptr(ptr,ptr,bytes,0,0);
74 acc_map_data(ptr,ptr,bytes);
79 dealloc = [] (
void *ptr ) {
84 alloc = [] (
size_t bytes ) ->
void* {
85 if (bytes == 0)
return nullptr;
87 hipMalloc(&ptr,bytes);
88 if (ptr ==
nullptr)
yakl_throw(
"ERROR: hipMalloc returned nullptr. You have likely run out of memory");
92 dealloc = [] (
void *ptr ) {
97 #elif defined (YAKL_ARCH_SYCL)
98 #if defined (YAKL_MANAGED_MEMORY)
99 alloc = [] (
size_t bytes ) ->
void* {
100 if (bytes == 0)
return nullptr;
102 void *ptr = sycl::malloc_shared(bytes,sycl_default_stream());
103 if (ptr ==
nullptr)
yakl_throw(
"ERROR: sycl::malloc_shared returned nullptr. You have likely run out of memory");
106 omp_target_associate_ptr(ptr,ptr,bytes,0,0);
110 acc_map_data(ptr,ptr,bytes);
114 dealloc = [] (
void *ptr ) {
115 sycl::free(ptr, sycl_default_stream());
119 alloc = [] (
size_t bytes ) ->
void* {
120 if (bytes == 0)
return nullptr;
121 void *ptr = sycl::malloc_device(bytes,sycl_default_stream());
122 if (ptr ==
nullptr)
yakl_throw(
"ERROR: sycl::malloc_device returned nullptr. You have likely run out of memory");
126 dealloc = [] (
void *ptr ) {
127 sycl::free(ptr, sycl_default_stream());
132 alloc = [] (
size_t bytes ) ->
void* {
133 if (bytes == 0)
return nullptr;
134 void *ptr = ::malloc(bytes);
135 if (ptr ==
nullptr)
yakl_throw(
"ERROR: malloc returned nullptr. You have likely run out of memory");
138 dealloc = [] (
void *ptr ) {
153 get_yakl_instance().alloc_device_func = [] (
size_t bytes ,
char const *label) ->
void * {
154 #ifdef YAKL_MEMORY_DEBUG
155 if (
yakl_mainproc()) std::cout <<
"MEMORY_DEBUG: Allocating label \"" << label <<
"\" of size " << bytes <<
" bytes" << std::endl;
157 void * ptr = get_yakl_instance().pool.allocate( bytes , label );
158 #ifdef YAKL_MEMORY_DEBUG
159 if (
yakl_mainproc()) std::cout <<
"MEMORY_DEBUG: Successfully allocated label \"" << label
160 <<
" with pointer address " << ptr << std::endl;
164 get_yakl_instance().free_device_func = [] (
void *ptr ,
char const *label) {
165 #ifdef YAKL_MEMORY_DEBUG
166 if (
yakl_mainproc()) std::cout <<
"MEMORY_DEBUG: Freeing label \"" << label <<
"\" with pointer address " << ptr << std::endl;
168 get_yakl_instance().pool.free( ptr , label );
171 std::function<
void *( size_t)> alloc;
172 std::function<void (
void *)> dealloc;
173 set_device_alloc_free(alloc , dealloc);
174 get_yakl_instance().alloc_device_func = [=] (
size_t bytes ,
char const *label) ->
void * {
175 #ifdef YAKL_MEMORY_DEBUG
176 if (
yakl_mainproc()) std::cout <<
"MEMORY_DEBUG: Allocating label \"" << label <<
"\" of size " << bytes <<
" bytes" << std::endl;
178 void * ptr = alloc(bytes);
179 #ifdef YAKL_MEMORY_DEBUG
180 if (
yakl_mainproc()) std::cout <<
"MEMORY_DEBUG: Successfully allocated label \"" << label
181 <<
" with pointer address " << ptr << std::endl;
185 get_yakl_instance().free_device_func = [=] (
void *ptr ,
char const *label) {
186 #ifdef YAKL_MEMORY_DEBUG
187 if (
yakl_mainproc()) std::cout <<
"MEMORY_DEBUG: Freeing label \"" << label <<
"\" with pointer address " << ptr << std::endl;
193 get_yakl_instance().device_allocators_are_default =
true;
205 fence(); get_yakl_instance().alloc_device_func = [=] (
size_t bytes ,
char const *label) ->
void * {
return func(bytes); };
206 get_yakl_instance().device_allocators_are_default =
false;
215 fence(); get_yakl_instance().free_device_func = [=] (
void *ptr ,
char const *label) { func(ptr); };
216 get_yakl_instance().device_allocators_are_default =
false;
224 inline void set_device_allocator ( std::function<
void *(
size_t ,
char const *)> func ) {
fence(); get_yakl_instance().alloc_device_func = func; }
234 inline void *
alloc_device(
size_t bytes,
char const *label) {
return get_yakl_instance().alloc_device_func(bytes,label); }
237 inline void free_device (
void * ptr ,
char const *label) { get_yakl_instance().free_device_func (ptr ,label); }