HAMR
The Heterogeneous Accelerator Memory Resource
hamr_openmp_allocator.h
1 #ifndef hamr_openmp_allocator_h
2 #define hamr_openmp_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 OpenMP
11 template <typename T, typename E = void>
12 class openmp_deleter {};
13 
14 /// a deleter for arrays allocated with OpenMP, specialized for objects
15 template <typename T>
16 class HAMR_EXPORT openmp_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  openmp_deleter(T *ptr, size_t n, int dev);
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  int m_dev;
35 };
36 
37 
38 
39 
40 
41 
42 /// a deleter for arrays allocated with OpenMP, specialized for numbers
43 template <typename T>
44 class HAMR_EXPORT openmp_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  openmp_deleter(T *ptr, size_t n, int dev);
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  int m_dev;
63 };
64 
65 
66 
67 
68 
69 
70 
71 /// a class for allocating arrays with OpenMP
72 template <typename T, typename E = void>
73 struct openmp_allocator {};
74 
75 /// a class for allocating arrays with OpenMP, specialized for objects
76 template <typename T>
77 struct HAMR_EXPORT openmp_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
78 {
79  /** allocate an array of n elements.
80  * @param[in] n the number of elements to allocate
81  * @returns a shared pointer to the array that holds a deleter for the memory
82  */
83  static std::shared_ptr<T> allocate(size_t n) HAMR_EXPORT;
84 
85  /** allocate an array of n elements.
86  * @param[in] n the number of elements to allocate
87  * @param[in] val a value to initialize the elements to
88  * @returns a shared pointer to the array that holds a deleter for the memory
89  */
90 
91  static std::shared_ptr<T> allocate(size_t n, const T &val) HAMR_EXPORT;
92 
93  /** allocate an array of n elements.
94  * @param[in] n the number of elements to allocate
95  * @param[in] vals an array of n elements to initialize the elements with
96  * @returns a shared pointer to the array that holds a deleter for the memory
97  */
98  template <typename U>
99  static std::shared_ptr<T> allocate(size_t n, const U *vals) HAMR_EXPORT;
100 };
101 
102 
103 
104 
105 
106 
107 /// a class for allocating arrays with OpenMP, specialized for numbers
108 template <typename T>
109 struct HAMR_EXPORT openmp_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
110 {
111  /** allocate an array of n elements.
112  * @param[in] n the number of elements to allocate
113  * @returns a shared pointer to the array that holds a deleter for the memory
114  */
115  static std::shared_ptr<T> allocate(size_t n) HAMR_EXPORT;
116 
117  /** allocate an array of n elements.
118  * @param[in] n the number of elements to allocate
119  * @param[in] val a value to initialize the elements to
120  * @returns a shared pointer to the array that holds a deleter for the memory
121  */
122  static std::shared_ptr<T> allocate(size_t n, const T &val) HAMR_EXPORT;
123 
124  /** allocate an array of n elements.
125  * @param[in] n the number of elements to allocate
126  * @param[in] vals an array of n elements to initialize the elements with
127  * @returns a shared pointer to the array that holds a deleter for the memory
128  */
129  template <typename U>
130  static std::shared_ptr<T> allocate(size_t n, const U *vals) HAMR_EXPORT;
131 };
132 
133 }
134 
135 #if !defined(HAMR_SEPARATE_IMPL)
136 #include "hamr_openmp_allocator_impl.h"
137 #endif
138 
139 #endif
hamr::openmp_allocator
a class for allocating arrays with OpenMP
Definition: hamr_openmp_allocator.h:73
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13
hamr::openmp_deleter
a deleter for arrays allocated with OpenMP
Definition: hamr_openmp_allocator.h:12