Skip to content

Commit 74c8670

Browse files
authored
native version dpnp.any() (#637)
* native version dpnp.any()
1 parent 4a498c1 commit 74c8670

File tree

6 files changed

+64
-25
lines changed

6 files changed

+64
-25
lines changed

dpnp/backend/include/dpnp_iface.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ void dpnp_memory_memcpy_c(void* dst, const void* src, size_t size_in_bytes);
116116
template <typename _DataType, typename _ResultType>
117117
INP_DLLEXPORT void dpnp_all_c(void* array, void* result, const size_t size);
118118

119+
/**
120+
* @ingroup BACKEND_API
121+
* @brief Test whether any array element along a given axis evaluates to True.
122+
*
123+
* @param [in] array Input array.
124+
* @param [out] result Output array.
125+
* @param [in] size Number of input elements in `array`.
126+
*/
127+
template <typename _DataType, typename _ResultType>
128+
INP_DLLEXPORT void dpnp_any_c(const void* array, void* result, const size_t size);
129+
119130
/**
120131
* @ingroup BACKEND_API
121132
* @brief Array initialization

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ enum class DPNPFuncName : size_t
6161
DPNP_FN_ABSOLUTE, /**< Used in numpy.absolute() implementation */
6262
DPNP_FN_ADD, /**< Used in numpy.add() implementation */
6363
DPNP_FN_ALL, /**< Used in numpy.all() implementation */
64+
DPNP_FN_ANY, /**< Used in numpy.any() implementation */
6465
DPNP_FN_ARANGE, /**< Used in numpy.arange() implementation */
6566
DPNP_FN_ARCCOS, /**< Used in numpy.arccos() implementation */
6667
DPNP_FN_ARCCOSH, /**< Used in numpy.arccosh() implementation */

dpnp/backend/kernels/dpnp_krnl_logic.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ void dpnp_all_c(void* array1_in, void* result1, const size_t size)
5252
return;
5353
}
5454

55+
template <typename _DataType, typename _ResultType>
56+
void dpnp_any_c(const void* array1_in, void* result1, const size_t size)
57+
{
58+
if ((array1_in == nullptr) || (result1 == nullptr))
59+
{
60+
return;
61+
}
62+
63+
const _DataType* array_in = reinterpret_cast<const _DataType*>(array1_in);
64+
_ResultType* result = reinterpret_cast<_ResultType*>(result1);
65+
66+
bool res = false;
67+
68+
for (size_t i = 0; i < size; ++i)
69+
{
70+
if (array_in[i])
71+
{
72+
res = true;
73+
break;
74+
}
75+
}
76+
77+
result[0] = res;
78+
79+
return;
80+
}
81+
5582
void func_map_init_logic(func_map_t& fmap)
5683
{
5784
fmap[DPNPFuncName::DPNP_FN_ALL][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_all_c<bool, bool>};
@@ -60,5 +87,11 @@ void func_map_init_logic(func_map_t& fmap)
6087
fmap[DPNPFuncName::DPNP_FN_ALL][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_all_c<float, bool>};
6188
fmap[DPNPFuncName::DPNP_FN_ALL][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_all_c<double, bool>};
6289

90+
fmap[DPNPFuncName::DPNP_FN_ANY][eft_BLN][eft_BLN] = {eft_BLN, (void*)dpnp_any_c<bool, bool>};
91+
fmap[DPNPFuncName::DPNP_FN_ANY][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_any_c<int, bool>};
92+
fmap[DPNPFuncName::DPNP_FN_ANY][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_any_c<long, bool>};
93+
fmap[DPNPFuncName::DPNP_FN_ANY][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_any_c<float, bool>};
94+
fmap[DPNPFuncName::DPNP_FN_ANY][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_any_c<double, bool>};
95+
6396
return;
6497
}

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
3434
DPNP_FN_ABSOLUTE
3535
DPNP_FN_ADD
3636
DPNP_FN_ALL
37+
DPNP_FN_ANY
3738
DPNP_FN_ARANGE
3839
DPNP_FN_ARCCOS
3940
DPNP_FN_ARCCOSH

dpnp/dpnp_algo/dpnp_algo_logic.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ cpdef dparray dpnp_all(dparray array1):
8080
cpdef dparray dpnp_any(dparray array1):
8181
cdef dparray result = dparray((1,), dtype=numpy.bool)
8282

83-
res = False
84-
for i in range(array1.size):
85-
if numpy.bool(array1[i]):
86-
res = True
87-
break
83+
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(array1.dtype)
84+
85+
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ANY, param1_type, param1_type)
8886

89-
result[0] = res
87+
cdef custom_logic_1in_1out_func_ptr_t func = <custom_logic_1in_1out_func_ptr_t > kernel_data.ptr
88+
89+
func(array1.get_data(), result.get_data(), array1.size)
9090

9191
return result
9292

dpnp/dpnp_iface_logic.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ def all(in_array1, axis=None, out=None, keepdims=False):
107107
108108
"""
109109

110-
is_dparray1 = isinstance(in_array1, dparray)
111-
112110
if not use_origin_backend(in_array1):
113111
if not isinstance(in_array1, dparray):
114112
pass
@@ -164,23 +162,18 @@ def any(in_array1, axis=None, out=None, keepdims=False):
164162
165163
"""
166164

167-
is_dparray1 = isinstance(in_array1, dparray)
168-
169-
if (not use_origin_backend(in_array1) and is_dparray1):
170-
if axis is not None:
171-
checker_throw_value_error("any", "axis", type(axis), None)
172-
if out is not None:
173-
checker_throw_value_error("any", "out", type(out), None)
174-
if keepdims is not False:
175-
checker_throw_value_error("any", "keepdims", keepdims, False)
176-
177-
result = dpnp_any(in_array1)
178-
179-
# scalar returned
180-
if result.shape == (1,):
181-
return result.dtype.type(result[0])
182-
183-
return result
165+
if (not use_origin_backend(in_array1)):
166+
if not isinstance(in_array1, dparray):
167+
pass
168+
elif axis is not None:
169+
pass
170+
elif out is not None:
171+
pass
172+
elif keepdims is not False:
173+
pass
174+
else:
175+
result = dpnp_any(in_array1)
176+
return result[0]
184177

185178
return call_origin(numpy.any, axis, out, keepdims)
186179

0 commit comments

Comments
 (0)