1 #ifndef hamr_buffer_handle_h
2 #define hamr_buffer_handle_h
4 #include "hamr_config.h"
7 #include "hamr_gil_state.h"
19 #define array_interface_tt_declare(ENDIAN, KIND, SIZE, CPP_T) \
21 template <> struct array_interface_tt<CPP_T> \
24 static constexpr size_t elemsize() \
29 static constexpr char typekind() \
34 static constexpr bool little_endian() \
36 return (#ENDIAN) [0] == '<'; \
39 static constexpr const char *descr() \
41 return #ENDIAN #KIND #SIZE; \
44 array_interface_tt_declare(<, i, 1,
char)
45 array_interface_tt_declare(<, i, 2,
short)
46 array_interface_tt_declare(<, i, 4,
int)
47 array_interface_tt_declare(<, i, 8,
long)
48 array_interface_tt_declare(<, i, 8,
long long)
49 array_interface_tt_declare(<, u, 1,
unsigned char)
50 array_interface_tt_declare(<, u, 2,
unsigned short)
51 array_interface_tt_declare(<, u, 4,
unsigned int)
52 array_interface_tt_declare(<, u, 8,
unsigned long)
53 array_interface_tt_declare(<, u, 8,
unsigned long long)
54 array_interface_tt_declare(<, f, 4,
float)
55 array_interface_tt_declare(<, f, 8,
double)
62 #define buffer_handle_tt_declare(CPP_T, SWIG_T) \
63 template <> struct buffer_handle_tt<CPP_T> \
65 static swig_type_info *swig_type() \
71 buffer_handle_tt_declare(
float, SWIGTYPE_p_hamr__buffer_handleT_float_t)
72 buffer_handle_tt_declare(
double, SWIGTYPE_p_hamr__buffer_handleT_double_t)
73 buffer_handle_tt_declare(
char, SWIGTYPE_p_hamr__buffer_handleT_char_t)
74 buffer_handle_tt_declare(
short, SWIGTYPE_p_hamr__buffer_handleT_short_t)
75 buffer_handle_tt_declare(
int, SWIGTYPE_p_hamr__buffer_handleT_int_t)
76 buffer_handle_tt_declare(
long, SWIGTYPE_p_hamr__buffer_handleT_long_t)
77 buffer_handle_tt_declare(
long long, SWIGTYPE_p_hamr__buffer_handleT_long_long_t)
78 buffer_handle_tt_declare(
unsigned char, SWIGTYPE_p_hamr__buffer_handleT_unsigned_char_t)
79 buffer_handle_tt_declare(
unsigned short, SWIGTYPE_p_hamr__buffer_handleT_unsigned_short_t)
80 buffer_handle_tt_declare(
unsigned int, SWIGTYPE_p_hamr__buffer_handleT_unsigned_int_t)
81 buffer_handle_tt_declare(
unsigned long, SWIGTYPE_p_hamr__buffer_handleT_unsigned_long_t)
82 buffer_handle_tt_declare(
unsigned long long, SWIGTYPE_p_hamr__buffer_handleT_unsigned_long_long_t)
97 m_read_only(0), m_host_accessible(0), m_cuda_accessible(0),
107 buffer_handle(
const std::shared_ptr<const T> &src,
size_t size,
129 PyObject *get_cuda_array_interface();
134 PyObject *get_numpy_array_interface();
142 PyObject *get_array_interface();
144 void to_stream(std::ostream &os)
const;
146 std::shared_ptr<T> m_data;
149 int m_host_accessible;
150 int m_cuda_accessible;
155 template <
typename T>
156 std::ostream &operator<<(std::ostream &os,
const buffer_handle<T> &buf)
163 template <
typename T>
164 void buffer_handle<T>::to_stream(std::ostream &os)
const
166 os <<
"buffer_handle<" <<
typeid(T).name() <<
sizeof(T)
167 <<
"> m_data = " <<
size_t(this->m_data.get())
168 <<
" m_size = " << this->m_size <<
" m_read_only = "
169 << this->m_read_only <<
" m_host_accessible = "
170 << this->m_host_accessible <<
" m_cuda_accessible = "
171 << this->m_cuda_accessible;
175 template <
typename T>
178 size_t stream) : m_data(src), m_size(size), m_read_only(read_only),
184 std::cerr <<
"buffer_handle::construct " << *
this << std::endl;
189 template <
typename T>
198 template <
typename T>
203 std::cerr <<
"buffer_handle::destruct " << *
this << std::endl;
208 template <
typename T>
210 m_data(other.m_data), m_size(other.m_size),
211 m_read_only(other.m_read_only), m_host_accessible(other.m_host_accessible),
212 m_cuda_accessible(other.m_cuda_accessible), m_stream(other.m_stream)
216 std::cerr <<
"buffer_handle::copy construct " << *
this << std::endl;
221 template <
typename T>
223 m_data(std::move(other.m_data)), m_size(other.m_size),
224 m_read_only(other.m_read_only), m_host_accessible(other.m_host_accessible),
225 m_cuda_accessible(other.m_cuda_accessible), m_stream(other.m_stream)
229 std::cerr <<
"buffer_handle::move construct " << *
this << std::endl;
234 template <
typename T>
239 if (!m_cuda_accessible)
241 PyErr_SetString(PyExc_AttributeError,
242 "The data is not accessible from CUDA");
248 std::cerr <<
"buffer_handle<" <<
typeid(T).name() <<
sizeof(T)
249 <<
">::get_cuda_array_interface()" << std::endl;
252 return this->get_array_interface();
256 template <
typename T>
261 if (!m_host_accessible)
263 PyErr_SetString(PyExc_AttributeError,
264 "The data is not accessible from the host");
270 std::cerr <<
"buffer_handle<" <<
typeid(T).name() <<
sizeof(T)
271 <<
">::get_numpy_array_interface()" << std::endl;
274 return this->get_array_interface();
278 template <
typename T>
282 PyObject *shape = PyTuple_New(1);
283 PyTuple_SetItem(shape, 0, PyLong_FromLong(m_size));
290 PyObject *descr_tup = PyTuple_New(2);
291 PyTuple_SetItem(descr_tup, 0, PyUnicode_FromString(
""));
292 PyTuple_SetItem(descr_tup, 1, typestr);
293 PyObject *descr = PyList_New(1);
294 PyList_SetItem(descr, 0, descr_tup);
297 PyObject *
data = PyTuple_New(2);
298 PyTuple_SetItem(
data, 0, PyLong_FromSize_t(
size_t(m_data.get())));
299 PyTuple_SetItem(
data, 1, PyBool_FromLong(m_read_only));
302 PyObject *strides = Py_None;
305 PyObject *mask = Py_None;
308 PyObject *strm = PyLong_FromSize_t(m_stream);
311 PyObject *version = PyLong_FromLong(3);
314 PyObject *aint = PyDict_New();
315 PyDict_SetItemString(aint,
"shape", shape);
316 PyDict_SetItemString(aint,
"typestr", typestr);
317 PyDict_SetItemString(aint,
"descr", descr);
318 PyDict_SetItemString(aint,
"data",
data);
319 PyDict_SetItemString(aint,
"strides", strides);
320 PyDict_SetItemString(aint,
"mask", mask);
321 PyDict_SetItemString(aint,
"stream", strm);
322 PyDict_SetItemString(aint,
"version", version);