HAMR
The Heterogeneous Accelerator Memory Resource
hamr_buffer_allocator.h
Go to the documentation of this file.
1 #ifndef hamr_buffer_allocator_h
2 #define hamr_buffer_allocator_h
3 
4 ///@file
5 
6 #include "hamr_config.h"
7 #include <cassert>
8 
9 namespace hamr
10 {
11 
12 /// allocator types that may be used with hamr::buffer
13 enum class buffer_allocator
14 {
15  same = -2, ///< propagate the current allocator
16  none = -1, ///< no allocator specified
17  cpp = 0, ///< allocates memory with new
18  malloc = 1, ///< allocates memory with malloc
19  cuda = 2, ///< allocates memory with cudaMalloc
20  cuda_async = 3,///< allocates memory with cudaMallocAsync
21  cuda_uva = 4, ///< allocates memory with cudaMallocManaged
22  cuda_host = 5, ///< allocates memory with cudaMallocHost
23  hip = 6, ///< allocates memory with hipMalloc
24  hip_uva = 7, ///< allocates memory with hipMallocManaged
25  openmp = 8 ///< allocates memory with OpenMP device offload API
26 };
27 
28 /// return the human readable name of the allocator
29 HAMR_EXPORT
30 const char *get_allocator_name(buffer_allocator alloc);
31 
32 /// @returns true if the allocator creates host accessible memory
33 inline
34 HAMR_EXPORT
36 {
37  return (alloc == buffer_allocator::cpp) ||
38  (alloc == buffer_allocator::malloc) ||
39  (alloc == buffer_allocator::cuda_uva) ||
40  (alloc == buffer_allocator::cuda_host) ||
41  (alloc == buffer_allocator::hip_uva);
42 }
43 
44 /// @returns true if the allocator creates CUDA accessible memory
45 inline
46 HAMR_EXPORT
48 {
49  return (alloc == buffer_allocator::cuda) ||
50  (alloc == buffer_allocator::cuda_async) ||
51  (alloc == buffer_allocator::cuda_uva) ||
52  (alloc == buffer_allocator::hip) ||
53  (alloc == buffer_allocator::hip_uva) ||
54  (alloc == buffer_allocator::openmp);
55 }
56 
57 /// @returns true if the allocator creates HIP accessible memory
58 inline
59 HAMR_EXPORT
61 {
62  return (alloc == buffer_allocator::cuda) ||
63  (alloc == buffer_allocator::cuda_async) ||
64  (alloc == buffer_allocator::cuda_uva) ||
65  (alloc == buffer_allocator::hip) ||
66  (alloc == buffer_allocator::hip_uva);
67 }
68 
69 /// @returns true if the allocator creates OPENMP accessible memory
70 inline
71 HAMR_EXPORT
73 {
74  return (alloc == buffer_allocator::cuda) ||
75  (alloc == buffer_allocator::cuda_async) ||
76  (alloc == buffer_allocator::cuda_uva) ||
77  (alloc == buffer_allocator::openmp);
78 }
79 
80 /// asserts that the passed value is one of the known allocators
81 inline
82 HAMR_EXPORT
84 {
85  (void) alloc;
86  assert((alloc == buffer_allocator::cpp)
87  || (alloc == buffer_allocator::malloc)
88 #if defined(HAMR_ENABLE_CUDA)
89  || (alloc == buffer_allocator::cuda)
90  || (alloc == buffer_allocator::cuda_async)
91  || (alloc == buffer_allocator::cuda_uva)
92  || (alloc == buffer_allocator::cuda_host)
93 #endif
94 #if defined(HAMR_ENABLE_HIP)
95  || (alloc == buffer_allocator::hip)
96  || (alloc == buffer_allocator::hip_uva)
97 #endif
98 #if defined(HAMR_ENABLE_OPENMP)
99  || (alloc == buffer_allocator::openmp)
100 #endif
101  );
102 }
103 
104 /// get the allocator type most suitable for the current build configuration.
106 {
107 #if defined(HAMR_ENABLE_CUDA)
109 #elif defined(HAMR_ENABLE_HIP)
110  return buffer_allocator::hip;
111 #elif defined(HAMR_ENABLE_OPENMP)
113 #else
115 #endif
116 }
117 
118 /// get the allocator type most suitable for the current build configuration.
120 {
121 #if defined(HAMR_ENABLE_CUDA)
123 #elif defined(HAMR_ENABLE_HIP)
125 #elif defined(HAMR_ENABLE_OPENMP)
127 #else
129 #endif
130 }
131 
132 }
133 
134 #endif
hamr::buffer_allocator::same
@ same
propagate the current allocator
hamr::buffer_allocator::malloc
@ malloc
allocates memory with malloc
hamr::buffer_allocator::cuda_host
@ cuda_host
allocates memory with cudaMallocHost
hamr::buffer_allocator::none
@ none
no allocator specified
hamr::get_allocator_name
const HAMR_EXPORT char * get_allocator_name(buffer_allocator alloc)
return the human readable name of the allocator
hamr::buffer_allocator::cuda_uva
@ cuda_uva
allocates memory with cudaMallocManaged
hamr::buffer_allocator::openmp
@ openmp
allocates memory with OpenMP device offload API
hamr::openmp_accessible
HAMR_EXPORT int openmp_accessible(buffer_allocator alloc)
Definition: hamr_buffer_allocator.h:72
hamr::buffer_allocator::hip_uva
@ hip_uva
allocates memory with hipMallocManaged
hamr::get_device_allocator
HAMR_EXPORT buffer_allocator get_device_allocator()
get the allocator type most suitable for the current build configuration.
Definition: hamr_buffer_allocator.h:105
hamr::host_accessible
HAMR_EXPORT int host_accessible(buffer_allocator alloc)
Definition: hamr_buffer_allocator.h:35
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13
hamr::get_host_allocator
HAMR_EXPORT buffer_allocator get_host_allocator()
get the allocator type most suitable for the current build configuration.
Definition: hamr_buffer_allocator.h:119
hamr::buffer_allocator::cuda
@ cuda
allocates memory with cudaMalloc
hamr::cuda_accessible
HAMR_EXPORT int cuda_accessible(buffer_allocator alloc)
Definition: hamr_buffer_allocator.h:47
hamr::hip_accessible
HAMR_EXPORT int hip_accessible(buffer_allocator alloc)
Definition: hamr_buffer_allocator.h:60
hamr::buffer_allocator::cuda_async
@ cuda_async
allocates memory with cudaMallocAsync
hamr::assert_valid_allocator
HAMR_EXPORT void assert_valid_allocator(buffer_allocator alloc)
asserts that the passed value is one of the known allocators
Definition: hamr_buffer_allocator.h:83
hamr::buffer_allocator::hip
@ hip
allocates memory with hipMalloc
hamr::buffer_allocator
buffer_allocator
allocator types that may be used with hamr::buffer
Definition: hamr_buffer_allocator.h:13
hamr::buffer_allocator::cpp
@ cpp
allocates memory with new