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() {
return *default_sycl_queue_; }
199 py::object default_usm_memory_pyobj() {
return *default_usm_memory_; }
200 py::object default_usm_ndarray_pyobj() {
return *default_usm_ndarray_; }
201 py::object as_usm_memory_pyobj() {
return *as_usm_memory_; }
206 void operator()(py::object *p)
const
208 const bool initialized = Py_IsInitialized();
209#if PY_VERSION_HEX < 0x30d0000
210 const bool finalizing = _Py_IsFinalizing();
212 const bool finalizing = Py_IsFinalizing();
214 const bool guard = initialized && !finalizing;
222 std::shared_ptr<py::object> default_sycl_queue_;
223 std::shared_ptr<py::object> default_usm_memory_;
224 std::shared_ptr<py::object> default_usm_ndarray_;
225 std::shared_ptr<py::object> as_usm_memory_;
228 : Py_SyclDeviceType_(
nullptr), PySyclDeviceType_(
nullptr),
229 Py_SyclContextType_(
nullptr), PySyclContextType_(
nullptr),
230 Py_SyclEventType_(
nullptr), PySyclEventType_(
nullptr),
231 Py_SyclQueueType_(
nullptr), PySyclQueueType_(
nullptr),
232 Py_MemoryType_(
nullptr), PyMemoryUSMDeviceType_(
nullptr),
233 PyMemoryUSMSharedType_(
nullptr), PyMemoryUSMHostType_(
nullptr),
234 PyUSMArrayType_(
nullptr), PySyclProgramType_(
nullptr),
235 PySyclKernelType_(
nullptr), SyclDevice_GetDeviceRef_(
nullptr),
236 SyclDevice_Make_(
nullptr), SyclContext_GetContextRef_(
nullptr),
237 SyclContext_Make_(
nullptr), SyclEvent_GetEventRef_(
nullptr),
238 SyclEvent_Make_(
nullptr), SyclQueue_GetQueueRef_(
nullptr),
239 SyclQueue_Make_(
nullptr), Memory_GetUsmPointer_(
nullptr),
240 Memory_GetOpaquePointer_(
nullptr), Memory_GetContextRef_(
nullptr),
241 Memory_GetQueueRef_(
nullptr), Memory_GetNumBytes_(
nullptr),
242 Memory_Make_(
nullptr), SyclKernel_GetKernelRef_(
nullptr),
243 SyclKernel_Make_(
nullptr), SyclProgram_GetKernelBundleRef_(
nullptr),
244 SyclProgram_Make_(
nullptr), USM_ARRAY_C_CONTIGUOUS_(0),
245 USM_ARRAY_F_CONTIGUOUS_(0), USM_ARRAY_WRITABLE_(0), UAR_BOOL_(-1),
246 UAR_BYTE_(-1), UAR_UBYTE_(-1), UAR_SHORT_(-1), UAR_USHORT_(-1),
247 UAR_INT_(-1), UAR_UINT_(-1), UAR_LONG_(-1), UAR_ULONG_(-1),
248 UAR_LONGLONG_(-1), UAR_ULONGLONG_(-1), UAR_FLOAT_(-1),
249 UAR_DOUBLE_(-1), UAR_CFLOAT_(-1), UAR_CDOUBLE_(-1),
250 UAR_TYPE_SENTINEL_(-1), UAR_HALF_(-1), UAR_INT8_(-1), UAR_UINT8_(-1),
251 UAR_INT16_(-1), UAR_UINT16_(-1), UAR_INT32_(-1), UAR_UINT32_(-1),
252 UAR_INT64_(-1), UAR_UINT64_(-1), default_sycl_queue_{},
253 default_usm_memory_{}, default_usm_ndarray_{}, as_usm_memory_{}
258 import_dpctl___sycl_device();
259 import_dpctl___sycl_context();
260 import_dpctl___sycl_event();
261 import_dpctl___sycl_queue();
262 import_dpctl__memory___memory();
263 import_dpctl__program___program();
265 import_dpnp__tensor___usmarray();
268 this->Py_SyclDeviceType_ = &Py_SyclDeviceType;
269 this->PySyclDeviceType_ = &PySyclDeviceType;
270 this->Py_SyclContextType_ = &Py_SyclContextType;
271 this->PySyclContextType_ = &PySyclContextType;
272 this->Py_SyclEventType_ = &Py_SyclEventType;
273 this->PySyclEventType_ = &PySyclEventType;
274 this->Py_SyclQueueType_ = &Py_SyclQueueType;
275 this->PySyclQueueType_ = &PySyclQueueType;
276 this->Py_MemoryType_ = &Py_MemoryType;
277 this->PyMemoryUSMDeviceType_ = &PyMemoryUSMDeviceType;
278 this->PyMemoryUSMSharedType_ = &PyMemoryUSMSharedType;
279 this->PyMemoryUSMHostType_ = &PyMemoryUSMHostType;
280 this->PyUSMArrayType_ = &PyUSMArrayType;
281 this->PySyclProgramType_ = &PySyclProgramType;
282 this->PySyclKernelType_ = &PySyclKernelType;
285 this->SyclDevice_GetDeviceRef_ = SyclDevice_GetDeviceRef;
286 this->SyclDevice_Make_ = SyclDevice_Make;
289 this->SyclContext_GetContextRef_ = SyclContext_GetContextRef;
290 this->SyclContext_Make_ = SyclContext_Make;
293 this->SyclEvent_GetEventRef_ = SyclEvent_GetEventRef;
294 this->SyclEvent_Make_ = SyclEvent_Make;
297 this->SyclQueue_GetQueueRef_ = SyclQueue_GetQueueRef;
298 this->SyclQueue_Make_ = SyclQueue_Make;
301 this->Memory_GetUsmPointer_ = Memory_GetUsmPointer;
302 this->Memory_GetOpaquePointer_ = Memory_GetOpaquePointer;
303 this->Memory_GetContextRef_ = Memory_GetContextRef;
304 this->Memory_GetQueueRef_ = Memory_GetQueueRef;
305 this->Memory_GetNumBytes_ = Memory_GetNumBytes;
306 this->Memory_Make_ = Memory_Make;
309 this->SyclKernel_GetKernelRef_ = SyclKernel_GetKernelRef;
310 this->SyclKernel_Make_ = SyclKernel_Make;
311 this->SyclProgram_GetKernelBundleRef_ = SyclProgram_GetKernelBundleRef;
312 this->SyclProgram_Make_ = SyclProgram_Make;
315 this->USM_ARRAY_C_CONTIGUOUS_ = USM_ARRAY_C_CONTIGUOUS;
316 this->USM_ARRAY_F_CONTIGUOUS_ = USM_ARRAY_F_CONTIGUOUS;
317 this->USM_ARRAY_WRITABLE_ = USM_ARRAY_WRITABLE;
318 this->UAR_BOOL_ = UAR_BOOL;
319 this->UAR_BYTE_ = UAR_BYTE;
320 this->UAR_UBYTE_ = UAR_UBYTE;
321 this->UAR_SHORT_ = UAR_SHORT;
322 this->UAR_USHORT_ = UAR_USHORT;
323 this->UAR_INT_ = UAR_INT;
324 this->UAR_UINT_ = UAR_UINT;
325 this->UAR_LONG_ = UAR_LONG;
326 this->UAR_ULONG_ = UAR_ULONG;
327 this->UAR_LONGLONG_ = UAR_LONGLONG;
328 this->UAR_ULONGLONG_ = UAR_ULONGLONG;
329 this->UAR_FLOAT_ = UAR_FLOAT;
330 this->UAR_DOUBLE_ = UAR_DOUBLE;
331 this->UAR_CFLOAT_ = UAR_CFLOAT;
332 this->UAR_CDOUBLE_ = UAR_CDOUBLE;
333 this->UAR_TYPE_SENTINEL_ = UAR_TYPE_SENTINEL;
334 this->UAR_HALF_ = UAR_HALF;
337 this->UAR_INT8_ = UAR_BYTE;
338 this->UAR_UINT8_ = UAR_UBYTE;
339 this->UAR_INT16_ = UAR_SHORT;
340 this->UAR_UINT16_ = UAR_USHORT;
342 platform_typeid_lookup<std::int32_t, long, int, short>(
343 UAR_LONG, UAR_INT, UAR_SHORT);
345 platform_typeid_lookup<std::uint32_t,
unsigned long,
unsigned int,
346 unsigned short>(UAR_ULONG, UAR_UINT,
349 platform_typeid_lookup<std::int64_t, long, long long, int>(
350 UAR_LONG, UAR_LONGLONG, UAR_INT);
352 platform_typeid_lookup<std::uint64_t,
unsigned long,
353 unsigned long long,
unsigned int>(
354 UAR_ULONG, UAR_ULONGLONG, UAR_UINT);
359 PySyclQueueObject *py_q_tmp =
360 SyclQueue_Make(
reinterpret_cast<DPCTLSyclQueueRef
>(&q_));
361 const py::object &py_sycl_queue = py::reinterpret_steal<py::object>(
362 reinterpret_cast<PyObject *
>(py_q_tmp));
364 default_sycl_queue_ = std::shared_ptr<py::object>(
365 new py::object(py_sycl_queue), Deleter{});
367 py::module_ mod_memory = py::module_::import(
"dpctl.memory");
368 const py::object &py_as_usm_memory = mod_memory.attr(
"as_usm_memory");
369 as_usm_memory_ = std::shared_ptr<py::object>(
370 new py::object{py_as_usm_memory}, Deleter{});
372 auto mem_kl = mod_memory.attr(
"MemoryUSMHost");
373 const py::object &py_default_usm_memory =
374 mem_kl(1, py::arg(
"queue") = py_sycl_queue);
375 default_usm_memory_ = std::shared_ptr<py::object>(
376 new py::object{py_default_usm_memory}, Deleter{});
378 py::module_ mod_usmarray = py::module_::import(
"dpnp.tensor._usmarray");
379 auto tensor_kl = mod_usmarray.attr(
"usm_ndarray");
381 const py::object &py_default_usm_ndarray =
382 tensor_kl(py::tuple(), py::arg(
"dtype") = py::str(
"u1"),
383 py::arg(
"buffer") = py_default_usm_memory);
385 default_usm_ndarray_ = std::shared_ptr<py::object>(
386 new py::object{py_default_usm_ndarray}, Deleter{});
917 PYBIND11_OBJECT(
usm_ndarray, py::object, [](PyObject *o) ->
bool {
918 return PyObject_TypeCheck(
919 o, ::dpctl::detail::dpctl_capi::get().PyUSMArrayType_) != 0;
924 ::dpctl::detail::dpctl_capi::get().default_usm_ndarray_pyobj(),
928 throw py::error_already_set();
931 char *get_data()
const
933 PyUSMArrayObject *raw_ar = usm_array_ptr();
934 return raw_ar->data_;
937 template <
typename T>
940 return reinterpret_cast<T *
>(get_data());
945 PyUSMArrayObject *raw_ar = usm_array_ptr();
949 const py::ssize_t *get_shape_raw()
const
951 PyUSMArrayObject *raw_ar = usm_array_ptr();
952 return raw_ar->shape_;
955 std::vector<py::ssize_t> get_shape_vector()
const
957 auto raw_sh = get_shape_raw();
958 auto nd = get_ndim();
960 std::vector<py::ssize_t> shape_vector(raw_sh, raw_sh + nd);
964 py::ssize_t get_shape(
int i)
const
966 auto shape_ptr = get_shape_raw();
970 const py::ssize_t *get_strides_raw()
const
972 PyUSMArrayObject *raw_ar = usm_array_ptr();
973 return raw_ar->strides_;
976 std::vector<py::ssize_t> get_strides_vector()
const
978 auto raw_st = get_strides_raw();
979 auto nd = get_ndim();
981 if (raw_st ==
nullptr) {
982 auto is_c_contig = is_c_contiguous();
983 auto is_f_contig = is_f_contiguous();
984 auto raw_sh = get_shape_raw();
986 const auto &contig_strides = c_contiguous_strides(nd, raw_sh);
987 return contig_strides;
989 else if (is_f_contig) {
990 const auto &contig_strides = f_contiguous_strides(nd, raw_sh);
991 return contig_strides;
994 throw std::runtime_error(
"Invalid array encountered when "
999 std::vector<py::ssize_t> st_vec(raw_st, raw_st + nd);
1004 py::ssize_t get_size()
const
1006 PyUSMArrayObject *raw_ar = usm_array_ptr();
1008 int ndim = raw_ar->nd_;
1009 const py::ssize_t *shape = raw_ar->shape_;
1011 py::ssize_t nelems = 1;
1012 for (
int i = 0; i < ndim; ++i) {
1016 assert(nelems >= 0);
1020 std::pair<py::ssize_t, py::ssize_t> get_minmax_offsets()
const
1022 PyUSMArrayObject *raw_ar = usm_array_ptr();
1024 int nd = raw_ar->nd_;
1025 const py::ssize_t *shape = raw_ar->shape_;
1026 const py::ssize_t *strides = raw_ar->strides_;
1028 py::ssize_t offset_min = 0;
1029 py::ssize_t offset_max = 0;
1030 if (strides ==
nullptr) {
1031 py::ssize_t stride(1);
1032 for (
int i = 0; i < nd; ++i) {
1033 offset_max += stride * (shape[i] - 1);
1038 for (
int i = 0; i < nd; ++i) {
1039 py::ssize_t delta = strides[i] * (shape[i] - 1);
1040 if (strides[i] > 0) {
1041 offset_max += delta;
1044 offset_min += delta;
1048 return std::make_pair(offset_min, offset_max);
1051 sycl::queue get_queue()
const
1053 PyUSMArrayObject *raw_ar = usm_array_ptr();
1054 Py_MemoryObject *mem_obj =
1055 reinterpret_cast<Py_MemoryObject *
>(raw_ar->base_);
1057 auto const &api = ::dpctl::detail::dpctl_capi::get();
1058 DPCTLSyclQueueRef QRef = api.Memory_GetQueueRef_(mem_obj);
1059 return *(
reinterpret_cast<sycl::queue *
>(QRef));
1062 sycl::device get_device()
const
1064 PyUSMArrayObject *raw_ar = usm_array_ptr();
1065 Py_MemoryObject *mem_obj =
1066 reinterpret_cast<Py_MemoryObject *
>(raw_ar->base_);
1068 auto const &api = ::dpctl::detail::dpctl_capi::get();
1069 DPCTLSyclQueueRef QRef = api.Memory_GetQueueRef_(mem_obj);
1070 return reinterpret_cast<sycl::queue *
>(QRef)->get_device();
1073 int get_typenum()
const
1075 PyUSMArrayObject *raw_ar = usm_array_ptr();
1076 return raw_ar->typenum_;
1079 int get_flags()
const
1081 PyUSMArrayObject *raw_ar = usm_array_ptr();
1082 return raw_ar->flags_;
1085 int get_elemsize()
const
1087 int typenum = get_typenum();
1088 auto const &api = ::dpctl::detail::dpctl_capi::get();
1091 if (typenum == api.UAR_BOOL_)
1093 if (typenum == api.UAR_BYTE_)
1095 if (typenum == api.UAR_UBYTE_)
1097 if (typenum == api.UAR_SHORT_)
1099 if (typenum == api.UAR_USHORT_)
1101 if (typenum == api.UAR_INT_)
1103 if (typenum == api.UAR_UINT_)
1105 if (typenum == api.UAR_LONG_)
1106 return sizeof(long);
1107 if (typenum == api.UAR_ULONG_)
1108 return sizeof(
unsigned long);
1109 if (typenum == api.UAR_LONGLONG_)
1111 if (typenum == api.UAR_ULONGLONG_)
1113 if (typenum == api.UAR_FLOAT_)
1115 if (typenum == api.UAR_DOUBLE_)
1117 if (typenum == api.UAR_CFLOAT_)
1119 if (typenum == api.UAR_CDOUBLE_)
1121 if (typenum == api.UAR_HALF_)
1127 bool is_c_contiguous()
const
1129 int flags = get_flags();
1130 auto const &api = ::dpctl::detail::dpctl_capi::get();
1131 return static_cast<bool>(flags & api.USM_ARRAY_C_CONTIGUOUS_);
1134 bool is_f_contiguous()
const
1136 int flags = get_flags();
1137 auto const &api = ::dpctl::detail::dpctl_capi::get();
1138 return static_cast<bool>(flags & api.USM_ARRAY_F_CONTIGUOUS_);
1141 bool is_writable()
const
1143 int flags = get_flags();
1144 auto const &api = ::dpctl::detail::dpctl_capi::get();
1145 return static_cast<bool>(flags & api.USM_ARRAY_WRITABLE_);
1151 PyUSMArrayObject *raw_ar = usm_array_ptr();
1153 PyObject *usm_data = raw_ar->base_;
1154 Py_XINCREF(usm_data);
1157 return py::reinterpret_steal<py::object>(usm_data);
1160 bool is_managed_by_smart_ptr()
const
1162 PyUSMArrayObject *raw_ar = usm_array_ptr();
1163 PyObject *usm_data = raw_ar->base_;
1165 auto const &api = ::dpctl::detail::dpctl_capi::get();
1166 if (!PyObject_TypeCheck(usm_data, api.Py_MemoryType_)) {
1170 Py_MemoryObject *mem_obj =
1171 reinterpret_cast<Py_MemoryObject *
>(usm_data);
1172 const void *opaque_ptr = api.Memory_GetOpaquePointer_(mem_obj);
1174 return bool(opaque_ptr);
1177 const std::shared_ptr<void> &get_smart_ptr_owner()
const
1179 PyUSMArrayObject *raw_ar = usm_array_ptr();
1180 PyObject *usm_data = raw_ar->base_;
1182 auto const &api = ::dpctl::detail::dpctl_capi::get();
1184 if (!PyObject_TypeCheck(usm_data, api.Py_MemoryType_)) {
1185 throw std::runtime_error(
1186 "usm_ndarray object does not have Memory object "
1187 "managing lifetime of USM allocation");
1190 Py_MemoryObject *mem_obj =
1191 reinterpret_cast<Py_MemoryObject *
>(usm_data);
1192 void *opaque_ptr = api.Memory_GetOpaquePointer_(mem_obj);
1196 reinterpret_cast<std::shared_ptr<void> *
>(opaque_ptr);
1200 throw std::runtime_error(
1201 "Memory object underlying usm_ndarray does not have "
1202 "smart pointer managing lifetime of USM allocation");
1207 PyUSMArrayObject *usm_array_ptr()
const
1209 return reinterpret_cast<PyUSMArrayObject *
>(m_ptr);