HAMR
The Heterogeneous Accelerator Memory Resource
hamr_hip_malloc_allocator.h
1 #ifndef hamr_hip_malloc_allocator_h
2 #define hamr_hip_malloc_allocator_h
3 
4 #include "hamr_config.h"
5 #include <type_traits>
6 #include <memory>
7 
8 namespace hamr
9 {
10 
11 /// a deleter for arrays allocated with hip_malloc
12 template <typename T, typename E = void>
14 
15 /// a deleter for arrays allocated with hip_malloc, specialized for objects
16 template <typename T>
17 class HAMR_EXPORT hip_malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
18 {
19 public:
20  /** constructs the deleter
21  * @param[in] ptr the pointer to the array to delete
22  * @param[in] n the number of elements in the array
23  */
24  hip_malloc_deleter(T *ptr, size_t n);
25 
26  /** deletes the array
27  * @param[in] ptr the pointer to the array to delete. must be the same as
28  * that passed during construction.
29  */
30  void operator()(T *ptr);
31 
32 private:
33  T *m_ptr;
34  size_t m_elem;
35 };
36 
37 
38 
39 
40 
41 
42 /// a deleter for arrays allocated with hip_malloc, specialized for numbers
43 template <typename T>
44 class HAMR_EXPORT hip_malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
45 {
46 public:
47  /** constructs the deleter
48  * @param[in] ptr the pointer to the array to delete
49  * @param[in] n the number of elements in the array
50  */
51  hip_malloc_deleter(T *ptr, size_t n);
52 
53  /** deletes the array
54  * @param[in] ptr the pointer to the array to delete. must be the same as
55  * that passed during construction.
56  */
57  void operator()(T *ptr);
58 
59 private:
60  T *m_ptr;
61  size_t m_elem;
62 };
63 
64 
65 
66 
67 
68 
69 /// a class for allocating arrays with hip_malloc
70 template <typename T, typename E = void>
72 
73 /// a class for allocating arrays with hip_malloc, specialized for objects
74 template <typename T>
75 struct HAMR_EXPORT hip_malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
76 {
77  /** allocate an array of n elements.
78  * @param[in] n the number of elements to allocate
79  * @returns a shared pointer to the array that holds a deleter for the
80  * memory
81  */
82  static std::shared_ptr<T> allocate(size_t n);
83 
84  /** allocate an array of n elements.
85  * @param[in] n the number of elements to allocate
86  * @param[in] val a value to initialize the elements to
87  * @returns a shared pointer to the array that holds a deleter for the
88  * memory
89  */
90  static std::shared_ptr<T> allocate(size_t n, const T &val);
91 
92  /** allocate an array of n elements.
93  * @param[in] n the number of elements to allocate
94  * @param[in] vals an array of values to initialize the elements with
95  * @param[in] hipVals a flag set to true if vals are accessible by codes
96  * running in HIP
97  * @returns a shared pointer to the array that holds a deleter for the
98  * memory
99  */
100  template <typename U>
101  static std::shared_ptr<T> allocate(size_t n, const U *vals, bool hipVals = false);
102 };
103 
104 
105 
106 
107 
108 /// a class for allocating arrays with hip_malloc, specialized for numbers
109 template <typename T>
110 struct HAMR_EXPORT hip_malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
111 {
112  /** allocate an array of n elements.
113  * @param[in] n the number of elements to allocate
114  * @returns a shared pointer to the array that holds a deleter for the
115  * memory
116  */
117  static std::shared_ptr<T> allocate(size_t n);
118 
119  /** allocate an array of n elements.
120  * @param[in] n the number of elements to allocate
121  * @param[in] val a value to initialize the elements to
122  * @returns a shared pointer to the array that holds a deleter for the
123  * memory
124  */
125  static std::shared_ptr<T> allocate(size_t n, const T &val);
126 
127  /** allocate an array of n elements.
128  * @param[in] n the number of elements to allocate
129  * @param[in] vals an array of values to initialize the elements with
130  * @param[in] hipVals a flag set to true if vals are accessible by codes
131  * running in HIP
132  * @returns a shared pointer to the array that holds a
133  * deleter for the memory
134  */
135  template <typename U>
136  static std::shared_ptr<T> allocate(size_t n, const U *vals, bool hipVals = false);
137 };
138 
139 }
140 
141 #if !defined(HAMR_SEPARATE_IMPL)
142 #include "hamr_hip_malloc_allocator_impl.h"
143 #endif
144 
145 #endif
hamr::hip_malloc_allocator
a class for allocating arrays with hip_malloc
Definition: hamr_hip_malloc_allocator.h:71
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13
hamr::hip_malloc_deleter
a deleter for arrays allocated with hip_malloc
Definition: hamr_hip_malloc_allocator.h:13