102 PyTypeObject *Py_SyclDeviceType_;
103 PyTypeObject *PySyclDeviceType_;
104 PyTypeObject *Py_SyclContextType_;
105 PyTypeObject *PySyclContextType_;
106 PyTypeObject *Py_SyclEventType_;
107 PyTypeObject *PySyclEventType_;
108 PyTypeObject *Py_SyclQueueType_;
109 PyTypeObject *PySyclQueueType_;
110 PyTypeObject *Py_MemoryType_;
111 PyTypeObject *PyMemoryUSMDeviceType_;
112 PyTypeObject *PyMemoryUSMSharedType_;
113 PyTypeObject *PyMemoryUSMHostType_;
114 PyTypeObject *PyUSMArrayType_;
115 PyTypeObject *PySyclProgramType_;
116 PyTypeObject *PySyclKernelType_;
118 DPCTLSyclDeviceRef (*SyclDevice_GetDeviceRef_)(PySyclDeviceObject *);
119 PySyclDeviceObject *(*SyclDevice_Make_)(DPCTLSyclDeviceRef);
121 DPCTLSyclContextRef (*SyclContext_GetContextRef_)(PySyclContextObject *);
122 PySyclContextObject *(*SyclContext_Make_)(DPCTLSyclContextRef);
124 DPCTLSyclEventRef (*SyclEvent_GetEventRef_)(PySyclEventObject *);
125 PySyclEventObject *(*SyclEvent_Make_)(DPCTLSyclEventRef);
127 DPCTLSyclQueueRef (*SyclQueue_GetQueueRef_)(PySyclQueueObject *);
128 PySyclQueueObject *(*SyclQueue_Make_)(DPCTLSyclQueueRef);
131 DPCTLSyclUSMRef (*Memory_GetUsmPointer_)(Py_MemoryObject *);
132 void *(*Memory_GetOpaquePointer_)(Py_MemoryObject *);
133 DPCTLSyclContextRef (*Memory_GetContextRef_)(Py_MemoryObject *);
134 DPCTLSyclQueueRef (*Memory_GetQueueRef_)(Py_MemoryObject *);
135 size_t (*Memory_GetNumBytes_)(Py_MemoryObject *);
136 PyObject *(*Memory_Make_)(DPCTLSyclUSMRef,
142 DPCTLSyclKernelRef (*SyclKernel_GetKernelRef_)(PySyclKernelObject *);
143 PySyclKernelObject *(*SyclKernel_Make_)(DPCTLSyclKernelRef,
const char *);
145 DPCTLSyclKernelBundleRef (*SyclProgram_GetKernelBundleRef_)(
146 PySyclProgramObject *);
147 PySyclProgramObject *(*SyclProgram_Make_)(DPCTLSyclKernelBundleRef);
149 int USM_ARRAY_C_CONTIGUOUS_;
150 int USM_ARRAY_F_CONTIGUOUS_;
151 int USM_ARRAY_WRITABLE_;
152 int UAR_BOOL_, UAR_BYTE_, UAR_UBYTE_, UAR_SHORT_, UAR_USHORT_, UAR_INT_,
153 UAR_UINT_, UAR_LONG_, UAR_ULONG_, UAR_LONGLONG_, UAR_ULONGLONG_,
154 UAR_FLOAT_, UAR_DOUBLE_, UAR_CFLOAT_, UAR_CDOUBLE_, UAR_TYPE_SENTINEL_,
156 int UAR_INT8_, UAR_UINT8_, UAR_INT16_, UAR_UINT16_, UAR_INT32_, UAR_UINT32_,
157 UAR_INT64_, UAR_UINT64_;
159 bool PySyclDevice_Check_(PyObject *obj)
const
161 return PyObject_TypeCheck(obj, PySyclDeviceType_) != 0;
163 bool PySyclContext_Check_(PyObject *obj)
const
165 return PyObject_TypeCheck(obj, PySyclContextType_) != 0;
167 bool PySyclEvent_Check_(PyObject *obj)
const
169 return PyObject_TypeCheck(obj, PySyclEventType_) != 0;
171 bool PySyclQueue_Check_(PyObject *obj)
const
173 return PyObject_TypeCheck(obj, PySyclQueueType_) != 0;
175 bool PySyclKernel_Check_(PyObject *obj)
const
177 return PyObject_TypeCheck(obj, PySyclKernelType_) != 0;
179 bool PySyclProgram_Check_(PyObject *obj)
const
181 return PyObject_TypeCheck(obj, PySyclProgramType_) != 0;
186 as_usm_memory_.reset();
187 default_usm_ndarray_.reset();
188 default_usm_memory_.reset();
189 default_sycl_queue_.reset();
198 py::object default_sycl_queue_pyobj()
200 return *default_sycl_queue_;
202 py::object default_usm_memory_pyobj()
204 return *default_usm_memory_;
206 py::object default_usm_ndarray_pyobj()
208 return *default_usm_ndarray_;
210 py::object as_usm_memory_pyobj()
212 return *as_usm_memory_;
218 void operator()(py::object *p)
const
220 const bool initialized = Py_IsInitialized();
221#if PY_VERSION_HEX < 0x30d0000
222 const bool finalizing = _Py_IsFinalizing();
224 const bool finalizing = Py_IsFinalizing();
226 const bool guard = initialized && !finalizing;
234 std::shared_ptr<py::object> default_sycl_queue_;
235 std::shared_ptr<py::object> default_usm_memory_;
236 std::shared_ptr<py::object> default_usm_ndarray_;
237 std::shared_ptr<py::object> as_usm_memory_;
240 : Py_SyclDeviceType_(
nullptr), PySyclDeviceType_(
nullptr),
241 Py_SyclContextType_(
nullptr), PySyclContextType_(
nullptr),
242 Py_SyclEventType_(
nullptr), PySyclEventType_(
nullptr),
243 Py_SyclQueueType_(
nullptr), PySyclQueueType_(
nullptr),
244 Py_MemoryType_(
nullptr), PyMemoryUSMDeviceType_(
nullptr),
245 PyMemoryUSMSharedType_(
nullptr), PyMemoryUSMHostType_(
nullptr),
246 PyUSMArrayType_(
nullptr), PySyclProgramType_(
nullptr),
247 PySyclKernelType_(
nullptr), SyclDevice_GetDeviceRef_(
nullptr),
248 SyclDevice_Make_(
nullptr), SyclContext_GetContextRef_(
nullptr),
249 SyclContext_Make_(
nullptr), SyclEvent_GetEventRef_(
nullptr),
250 SyclEvent_Make_(
nullptr), SyclQueue_GetQueueRef_(
nullptr),
251 SyclQueue_Make_(
nullptr), Memory_GetUsmPointer_(
nullptr),
252 Memory_GetOpaquePointer_(
nullptr), Memory_GetContextRef_(
nullptr),
253 Memory_GetQueueRef_(
nullptr), Memory_GetNumBytes_(
nullptr),
254 Memory_Make_(
nullptr), SyclKernel_GetKernelRef_(
nullptr),
255 SyclKernel_Make_(
nullptr), SyclProgram_GetKernelBundleRef_(
nullptr),
256 SyclProgram_Make_(
nullptr), USM_ARRAY_C_CONTIGUOUS_(0),
257 USM_ARRAY_F_CONTIGUOUS_(0), USM_ARRAY_WRITABLE_(0), UAR_BOOL_(-1),
258 UAR_BYTE_(-1), UAR_UBYTE_(-1), UAR_SHORT_(-1), UAR_USHORT_(-1),
259 UAR_INT_(-1), UAR_UINT_(-1), UAR_LONG_(-1), UAR_ULONG_(-1),
260 UAR_LONGLONG_(-1), UAR_ULONGLONG_(-1), UAR_FLOAT_(-1),
261 UAR_DOUBLE_(-1), UAR_CFLOAT_(-1), UAR_CDOUBLE_(-1),
262 UAR_TYPE_SENTINEL_(-1), UAR_HALF_(-1), UAR_INT8_(-1), UAR_UINT8_(-1),
263 UAR_INT16_(-1), UAR_UINT16_(-1), UAR_INT32_(-1), UAR_UINT32_(-1),
264 UAR_INT64_(-1), UAR_UINT64_(-1), default_sycl_queue_{},
265 default_usm_memory_{}, default_usm_ndarray_{}, as_usm_memory_{}
270 import_dpctl___sycl_device();
271 import_dpctl___sycl_context();
272 import_dpctl___sycl_event();
273 import_dpctl___sycl_queue();
274 import_dpctl__memory___memory();
275 import_dpctl__program___program();
277 import_dpnp__tensor___usmarray();
280 this->Py_SyclDeviceType_ = &Py_SyclDeviceType;
281 this->PySyclDeviceType_ = &PySyclDeviceType;
282 this->Py_SyclContextType_ = &Py_SyclContextType;
283 this->PySyclContextType_ = &PySyclContextType;
284 this->Py_SyclEventType_ = &Py_SyclEventType;
285 this->PySyclEventType_ = &PySyclEventType;
286 this->Py_SyclQueueType_ = &Py_SyclQueueType;
287 this->PySyclQueueType_ = &PySyclQueueType;
288 this->Py_MemoryType_ = &Py_MemoryType;
289 this->PyMemoryUSMDeviceType_ = &PyMemoryUSMDeviceType;
290 this->PyMemoryUSMSharedType_ = &PyMemoryUSMSharedType;
291 this->PyMemoryUSMHostType_ = &PyMemoryUSMHostType;
292 this->PyUSMArrayType_ = &PyUSMArrayType;
293 this->PySyclProgramType_ = &PySyclProgramType;
294 this->PySyclKernelType_ = &PySyclKernelType;
297 this->SyclDevice_GetDeviceRef_ = SyclDevice_GetDeviceRef;
298 this->SyclDevice_Make_ = SyclDevice_Make;
301 this->SyclContext_GetContextRef_ = SyclContext_GetContextRef;
302 this->SyclContext_Make_ = SyclContext_Make;
305 this->SyclEvent_GetEventRef_ = SyclEvent_GetEventRef;
306 this->SyclEvent_Make_ = SyclEvent_Make;
309 this->SyclQueue_GetQueueRef_ = SyclQueue_GetQueueRef;
310 this->SyclQueue_Make_ = SyclQueue_Make;
313 this->Memory_GetUsmPointer_ = Memory_GetUsmPointer;
314 this->Memory_GetOpaquePointer_ = Memory_GetOpaquePointer;
315 this->Memory_GetContextRef_ = Memory_GetContextRef;
316 this->Memory_GetQueueRef_ = Memory_GetQueueRef;
317 this->Memory_GetNumBytes_ = Memory_GetNumBytes;
318 this->Memory_Make_ = Memory_Make;
321 this->SyclKernel_GetKernelRef_ = SyclKernel_GetKernelRef;
322 this->SyclKernel_Make_ = SyclKernel_Make;
323 this->SyclProgram_GetKernelBundleRef_ = SyclProgram_GetKernelBundleRef;
324 this->SyclProgram_Make_ = SyclProgram_Make;
327 this->USM_ARRAY_C_CONTIGUOUS_ = USM_ARRAY_C_CONTIGUOUS;
328 this->USM_ARRAY_F_CONTIGUOUS_ = USM_ARRAY_F_CONTIGUOUS;
329 this->USM_ARRAY_WRITABLE_ = USM_ARRAY_WRITABLE;
330 this->UAR_BOOL_ = UAR_BOOL;
331 this->UAR_BYTE_ = UAR_BYTE;
332 this->UAR_UBYTE_ = UAR_UBYTE;
333 this->UAR_SHORT_ = UAR_SHORT;
334 this->UAR_USHORT_ = UAR_USHORT;
335 this->UAR_INT_ = UAR_INT;
336 this->UAR_UINT_ = UAR_UINT;
337 this->UAR_LONG_ = UAR_LONG;
338 this->UAR_ULONG_ = UAR_ULONG;
339 this->UAR_LONGLONG_ = UAR_LONGLONG;
340 this->UAR_ULONGLONG_ = UAR_ULONGLONG;
341 this->UAR_FLOAT_ = UAR_FLOAT;
342 this->UAR_DOUBLE_ = UAR_DOUBLE;
343 this->UAR_CFLOAT_ = UAR_CFLOAT;
344 this->UAR_CDOUBLE_ = UAR_CDOUBLE;
345 this->UAR_TYPE_SENTINEL_ = UAR_TYPE_SENTINEL;
346 this->UAR_HALF_ = UAR_HALF;
349 this->UAR_INT8_ = UAR_BYTE;
350 this->UAR_UINT8_ = UAR_UBYTE;
351 this->UAR_INT16_ = UAR_SHORT;
352 this->UAR_UINT16_ = UAR_USHORT;
354 platform_typeid_lookup<std::int32_t, long, int, short>(
355 UAR_LONG, UAR_INT, UAR_SHORT);
357 platform_typeid_lookup<std::uint32_t,
unsigned long,
unsigned int,
358 unsigned short>(UAR_ULONG, UAR_UINT,
361 platform_typeid_lookup<std::int64_t, long, long long, int>(
362 UAR_LONG, UAR_LONGLONG, UAR_INT);
364 platform_typeid_lookup<std::uint64_t,
unsigned long,
365 unsigned long long,
unsigned int>(
366 UAR_ULONG, UAR_ULONGLONG, UAR_UINT);
371 PySyclQueueObject *py_q_tmp =
372 SyclQueue_Make(
reinterpret_cast<DPCTLSyclQueueRef
>(&q_));
373 const py::object &py_sycl_queue = py::reinterpret_steal<py::object>(
374 reinterpret_cast<PyObject *
>(py_q_tmp));
376 default_sycl_queue_ = std::shared_ptr<py::object>(
377 new py::object(py_sycl_queue), Deleter{});
379 py::module_ mod_memory = py::module_::import(
"dpctl.memory");
380 const py::object &py_as_usm_memory = mod_memory.attr(
"as_usm_memory");
381 as_usm_memory_ = std::shared_ptr<py::object>(
382 new py::object{py_as_usm_memory}, Deleter{});
384 auto mem_kl = mod_memory.attr(
"MemoryUSMHost");
385 const py::object &py_default_usm_memory =
386 mem_kl(1, py::arg(
"queue") = py_sycl_queue);
387 default_usm_memory_ = std::shared_ptr<py::object>(
388 new py::object{py_default_usm_memory}, Deleter{});
390 py::module_ mod_usmarray = py::module_::import(
"dpnp.tensor._usmarray");
391 auto tensor_kl = mod_usmarray.attr(
"usm_ndarray");
393 const py::object &py_default_usm_ndarray =
394 tensor_kl(py::tuple(), py::arg(
"dtype") = py::str(
"u1"),
395 py::arg(
"buffer") = py_default_usm_memory);
397 default_usm_ndarray_ = std::shared_ptr<py::object>(
398 new py::object{py_default_usm_ndarray}, Deleter{});
929 PYBIND11_OBJECT(
usm_ndarray, py::object, [](PyObject *o) ->
bool {
930 return PyObject_TypeCheck(
931 o, ::dpctl::detail::dpctl_capi::get().PyUSMArrayType_) != 0;
936 ::dpctl::detail::dpctl_capi::get().default_usm_ndarray_pyobj(),
940 throw py::error_already_set();
943 char *get_data()
const
945 PyUSMArrayObject *raw_ar = usm_array_ptr();
946 return raw_ar->data_;
949 template <
typename T>
952 return reinterpret_cast<T *
>(get_data());
957 PyUSMArrayObject *raw_ar = usm_array_ptr();
961 const py::ssize_t *get_shape_raw()
const
963 PyUSMArrayObject *raw_ar = usm_array_ptr();
964 return raw_ar->shape_;
967 std::vector<py::ssize_t> get_shape_vector()
const
969 auto raw_sh = get_shape_raw();
970 auto nd = get_ndim();
972 std::vector<py::ssize_t> shape_vector(raw_sh, raw_sh + nd);
976 py::ssize_t get_shape(
int i)
const
978 auto shape_ptr = get_shape_raw();
982 const py::ssize_t *get_strides_raw()
const
984 PyUSMArrayObject *raw_ar = usm_array_ptr();
985 return raw_ar->strides_;
988 std::vector<py::ssize_t> get_strides_vector()
const
990 auto raw_st = get_strides_raw();
991 auto nd = get_ndim();
993 if (raw_st ==
nullptr) {
994 auto is_c_contig = is_c_contiguous();
995 auto is_f_contig = is_f_contiguous();
996 auto raw_sh = get_shape_raw();
998 const auto &contig_strides = c_contiguous_strides(nd, raw_sh);
999 return contig_strides;
1001 else if (is_f_contig) {
1002 const auto &contig_strides = f_contiguous_strides(nd, raw_sh);
1003 return contig_strides;
1006 throw std::runtime_error(
"Invalid array encountered when "
1007 "building strides");
1011 std::vector<py::ssize_t> st_vec(raw_st, raw_st + nd);
1016 py::ssize_t get_size()
const
1018 PyUSMArrayObject *raw_ar = usm_array_ptr();
1020 int ndim = raw_ar->nd_;
1021 const py::ssize_t *shape = raw_ar->shape_;
1023 py::ssize_t nelems = 1;
1024 for (
int i = 0; i < ndim; ++i) {
1028 assert(nelems >= 0);
1032 std::pair<py::ssize_t, py::ssize_t> get_minmax_offsets()
const
1034 PyUSMArrayObject *raw_ar = usm_array_ptr();
1036 int nd = raw_ar->nd_;
1037 const py::ssize_t *shape = raw_ar->shape_;
1038 const py::ssize_t *strides = raw_ar->strides_;
1040 py::ssize_t offset_min = 0;
1041 py::ssize_t offset_max = 0;
1042 if (strides ==
nullptr) {
1043 py::ssize_t stride(1);
1044 for (
int i = 0; i < nd; ++i) {
1045 offset_max += stride * (shape[i] - 1);
1050 for (
int i = 0; i < nd; ++i) {
1051 py::ssize_t delta = strides[i] * (shape[i] - 1);
1052 if (strides[i] > 0) {
1053 offset_max += delta;
1056 offset_min += delta;
1060 return std::make_pair(offset_min, offset_max);
1063 sycl::queue get_queue()
const
1065 PyUSMArrayObject *raw_ar = usm_array_ptr();
1066 Py_MemoryObject *mem_obj =
1067 reinterpret_cast<Py_MemoryObject *
>(raw_ar->base_);
1069 auto const &api = ::dpctl::detail::dpctl_capi::get();
1070 DPCTLSyclQueueRef QRef = api.Memory_GetQueueRef_(mem_obj);
1071 return *(
reinterpret_cast<sycl::queue *
>(QRef));
1074 sycl::device get_device()
const
1076 PyUSMArrayObject *raw_ar = usm_array_ptr();
1077 Py_MemoryObject *mem_obj =
1078 reinterpret_cast<Py_MemoryObject *
>(raw_ar->base_);
1080 auto const &api = ::dpctl::detail::dpctl_capi::get();
1081 DPCTLSyclQueueRef QRef = api.Memory_GetQueueRef_(mem_obj);
1082 return reinterpret_cast<sycl::queue *
>(QRef)->get_device();
1085 int get_typenum()
const
1087 PyUSMArrayObject *raw_ar = usm_array_ptr();
1088 return raw_ar->typenum_;
1091 int get_flags()
const
1093 PyUSMArrayObject *raw_ar = usm_array_ptr();
1094 return raw_ar->flags_;
1097 int get_elemsize()
const
1099 int typenum = get_typenum();
1100 auto const &api = ::dpctl::detail::dpctl_capi::get();
1103 if (typenum == api.UAR_BOOL_)
1105 if (typenum == api.UAR_BYTE_)
1107 if (typenum == api.UAR_UBYTE_)
1109 if (typenum == api.UAR_SHORT_)
1111 if (typenum == api.UAR_USHORT_)
1113 if (typenum == api.UAR_INT_)
1115 if (typenum == api.UAR_UINT_)
1117 if (typenum == api.UAR_LONG_)
1118 return sizeof(long);
1119 if (typenum == api.UAR_ULONG_)
1120 return sizeof(
unsigned long);
1121 if (typenum == api.UAR_LONGLONG_)
1123 if (typenum == api.UAR_ULONGLONG_)
1125 if (typenum == api.UAR_FLOAT_)
1127 if (typenum == api.UAR_DOUBLE_)
1129 if (typenum == api.UAR_CFLOAT_)
1131 if (typenum == api.UAR_CDOUBLE_)
1133 if (typenum == api.UAR_HALF_)
1139 bool is_c_contiguous()
const
1141 int flags = get_flags();
1142 auto const &api = ::dpctl::detail::dpctl_capi::get();
1143 return static_cast<bool>(flags & api.USM_ARRAY_C_CONTIGUOUS_);
1146 bool is_f_contiguous()
const
1148 int flags = get_flags();
1149 auto const &api = ::dpctl::detail::dpctl_capi::get();
1150 return static_cast<bool>(flags & api.USM_ARRAY_F_CONTIGUOUS_);
1153 bool is_writable()
const
1155 int flags = get_flags();
1156 auto const &api = ::dpctl::detail::dpctl_capi::get();
1157 return static_cast<bool>(flags & api.USM_ARRAY_WRITABLE_);
1163 PyUSMArrayObject *raw_ar = usm_array_ptr();
1165 PyObject *usm_data = raw_ar->base_;
1166 Py_XINCREF(usm_data);
1169 return py::reinterpret_steal<py::object>(usm_data);
1172 bool is_managed_by_smart_ptr()
const
1174 PyUSMArrayObject *raw_ar = usm_array_ptr();
1175 PyObject *usm_data = raw_ar->base_;
1177 auto const &api = ::dpctl::detail::dpctl_capi::get();
1178 if (!PyObject_TypeCheck(usm_data, api.Py_MemoryType_)) {
1182 Py_MemoryObject *mem_obj =
1183 reinterpret_cast<Py_MemoryObject *
>(usm_data);
1184 const void *opaque_ptr = api.Memory_GetOpaquePointer_(mem_obj);
1186 return bool(opaque_ptr);
1189 const std::shared_ptr<void> &get_smart_ptr_owner()
const
1191 PyUSMArrayObject *raw_ar = usm_array_ptr();
1192 PyObject *usm_data = raw_ar->base_;
1194 auto const &api = ::dpctl::detail::dpctl_capi::get();
1196 if (!PyObject_TypeCheck(usm_data, api.Py_MemoryType_)) {
1197 throw std::runtime_error(
1198 "usm_ndarray object does not have Memory object "
1199 "managing lifetime of USM allocation");
1202 Py_MemoryObject *mem_obj =
1203 reinterpret_cast<Py_MemoryObject *
>(usm_data);
1204 void *opaque_ptr = api.Memory_GetOpaquePointer_(mem_obj);
1208 reinterpret_cast<std::shared_ptr<void> *
>(opaque_ptr);
1212 throw std::runtime_error(
1213 "Memory object underlying usm_ndarray does not have "
1214 "smart pointer managing lifetime of USM allocation");
1219 PyUSMArrayObject *usm_array_ptr()
const
1221 return reinterpret_cast<PyUSMArrayObject *
>(m_ptr);