33 #include <hip/hip_runtime.h>
36 #include <cuda_runtime.h>
103 template <
typename T>
116 throw std::invalid_argument(
"attempting to construct a TargetAllocator with an invalid target");
126 return this->_target;
136 throw std::invalid_argument(
"attempting to set TargetAllocator to an invalid target");
139 this->_target = new_target;
155 using type =
typename std::conditional<std::is_void<T>::value, char, T>::type;
157 std::size_t n_bytes = n *
sizeof(type);
158 switch (this->_target) {
161 std::allocator<type> std_alloc;
162 ptr = std_alloc.allocate(n);
168 std::memset((
void *) ptr, 0, n_bytes);
173 #ifdef HAS_ROCM_MODEL
181 if (this->_always_managed) {
182 error = hipMallocManaged(&hip_ptr, n_bytes);
186 int managed_memory = 0;
187 hipGetDevice(&device);
188 hipDeviceGetAttribute(&managed_memory, hipDeviceAttributeManagedMemory, device);
191 if (managed_memory) {
192 error = hipMallocManaged(&hip_ptr, n_bytes);
195 error = hipMalloc(&hip_ptr, n_bytes);
198 if (error != hipSuccess) {
199 throw std::runtime_error(hipGetErrorString(error));
201 error = hipMemset(hip_ptr, 0, n_bytes);
202 if (error != hipSuccess) {
203 throw std::runtime_error(hipGetErrorString(error));
208 throw std::invalid_argument(
"target MLIR_ROCM is unavailable");
212 #ifdef HAS_CUDA_MODEL
217 error = cudaMallocManaged(&cuda_ptr, n_bytes);
218 if (error != cudaSuccess) {
219 throw std::runtime_error(cudaGetErrorString(error));
221 error = cudaMemset(cuda_ptr, 0, n_bytes);
222 if (error != cudaSuccess) {
223 throw std::runtime_error(cudaGetErrorString(error));
228 throw std::invalid_argument(
"target MLIR_CUDA is unavailable");
232 throw std::invalid_argument(
"unknown allocation target");
247 using type =
typename std::conditional<std::is_void<T>::value, char, T>::type;
248 switch (this->_target) {
251 std::allocator<type> std_alloc;
252 std_alloc.deallocate((type*) ptr, n);
256 #ifdef HAS_ROCM_MODEL
259 error = hipFree(ptr);
260 if (error != hipSuccess) {
261 throw std::runtime_error(hipGetErrorString(error));
265 throw std::invalid_argument(
"target MLIR_ROCM is unavailable");
269 #ifdef HAS_CUDA_MODEL
272 error = cudaFree(ptr);
273 if (error != cudaSuccess) {
274 throw std::runtime_error(cudaGetErrorString(error));
278 throw std::invalid_argument(
"target MLIR_CUDA is unavailable");
282 throw std::invalid_argument(
"unknown allocation target");
288 bool _always_managed;
T * allocate_on_target(Target target, std::size_t n, bool always_managed=false, bool do_zero=true)
Utility function for allocating memory on a target. See TargetAllocator.
bool is_concrete(Target const target)
Checks if target is a real, concrete target.
Target
enum that represents different targets to run ionic models on.
@ MLIR_ROCM
ROCM code for AMD GPUs generated with MLIR.
@ CPU
baseline CPU model generated with the original opencarp code generator
@ UNKNOWN
special value to handle unknown targets
@ MLIR_CUDA
CUDA code for NVIDIA GPUs generated with MLIR.
@ N_TARGETS
a token to indicate the maximum number of targets
@ MLIR_CPU
vectorized CPU code generated with MLIR
std::string get_string_from_target(Target const target)
Get a string representation of a given target.
std::string get_target_list_string()
Returns a string containing the list of available targets.
bool is_gpu(Target const target)
Checks if this is a GPU target.
void deallocate_on_target(Target target, T *ptr)
Utility function for deallocating memory on a target. See TargetAllocator.
Target get_target_from_string(std::string const str)
Returns a value from the Target enum from a given string.
Allocator structure for dynamically allocating memory on multiple targets.
T * allocate(std::size_t n, bool do_zero=true)
Allocate memory for type T.
void deallocate(T *ptr, std::size_t n=0)
Deallocate memory pointed by ptr.
Target get_target() const
Get the target for this allocator.
TargetAllocator(Target target, bool always_managed=false)
Construct a TargetAllocator.
void set_target(Target new_target)
Set a new target for this allocator.
T value_type
type to allocate