Skip to content

Commit 0afe971

Browse files
authored
add kernel in functions all, any (#640)
* add kernel in functions all, any
1 parent eab97b7 commit 0afe971

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

dpnp/backend/include/dpnp_iface.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void dpnp_memory_memcpy_c(void* dst, const void* src, size_t size_in_bytes);
114114
* @param [in] size Number of input elements in `array`.
115115
*/
116116
template <typename _DataType, typename _ResultType>
117-
INP_DLLEXPORT void dpnp_all_c(void* array, void* result, const size_t size);
117+
INP_DLLEXPORT void dpnp_all_c(const void* array, void* result, const size_t size);
118118

119119
/**
120120
* @ingroup BACKEND_API

dpnp/backend/kernels/dpnp_krnl_logic.cpp

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,90 @@
2929
#include "dpnp_iface.hpp"
3030
#include "queue_sycl.hpp"
3131

32+
33+
template <typename _DataType, typename _ResultType>
34+
class dpnp_all_c_kernel;
35+
3236
template <typename _DataType, typename _ResultType>
33-
void dpnp_all_c(void* array1_in, void* result1, const size_t size)
37+
void dpnp_all_c(const void* array1_in, void* result1, const size_t size)
3438
{
35-
_DataType* array_in = reinterpret_cast<_DataType*>(array1_in);
39+
cl::sycl::event event;
40+
41+
const _DataType* array_in = reinterpret_cast<const _DataType*>(array1_in);
3642
_ResultType* result = reinterpret_cast<_ResultType*>(result1);
3743

38-
bool res = true;
44+
if (!array1_in || !result1)
45+
{
46+
return;
47+
}
48+
49+
result[0] = true;
3950

40-
for (size_t i = 0; i < size; ++i)
51+
if (!size)
4152
{
53+
return;
54+
}
55+
56+
cl::sycl::range<1> gws(size);
57+
auto kernel_parallel_for_func = [=](cl::sycl::id<1> global_id) {
58+
size_t i = global_id[0];
59+
4260
if (!array_in[i])
4361
{
44-
res = false;
45-
break;
62+
result[0] = false;
4663
}
47-
}
64+
};
4865

49-
result[0] = res;
66+
auto kernel_func = [&](cl::sycl::handler& cgh) {
67+
cgh.parallel_for<class dpnp_all_c_kernel<_DataType, _ResultType>>(gws, kernel_parallel_for_func);
68+
};
5069

51-
return;
70+
event = DPNP_QUEUE.submit(kernel_func);
71+
72+
event.wait();
5273
}
5374

75+
template <typename _DataType, typename _ResultType>
76+
class dpnp_any_c_kernel;
77+
5478
template <typename _DataType, typename _ResultType>
5579
void dpnp_any_c(const void* array1_in, void* result1, const size_t size)
5680
{
57-
if ((array1_in == nullptr) || (result1 == nullptr))
58-
{
59-
return;
60-
}
81+
cl::sycl::event event;
6182

6283
const _DataType* array_in = reinterpret_cast<const _DataType*>(array1_in);
6384
_ResultType* result = reinterpret_cast<_ResultType*>(result1);
6485

65-
bool res = false;
6686

67-
for (size_t i = 0; i < size; ++i)
87+
if (!array1_in || !result1)
88+
{
89+
return;
90+
}
91+
92+
result[0] = false;
93+
94+
if (!size)
6895
{
96+
return;
97+
}
98+
99+
cl::sycl::range<1> gws(size);
100+
auto kernel_parallel_for_func = [=](cl::sycl::id<1> global_id) {
101+
size_t i = global_id[0];
102+
69103
if (array_in[i])
70104
{
71-
res = true;
72-
break;
105+
result[0] = true;
73106
}
74-
}
107+
};
75108

76-
result[0] = res;
109+
auto kernel_func = [&](cl::sycl::handler& cgh) {
110+
cgh.parallel_for<class dpnp_any_c_kernel<_DataType, _ResultType>>(gws, kernel_parallel_for_func);
111+
};
77112

78-
return;
113+
event = DPNP_QUEUE.submit(kernel_func);
114+
115+
event.wait();
79116
}
80117

81118
void func_map_init_logic(func_map_t& fmap)

tests/test_logic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
[numpy.float64, numpy.float32, numpy.int64, numpy.int32, numpy.bool, numpy.bool_],
1010
ids=['float64', 'float32', 'int64', 'int32', 'bool', 'bool_'])
1111
@pytest.mark.parametrize("shape",
12-
[(4,), (2, 3), (2, 2, 2)],
13-
ids=['(4,)', '(2,3)', '(2,2,2)'])
12+
[(0,), (4,), (2, 3), (2, 2, 2)],
13+
ids=['(0,)', '(4,)', '(2,3)', '(2,2,2)'])
1414
def test_all(type, shape):
1515
size = 1
1616
for i in range(len(shape)):
@@ -42,8 +42,8 @@ def test_all(type, shape):
4242
[numpy.float64, numpy.float32, numpy.int64, numpy.int32, numpy.bool, numpy.bool_],
4343
ids=['float64', 'float32', 'int64', 'int32', 'bool', 'bool_'])
4444
@pytest.mark.parametrize("shape",
45-
[(4,), (2, 3), (2, 2, 2)],
46-
ids=['(4,)', '(2,3)', '(2,2,2)'])
45+
[(0,), (4,), (2, 3), (2, 2, 2)],
46+
ids=['(0,)', '(4,)', '(2,3)', '(2,2,2)'])
4747
def test_any(type, shape):
4848
size = 1
4949
for i in range(len(shape)):

0 commit comments

Comments
 (0)