18 #include <hip/hip_runtime.h>
21 #include <cuda_runtime.h>
99 TargetAllocator(
Target target,
bool always_managed =
false) : _target(target), _always_managed(always_managed) {
101 throw std::invalid_argument(
"attempting to construct a TargetAllocator with an invalid target");
111 return this->_target;
121 throw std::invalid_argument(
"attempting to set TargetAllocator to an invalid target");
124 this->_target = new_target;
140 using type =
typename std::conditional<std::is_void<T>::value, char, T>::type;
142 std::size_t n_bytes = n *
sizeof(type);
143 switch (this->_target) {
146 std::allocator<type> std_alloc;
149 ptr = std_alloc.allocate(n);
155 std::memset((
void *) ptr, 0, n_bytes);
162 #ifdef HAS_ROCM_MODEL
170 if (this->_always_managed) {
171 error = hipMallocManaged(&hip_ptr, n_bytes);
175 int managed_memory = 0;
176 hipGetDevice(&device);
177 hipDeviceGetAttribute(&managed_memory, hipDeviceAttributeManagedMemory, device);
180 if (managed_memory) {
181 error = hipMallocManaged(&hip_ptr, n_bytes);
184 error = hipMalloc(&hip_ptr, n_bytes);
187 if (error != hipSuccess) {
188 throw std::runtime_error(hipGetErrorString(error));
190 error = hipMemset(hip_ptr, 0, n_bytes);
191 if (error != hipSuccess) {
192 throw std::runtime_error(hipGetErrorString(error));
197 throw std::invalid_argument(
"target MLIR_ROCM is unavailable");
201 #ifdef HAS_CUDA_MODEL
206 error = cudaMallocManaged(&cuda_ptr, n_bytes);
207 if (error != cudaSuccess) {
208 throw std::runtime_error(cudaGetErrorString(error));
210 error = cudaMemset(cuda_ptr, 0, n_bytes);
211 if (error != cudaSuccess) {
212 throw std::runtime_error(cudaGetErrorString(error));
217 throw std::invalid_argument(
"target MLIR_CUDA is unavailable");
221 throw std::invalid_argument(
"unknown allocation target");
236 using type =
typename std::conditional<std::is_void<T>::value, char, T>::type;
237 switch (this->_target) {
240 std::allocator<type> std_alloc;
241 std_alloc.deallocate((type*) ptr, n);
245 #ifdef HAS_ROCM_MODEL
248 error = hipFree(ptr);
249 if (error != hipSuccess) {
250 throw std::runtime_error(hipGetErrorString(error));
254 throw std::invalid_argument(
"target MLIR_ROCM is unavailable");
258 #ifdef HAS_CUDA_MODEL
261 error = cudaFree(ptr);
262 if (error != cudaSuccess) {
263 throw std::runtime_error(cudaGetErrorString(error));
267 throw std::invalid_argument(
"target MLIR_CUDA is unavailable");
271 throw std::invalid_argument(
"unknown allocation target");
277 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