HAMR
The Heterogeneous Accelerator Memory Resource
hamr_cuda_malloc_uva_allocator.h
1 #ifndef hamr_cuda_malloc_uva_allocator_h
2 #define hamr_cuda_malloc_uva_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 cuda_malloc_uva
12 template <typename T, typename E = void>
14 
15 /// a deleter for arrays allocated with cuda_malloc_uva, specialized for objects
16 template <typename T>
17 class cuda_malloc_uva_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  cuda_malloc_uva_deleter(cudaStream_t str, 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  cudaStream_t m_str;
36 };
37 
38 
39 
40 
41 
42 
43 /// a deleter for arrays allocated with cuda_malloc_uva, specialized for numbers
44 template <typename T>
45 class cuda_malloc_uva_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
46 {
47 public:
48  /** constructs the deleter
49  * @param[in] ptr the pointer to the array to delete
50  * @param[in] n the number of elements in the array
51  */
52  cuda_malloc_uva_deleter(cudaStream_t str, T *ptr, size_t n);
53 
54  /** deletes the array
55  * @param[in] ptr the pointer to the array to delete. must be the same as
56  * that passed during construction.
57  */
58  void operator()(T *ptr);
59 
60 private:
61  T *m_ptr;
62  size_t m_elem;
63 };
64 
65 
66 
67 
68 
69 
70 /// a class for allocating arrays with cuda_malloc_uva
71 template <typename T, typename E = void>
73 
74 /// a class for allocating arrays with cuda_malloc_uva, specialized for objects
75 template <typename T>
76 struct HAMR_EXPORT cuda_malloc_uva_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
77 {
78  /** allocate an array of n elements.
79  * @param[in] str a stream used to order operations, or null for the
80  * default stream
81  * @param[in] n the number of elements to allocate
82  * @returns a shared point to the array that holds a deleter for the memory
83  */
84  static std::shared_ptr<T> allocate(cudaStream_t str, size_t n);
85 
86  /** allocate an array of n elements.
87  * @param[in] str a stream used to order operations, or null for the
88  * default stream
89  * @param[in] n the number of elements to allocate
90  * @param[in] val a value to initialize the elements to
91  * @returns a shared point to the array that holds a deleter for the memory
92  */
93  static std::shared_ptr<T> allocate(cudaStream_t str, size_t n, const T &val);
94 
95  /** allocate an array of n elements.
96  * @param[in] str a stream used to order operations, or null for the
97  * default stream
98  * @param[in] n the number of elements to allocate
99  * @param[in] vals an array of values to initialize the elements with
100  * @param[in] cudaVals a flag that is set to true if vals is accessible from codes running in CUDA
101  * @returns a shared point to the array that holds a deleter for the memory
102  */
103  template <typename U>
104  static std::shared_ptr<T> allocate(cudaStream_t str, size_t n, const U *vals, bool cudaVals = false);
105 };
106 
107 
108 
109 
110 
111 /// a class for allocating arrays with cuda_malloc_uva, specialized for numbers
112 template <typename T>
113 struct HAMR_EXPORT cuda_malloc_uva_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
114 {
115  /** allocate an array of n elements.
116  * @param[in] str a stream used to order operations, or null for the
117  * default stream
118  * @param[in] n the number of elements to allocate
119  * @returns a shared point to the array that holds a deleter for the memory
120  */
121  static std::shared_ptr<T> allocate(cudaStream_t str, size_t n);
122 
123  /** allocate an array of n elements.
124  * @param[in] str a stream used to order operations, or null for the
125  * default stream
126  * @param[in] n the number of elements to allocate
127  * @param[in] val a value to initialize the elements to
128  * @returns a shared point to the array that holds a deleter for the memory
129  */
130  static std::shared_ptr<T> allocate(cudaStream_t str, size_t n, const T &val);
131 
132  /** allocate an array of n elements.
133  * @param[in] str a stream used to order operations, or null for the
134  * default stream
135  * @param[in] n the number of elements to allocate
136  * @param[in] vals an array of values to initialize the elements with
137  * @param[in] cudaVals a flag set to true if vals is accessible from codes running in CUDA
138  * @returns a shared point to the array that holds a deleter for the memory
139  */
140  template <typename U>
141  static std::shared_ptr<T> allocate(cudaStream_t str, size_t n, const U *vals, bool cudaVals = false);
142 };
143 
144 }
145 
146 #if !defined(HAMR_SEPARATE_IMPL)
147 #include "hamr_cuda_malloc_uva_allocator_impl.h"
148 #endif
149 
150 #endif
hamr::cuda_malloc_uva_allocator
a class for allocating arrays with cuda_malloc_uva
Definition: hamr_cuda_malloc_uva_allocator.h:72
hamr::cuda_malloc_uva_deleter
a deleter for arrays allocated with cuda_malloc_uva
Definition: hamr_cuda_malloc_uva_allocator.h:13
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13