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