1 #ifndef hamr_hip_malloc_allocator_h
2 #define hamr_hip_malloc_allocator_h
12 #include <hip/hip_runtime.h>
15 #include "hamr_config.h"
16 #include "hamr_hip_kernels.h"
23 template <
typename T,
typename E =
void>
28 class HAMR_EXPORT
hip_malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
41 void operator()(T *ptr);
53 #if defined(HAMR_VERBOSE)
56 std::cerr <<
"created hip_malloc_deleter for array of " << n
57 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
58 <<
" at " << m_ptr << std::endl;
66 hip_malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
69 #if !defined(HAMR_HIP_OBJECTS)
71 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
72 " hip_malloc_deleter dealllocate objects failed."
73 " HAMR_HIP_OBJECTS is not enabled" << std::endl;
84 n_blocks, thread_grid))
86 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
87 " Failed to determine launch properties." << std::endl;
92 hipError_t ierr = hipSuccess;
93 hip_kernels::destruct<T><<<block_grid, thread_grid>>>(ptr, m_elem);
94 if ((ierr = hipGetLastError()) != hipSuccess)
96 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
97 " Failed to launch the construct kernel. "
98 << hipGetErrorString(ierr) << std::endl;
106 #if defined(HAMR_VERBOSE)
109 std::cerr <<
"hip_malloc_deleter deleting array of " << m_elem
110 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
111 <<
" at " << m_ptr << std::endl;
123 template <
typename T>
137 void operator()(T *ptr);
145 template <
typename T>
149 #if defined(HAMR_VERBOSE)
152 std::cerr <<
"created hip_malloc_deleter for array of " << n
153 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
154 <<
" at " << m_ptr << std::endl;
160 template <
typename T>
162 hip_malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
165 assert(ptr == m_ptr);
167 #if defined(HAMR_VERBOSE)
170 std::cerr <<
"hip_malloc_deleter deleting array of " << m_elem
171 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
172 <<
" at " << m_ptr << std::endl;
177 hipError_t ierr = hipSuccess;
187 template <
typename T,
typename E =
void>
191 template <
typename T>
199 static std::shared_ptr<T> allocate(
size_t n);
207 static std::shared_ptr<T> allocate(
size_t n,
const T &val);
217 template <
typename U>
218 static std::shared_ptr<T> allocate(
size_t n,
const U *vals,
bool hipVals =
false);
222 template <
typename T>
225 ::allocate(
size_t n_elem)
227 #if !defined(HAMR_HIP_OBJECTS)
229 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
230 " hip_malloc_allocator allocate objects failed."
231 " HAMR_HIP_OBJECTS is not enabled" << std::endl;
235 size_t n_bytes = n_elem*
sizeof(T);
239 hipError_t ierr = hipSuccess;
240 if ((ierr = hipMalloc(&ptr, n_bytes)) != hipSuccess)
242 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
243 " Failed to hipMalloc " << n_elem <<
" of "
244 <<
typeid(T).name() <<
" total " << n_bytes <<
" bytes. "
245 << hipGetErrorString(ierr) << std::endl;
253 dim3 thread_grid = 0;
255 n_blocks, thread_grid))
257 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
258 " Failed to determine launch properties. "
259 << hipGetErrorString(ierr) << std::endl;
264 hip_kernels::construct<T><<<block_grid, thread_grid>>>(ptr, n_elem);
265 if ((ierr = hipGetLastError()) != hipSuccess)
267 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
268 " Failed to launch the construct kernel. "
269 << hipGetErrorString(ierr) << std::endl;
273 #if defined(HAMR_VERBOSE)
276 std::cerr <<
"hip_malloc_allocator allocating array of " << n_elem
277 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
278 <<
" at " << ptr << std::endl;
283 return std::shared_ptr<T>(ptr, hip_malloc_deleter<T>(ptr, n_elem));
288 template <
typename T>
290 hip_malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
291 ::allocate(
size_t n_elem,
const T &val)
293 #if !defined(HAMR_HIP_OBJECTS)
296 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
297 " hip_malloc_allocator allocate objects failed."
298 " HAMR_HIP_OBJECTS is not enabled" << std::endl;
302 size_t n_bytes = n_elem*
sizeof(T);
306 hipError_t ierr = hipSuccess;
307 if ((ierr = hipMalloc(&ptr, n_bytes)) != hipSuccess)
309 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
310 " Failed to hipMalloc " << n_elem <<
" of "
311 <<
typeid(T).name() <<
sizeof(T) <<
" total " << n_bytes
312 <<
" bytes. " << hipGetErrorString(ierr) << std::endl;
320 dim3 thread_grid = 0;
324 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
325 " Failed to determine launch properties. "
326 << hipGetErrorString(ierr) << std::endl;
331 hip_kernels::construct<T><<<block_grid, thread_grid>>>(ptr, n_elem, val);
332 if ((ierr = hipGetLastError()) != hipSuccess)
334 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
335 " Failed to launch the construct kernel. "
336 << hipGetErrorString(ierr) << std::endl;
340 #if defined(HAMR_VERBOSE)
343 std::cerr <<
"hip_malloc_allocator allocating array of " << n_elem
344 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
345 <<
" at " << ptr <<
" initialized to " << val << std::endl;
350 return std::shared_ptr<T>(ptr, hip_malloc_deleter<T>(ptr, n_elem));
355 template <
typename T>
356 template <
typename U>
358 hip_malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
359 ::allocate(
size_t n_elem,
const U *vals,
bool hipVals)
361 #if !defined(HAMR_HIP_OBJECTS)
365 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
366 " hip_malloc_allocator allocate objects failed."
367 " HAMR_HIP_OBJECTS is not enabled" << std::endl;
371 size_t n_bytes = n_elem*
sizeof(T);
375 hipError_t ierr = hipSuccess;
376 if ((ierr = hipMalloc(&ptr, n_bytes)) != hipSuccess)
378 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
379 " Failed to hipMalloc " << n_elem <<
" of "
380 <<
typeid(T).name() <<
" total " << n_bytes <<
" bytes. "
381 << hipGetErrorString(ierr) << std::endl;
389 size_t n_bytes_vals = n_elem*
sizeof(U);
390 if ((ierr = hipMalloc(&tmp, n_bytes_vals)) != hipSuccess)
392 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
393 " Failed to hipMalloc " << n_elem <<
" of "
394 <<
typeid(T).name() <<
" total " << n_bytes_vals <<
"bytes. "
395 << hipGetErrorString(ierr) << std::endl;
399 if ((ierr = hipMemcpy(tmp, vals, n_bytes_vals, hipMemcpyHostToDevice)) != hipSuccess)
401 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
402 " Failed to hipMemcpy array of " << n_elem
403 <<
" of " <<
typeid(T).name() <<
" total " << n_bytes_vals <<
"bytes. "
404 << hipGetErrorString(ierr) << std::endl;
415 dim3 thread_grid = 0;
417 n_blocks, thread_grid))
419 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
420 " Failed to determine launch properties. "
421 << hipGetErrorString(ierr) << std::endl;
426 hip_kernels::construct<T><<<block_grid, thread_grid>>>(ptr, n_elem, vals);
427 if ((ierr = hipGetLastError()) != hipSuccess)
429 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
430 " Failed to launch the construct kernel. "
431 << hipGetErrorString(ierr) << std::endl;
442 #if defined(HAMR_VERBOSE)
445 std::cerr <<
"hip_malloc_allocator allocating array of " << n_elem
446 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
447 <<
" at " << ptr <<
" initialized from the "
448 << (hipVals ?
"HIP" :
"CPU") <<
" array of objects of "
449 <<
typeid(U).name() <<
sizeof(U) <<
" at " << vals
455 return std::shared_ptr<T>(ptr, hip_malloc_deleter<T>(ptr, n_elem));
463 template <
typename T>
471 static std::shared_ptr<T> allocate(
size_t n);
479 static std::shared_ptr<T> allocate(
size_t n,
const T &val);
489 template <
typename U>
490 static std::shared_ptr<T> allocate(
size_t n,
const U *vals,
bool hipVals =
false);
494 template <
typename T>
497 ::allocate(
size_t n_elem)
499 size_t n_bytes = n_elem*
sizeof(T);
503 hipError_t ierr = hipSuccess;
504 if ((ierr = hipMalloc(&ptr, n_bytes)) != hipSuccess)
506 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
507 " Failed to hipMalloc " << n_elem <<
" of "
508 <<
typeid(T).name() <<
" total " << n_bytes <<
"bytes. "
509 << hipGetErrorString(ierr) << std::endl;
514 #if defined(HAMR_INIT_ALLOC)
515 hipMemset(ptr, 0, n_bytes);
518 #if defined(HAMR_VERBOSE)
521 std::cerr <<
"hip_malloc_allocator allocating array of " << n_elem
522 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
523 <<
" at " << ptr << std::endl;
528 return std::shared_ptr<T>(ptr, hip_malloc_deleter<T>(ptr, n_elem));
532 template <
typename T>
534 hip_malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
535 ::allocate(
size_t n_elem,
const T &val)
537 size_t n_bytes = n_elem*
sizeof(T);
541 hipError_t ierr = hipSuccess;
542 if ((ierr = hipMalloc(&ptr, n_bytes)) != hipSuccess)
544 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
545 " Failed to hipMalloc " << n_elem <<
" of "
546 <<
typeid(T).name() <<
" total " << n_bytes <<
"bytes. "
547 << hipGetErrorString(ierr) << std::endl;
555 dim3 thread_grid = 0;
557 n_blocks, thread_grid))
559 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
560 " Failed to determine launch properties. "
561 << hipGetErrorString(ierr) << std::endl;
566 hip_kernels::fill<T><<<block_grid, thread_grid>>>(ptr, n_elem, val);
567 if ((ierr = hipGetLastError()) != hipSuccess)
569 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
570 " Failed to launch the construct kernel. "
571 << hipGetErrorString(ierr) << std::endl;
575 #if defined(HAMR_VERBOSE)
578 std::cerr <<
"hip_malloc_allocator allocating array of " << n_elem
579 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
580 <<
" at " << ptr <<
" initialized to " << val << std::endl;
585 return std::shared_ptr<T>(ptr, hip_malloc_deleter<T>(ptr, n_elem));
589 template <
typename T>
590 template <
typename U>
592 hip_malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
593 ::allocate(
size_t n_elem,
const U *vals,
bool hipVals)
595 size_t n_bytes = n_elem*
sizeof(T);
599 hipError_t ierr = hipSuccess;
600 if ((ierr = hipMalloc(&ptr, n_bytes)) != hipSuccess)
602 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
603 " Failed to hipMalloc " << n_elem <<
" of "
604 <<
typeid(T).name() <<
" total " << n_bytes <<
"bytes. "
605 << hipGetErrorString(ierr) << std::endl;
613 size_t n_bytes_vals = n_elem*
sizeof(U);
615 if ((ierr = hipMalloc(&tmp, n_bytes_vals)) != hipSuccess)
617 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
618 " Failed to hipMalloc " << n_elem <<
" of "
619 <<
typeid(T).name() <<
" total " << n_bytes_vals <<
"bytes. "
620 << hipGetErrorString(ierr) << std::endl;
624 if ((ierr = hipMemcpy(tmp, vals, n_bytes_vals,
625 hipMemcpyHostToDevice)) != hipSuccess)
627 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
628 " Failed to hipMemcpy array of " << n_elem
629 <<
" of " <<
typeid(T).name() <<
" total " << n_bytes_vals <<
"bytes. "
630 << hipGetErrorString(ierr) << std::endl;
641 dim3 thread_grid = 0;
643 n_blocks, thread_grid))
645 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
646 " Failed to determine launch properties. "
647 << hipGetErrorString(ierr) << std::endl;
652 hip_kernels::fill<T><<<block_grid, thread_grid>>>(ptr, n_elem, vals);
653 if ((ierr = hipGetLastError()) != hipSuccess)
655 std::cerr <<
"[" << __FILE__ <<
":" << __LINE__ <<
"] ERROR:"
656 " Failed to launch the construct kernel. "
657 << hipGetErrorString(ierr) << std::endl;
668 #if defined(HAMR_VERBOSE)
671 std::cerr <<
"hip_malloc_allocator allocating array of " << n_elem
672 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
673 <<
" at " << ptr <<
" initialized from " << (hipVals ?
"HIP" :
"CPU")
674 <<
" array at " << vals << std::endl;
680 return std::shared_ptr<T>(ptr, hip_malloc_deleter<T>(ptr, n_elem));