CPPuddle
hip_underlying_allocators.hpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2024 Gregor Daiß
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef HIP_UNDERLYING_ALLOCATORS_HPP
7 #define HIP_UNDERLYING_ALLOCATORS_HPP
8 
9 #include <hip/hip_runtime.h>
10 #include <stdexcept>
11 #include <string>
12 
13 namespace cppuddle {
14 namespace memory_recycling {
15 namespace detail {
17 template <class T> struct hip_pinned_allocator {
18  using value_type = T;
19  hip_pinned_allocator() noexcept = default;
20  template <class U>
21  explicit hip_pinned_allocator(hip_pinned_allocator<U> const &) noexcept {}
22  T *allocate(std::size_t n) {
23  T *data;
24  // hipError_t error =
25  // hipMallocHost(reinterpret_cast<void **>(&data), n * sizeof(T));
26 
27  // Even though marked as deprecated, the HIP docs recommend using hipHostMalloc
28  // (not hipMallocHost) for async memcpys
29  // https://rocmdocs.amd.com/en/latest/ROCm_API_References/HIP_API/Memory-Management.html#hipmemcpyasync
30  hipError_t error =
31  hipHostMalloc(reinterpret_cast<void **>(&data), n * sizeof(T));
32  if (error != hipSuccess) {
33  std::string msg =
34  std::string(
35  "hip_pinned_allocator failed due to hipMallocHost failure : ") +
36  std::string(hipGetErrorString(error));
37  throw std::runtime_error(msg);
38  }
39  return data;
40  }
41  void deallocate(T *p, std::size_t n) {
42  hipError_t error = hipHostFree(p);
43  if (error != hipSuccess) {
44  std::string msg =
45  std::string(
46  "hip_pinned_allocator failed due to hipFreeHost failure : ") +
47  std::string(hipGetErrorString(error));
48  throw std::runtime_error(msg);
49  }
50  }
51 };
52 template <class T, class U>
53 constexpr bool operator==(hip_pinned_allocator<T> const &,
54  hip_pinned_allocator<U> const &) noexcept {
55  return true;
56 }
57 template <class T, class U>
58 constexpr bool operator!=(hip_pinned_allocator<T> const &,
59  hip_pinned_allocator<U> const &) noexcept {
60  return false;
61 }
62 
64 template <class T> struct hip_device_allocator {
65  using value_type = T;
66  hip_device_allocator() noexcept = default;
67  template <class U>
68  explicit hip_device_allocator(hip_device_allocator<U> const &) noexcept {}
69  T *allocate(std::size_t n) {
70  T *data;
71  hipError_t error = hipMalloc(&data, n * sizeof(T));
72  if (error != hipSuccess) {
73  std::string msg =
74  std::string(
75  "hip_device_allocator failed due to hipMalloc failure : ") +
76  std::string(hipGetErrorString(error));
77  throw std::runtime_error(msg);
78  }
79  return data;
80  }
81  void deallocate(T *p, std::size_t n) {
82  hipError_t error = hipFree(p);
83  if (error != hipSuccess) {
84  std::string msg =
85  std::string(
86  "hip_device_allocator failed due to hipFree failure : ") +
87  std::string(hipGetErrorString(error));
88  throw std::runtime_error(msg);
89  }
90  }
91 };
92 template <class T, class U>
93 constexpr bool operator==(hip_device_allocator<T> const &,
94  hip_device_allocator<U> const &) noexcept {
95  return true;
96 }
97 template <class T, class U>
98 constexpr bool operator!=(hip_device_allocator<T> const &,
99  hip_device_allocator<U> const &) noexcept {
100  return false;
101 }
102 
103 } // end namespace detail
104 } // namespace memory_recycling
105 } // end namespace cppuddle
106 
107 #endif
constexpr bool operator!=(recycle_allocator< T, Host_Allocator > const &, recycle_allocator< U, Host_Allocator > const &) noexcept
Definition: buffer_management.hpp:830
constexpr bool operator==(recycle_allocator< T, Host_Allocator > const &, recycle_allocator< U, Host_Allocator > const &) noexcept
Definition: buffer_management.hpp:821
Definition: config.hpp:31
Underlying allocator for HIP device memory.
Definition: hip_underlying_allocators.hpp:64
void deallocate(T *p, std::size_t n)
Definition: hip_underlying_allocators.hpp:81
T value_type
Definition: hip_underlying_allocators.hpp:65
T * allocate(std::size_t n)
Definition: hip_underlying_allocators.hpp:69
Underlying host allocator for HIP pinned memory.
Definition: hip_underlying_allocators.hpp:17
T * allocate(std::size_t n)
Definition: hip_underlying_allocators.hpp:22
T value_type
Definition: hip_underlying_allocators.hpp:18
void deallocate(T *p, std::size_t n)
Definition: hip_underlying_allocators.hpp:41