Skip to content

Commit

Permalink
Update indexing functions (#1814)
Browse files Browse the repository at this point in the history
* Update indexing functions

* Add CFD and tests

* Improve code coverage

* address comments

* address comments

* Added commit

---------

Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com>
  • Loading branch information
npolina4 and antonwolfy authored May 22, 2024
1 parent 6abf2f4 commit b4578dd
Show file tree
Hide file tree
Showing 12 changed files with 849 additions and 398 deletions.
18 changes: 7 additions & 11 deletions dpnp/backend/include/dpnp_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ enum class DPNPFuncName : size_t
DPNP_FN_DET, /**< Used in numpy.linalg.det() impl */
DPNP_FN_DIAG, /**< Used in numpy.diag() impl */
DPNP_FN_DIAG_INDICES, /**< Used in numpy.diag_indices() impl */
DPNP_FN_DIAG_INDICES_EXT, /**< Used in numpy.diag_indices() impl, requires
extra parameters */
DPNP_FN_DIAGONAL, /**< Used in numpy.diagonal() impl */
DPNP_FN_DIVIDE, /**< Used in numpy.divide() impl */
DPNP_FN_DOT, /**< Used in numpy.dot() impl */
DPNP_FN_DIAGONAL, /**< Used in numpy.diagonal() impl */
DPNP_FN_DIVIDE, /**< Used in numpy.divide() impl */
DPNP_FN_DOT, /**< Used in numpy.dot() impl */
DPNP_FN_DOT_EXT, /**< Used in numpy.dot() impl, requires extra parameters */
DPNP_FN_EDIFF1D, /**< Used in numpy.ediff1d() impl */
DPNP_FN_EDIFF1D_EXT, /**< Used in numpy.ediff1d() impl, requires extra
Expand All @@ -140,12 +138,10 @@ enum class DPNPFuncName : size_t
DPNP_FN_FFT_RFFT_EXT, /**< Used in numpy.fft.rfft() impl, requires extra
parameters */
DPNP_FN_FILL_DIAGONAL, /**< Used in numpy.fill_diagonal() impl */
DPNP_FN_FILL_DIAGONAL_EXT, /**< Used in numpy.fill_diagonal() impl, requires
extra parameters */
DPNP_FN_FLATTEN, /**< Used in numpy.flatten() impl */
DPNP_FN_FLOOR, /**< Used in numpy.floor() impl */
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() impl */
DPNP_FN_FMOD, /**< Used in numpy.fmod() impl */
DPNP_FN_FLATTEN, /**< Used in numpy.flatten() impl */
DPNP_FN_FLOOR, /**< Used in numpy.floor() impl */
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() impl */
DPNP_FN_FMOD, /**< Used in numpy.fmod() impl */
DPNP_FN_FMOD_EXT, /**< Used in numpy.fmod() impl, requires extra parameters
*/
DPNP_FN_FULL, /**< Used in numpy.full() impl */
Expand Down
43 changes: 5 additions & 38 deletions dpnp/backend/kernels/dpnp_krnl_indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,6 @@ template <typename _DataType>
void (*dpnp_diag_indices_default_c)(void *,
size_t) = dpnp_diag_indices_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef (*dpnp_diag_indices_ext_c)(DPCTLSyclQueueRef,
void *,
size_t,
const DPCTLEventVectorRef) =
dpnp_diag_indices_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef dpnp_diagonal_c(DPCTLSyclQueueRef q_ref,
void *array1_in,
Expand Down Expand Up @@ -188,9 +181,11 @@ DPCTLSyclEventRef dpnp_diagonal_c(DPCTLSyclQueueRef q_ref,
_DataType *array_1 = input1_ptr.get_ptr();
_DataType *result = result_ptr.get_ptr();

const size_t res_shape_ndim_sub_1 =
static_cast<size_t>(res_shape[res_ndim - 1]);

if (res_ndim <= 1) {
for (size_t i = 0; i < static_cast<size_t>(res_shape[res_ndim - 1]);
++i) {
for (size_t i = 0; i < res_shape_ndim_sub_1; ++i) {
result[i] = array_1[i * shape[res_ndim] + i + offset];
}
}
Expand Down Expand Up @@ -225,8 +220,7 @@ DPCTLSyclEventRef dpnp_diagonal_c(DPCTLSyclQueueRef q_ref,
index += 1;
}

for (size_t i = 0; i < static_cast<size_t>(res_shape[res_ndim - 1]);
i++) {
for (size_t i = 0; i < res_shape_ndim_sub_1; i++) {
for (size_t j = 0; j < xyz.size(); j++) {
std::vector<size_t> ind_list = xyz[j];
if (ind_list.size() == 0) {
Expand Down Expand Up @@ -364,15 +358,6 @@ void (*dpnp_fill_diagonal_default_c)(void *,
const size_t) =
dpnp_fill_diagonal_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef (*dpnp_fill_diagonal_ext_c)(DPCTLSyclQueueRef,
void *,
void *,
shape_elem_type *,
const size_t,
const DPCTLEventVectorRef) =
dpnp_fill_diagonal_c<_DataType>;

template <typename _DataType>
DPCTLSyclEventRef dpnp_nonzero_c(DPCTLSyclQueueRef q_ref,
const void *in_array1,
Expand Down Expand Up @@ -897,15 +882,6 @@ void func_map_init_indexing_func(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_diag_indices_default_c<double>};

fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_diag_indices_ext_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_diag_indices_ext_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_diag_indices_ext_c<float>};
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_diag_indices_ext_c<double>};

fmap[DPNPFuncName::DPNP_FN_DIAGONAL][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_diagonal_default_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_DIAGONAL][eft_LNG][eft_LNG] = {
Expand All @@ -924,15 +900,6 @@ void func_map_init_indexing_func(func_map_t &fmap)
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_fill_diagonal_default_c<double>};

fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_fill_diagonal_ext_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_LNG][eft_LNG] = {
eft_LNG, (void *)dpnp_fill_diagonal_ext_c<int64_t>};
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_FLT][eft_FLT] = {
eft_FLT, (void *)dpnp_fill_diagonal_ext_c<float>};
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_DBL][eft_DBL] = {
eft_DBL, (void *)dpnp_fill_diagonal_ext_c<double>};

fmap[DPNPFuncName::DPNP_FN_NONZERO][eft_INT][eft_INT] = {
eft_INT, (void *)dpnp_nonzero_default_c<int32_t>};
fmap[DPNPFuncName::DPNP_FN_NONZERO][eft_LNG][eft_LNG] = {
Expand Down
2 changes: 0 additions & 2 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_COPY_EXT
DPNP_FN_CORRELATE_EXT
DPNP_FN_DEGREES_EXT
DPNP_FN_DIAG_INDICES_EXT
DPNP_FN_EDIFF1D_EXT
DPNP_FN_ERF_EXT
DPNP_FN_FABS_EXT
DPNP_FN_FFT_FFT_EXT
DPNP_FN_FFT_RFFT_EXT
DPNP_FN_FILL_DIAGONAL_EXT
DPNP_FN_FMOD_EXT
DPNP_FN_MAXIMUM_EXT
DPNP_FN_MEDIAN_EXT
Expand Down
166 changes: 0 additions & 166 deletions dpnp/dpnp_algo/dpnp_algo_indexing.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,13 @@ and the rest of the library

__all__ += [
"dpnp_choose",
"dpnp_diag_indices",
"dpnp_fill_diagonal",
"dpnp_putmask",
"dpnp_select",
"dpnp_tril_indices",
"dpnp_tril_indices_from",
"dpnp_triu_indices",
"dpnp_triu_indices_from"
]

ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_choose_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , void ** , size_t, size_t, size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_diag_indices)(c_dpctl.DPCTLSyclQueueRef,
void * , size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*custom_indexing_2in_func_ptr_t)(c_dpctl.DPCTLSyclQueueRef,
void *, void * , shape_elem_type * , const size_t,
const c_dpctl.DPCTLEventVectorRef)


cpdef utils.dpnp_descriptor dpnp_choose(utils.dpnp_descriptor x1, list choices1):
cdef vector[void * ] choices
Expand Down Expand Up @@ -105,71 +92,6 @@ cpdef utils.dpnp_descriptor dpnp_choose(utils.dpnp_descriptor x1, list choices1)
return res_array


cpdef tuple dpnp_diag_indices(n, ndim):
cdef size_t res_size = 0 if n < 0 else n

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dpnp.int64)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_DIAG_INDICES_EXT, param1_type, param1_type)

cdef fptr_dpnp_diag_indices func = <fptr_dpnp_diag_indices > kernel_data.ptr

cdef c_dpctl.SyclQueue q
cdef c_dpctl.DPCTLSyclQueueRef q_ref
cdef c_dpctl.DPCTLSyclEventRef event_ref

res_list = []
cdef utils.dpnp_descriptor res_arr
cdef shape_type_c result_shape = utils._object_to_tuple(res_size)
for i in range(ndim):
res_arr = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)

q = <c_dpctl.SyclQueue> res_arr.get_array().sycl_queue
q_ref = q.get_queue_ref()

event_ref = func(q_ref, res_arr.get_data(), res_size, NULL)

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

res_list.append(res_arr.get_pyobj())

return tuple(res_list)


cpdef dpnp_fill_diagonal(dpnp_descriptor x1, val):
x1_obj = x1.get_array()

cdef shape_type_c x1_shape = x1.shape
cdef utils.dpnp_descriptor val_arr = utils_py.create_output_descriptor_py((1,),
x1.dtype,
None,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

val_arr.get_pyobj()[0] = val

cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_FILL_DIAGONAL_EXT, param1_type, param1_type)

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> x1_obj.sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef custom_indexing_2in_func_ptr_t func = <custom_indexing_2in_func_ptr_t > kernel_data.ptr

cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
x1.get_data(),
val_arr.get_data(),
x1_shape.data(),
x1.ndim,
NULL) # dep_events_ref

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)


cpdef dpnp_putmask(utils.dpnp_descriptor arr, utils.dpnp_descriptor mask, utils.dpnp_descriptor values):
cdef int values_size = values.size

Expand Down Expand Up @@ -197,91 +119,3 @@ cpdef utils.dpnp_descriptor dpnp_select(list condlist, list choicelist, default)
res_array.get_pyobj()[ind] = val

return res_array


cpdef tuple dpnp_tril_indices(n, k=0, m=None):
array1 = []
array2 = []
if m is None:
for i in range(n):
for j in range(i + 1 + k):
if j >= n:
continue
else:
array1.append(i)
array2.append(j)
else:
for i in range(n):
for j in range(i + 1 + k):
if j < m:
array1.append(i)
array2.append(j)

array1 = dpnp.array(array1, dtype=dpnp.int64)
array2 = dpnp.array(array2, dtype=dpnp.int64)
return (array1, array2)


cpdef tuple dpnp_tril_indices_from(dpnp_descriptor arr, k=0):
m = arr.shape[0]
n = arr.shape[1]
array1 = []
array2 = []
if m is None:
for i in range(n):
for j in range(i + 1 + k):
if j >= n:
continue
else:
array1.append(i)
array2.append(j)
else:
for i in range(n):
for j in range(i + 1 + k):
if j < m:
array1.append(i)
array2.append(j)

array1 = dpnp.array(array1, dtype=dpnp.int64)
array2 = dpnp.array(array2, dtype=dpnp.int64)
return (array1, array2)


cpdef tuple dpnp_triu_indices(n, k=0, m=None):
array1 = []
array2 = []
if m is None:
for i in range(n):
for j in range(i + k, n):
array1.append(i)
array2.append(j)
else:
for i in range(n):
for j in range(i + k, m):
array1.append(i)
array2.append(j)

array1 = dpnp.array(array1, dtype=dpnp.int64)
array2 = dpnp.array(array2, dtype=dpnp.int64)
return (array1, array2)


cpdef tuple dpnp_triu_indices_from(dpnp_descriptor arr, k=0):
m = arr.shape[0]
n = arr.shape[1]
array1 = []
array2 = []
if m is None:
for i in range(n):
for j in range(i + k, n):
array1.append(i)
array2.append(j)
else:
for i in range(n):
for j in range(i + k, m):
array1.append(i)
array2.append(j)

array1 = dpnp.array(array1, dtype=dpnp.int64)
array2 = dpnp.array(array2, dtype=dpnp.int64)
return (array1, array2)
Loading

0 comments on commit b4578dd

Please sign in to comment.