MCPcopy Create free account
hub / github.com/numpy/numpy / array_get_dl_device

Function array_get_dl_device

numpy/core/src/multiarray/dlpack.c:96–119  ·  view source on GitHub ↗

This function cannot return NULL, but it can fail, So call PyErr_Occurred to check if it failed after calling it.

Source from the content-addressed store, hash-verified

94// So call PyErr_Occurred to check if it failed after
95// calling it.
96static DLDevice
97array_get_dl_device(PyArrayObject *self) {
98 DLDevice ret;
99 ret.device_type = kDLCPU;
100 ret.device_id = 0;
101 PyObject *base = PyArray_BASE(self);
102
103 // walk the bases (see gh-20340)
104 while (base != NULL && PyArray_Check(base)) {
105 base = PyArray_BASE((PyArrayObject *)base);
106 }
107
108 // The outer if is due to the fact that NumPy arrays are on the CPU
109 // by default (if not created from DLPack).
110 if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) {
111 DLManagedTensor *managed = PyCapsule_GetPointer(
112 base, NPY_DLPACK_INTERNAL_CAPSULE_NAME);
113 if (managed == NULL) {
114 return ret;
115 }
116 return managed->dl_tensor.device;
117 }
118 return ret;
119}
120
121
122PyObject *

Callers 2

array_dlpackFunction · 0.85
array_dlpack_deviceFunction · 0.85

Calls 1

PyArray_BASEFunction · 0.85

Tested by

no test coverage detected