81 PyTypeObject *Py_SyclDeviceType_;
82 PyTypeObject *PySyclDeviceType_;
83 PyTypeObject *Py_SyclContextType_;
84 PyTypeObject *PySyclContextType_;
85 PyTypeObject *Py_SyclEventType_;
86 PyTypeObject *PySyclEventType_;
87 PyTypeObject *Py_SyclQueueType_;
88 PyTypeObject *PySyclQueueType_;
89 PyTypeObject *Py_MemoryType_;
90 PyTypeObject *PyMemoryUSMDeviceType_;
91 PyTypeObject *PyMemoryUSMSharedType_;
92 PyTypeObject *PyMemoryUSMHostType_;
93 PyTypeObject *PyUSMArrayType_;
94 PyTypeObject *PySyclProgramType_;
95 PyTypeObject *PySyclKernelType_;
97 DPCTLSyclDeviceRef (*SyclDevice_GetDeviceRef_)(PySyclDeviceObject *);
98 PySyclDeviceObject *(*SyclDevice_Make_)(DPCTLSyclDeviceRef);
100 DPCTLSyclContextRef (*SyclContext_GetContextRef_)(PySyclContextObject *);
101 PySyclContextObject *(*SyclContext_Make_)(DPCTLSyclContextRef);
103 DPCTLSyclEventRef (*SyclEvent_GetEventRef_)(PySyclEventObject *);
104 PySyclEventObject *(*SyclEvent_Make_)(DPCTLSyclEventRef);
106 DPCTLSyclQueueRef (*SyclQueue_GetQueueRef_)(PySyclQueueObject *);
107 PySyclQueueObject *(*SyclQueue_Make_)(DPCTLSyclQueueRef);
110 DPCTLSyclUSMRef (*Memory_GetUsmPointer_)(Py_MemoryObject *);
111 void *(*Memory_GetOpaquePointer_)(Py_MemoryObject *);
112 DPCTLSyclContextRef (*Memory_GetContextRef_)(Py_MemoryObject *);
113 DPCTLSyclQueueRef (*Memory_GetQueueRef_)(Py_MemoryObject *);
114 size_t (*Memory_GetNumBytes_)(Py_MemoryObject *);
115 PyObject *(*Memory_Make_)(DPCTLSyclUSMRef,
121 DPCTLSyclKernelRef (*SyclKernel_GetKernelRef_)(PySyclKernelObject *);
122 PySyclKernelObject *(*SyclKernel_Make_)(DPCTLSyclKernelRef,
const char *);
124 DPCTLSyclKernelBundleRef (*SyclProgram_GetKernelBundleRef_)(
125 PySyclProgramObject *);
126 PySyclProgramObject *(*SyclProgram_Make_)(DPCTLSyclKernelBundleRef);
128 int USM_ARRAY_C_CONTIGUOUS_;
129 int USM_ARRAY_F_CONTIGUOUS_;
130 int USM_ARRAY_WRITABLE_;
131 int UAR_BOOL_, UAR_BYTE_, UAR_UBYTE_, UAR_SHORT_, UAR_USHORT_, UAR_INT_,
132 UAR_UINT_, UAR_LONG_, UAR_ULONG_, UAR_LONGLONG_, UAR_ULONGLONG_,
133 UAR_FLOAT_, UAR_DOUBLE_, UAR_CFLOAT_, UAR_CDOUBLE_, UAR_TYPE_SENTINEL_,
135 int UAR_INT8_, UAR_UINT8_, UAR_INT16_, UAR_UINT16_, UAR_INT32_, UAR_UINT32_,
136 UAR_INT64_, UAR_UINT64_;
138 bool PySyclDevice_Check_(PyObject *obj)
const
140 return PyObject_TypeCheck(obj, PySyclDeviceType_) != 0;
142 bool PySyclContext_Check_(PyObject *obj)
const
144 return PyObject_TypeCheck(obj, PySyclContextType_) != 0;
146 bool PySyclEvent_Check_(PyObject *obj)
const
148 return PyObject_TypeCheck(obj, PySyclEventType_) != 0;
150 bool PySyclQueue_Check_(PyObject *obj)
const
152 return PyObject_TypeCheck(obj, PySyclQueueType_) != 0;
154 bool PySyclKernel_Check_(PyObject *obj)
const
156 return PyObject_TypeCheck(obj, PySyclKernelType_) != 0;
158 bool PySyclProgram_Check_(PyObject *obj)
const
160 return PyObject_TypeCheck(obj, PySyclProgramType_) != 0;
165 as_usm_memory_.reset();
166 default_usm_ndarray_.reset();
167 default_usm_memory_.reset();
168 default_sycl_queue_.reset();
177 py::object default_sycl_queue_pyobj() {
return *default_sycl_queue_; }
178 py::object default_usm_memory_pyobj() {
return *default_usm_memory_; }
179 py::object default_usm_ndarray_pyobj() {
return *default_usm_ndarray_; }
180 py::object as_usm_memory_pyobj() {
return *as_usm_memory_; }
185 void operator()(py::object *p)
const
187 const bool initialized = Py_IsInitialized();
188#if PY_VERSION_HEX < 0x30d0000
189 const bool finalizing = _Py_IsFinalizing();
191 const bool finalizing = Py_IsFinalizing();
193 const bool guard = initialized && !finalizing;
201 std::shared_ptr<py::object> default_sycl_queue_;
202 std::shared_ptr<py::object> default_usm_memory_;
203 std::shared_ptr<py::object> default_usm_ndarray_;
204 std::shared_ptr<py::object> as_usm_memory_;
207 : Py_SyclDeviceType_(
nullptr), PySyclDeviceType_(
nullptr),
208 Py_SyclContextType_(
nullptr), PySyclContextType_(
nullptr),
209 Py_SyclEventType_(
nullptr), PySyclEventType_(
nullptr),
210 Py_SyclQueueType_(
nullptr), PySyclQueueType_(
nullptr),
211 Py_MemoryType_(
nullptr), PyMemoryUSMDeviceType_(
nullptr),
212 PyMemoryUSMSharedType_(
nullptr), PyMemoryUSMHostType_(
nullptr),
213 PyUSMArrayType_(
nullptr), PySyclProgramType_(
nullptr),
214 PySyclKernelType_(
nullptr), SyclDevice_GetDeviceRef_(
nullptr),
215 SyclDevice_Make_(
nullptr), SyclContext_GetContextRef_(
nullptr),
216 SyclContext_Make_(
nullptr), SyclEvent_GetEventRef_(
nullptr),
217 SyclEvent_Make_(
nullptr), SyclQueue_GetQueueRef_(
nullptr),
218 SyclQueue_Make_(
nullptr), Memory_GetUsmPointer_(
nullptr),
219 Memory_GetOpaquePointer_(
nullptr), Memory_GetContextRef_(
nullptr),
220 Memory_GetQueueRef_(
nullptr), Memory_GetNumBytes_(
nullptr),
221 Memory_Make_(
nullptr), SyclKernel_GetKernelRef_(
nullptr),
222 SyclKernel_Make_(
nullptr), SyclProgram_GetKernelBundleRef_(
nullptr),
223 SyclProgram_Make_(
nullptr), USM_ARRAY_C_CONTIGUOUS_(0),
224 USM_ARRAY_F_CONTIGUOUS_(0), USM_ARRAY_WRITABLE_(0), UAR_BOOL_(-1),
225 UAR_BYTE_(-1), UAR_UBYTE_(-1), UAR_SHORT_(-1), UAR_USHORT_(-1),
226 UAR_INT_(-1), UAR_UINT_(-1), UAR_LONG_(-1), UAR_ULONG_(-1),
227 UAR_LONGLONG_(-1), UAR_ULONGLONG_(-1), UAR_FLOAT_(-1),
228 UAR_DOUBLE_(-1), UAR_CFLOAT_(-1), UAR_CDOUBLE_(-1),
229 UAR_TYPE_SENTINEL_(-1), UAR_HALF_(-1), UAR_INT8_(-1), UAR_UINT8_(-1),
230 UAR_INT16_(-1), UAR_UINT16_(-1), UAR_INT32_(-1), UAR_UINT32_(-1),
231 UAR_INT64_(-1), UAR_UINT64_(-1), default_sycl_queue_{},
232 default_usm_memory_{}, default_usm_ndarray_{}, as_usm_memory_{}
239 import_dpnp__tensor___usmarray();
242 this->Py_SyclDeviceType_ = &Py_SyclDeviceType;
243 this->PySyclDeviceType_ = &PySyclDeviceType;
244 this->Py_SyclContextType_ = &Py_SyclContextType;
245 this->PySyclContextType_ = &PySyclContextType;
246 this->Py_SyclEventType_ = &Py_SyclEventType;
247 this->PySyclEventType_ = &PySyclEventType;
248 this->Py_SyclQueueType_ = &Py_SyclQueueType;
249 this->PySyclQueueType_ = &PySyclQueueType;
250 this->Py_MemoryType_ = &Py_MemoryType;
251 this->PyMemoryUSMDeviceType_ = &PyMemoryUSMDeviceType;
252 this->PyMemoryUSMSharedType_ = &PyMemoryUSMSharedType;
253 this->PyMemoryUSMHostType_ = &PyMemoryUSMHostType;
254 this->PyUSMArrayType_ = &PyUSMArrayType;
255 this->PySyclProgramType_ = &PySyclProgramType;
256 this->PySyclKernelType_ = &PySyclKernelType;
259 this->SyclDevice_GetDeviceRef_ = SyclDevice_GetDeviceRef;
260 this->SyclDevice_Make_ = SyclDevice_Make;
263 this->SyclContext_GetContextRef_ = SyclContext_GetContextRef;
264 this->SyclContext_Make_ = SyclContext_Make;
267 this->SyclEvent_GetEventRef_ = SyclEvent_GetEventRef;
268 this->SyclEvent_Make_ = SyclEvent_Make;
271 this->SyclQueue_GetQueueRef_ = SyclQueue_GetQueueRef;
272 this->SyclQueue_Make_ = SyclQueue_Make;
275 this->Memory_GetUsmPointer_ = Memory_GetUsmPointer;
276 this->Memory_GetOpaquePointer_ = Memory_GetOpaquePointer;
277 this->Memory_GetContextRef_ = Memory_GetContextRef;
278 this->Memory_GetQueueRef_ = Memory_GetQueueRef;
279 this->Memory_GetNumBytes_ = Memory_GetNumBytes;
280 this->Memory_Make_ = Memory_Make;
283 this->SyclKernel_GetKernelRef_ = SyclKernel_GetKernelRef;
284 this->SyclKernel_Make_ = SyclKernel_Make;
285 this->SyclProgram_GetKernelBundleRef_ = SyclProgram_GetKernelBundleRef;
286 this->SyclProgram_Make_ = SyclProgram_Make;
289 this->USM_ARRAY_C_CONTIGUOUS_ = USM_ARRAY_C_CONTIGUOUS;
290 this->USM_ARRAY_F_CONTIGUOUS_ = USM_ARRAY_F_CONTIGUOUS;
291 this->USM_ARRAY_WRITABLE_ = USM_ARRAY_WRITABLE;
292 this->UAR_BOOL_ = UAR_BOOL;
293 this->UAR_BYTE_ = UAR_BYTE;
294 this->UAR_UBYTE_ = UAR_UBYTE;
295 this->UAR_SHORT_ = UAR_SHORT;
296 this->UAR_USHORT_ = UAR_USHORT;
297 this->UAR_INT_ = UAR_INT;
298 this->UAR_UINT_ = UAR_UINT;
299 this->UAR_LONG_ = UAR_LONG;
300 this->UAR_ULONG_ = UAR_ULONG;
301 this->UAR_LONGLONG_ = UAR_LONGLONG;
302 this->UAR_ULONGLONG_ = UAR_ULONGLONG;
303 this->UAR_FLOAT_ = UAR_FLOAT;
304 this->UAR_DOUBLE_ = UAR_DOUBLE;
305 this->UAR_CFLOAT_ = UAR_CFLOAT;
306 this->UAR_CDOUBLE_ = UAR_CDOUBLE;
307 this->UAR_TYPE_SENTINEL_ = UAR_TYPE_SENTINEL;
308 this->UAR_HALF_ = UAR_HALF;
311 this->UAR_INT8_ = UAR_BYTE;
312 this->UAR_UINT8_ = UAR_UBYTE;
313 this->UAR_INT16_ = UAR_SHORT;
314 this->UAR_UINT16_ = UAR_USHORT;
316 platform_typeid_lookup<std::int32_t, long, int, short>(
317 UAR_LONG, UAR_INT, UAR_SHORT);
319 platform_typeid_lookup<std::uint32_t,
unsigned long,
unsigned int,
320 unsigned short>(UAR_ULONG, UAR_UINT,
323 platform_typeid_lookup<std::int64_t, long, long long, int>(
324 UAR_LONG, UAR_LONGLONG, UAR_INT);
326 platform_typeid_lookup<std::uint64_t,
unsigned long,
327 unsigned long long,
unsigned int>(
328 UAR_ULONG, UAR_ULONGLONG, UAR_UINT);
333 PySyclQueueObject *py_q_tmp =
334 SyclQueue_Make(
reinterpret_cast<DPCTLSyclQueueRef
>(&q_));
335 const py::object &py_sycl_queue = py::reinterpret_steal<py::object>(
336 reinterpret_cast<PyObject *
>(py_q_tmp));
338 default_sycl_queue_ = std::shared_ptr<py::object>(
339 new py::object(py_sycl_queue), Deleter{});
341 py::module_ mod_memory = py::module_::import(
"dpctl.memory");
342 const py::object &py_as_usm_memory = mod_memory.attr(
"as_usm_memory");
343 as_usm_memory_ = std::shared_ptr<py::object>(
344 new py::object{py_as_usm_memory}, Deleter{});
346 auto mem_kl = mod_memory.attr(
"MemoryUSMHost");
347 const py::object &py_default_usm_memory =
348 mem_kl(1, py::arg(
"queue") = py_sycl_queue);
349 default_usm_memory_ = std::shared_ptr<py::object>(
350 new py::object{py_default_usm_memory}, Deleter{});
352 py::module_ mod_usmarray = py::module_::import(
"dpnp.tensor._usmarray");
353 auto tensor_kl = mod_usmarray.attr(
"usm_ndarray");
355 const py::object &py_default_usm_ndarray =
356 tensor_kl(py::tuple(), py::arg(
"dtype") = py::str(
"u1"),
357 py::arg(
"buffer") = py_default_usm_memory);
359 default_usm_ndarray_ = std::shared_ptr<py::object>(
360 new py::object{py_default_usm_ndarray}, Deleter{});
891 PYBIND11_OBJECT(
usm_ndarray, py::object, [](PyObject *o) ->
bool {
892 return PyObject_TypeCheck(
893 o, ::dpctl::detail::dpctl_capi::get().PyUSMArrayType_) != 0;
898 ::dpctl::detail::dpctl_capi::get().default_usm_ndarray_pyobj(),
902 throw py::error_already_set();
905 char *get_data()
const
907 PyUSMArrayObject *raw_ar = usm_array_ptr();
908 return raw_ar->data_;
911 template <
typename T>
914 return reinterpret_cast<T *
>(get_data());
919 PyUSMArrayObject *raw_ar = usm_array_ptr();
923 const py::ssize_t *get_shape_raw()
const
925 PyUSMArrayObject *raw_ar = usm_array_ptr();
926 return raw_ar->shape_;
929 std::vector<py::ssize_t> get_shape_vector()
const
931 auto raw_sh = get_shape_raw();
932 auto nd = get_ndim();
934 std::vector<py::ssize_t> shape_vector(raw_sh, raw_sh + nd);
938 py::ssize_t get_shape(
int i)
const
940 auto shape_ptr = get_shape_raw();
944 const py::ssize_t *get_strides_raw()
const
946 PyUSMArrayObject *raw_ar = usm_array_ptr();
947 return raw_ar->strides_;
950 std::vector<py::ssize_t> get_strides_vector()
const
952 auto raw_st = get_strides_raw();
953 auto nd = get_ndim();
955 if (raw_st ==
nullptr) {
956 auto is_c_contig = is_c_contiguous();
957 auto is_f_contig = is_f_contiguous();
958 auto raw_sh = get_shape_raw();
960 const auto &contig_strides = c_contiguous_strides(nd, raw_sh);
961 return contig_strides;
963 else if (is_f_contig) {
964 const auto &contig_strides = f_contiguous_strides(nd, raw_sh);
965 return contig_strides;
968 throw std::runtime_error(
"Invalid array encountered when "
973 std::vector<py::ssize_t> st_vec(raw_st, raw_st + nd);
978 py::ssize_t get_size()
const
980 PyUSMArrayObject *raw_ar = usm_array_ptr();
982 int ndim = raw_ar->nd_;
983 const py::ssize_t *shape = raw_ar->shape_;
985 py::ssize_t nelems = 1;
986 for (
int i = 0; i < ndim; ++i) {
994 std::pair<py::ssize_t, py::ssize_t> get_minmax_offsets()
const
996 PyUSMArrayObject *raw_ar = usm_array_ptr();
998 int nd = raw_ar->nd_;
999 const py::ssize_t *shape = raw_ar->shape_;
1000 const py::ssize_t *strides = raw_ar->strides_;
1002 py::ssize_t offset_min = 0;
1003 py::ssize_t offset_max = 0;
1004 if (strides ==
nullptr) {
1005 py::ssize_t stride(1);
1006 for (
int i = 0; i < nd; ++i) {
1007 offset_max += stride * (shape[i] - 1);
1012 for (
int i = 0; i < nd; ++i) {
1013 py::ssize_t delta = strides[i] * (shape[i] - 1);
1014 if (strides[i] > 0) {
1015 offset_max += delta;
1018 offset_min += delta;
1022 return std::make_pair(offset_min, offset_max);
1025 sycl::queue get_queue()
const
1027 PyUSMArrayObject *raw_ar = usm_array_ptr();
1028 Py_MemoryObject *mem_obj =
1029 reinterpret_cast<Py_MemoryObject *
>(raw_ar->base_);
1031 auto const &api = ::dpctl::detail::dpctl_capi::get();
1032 DPCTLSyclQueueRef QRef = api.Memory_GetQueueRef_(mem_obj);
1033 return *(
reinterpret_cast<sycl::queue *
>(QRef));
1036 sycl::device get_device()
const
1038 PyUSMArrayObject *raw_ar = usm_array_ptr();
1039 Py_MemoryObject *mem_obj =
1040 reinterpret_cast<Py_MemoryObject *
>(raw_ar->base_);
1042 auto const &api = ::dpctl::detail::dpctl_capi::get();
1043 DPCTLSyclQueueRef QRef = api.Memory_GetQueueRef_(mem_obj);
1044 return reinterpret_cast<sycl::queue *
>(QRef)->get_device();
1047 int get_typenum()
const
1049 PyUSMArrayObject *raw_ar = usm_array_ptr();
1050 return raw_ar->typenum_;
1053 int get_flags()
const
1055 PyUSMArrayObject *raw_ar = usm_array_ptr();
1056 return raw_ar->flags_;
1059 int get_elemsize()
const
1061 int typenum = get_typenum();
1062 auto const &api = ::dpctl::detail::dpctl_capi::get();
1065 if (typenum == api.UAR_BOOL_)
1067 if (typenum == api.UAR_BYTE_)
1069 if (typenum == api.UAR_UBYTE_)
1071 if (typenum == api.UAR_SHORT_)
1073 if (typenum == api.UAR_USHORT_)
1075 if (typenum == api.UAR_INT_)
1077 if (typenum == api.UAR_UINT_)
1079 if (typenum == api.UAR_LONG_)
1080 return sizeof(long);
1081 if (typenum == api.UAR_ULONG_)
1082 return sizeof(
unsigned long);
1083 if (typenum == api.UAR_LONGLONG_)
1085 if (typenum == api.UAR_ULONGLONG_)
1087 if (typenum == api.UAR_FLOAT_)
1089 if (typenum == api.UAR_DOUBLE_)
1091 if (typenum == api.UAR_CFLOAT_)
1093 if (typenum == api.UAR_CDOUBLE_)
1095 if (typenum == api.UAR_HALF_)
1101 bool is_c_contiguous()
const
1103 int flags = get_flags();
1104 auto const &api = ::dpctl::detail::dpctl_capi::get();
1105 return static_cast<bool>(flags & api.USM_ARRAY_C_CONTIGUOUS_);
1108 bool is_f_contiguous()
const
1110 int flags = get_flags();
1111 auto const &api = ::dpctl::detail::dpctl_capi::get();
1112 return static_cast<bool>(flags & api.USM_ARRAY_F_CONTIGUOUS_);
1115 bool is_writable()
const
1117 int flags = get_flags();
1118 auto const &api = ::dpctl::detail::dpctl_capi::get();
1119 return static_cast<bool>(flags & api.USM_ARRAY_WRITABLE_);
1125 PyUSMArrayObject *raw_ar = usm_array_ptr();
1127 PyObject *usm_data = raw_ar->base_;
1128 Py_XINCREF(usm_data);
1131 return py::reinterpret_steal<py::object>(usm_data);
1134 bool is_managed_by_smart_ptr()
const
1136 PyUSMArrayObject *raw_ar = usm_array_ptr();
1137 PyObject *usm_data = raw_ar->base_;
1139 auto const &api = ::dpctl::detail::dpctl_capi::get();
1140 if (!PyObject_TypeCheck(usm_data, api.Py_MemoryType_)) {
1144 Py_MemoryObject *mem_obj =
1145 reinterpret_cast<Py_MemoryObject *
>(usm_data);
1146 const void *opaque_ptr = api.Memory_GetOpaquePointer_(mem_obj);
1148 return bool(opaque_ptr);
1151 const std::shared_ptr<void> &get_smart_ptr_owner()
const
1153 PyUSMArrayObject *raw_ar = usm_array_ptr();
1154 PyObject *usm_data = raw_ar->base_;
1156 auto const &api = ::dpctl::detail::dpctl_capi::get();
1158 if (!PyObject_TypeCheck(usm_data, api.Py_MemoryType_)) {
1159 throw std::runtime_error(
1160 "usm_ndarray object does not have Memory object "
1161 "managing lifetime of USM allocation");
1164 Py_MemoryObject *mem_obj =
1165 reinterpret_cast<Py_MemoryObject *
>(usm_data);
1166 void *opaque_ptr = api.Memory_GetOpaquePointer_(mem_obj);
1170 reinterpret_cast<std::shared_ptr<void> *
>(opaque_ptr);
1174 throw std::runtime_error(
1175 "Memory object underlying usm_ndarray does not have "
1176 "smart pointer managing lifetime of USM allocation");
1181 PyUSMArrayObject *usm_array_ptr()
const
1183 return reinterpret_cast<PyUSMArrayObject *
>(m_ptr);