Skip to content

Commit

Permalink
Improve box unbox tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzEeKkAa committed Oct 3, 2023
1 parent 39b5a5c commit ca62909
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 173 deletions.
46 changes: 25 additions & 21 deletions numba_dpex/core/runtime/_dpexrt_python.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ static int DPEXRT_sycl_usm_ndarray_from_python(PyObject *obj,
// Increment the ref count on obj to prevent CPython from garbage
// collecting the array.
// TODO: add extra description why do we need this
// usmarystruct_t -> meminfo -> dtor_info -> owner. Destructor calls decref.
// usmarystruct_t -> parent. No destructor.
// TODO: Do we have to move it into meminfo constructor?
Py_IncRef(obj);

DPEXRT_DEBUG(drt_debug_print(
Expand Down Expand Up @@ -836,6 +839,10 @@ static int DPEXRT_sycl_usm_ndarray_from_python(PyObject *obj,
arystruct->itemsize = itemsize;
arystruct->parent = obj;

DPEXRT_DEBUG(drt_debug_print(
"DPEXRT-DEBUG: in obj: %u -> %u: %u\n", (unsigned int)arystruct,
(unsigned int)arystruct->parent, (unsigned int)obj));

p = arystruct->shape_and_strides;

// Calculate the exponent from the arystruct->itemsize as we know
Expand Down Expand Up @@ -926,8 +933,13 @@ static PyObject *box_from_arystruct_parent(usmarystruct_t *arystruct,
return NULL;
}

if (UsmNDArray_GetNDim(arrayobj) != ndim)
if (UsmNDArray_GetNDim(arrayobj) != ndim) {
DPEXRT_DEBUG(drt_debug_print(
"DPEXRT-DEBUG: Arrayobj cannot be boxed "
"from parent as ndim in the arystruct is not the same as "
"the ndim in the parent object.\n"));
return NULL;
}

p = arystruct->shape_and_strides;
shape = UsmNDArray_GetShape(arrayobj);
Expand All @@ -936,8 +948,13 @@ static PyObject *box_from_arystruct_parent(usmarystruct_t *arystruct,
// Ensure the shape of the array to be boxed matches the shape of the
// original parent.
for (i = 0; i < ndim; i++, p++) {
if (shape[i] != *p)
if (shape[i] != *p) {
DPEXRT_DEBUG(drt_debug_print(
"DPEXRT-DEBUG: Arrayobj cannot be boxed "
"from parent as shape in the arystruct is not the same as "
"the shape in the parent object.\n"));
return NULL;
}
}
// Calculate the exponent from the arystruct->itemsize as we know
// itemsize is a power of two
Expand Down Expand Up @@ -1063,6 +1080,9 @@ DPEXRT_sycl_usm_ndarray_to_python_acqref(usmarystruct_t *arystruct,
__FILE__, __LINE__));

PyObject *obj = box_from_arystruct_parent(arystruct, ndim, descr);
DPEXRT_DEBUG(drt_debug_print(
"DPEXRT-DEBUG: obj: %u -> %u: %u\n", (unsigned int)arystruct,
(unsigned int)arystruct->parent, (unsigned int)obj));
if (obj) {
return obj;
}
Expand Down Expand Up @@ -1214,14 +1234,8 @@ DPEXRT_sycl_usm_ndarray_to_python_acqref(usmarystruct_t *arystruct,
static int DPEXRT_sycl_queue_from_python(PyObject *obj,
queuestruct_t *queue_struct)
{

struct PySyclQueueObject *queue_obj = NULL;
DPCTLSyclQueueRef queue_ref = NULL;
PyGILState_STATE gstate;

// Increment the ref count on obj to prevent CPython from garbage
// collecting the dpctl.SyclQueue object
Py_IncRef(obj);

// We are unconditionally casting obj to a struct PySyclQueueObject*. If
// the obj is not a struct PySyclQueueObject* then the SyclQueue_GetQueueRef
Expand Down Expand Up @@ -1261,11 +1275,6 @@ static int DPEXRT_sycl_queue_from_python(PyObject *obj,
"DPEXRT-ERROR: Failed to unbox dpctl SyclQueue into a Numba "
"queuestruct at %s, line %d\n",
__FILE__, __LINE__));
gstate = PyGILState_Ensure();
// decref the python object
Py_DECREF(obj);
// release the GIL
PyGILState_Release(gstate);

return -1;
}
Expand All @@ -1284,7 +1293,6 @@ static int DPEXRT_sycl_queue_from_python(PyObject *obj,
static PyObject *DPEXRT_sycl_queue_to_python(queuestruct_t *queuestruct)
{
PyObject *orig_queue = NULL;
PyGILState_STATE gstate;

orig_queue = queuestruct->parent;
// FIXME: Better error checking is needed to enforce the boxing of the queue
Expand All @@ -1298,11 +1306,9 @@ static PyObject *DPEXRT_sycl_queue_to_python(queuestruct_t *queuestruct)
return NULL;
}

gstate = PyGILState_Ensure();
// decref the parent python object as we did an incref when unboxing it
Py_DECREF(orig_queue);
// release the GIL
PyGILState_Release(gstate);
// We need to increase reference count because we are returning new
// reference to the same event.
Py_INCREF(orig_queue);

return orig_queue;
}
Expand All @@ -1323,10 +1329,8 @@ static PyObject *DPEXRT_sycl_queue_to_python(queuestruct_t *queuestruct)
static int DPEXRT_sycl_event_from_python(PyObject *obj,
eventstruct_t *event_struct)
{

struct PySyclEventObject *event_obj = NULL;
DPCTLSyclEventRef event_ref = NULL;
PyGILState_STATE gstate;

// We are unconditionally casting obj to a struct PySyclEventObject*. If
// the obj is not a struct PySyclEventObject* then the SyclEvent_GetEventRef
Expand Down
91 changes: 0 additions & 91 deletions numba_dpex/tests/core/types/DpctlSyclEvent/test_box_unbox.py

This file was deleted.

27 changes: 0 additions & 27 deletions numba_dpex/tests/core/types/DpctlSyclQueue/test_box_unbox.py

This file was deleted.

34 changes: 0 additions & 34 deletions numba_dpex/tests/core/types/range_types/test_unbox_box.py

This file was deleted.

Loading

0 comments on commit ca62909

Please sign in to comment.