Skip to content

Commit 26f640a

Browse files
RubtsowaLukichevaPolinaAlexander-Makaryev
authored
Dpnp ptp (#715)
* add python version ptp Co-authored-by: Lukicheva Polina <[email protected]> Co-authored-by: Alexander-Makaryev <[email protected]>
1 parent abd817e commit 26f640a

File tree

9 files changed

+184
-14
lines changed

9 files changed

+184
-14
lines changed

dpnp/backend/include/dpnp_iface.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,37 @@ INP_DLLEXPORT void dpnp_prod_c(void* result_out,
459459
const void* initial,
460460
const long* where);
461461

462+
/**
463+
* @ingroup BACKEND_API
464+
* @brief Range of values (maximum - minimum) along an axis.
465+
*
466+
* @param [out] result_out Output array.
467+
* @param [in] result_size Size of output array.
468+
* @param [in] result_ndim Number of output array dimensions.
469+
* @param [in] result_shape Shape of output array.
470+
* @param [in] result_strides Strides of output array.
471+
* @param [in] input_in First input array.
472+
* @param [in] input_size Size of first input array.
473+
* @param [in] input_ndim Number of first input array dimensions.
474+
* @param [in] input_shape Shape of first input array.
475+
* @param [in] input_strides Strides of first input array.
476+
* @param [in] axis Axis.
477+
* @param [in] naxis Number of elements in axis.
478+
*/
479+
template <typename _DataType>
480+
INP_DLLEXPORT void dpnp_ptp_c(void* result_out,
481+
const size_t result_size,
482+
const size_t result_ndim,
483+
const shape_elem_type* result_shape,
484+
const shape_elem_type* result_strides,
485+
const void* input_in,
486+
const size_t input_size,
487+
const size_t input_ndim,
488+
const shape_elem_type* input_shape,
489+
const shape_elem_type* input_strides,
490+
const shape_elem_type* axis,
491+
const size_t naxis);
492+
462493
/**
463494
* @ingroup BACKEND_API
464495
* @brief Product of array elements

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ enum class DPNPFuncName : size_t
149149
DPNP_FN_PLACE, /**< Used in numpy.place() implementation */
150150
DPNP_FN_POWER, /**< Used in numpy.power() implementation */
151151
DPNP_FN_PROD, /**< Used in numpy.prod() implementation */
152+
DPNP_FN_PTP, /**< Used in numpy.ptp() implementation */
152153
DPNP_FN_PUT, /**< Used in numpy.put() implementation */
153154
DPNP_FN_PUT_ALONG_AXIS, /**< Used in numpy.put_along_axis() implementation */
154155
DPNP_FN_QR, /**< Used in numpy.linalg.qr() implementation */

dpnp/backend/kernels/dpnp_krnl_arraycreation.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <iostream>
2727

2828
#include "dpnp_fptr.hpp"
29+
#include "dpnp_utils.hpp"
2930
#include "dpnp_iface.hpp"
3031
#include "dpnpc_memory_adapter.hpp"
3132
#include "queue_sycl.hpp"
@@ -205,6 +206,57 @@ void dpnp_ones_like_c(void* result, size_t size)
205206
dpnp_ones_c<_DataType>(result, size);
206207
}
207208

209+
template <typename _DataType>
210+
void dpnp_ptp_c(void* result1_out,
211+
const size_t result_size,
212+
const size_t result_ndim,
213+
const shape_elem_type* result_shape,
214+
const shape_elem_type* result_strides,
215+
const void* input1_in,
216+
const size_t input_size,
217+
const size_t input_ndim,
218+
const shape_elem_type* input_shape,
219+
const shape_elem_type* input_strides,
220+
const shape_elem_type* axis,
221+
const size_t naxis)
222+
{
223+
(void) result_strides;
224+
(void) input_strides;
225+
226+
if ((input1_in == nullptr) || (result1_out == nullptr))
227+
{
228+
return;
229+
}
230+
231+
if (input_ndim < 1)
232+
{
233+
return;
234+
}
235+
236+
DPNPC_ptr_adapter<_DataType> input1_ptr(input1_in, input_size, true);
237+
DPNPC_ptr_adapter<_DataType> result_ptr(result1_out, result_size, false, true);
238+
_DataType* arr = input1_ptr.get_ptr();
239+
_DataType* result = result_ptr.get_ptr();
240+
241+
_DataType* min_arr = reinterpret_cast<_DataType*>(dpnp_memory_alloc_c(result_size * sizeof(_DataType)));
242+
_DataType* max_arr = reinterpret_cast<_DataType*>(dpnp_memory_alloc_c(result_size * sizeof(_DataType)));
243+
244+
dpnp_min_c<_DataType>(arr, min_arr, result_size, input_shape, input_ndim, axis, naxis);
245+
dpnp_max_c<_DataType>(arr, max_arr, result_size, input_shape, input_ndim, axis, naxis);
246+
247+
shape_elem_type* _strides = reinterpret_cast<shape_elem_type*>(dpnp_memory_alloc_c(result_ndim * sizeof(shape_elem_type)));
248+
get_shape_offsets_inkernel(result_shape, result_ndim, _strides);
249+
250+
dpnp_subtract_c<_DataType, _DataType, _DataType>(result, result_size, result_ndim, result_shape, result_strides,
251+
max_arr, result_size, result_ndim, result_shape, _strides,
252+
min_arr, result_size, result_ndim, result_shape, _strides,
253+
NULL);
254+
255+
dpnp_memory_free_c(min_arr);
256+
dpnp_memory_free_c(max_arr);
257+
258+
}
259+
208260
template <typename _DataType_input, typename _DataType_output>
209261
void dpnp_vander_c(const void* array1_in, void* result1, const size_t size_in, const size_t N, const int increasing)
210262
{
@@ -635,6 +687,11 @@ void func_map_init_arraycreation(func_map_t& fmap)
635687
fmap[DPNPFuncName::DPNP_FN_ONES_LIKE][eft_C128][eft_C128] = {eft_C128,
636688
(void*)dpnp_ones_like_c<std::complex<double>>};
637689

690+
fmap[DPNPFuncName::DPNP_FN_PTP][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_ptp_c<int32_t>};
691+
fmap[DPNPFuncName::DPNP_FN_PTP][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_ptp_c<int64_t>};
692+
fmap[DPNPFuncName::DPNP_FN_PTP][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_ptp_c<float>};
693+
fmap[DPNPFuncName::DPNP_FN_PTP][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_ptp_c<double>};
694+
638695
fmap[DPNPFuncName::DPNP_FN_VANDER][eft_INT][eft_INT] = {eft_LNG, (void*)dpnp_vander_c<int32_t, int64_t>};
639696
fmap[DPNPFuncName::DPNP_FN_VANDER][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_vander_c<int64_t, int64_t>};
640697
fmap[DPNPFuncName::DPNP_FN_VANDER][eft_FLT][eft_FLT] = {eft_DBL, (void*)dpnp_vander_c<float, double>};

dpnp/dparray.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,9 @@ cdef class dparray:
11891189
"""Return a copy of the array."""
11901190
return copy(self, order=order)
11911191
1192+
def ptp(self, axis=None, out=None, keepdims=numpy._NoValue):
1193+
return ptp(self, axis=axis, out=out, keepdims=keepdims)
1194+
11921195
def tobytes(self, order='C'):
11931196
""" Construct Python bytes containing the raw data bytes in the array.
11941197

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
128128
DPNP_FN_PLACE
129129
DPNP_FN_POWER
130130
DPNP_FN_PROD
131+
DPNP_FN_PTP
131132
DPNP_FN_PUT
132133
DPNP_FN_QR
133134
DPNP_FN_RADIANS

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ __all__ += [
4747
"dpnp_meshgrid",
4848
"dpnp_ones",
4949
"dpnp_ones_like",
50+
"dpnp_ptp",
5051
"dpnp_trace",
5152
"dpnp_tri",
5253
"dpnp_tril",
@@ -59,6 +60,9 @@ __all__ += [
5960

6061
ctypedef void(*custom_1in_1out_func_ptr_t)(void * , void * , const int , shape_elem_type * , shape_elem_type * , const size_t, const size_t)
6162
ctypedef void(*ftpr_custom_vander_1in_1out_t)(void *, void * , size_t, size_t, int)
63+
ctypedef void(*custom_arraycreation_1in_1out_func_ptr_t)(void * , const size_t, const size_t, const shape_elem_type*, const shape_elem_type*,
64+
void * , const size_t, const size_t, const shape_elem_type*, const shape_elem_type*,
65+
const shape_elem_type*, const size_t)
6266
ctypedef void(*custom_indexing_1out_func_ptr_t)(void *, const size_t , const size_t , const int)
6367
ctypedef void(*fptr_dpnp_eye_t)(void * , int , const shape_elem_type * )
6468
ctypedef void(*fptr_dpnp_trace_t)(const void * , void * , const shape_elem_type * , const size_t)
@@ -297,6 +301,63 @@ cpdef utils.dpnp_descriptor dpnp_ones_like(result_shape, result_dtype):
297301
return call_fptr_1out(DPNP_FN_ONES_LIKE, utils._object_to_tuple(result_shape), result_dtype)
298302

299303

304+
cpdef dpnp_ptp(utils.dpnp_descriptor arr, axis=None):
305+
cdef shape_type_c shape_arr = arr.shape
306+
cdef shape_type_c output_shape
307+
if axis is None:
308+
axis_ = axis
309+
output_shape = (1,)
310+
else:
311+
if isinstance(axis, int):
312+
if axis < 0:
313+
axis_ = tuple([arr.ndim - axis])
314+
else:
315+
axis_ = tuple([axis])
316+
else:
317+
_axis_ = []
318+
for i in range(len(axis)):
319+
if axis[i] < 0:
320+
_axis_.append(arr.ndim - axis[i])
321+
else:
322+
_axis_.append(axis[i])
323+
axis_ = tuple(_axis_)
324+
325+
out_shape = []
326+
ind = 0
327+
for id, shape_axis in enumerate(shape_arr):
328+
if id not in axis_:
329+
out_shape.append(shape_axis)
330+
output_shape = tuple(out_shape)
331+
332+
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(arr.dtype)
333+
334+
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_PTP, param1_type, param1_type)
335+
336+
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(output_shape, kernel_data.return_type, None)
337+
338+
cdef custom_arraycreation_1in_1out_func_ptr_t func = <custom_arraycreation_1in_1out_func_ptr_t > kernel_data.ptr
339+
340+
cdef shape_type_c axis1
341+
cdef Py_ssize_t axis_size = 0
342+
cdef shape_type_c axis2 = axis1
343+
if axis_ is not None:
344+
axis1 = axis_
345+
axis2.reserve(len(axis1))
346+
for shape_it in axis1:
347+
if shape_it < 0:
348+
raise ValueError("DPNP dparray::__init__(): Negative values in 'shape' are not allowed")
349+
axis2.push_back(shape_it)
350+
axis_size = len(axis1)
351+
352+
cdef shape_type_c result_strides = utils.strides_to_vector(result.strides, result.shape)
353+
cdef shape_type_c arr_strides = utils.strides_to_vector(arr.strides, arr.shape)
354+
355+
func(result.get_data(), result.size, result.ndim, output_shape.data(), result_strides.data(),
356+
arr.get_data(), arr.size, arr.ndim, shape_arr.data(), arr_strides.data(), axis2.data(), axis_size)
357+
358+
return result
359+
360+
300361
cpdef utils.dpnp_descriptor dpnp_trace(utils.dpnp_descriptor arr, offset=0, axis1=0, axis2=1, dtype=None, out=None):
301362
if dtype is None:
302363
dtype_ = arr.dtype

dpnp/dpnp_iface_arraycreation.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"ogrid",
8080
"ones",
8181
"ones_like",
82+
"ptp",
8283
"trace",
8384
"tri",
8485
"tril",
@@ -1153,6 +1154,35 @@ def ones_like(x1, dtype=None, order='C', subok=False, shape=None):
11531154
return call_origin(numpy.ones_like, x1, dtype, order, subok, shape)
11541155

11551156

1157+
def ptp(arr, axis=None, out=None, keepdims=numpy._NoValue):
1158+
"""
1159+
Range of values (maximum - minimum) along an axis.
1160+
1161+
For full documentation refer to :obj:`numpy.ptp`.
1162+
1163+
Limitations
1164+
-----------
1165+
Input array is supported as :obj:`dpnp.ndarray`.
1166+
Parameters ``out`` and ``keepdims`` are supported only with default values.
1167+
"""
1168+
arr_desc = dpnp.get_dpnp_descriptor(arr)
1169+
if not arr_desc:
1170+
pass
1171+
elif axis is not None and not isinstance(axis, int):
1172+
pass
1173+
elif out is not None:
1174+
pass
1175+
elif keepdims is not numpy._NoValue:
1176+
pass
1177+
else:
1178+
result_obj = dpnp_ptp(arr_desc, axis=axis).get_pyobj()
1179+
result = dpnp.convert_single_elem_array_to_scalar(result_obj)
1180+
1181+
return result
1182+
1183+
return call_origin(numpy.ptp, arr, axis, out, keepdims)
1184+
1185+
11561186
def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None):
11571187
"""
11581188
Return the sum along diagonals of the array.

tests/skipped_tests.tbl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,10 +1452,3 @@ tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentil
14521452
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentile_scalar_q
14531453
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentile_tuple_axis
14541454
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentile_uxpected_interpolation
1455-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_all
1456-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_all_nan
1457-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis0
1458-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis1
1459-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis2
1460-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis_large
1461-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_nan

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,10 +1630,3 @@ tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentil
16301630
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentile_scalar_q
16311631
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentile_tuple_axis
16321632
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_percentile_uxpected_interpolation
1633-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_all
1634-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_all_nan
1635-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis0
1636-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis1
1637-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis2
1638-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_axis_large
1639-
tests/third_party/cupy/statistics_tests/test_order.py::TestOrder::test_ptp_nan

0 commit comments

Comments
 (0)