Skip to content

Commit b4578dd

Browse files
npolina4antonwolfy
andauthored
Update indexing functions (#1814)
* Update indexing functions * Add CFD and tests * Improve code coverage * address comments * address comments * Added commit --------- Co-authored-by: Anton <[email protected]>
1 parent 6abf2f4 commit b4578dd

File tree

12 files changed

+849
-398
lines changed

12 files changed

+849
-398
lines changed

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ enum class DPNPFuncName : size_t
112112
DPNP_FN_DET, /**< Used in numpy.linalg.det() impl */
113113
DPNP_FN_DIAG, /**< Used in numpy.diag() impl */
114114
DPNP_FN_DIAG_INDICES, /**< Used in numpy.diag_indices() impl */
115-
DPNP_FN_DIAG_INDICES_EXT, /**< Used in numpy.diag_indices() impl, requires
116-
extra parameters */
117-
DPNP_FN_DIAGONAL, /**< Used in numpy.diagonal() impl */
118-
DPNP_FN_DIVIDE, /**< Used in numpy.divide() impl */
119-
DPNP_FN_DOT, /**< Used in numpy.dot() impl */
115+
DPNP_FN_DIAGONAL, /**< Used in numpy.diagonal() impl */
116+
DPNP_FN_DIVIDE, /**< Used in numpy.divide() impl */
117+
DPNP_FN_DOT, /**< Used in numpy.dot() impl */
120118
DPNP_FN_DOT_EXT, /**< Used in numpy.dot() impl, requires extra parameters */
121119
DPNP_FN_EDIFF1D, /**< Used in numpy.ediff1d() impl */
122120
DPNP_FN_EDIFF1D_EXT, /**< Used in numpy.ediff1d() impl, requires extra
@@ -140,12 +138,10 @@ enum class DPNPFuncName : size_t
140138
DPNP_FN_FFT_RFFT_EXT, /**< Used in numpy.fft.rfft() impl, requires extra
141139
parameters */
142140
DPNP_FN_FILL_DIAGONAL, /**< Used in numpy.fill_diagonal() impl */
143-
DPNP_FN_FILL_DIAGONAL_EXT, /**< Used in numpy.fill_diagonal() impl, requires
144-
extra parameters */
145-
DPNP_FN_FLATTEN, /**< Used in numpy.flatten() impl */
146-
DPNP_FN_FLOOR, /**< Used in numpy.floor() impl */
147-
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() impl */
148-
DPNP_FN_FMOD, /**< Used in numpy.fmod() impl */
141+
DPNP_FN_FLATTEN, /**< Used in numpy.flatten() impl */
142+
DPNP_FN_FLOOR, /**< Used in numpy.floor() impl */
143+
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() impl */
144+
DPNP_FN_FMOD, /**< Used in numpy.fmod() impl */
149145
DPNP_FN_FMOD_EXT, /**< Used in numpy.fmod() impl, requires extra parameters
150146
*/
151147
DPNP_FN_FULL, /**< Used in numpy.full() impl */

dpnp/backend/kernels/dpnp_krnl_indexing.cpp

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,6 @@ template <typename _DataType>
150150
void (*dpnp_diag_indices_default_c)(void *,
151151
size_t) = dpnp_diag_indices_c<_DataType>;
152152

153-
template <typename _DataType>
154-
DPCTLSyclEventRef (*dpnp_diag_indices_ext_c)(DPCTLSyclQueueRef,
155-
void *,
156-
size_t,
157-
const DPCTLEventVectorRef) =
158-
dpnp_diag_indices_c<_DataType>;
159-
160153
template <typename _DataType>
161154
DPCTLSyclEventRef dpnp_diagonal_c(DPCTLSyclQueueRef q_ref,
162155
void *array1_in,
@@ -188,9 +181,11 @@ DPCTLSyclEventRef dpnp_diagonal_c(DPCTLSyclQueueRef q_ref,
188181
_DataType *array_1 = input1_ptr.get_ptr();
189182
_DataType *result = result_ptr.get_ptr();
190183

184+
const size_t res_shape_ndim_sub_1 =
185+
static_cast<size_t>(res_shape[res_ndim - 1]);
186+
191187
if (res_ndim <= 1) {
192-
for (size_t i = 0; i < static_cast<size_t>(res_shape[res_ndim - 1]);
193-
++i) {
188+
for (size_t i = 0; i < res_shape_ndim_sub_1; ++i) {
194189
result[i] = array_1[i * shape[res_ndim] + i + offset];
195190
}
196191
}
@@ -225,8 +220,7 @@ DPCTLSyclEventRef dpnp_diagonal_c(DPCTLSyclQueueRef q_ref,
225220
index += 1;
226221
}
227222

228-
for (size_t i = 0; i < static_cast<size_t>(res_shape[res_ndim - 1]);
229-
i++) {
223+
for (size_t i = 0; i < res_shape_ndim_sub_1; i++) {
230224
for (size_t j = 0; j < xyz.size(); j++) {
231225
std::vector<size_t> ind_list = xyz[j];
232226
if (ind_list.size() == 0) {
@@ -364,15 +358,6 @@ void (*dpnp_fill_diagonal_default_c)(void *,
364358
const size_t) =
365359
dpnp_fill_diagonal_c<_DataType>;
366360

367-
template <typename _DataType>
368-
DPCTLSyclEventRef (*dpnp_fill_diagonal_ext_c)(DPCTLSyclQueueRef,
369-
void *,
370-
void *,
371-
shape_elem_type *,
372-
const size_t,
373-
const DPCTLEventVectorRef) =
374-
dpnp_fill_diagonal_c<_DataType>;
375-
376361
template <typename _DataType>
377362
DPCTLSyclEventRef dpnp_nonzero_c(DPCTLSyclQueueRef q_ref,
378363
const void *in_array1,
@@ -897,15 +882,6 @@ void func_map_init_indexing_func(func_map_t &fmap)
897882
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES][eft_DBL][eft_DBL] = {
898883
eft_DBL, (void *)dpnp_diag_indices_default_c<double>};
899884

900-
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_INT][eft_INT] = {
901-
eft_INT, (void *)dpnp_diag_indices_ext_c<int32_t>};
902-
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_LNG][eft_LNG] = {
903-
eft_LNG, (void *)dpnp_diag_indices_ext_c<int64_t>};
904-
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_FLT][eft_FLT] = {
905-
eft_FLT, (void *)dpnp_diag_indices_ext_c<float>};
906-
fmap[DPNPFuncName::DPNP_FN_DIAG_INDICES_EXT][eft_DBL][eft_DBL] = {
907-
eft_DBL, (void *)dpnp_diag_indices_ext_c<double>};
908-
909885
fmap[DPNPFuncName::DPNP_FN_DIAGONAL][eft_INT][eft_INT] = {
910886
eft_INT, (void *)dpnp_diagonal_default_c<int32_t>};
911887
fmap[DPNPFuncName::DPNP_FN_DIAGONAL][eft_LNG][eft_LNG] = {
@@ -924,15 +900,6 @@ void func_map_init_indexing_func(func_map_t &fmap)
924900
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL][eft_DBL][eft_DBL] = {
925901
eft_DBL, (void *)dpnp_fill_diagonal_default_c<double>};
926902

927-
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_INT][eft_INT] = {
928-
eft_INT, (void *)dpnp_fill_diagonal_ext_c<int32_t>};
929-
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_LNG][eft_LNG] = {
930-
eft_LNG, (void *)dpnp_fill_diagonal_ext_c<int64_t>};
931-
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_FLT][eft_FLT] = {
932-
eft_FLT, (void *)dpnp_fill_diagonal_ext_c<float>};
933-
fmap[DPNPFuncName::DPNP_FN_FILL_DIAGONAL_EXT][eft_DBL][eft_DBL] = {
934-
eft_DBL, (void *)dpnp_fill_diagonal_ext_c<double>};
935-
936903
fmap[DPNPFuncName::DPNP_FN_NONZERO][eft_INT][eft_INT] = {
937904
eft_INT, (void *)dpnp_nonzero_default_c<int32_t>};
938905
fmap[DPNPFuncName::DPNP_FN_NONZERO][eft_LNG][eft_LNG] = {

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
3838
DPNP_FN_COPY_EXT
3939
DPNP_FN_CORRELATE_EXT
4040
DPNP_FN_DEGREES_EXT
41-
DPNP_FN_DIAG_INDICES_EXT
4241
DPNP_FN_EDIFF1D_EXT
4342
DPNP_FN_ERF_EXT
4443
DPNP_FN_FABS_EXT
4544
DPNP_FN_FFT_FFT_EXT
4645
DPNP_FN_FFT_RFFT_EXT
47-
DPNP_FN_FILL_DIAGONAL_EXT
4846
DPNP_FN_FMOD_EXT
4947
DPNP_FN_MAXIMUM_EXT
5048
DPNP_FN_MEDIAN_EXT

dpnp/dpnp_algo/dpnp_algo_indexing.pxi

Lines changed: 0 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,13 @@ and the rest of the library
3737

3838
__all__ += [
3939
"dpnp_choose",
40-
"dpnp_diag_indices",
41-
"dpnp_fill_diagonal",
4240
"dpnp_putmask",
4341
"dpnp_select",
44-
"dpnp_tril_indices",
45-
"dpnp_tril_indices_from",
46-
"dpnp_triu_indices",
47-
"dpnp_triu_indices_from"
4842
]
4943

5044
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_choose_t)(c_dpctl.DPCTLSyclQueueRef,
5145
void *, void * , void ** , size_t, size_t, size_t,
5246
const c_dpctl.DPCTLEventVectorRef)
53-
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_diag_indices)(c_dpctl.DPCTLSyclQueueRef,
54-
void * , size_t,
55-
const c_dpctl.DPCTLEventVectorRef)
56-
ctypedef c_dpctl.DPCTLSyclEventRef(*custom_indexing_2in_func_ptr_t)(c_dpctl.DPCTLSyclQueueRef,
57-
void *, void * , shape_elem_type * , const size_t,
58-
const c_dpctl.DPCTLEventVectorRef)
59-
6047

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

10794

108-
cpdef tuple dpnp_diag_indices(n, ndim):
109-
cdef size_t res_size = 0 if n < 0 else n
110-
111-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dpnp.int64)
112-
113-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_DIAG_INDICES_EXT, param1_type, param1_type)
114-
115-
cdef fptr_dpnp_diag_indices func = <fptr_dpnp_diag_indices > kernel_data.ptr
116-
117-
cdef c_dpctl.SyclQueue q
118-
cdef c_dpctl.DPCTLSyclQueueRef q_ref
119-
cdef c_dpctl.DPCTLSyclEventRef event_ref
120-
121-
res_list = []
122-
cdef utils.dpnp_descriptor res_arr
123-
cdef shape_type_c result_shape = utils._object_to_tuple(res_size)
124-
for i in range(ndim):
125-
res_arr = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
126-
127-
q = <c_dpctl.SyclQueue> res_arr.get_array().sycl_queue
128-
q_ref = q.get_queue_ref()
129-
130-
event_ref = func(q_ref, res_arr.get_data(), res_size, NULL)
131-
132-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
133-
c_dpctl.DPCTLEvent_Delete(event_ref)
134-
135-
res_list.append(res_arr.get_pyobj())
136-
137-
return tuple(res_list)
138-
139-
140-
cpdef dpnp_fill_diagonal(dpnp_descriptor x1, val):
141-
x1_obj = x1.get_array()
142-
143-
cdef shape_type_c x1_shape = x1.shape
144-
cdef utils.dpnp_descriptor val_arr = utils_py.create_output_descriptor_py((1,),
145-
x1.dtype,
146-
None,
147-
device=x1_obj.sycl_device,
148-
usm_type=x1_obj.usm_type,
149-
sycl_queue=x1_obj.sycl_queue)
150-
151-
val_arr.get_pyobj()[0] = val
152-
153-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
154-
155-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_FILL_DIAGONAL_EXT, param1_type, param1_type)
156-
157-
cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> x1_obj.sycl_queue
158-
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()
159-
160-
cdef custom_indexing_2in_func_ptr_t func = <custom_indexing_2in_func_ptr_t > kernel_data.ptr
161-
162-
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
163-
x1.get_data(),
164-
val_arr.get_data(),
165-
x1_shape.data(),
166-
x1.ndim,
167-
NULL) # dep_events_ref
168-
169-
with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
170-
c_dpctl.DPCTLEvent_Delete(event_ref)
171-
172-
17395
cpdef dpnp_putmask(utils.dpnp_descriptor arr, utils.dpnp_descriptor mask, utils.dpnp_descriptor values):
17496
cdef int values_size = values.size
17597

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

199121
return res_array
200-
201-
202-
cpdef tuple dpnp_tril_indices(n, k=0, m=None):
203-
array1 = []
204-
array2 = []
205-
if m is None:
206-
for i in range(n):
207-
for j in range(i + 1 + k):
208-
if j >= n:
209-
continue
210-
else:
211-
array1.append(i)
212-
array2.append(j)
213-
else:
214-
for i in range(n):
215-
for j in range(i + 1 + k):
216-
if j < m:
217-
array1.append(i)
218-
array2.append(j)
219-
220-
array1 = dpnp.array(array1, dtype=dpnp.int64)
221-
array2 = dpnp.array(array2, dtype=dpnp.int64)
222-
return (array1, array2)
223-
224-
225-
cpdef tuple dpnp_tril_indices_from(dpnp_descriptor arr, k=0):
226-
m = arr.shape[0]
227-
n = arr.shape[1]
228-
array1 = []
229-
array2 = []
230-
if m is None:
231-
for i in range(n):
232-
for j in range(i + 1 + k):
233-
if j >= n:
234-
continue
235-
else:
236-
array1.append(i)
237-
array2.append(j)
238-
else:
239-
for i in range(n):
240-
for j in range(i + 1 + k):
241-
if j < m:
242-
array1.append(i)
243-
array2.append(j)
244-
245-
array1 = dpnp.array(array1, dtype=dpnp.int64)
246-
array2 = dpnp.array(array2, dtype=dpnp.int64)
247-
return (array1, array2)
248-
249-
250-
cpdef tuple dpnp_triu_indices(n, k=0, m=None):
251-
array1 = []
252-
array2 = []
253-
if m is None:
254-
for i in range(n):
255-
for j in range(i + k, n):
256-
array1.append(i)
257-
array2.append(j)
258-
else:
259-
for i in range(n):
260-
for j in range(i + k, m):
261-
array1.append(i)
262-
array2.append(j)
263-
264-
array1 = dpnp.array(array1, dtype=dpnp.int64)
265-
array2 = dpnp.array(array2, dtype=dpnp.int64)
266-
return (array1, array2)
267-
268-
269-
cpdef tuple dpnp_triu_indices_from(dpnp_descriptor arr, k=0):
270-
m = arr.shape[0]
271-
n = arr.shape[1]
272-
array1 = []
273-
array2 = []
274-
if m is None:
275-
for i in range(n):
276-
for j in range(i + k, n):
277-
array1.append(i)
278-
array2.append(j)
279-
else:
280-
for i in range(n):
281-
for j in range(i + k, m):
282-
array1.append(i)
283-
array2.append(j)
284-
285-
array1 = dpnp.array(array1, dtype=dpnp.int64)
286-
array2 = dpnp.array(array2, dtype=dpnp.int64)
287-
return (array1, array2)

0 commit comments

Comments
 (0)