-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamr_buffer_handle.h
335 lines (285 loc) · 11.6 KB
/
hamr_buffer_handle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef hamr_buffer_handle_h
#define hamr_buffer_handle_h
#include "hamr_config.h"
#include "hamr_env.h"
#include "hamr_buffer_allocator.h"
#include "hamr_gil_state.h"
#include <Python.h>
#include <memory>
#include <iostream>
namespace hamr
{
/// traits for Numpy's array interface protocol
template <typename cpp_t> struct array_interface_tt
{};
#define array_interface_tt_declare(ENDIAN, KIND, SIZE, CPP_T) \
/** CPP_T traits for Numpy's array interface protocol */ \
template <> struct array_interface_tt<CPP_T> \
{ \
/** number of bytes per element (SIZE) */ \
static constexpr size_t elemsize() \
{ \
return SIZE; \
} \
/** char code for the type (KIND) */ \
static constexpr char typekind() \
{ \
return (#KIND) [0]; \
} \
/** true if the machine order is little endian */ \
static constexpr bool little_endian() \
{ \
return (#ENDIAN) [0] == '<'; \
} \
/** Numpy type descriptor string (ENDIAN KIND SIZE) */ \
static constexpr const char *descr() \
{ \
return #ENDIAN #KIND #SIZE; \
} \
};
array_interface_tt_declare(<, i, 1, char)
array_interface_tt_declare(<, i, 2, short)
array_interface_tt_declare(<, i, 4, int)
array_interface_tt_declare(<, i, 8, long)
array_interface_tt_declare(<, i, 8, long long)
array_interface_tt_declare(<, u, 1, unsigned char)
array_interface_tt_declare(<, u, 2, unsigned short)
array_interface_tt_declare(<, u, 4, unsigned int)
array_interface_tt_declare(<, u, 8, unsigned long)
array_interface_tt_declare(<, u, 8, unsigned long long)
array_interface_tt_declare(<, f, 4, float)
array_interface_tt_declare(<, f, 8, double)
/// type traits for constructing SWIG wrapped objects
template <typename cpp_t> struct buffer_handle_tt
{};
#define buffer_handle_tt_declare(CPP_T, SWIG_T) \
template <> struct buffer_handle_tt<CPP_T> \
{ \
static swig_type_info *swig_type() \
{ \
return SWIG_T; \
}; \
};
buffer_handle_tt_declare(float, SWIGTYPE_p_hamr__buffer_handleT_float_t)
buffer_handle_tt_declare(double, SWIGTYPE_p_hamr__buffer_handleT_double_t)
buffer_handle_tt_declare(char, SWIGTYPE_p_hamr__buffer_handleT_char_t)
buffer_handle_tt_declare(short, SWIGTYPE_p_hamr__buffer_handleT_short_t)
buffer_handle_tt_declare(int, SWIGTYPE_p_hamr__buffer_handleT_int_t)
buffer_handle_tt_declare(long, SWIGTYPE_p_hamr__buffer_handleT_long_t)
buffer_handle_tt_declare(long long, SWIGTYPE_p_hamr__buffer_handleT_long_long_t)
buffer_handle_tt_declare(unsigned char, SWIGTYPE_p_hamr__buffer_handleT_unsigned_char_t)
buffer_handle_tt_declare(unsigned short, SWIGTYPE_p_hamr__buffer_handleT_unsigned_short_t)
buffer_handle_tt_declare(unsigned int, SWIGTYPE_p_hamr__buffer_handleT_unsigned_int_t)
buffer_handle_tt_declare(unsigned long, SWIGTYPE_p_hamr__buffer_handleT_unsigned_long_t)
buffer_handle_tt_declare(unsigned long long, SWIGTYPE_p_hamr__buffer_handleT_unsigned_long_long_t)
/** A resource management class that is used to keep data shared with Python
* codes from a ::hamr_buffer alive while it is being accessed by the Python
* codes. The class also implements the Numpy array interface protocol and
* the Numba CUDA array interface protocol enabling seamless access by those
* libraries.
*/
template <typename T>
class HAMR_EXPORT buffer_handle
{
public:
/// construct an empty, and unusable object
buffer_handle() : m_data(nullptr), m_size(0),
m_read_only(0), m_host_accessible(0), m_cuda_accessible(0),
m_stream(0)
{}
/// construct from existing data
buffer_handle(const std::shared_ptr<T> &src, size_t size,
int read_only, int host_accessible, int cuda_accessible,
size_t stream);
/// construct from existing read only data
buffer_handle(const std::shared_ptr<const T> &src, size_t size,
int host_accessible, int cuda_accessible, size_t stream);
/// destruct
~buffer_handle();
/// copy construct
buffer_handle(const buffer_handle<T> &other);
/// move construct
buffer_handle(buffer_handle<T> &&other);
/// copy assign
buffer_handle<T> &operator=(const buffer_handle<T> &) = default;
/// move assign
buffer_handle<T> &operator=(buffer_handle<T> &&) = default;
/** returns a dictionary as described in the Numba CUDA array interface
* protocol if the data is accessible from CUDA. Otherwise, an
* AttributeError is rasied. */
PyObject *get_cuda_array_interface();
/** returns a dictionary as described in the Numpy __array_interface__
* protocol if the data is accessible from CUDA. Otherwise, an
* AttributeError is rasied. */
PyObject *get_numpy_array_interface();
//private:
/** returns a dictionary as decribed in the Numpy array interface and Numba
* CUDA array interface protocols. The caller must ensure the data is
* accessible in the desired technology before use. See
* ::get_numpy_array_interface and ::get_cuda_array_interface which
* implement check ensuring that this is true. */
PyObject *get_array_interface();
void to_stream(std::ostream &os) const;
std::shared_ptr<T> m_data;
size_t m_size;
int m_read_only;
int m_host_accessible;
int m_cuda_accessible;
size_t m_stream;
};
// **************************************************************************
template <typename T>
std::ostream &operator<<(std::ostream &os, const buffer_handle<T> &buf)
{
buf.to_stream(os);
return os;
}
// --------------------------------------------------------------------------
template <typename T>
void buffer_handle<T>::to_stream(std::ostream &os) const
{
os << "buffer_handle<" << typeid(T).name() << sizeof(T)
<< "> m_data = " << size_t(this->m_data.get())
<< " m_size = " << this->m_size << " m_read_only = "
<< this->m_read_only << " m_host_accessible = "
<< this->m_host_accessible << " m_cuda_accessible = "
<< this->m_cuda_accessible;
}
// --------------------------------------------------------------------------
template <typename T>
buffer_handle<T>::buffer_handle(const std::shared_ptr<T> &src,
size_t size, int read_only, int host_accessible, int cuda_accessible,
size_t stream) : m_data(src), m_size(size), m_read_only(read_only),
m_host_accessible(host_accessible), m_cuda_accessible(cuda_accessible),
m_stream(stream)
{
if (hamr::get_verbose())
{
std::cerr << "buffer_handle::construct " << *this << std::endl;
}
}
// --------------------------------------------------------------------------
template <typename T>
buffer_handle<T>::buffer_handle(const std::shared_ptr<const T> &src,
size_t size, int host_accessible, int cuda_accessible,
size_t stream) : buffer_handle(std::const_pointer_cast<T>(src), size,
1, host_accessible, cuda_accessible, stream)
{
}
// --------------------------------------------------------------------------
template <typename T>
buffer_handle<T>::~buffer_handle()
{
if (hamr::get_verbose())
{
std::cerr << "buffer_handle::destruct " << *this << std::endl;
}
}
// --------------------------------------------------------------------------
template <typename T>
buffer_handle<T>::buffer_handle(const buffer_handle<T> &other) :
m_data(other.m_data), m_size(other.m_size),
m_read_only(other.m_read_only), m_host_accessible(other.m_host_accessible),
m_cuda_accessible(other.m_cuda_accessible), m_stream(other.m_stream)
{
if (hamr::get_verbose())
{
std::cerr << "buffer_handle::copy construct " << *this << std::endl;
}
}
// --------------------------------------------------------------------------
template <typename T>
buffer_handle<T>::buffer_handle(buffer_handle<T> &&other) :
m_data(std::move(other.m_data)), m_size(other.m_size),
m_read_only(other.m_read_only), m_host_accessible(other.m_host_accessible),
m_cuda_accessible(other.m_cuda_accessible), m_stream(other.m_stream)
{
if (hamr::get_verbose())
{
std::cerr << "buffer_handle::move construct " << *this << std::endl;
}
}
// --------------------------------------------------------------------------
template <typename T>
PyObject *buffer_handle<T>::get_cuda_array_interface()
{
hamr::gil_state gil;
if (!m_cuda_accessible)
{
PyErr_SetString(PyExc_AttributeError,
"The data is not accessible from CUDA");
Py_RETURN_NONE;
}
if (hamr::get_verbose())
{
std::cerr << "buffer_handle<" << typeid(T).name() << sizeof(T)
<< ">::get_cuda_array_interface()" << std::endl;
}
return this->get_array_interface();
}
// --------------------------------------------------------------------------
template <typename T>
PyObject *buffer_handle<T>::get_numpy_array_interface()
{
hamr::gil_state gil;
if (!m_host_accessible)
{
PyErr_SetString(PyExc_AttributeError,
"The data is not accessible from the host");
Py_RETURN_NONE;
}
if (hamr::get_verbose())
{
std::cerr << "buffer_handle<" << typeid(T).name() << sizeof(T)
<< ">::get_numpy_array_interface()" << std::endl;
}
return this->get_array_interface();
}
// --------------------------------------------------------------------------
template <typename T>
PyObject *buffer_handle<T>::get_array_interface()
{
// shape
PyObject *shape = PyTuple_New(1);
PyTuple_SetItem(shape, 0, PyLong_FromLong(m_size));
// typestr
PyObject *typestr = PyUnicode_FromString(array_interface_tt<T>::descr());
Py_INCREF(typestr); // for use in descr
// descr
PyObject *descr_tup = PyTuple_New(2);
PyTuple_SetItem(descr_tup, 0, PyUnicode_FromString(""));
PyTuple_SetItem(descr_tup, 1, typestr);
PyObject *descr = PyList_New(1);
PyList_SetItem(descr, 0, descr_tup);
// data
PyObject *data = PyTuple_New(2);
PyTuple_SetItem(data, 0, PyLong_FromSize_t(size_t(m_data.get())));
PyTuple_SetItem(data, 1, PyBool_FromLong(m_read_only));
// strides
PyObject *strides = Py_None;
// mask
PyObject *mask = Py_None;
// stream
PyObject *strm = PyLong_FromSize_t(m_stream);
// version
PyObject *version = PyLong_FromLong(3);
// build the __array_interface__ dictionary
PyObject *aint = PyDict_New();
PyDict_SetItemString(aint, "shape", shape);
PyDict_SetItemString(aint, "typestr", typestr);
PyDict_SetItemString(aint, "descr", descr);
PyDict_SetItemString(aint, "data", data);
PyDict_SetItemString(aint, "strides", strides);
PyDict_SetItemString(aint, "mask", mask);
PyDict_SetItemString(aint, "stream", strm);
PyDict_SetItemString(aint, "version", version);
Py_DECREF(shape);
Py_DECREF(typestr);
Py_DECREF(descr);
Py_DECREF(data);
Py_DECREF(strm);
Py_DECREF(version);
return aint;
}
}
#endif