HAMR
The Heterogeneous Accelerator Memory Resource
hamr_buffer_util.h
Go to the documentation of this file.
1 #ifndef buffer_util_h
2 #define buffer_util_h
3 
4 /// @file
5 
6 /// heterogeneous accelerator memory resource
7 namespace hamr
8 {
9 
10 /// @cond
11 template <typename TT>
13 {
14  return std::make_tuple();
15 }
16 /// @endcond
17 
18 /** Calls hamr::buffer::get_host_accessible on a number of hamr::buffer
19  * instances.
20  *
21  * @tparam TT hamr::buffer<NT>
22  * @tparam PP a paramater pack of TT
23  * @param b a hamr::buffer<NT> instance
24  * @param args zero or more hamr::buffer<NT> instances
25  * @returns a tuple of std::shared_ptr<NT> and NT* one for each
26  * hamr::buffer<NT> passed in.
27  */
28 template <typename TT, typename... PP>
29 auto get_host_accessible(const TT &b, PP &&... args)
30 {
31  auto spb = b.get_host_accessible();
32  return std::tuple_cat(std::make_tuple
33  (spb, spb.get()), get_host_accessible<TT>(args...));
34 }
35 
36 /// @cond
37 template <typename TT>
39 {
40  return std::make_tuple();
41 }
42 /// @endcond
43 
44 /** Calls hamr::buffer::get_cuda_accessible on a number of hamr::buffer
45  * instances.
46  *
47  * @tparam TT hamr::buffer<NT>
48  * @tparam PP a paramater pack of TT
49  * @param b a hamr::buffer<NT> instance
50  * @param args zero or more hamr::buffer<NT> instances
51  * @returns a tuple of std::shared_ptr<NT> and NT* one for each
52  * hamr::buffer<NT> passed in.
53  */
54 template <typename TT, typename... PP>
55 auto get_cuda_accessible(const TT &b, PP &&... args)
56 {
57  auto spb = b.get_cuda_accessible();
58  return std::tuple_cat(std::make_tuple
59  (spb, spb.get()), get_cuda_accessible<TT>(args...));
60 }
61 
62 /// @cond
63 template <typename TT>
64 auto get_hip_accessible()
65 {
66  return std::make_tuple();
67 }
68 /// @endcond
69 
70 /** Calls hamr::buffer::get_hip_accessible on a number of hamr::buffer
71  * instances.
72  *
73  * @tparam TT hamr::buffer<NT>
74  * @tparam PP a paramater pack of TT
75  * @param b a hamr::buffer<NT> instance
76  * @param args zero or more hamr::buffer<NT> instances
77  * @returns a tuple of std::shared_ptr<NT> and NT* one for each
78  * hamr::buffer<NT> passed in.
79  */
80 template <typename TT, typename... PP>
81 auto get_hip_accessible(const TT &b, PP &&... args)
82 {
83  auto spb = b.get_hip_accessible();
84  return std::tuple_cat(std::make_tuple
85  (spb, spb.get()), get_hip_accessible<TT>(args...));
86 }
87 
88 /// @cond
89 template <typename TT>
91 {
92  return std::make_tuple();
93 }
94 /// @endcond
95 
96 /** Calls hamr::buffer::get_openmp_accessible on a number of hamr::buffer
97  * instances.
98  *
99  * @tparam TT hamr::buffer<NT>
100  * @tparam PP a paramater pack of TT
101  * @param b a hamr::buffer<NT> instance
102  * @param args zero or more hamr::buffer<NT> instances
103  * @returns a tuple of std::shared_ptr<NT> and NT* one for each
104  * hamr::buffer<NT> passed in.
105  */
106 template <typename TT, typename... PP>
107 auto get_openmp_accessible(const TT &b, PP &&... args)
108 {
109  auto spb = b.get_openmp_accessible();
110  return std::tuple_cat(std::make_tuple
111  (spb, spb.get()), get_openmp_accessible<TT>(args...));
112 }
113 
114 /// @cond
115 template <typename TT>
117 {
118  return std::make_tuple();
119 }
120 /// @endcond
121 
122 /** Calls hamr::buffer::get_device_accessible on a number of hamr::buffer
123  * instances.
124  *
125  * @tparam TT hamr::buffer<NT>
126  * @tparam PP a paramater pack of TT
127  * @param b a hamr::buffer<NT> instance
128  * @param args zero or more hamr::buffer<NT> instances
129  * @returns a tuple of std::shared_ptr<NT> and NT* one for each
130  * hamr::buffer<NT> passed in.
131  */
132 template <typename TT, typename... PP>
133 auto get_device_accessible(const TT &b, PP &&... args)
134 {
135  auto spb = b.get_device_accessible();
136  return std::tuple_cat(std::make_tuple
137  (spb, spb.get()), get_device_accessible<TT>(args...));
138 }
139 
140 
141 /** Calls hamr::buffer::data on a number of hamr::buffer instances.
142  *
143  * @tparam PP a paramater pack of hamr::buffer<NT>
144  * @param args any number of hamr::buffer<NT> instances
145  * @returns a tuple of NT* one for each hamr::buffer<NT> passed in.
146  */
147 template <typename... PP>
148 auto data(PP &&... args)
149 {
150  return std::make_tuple(args.data()...);
151 }
152 
153 /** Calls hamr::buffer::pointer on a number of hamr::buffer instances.
154  *
155  * @tparam PP a paramater pack of hamr::buffer<NT>
156  * @param args any number of hamr::buffer<NT> instances
157  * @returns a tuple of std::shared_ptr<NT> one for each hamr::buffer<NT> passed in.
158  */
159 template <typename... PP>
160 auto pointer(PP &&... args)
161 {
162  return std::make_tuple(args.pointer()...);
163 }
164 
165 /** Calls hamr::buffer::synchronize on a number of hamr::buffer<NT> instances.
166  * Note however that one typically need not call synchronize on multiple buffer
167  * instances that share the same stream. Synchronizing on any one of them will
168  * synchronize all.
169  *
170  * @tparam PP a paramater pack of hamr::buffer<NT>
171  * @param args any number of hamr::buffer<NT> instances
172  */
173 template <typename... PP>
174 void synchronize(PP &&... args)
175 {
176  (args.synchronize(), ...);
177 }
178 
179 /** constructs an un-initialized hamr::buffer<NT> with space for n_elem
180  * allocated and returns it along with the writable pointer to it's contents.
181  *
182  * @param[in] alloc the allocator to allocate memory with
183  * @param[in] n_elem the initial size of the allocated memory
184  * @returns a std::tuple with the newly constructed buffer in the first slot
185  * and a writable pointer to its internal memory in the second
186  */
187 template <typename NT>
188 auto make_buffer(buffer_allocator alloc, size_t n_elem)
189 {
190  hamr::buffer<NT> buf(alloc, n_elem);
191  return std::make_tuple(buf, buf.data());
192 }
193 
194 /** constructs an hamr:buffer<NT> with space for n_elem allocated and
195  * initialized and returns it along with the writable pointer to it's contents.
196  *
197  * @param[in] alloc the allocator to allocate memory with
198  * @param[in] n_elem the initial size of the allocated memory
199  * @param[in] ival the value used to initialize the allocated memory
200  * @returns a std::tuple with the newly constructed buffer in the first slot
201  * and a writable pointer to its internal memory in the second
202  */
203 template <typename NT>
204 auto make_buffer(buffer_allocator alloc, size_t n_elem, const NT &ival)
205 {
206  hamr::buffer<NT> buf(alloc, n_elem, ival);
207  return std::make_tuple(buf, buf.data());
208 }
209 
210 }
211 #endif
hamr::synchronize
void synchronize(PP &&... args)
Definition: hamr_buffer_util.h:174
hamr::get_host_accessible
auto get_host_accessible(const TT &b, PP &&... args)
Definition: hamr_buffer_util.h:29
hamr::make_buffer
auto make_buffer(buffer_allocator alloc, size_t n_elem)
Definition: hamr_buffer_util.h:188
hamr::get_hip_accessible
auto get_hip_accessible(const TT &b, PP &&... args)
Definition: hamr_buffer_util.h:81
hamr::pointer
auto pointer(PP &&... args)
Definition: hamr_buffer_util.h:160
hamr::buffer
A technology agnostic buffer that manages memory on the host, GPUs, and other accelerators.
Definition: hamr_buffer.h:30
hamr::get_cuda_accessible
auto get_cuda_accessible(const TT &b, PP &&... args)
Definition: hamr_buffer_util.h:55
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13
hamr::get_openmp_accessible
auto get_openmp_accessible(const TT &b, PP &&... args)
Definition: hamr_buffer_util.h:107
hamr::buffer_allocator
buffer_allocator
allocator types that may be used with hamr::buffer
Definition: hamr_buffer_allocator.h:13
hamr::get_device_accessible
auto get_device_accessible(const TT &b, PP &&... args)
Definition: hamr_buffer_util.h:133
hamr::data
auto data(PP &&... args)
Definition: hamr_buffer_util.h:148