Skip to content

Commit c366fb7

Browse files
authored
FULL add kernel (#558)
* FULL add kernel
1 parent 6b0c990 commit c366fb7

11 files changed

+135
-46
lines changed

dpnp/backend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ project(dpnp_project
164164
# Building logic...
165165
# -----------------------------------------------------------------------------------------------
166166
set(DPNP_SRC
167+
kernels/dpnp_krnl_arraycreation.cpp
167168
kernels/dpnp_krnl_bitwise.cpp
168169
kernels/dpnp_krnl_common.cpp
169170
kernels/dpnp_krnl_elemwise.cpp

dpnp/backend/include/dpnp_iface.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ void dpnp_memory_memcpy_c(void* dst, const void* src, size_t size_in_bytes);
118118
template <typename _DataType>
119119
INP_DLLEXPORT void dpnp_arange_c(size_t start, size_t step, void* result1, size_t size);
120120

121+
/**
122+
* @ingroup BACKEND_API
123+
* @brief Implementation of full function
124+
*
125+
* @param [in] array_in Input one-element array.
126+
* @param [out] result Output array.
127+
* @param [in] size Number of elements in the output array.
128+
*/
129+
template <typename _DataType>
130+
INP_DLLEXPORT void dpnp_full_c(void* array_in, void* result, const size_t size);
131+
121132
/**
122133
* @ingroup BACKEND_API
123134
* @brief Matrix multiplication.

dpnp/backend/include/dpnp_iface_fptr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum class DPNPFuncName : size_t
102102
DPNP_FN_FLOOR, /**< Used in numpy.floor() implementation */
103103
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() implementation */
104104
DPNP_FN_FMOD, /**< Used in numpy.fmod() implementation */
105+
DPNP_FN_FULL, /**< Used in numpy.full() implementation */
105106
DPNP_FN_HYPOT, /**< Used in numpy.hypot() implementation */
106107
DPNP_FN_INITVAL, /**< Used in numpy ones, ones_like, zeros, zeros_like, full, full_like impl */
107108
DPNP_FN_INV, /**< Used in numpy.linalg.inv() implementation */
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2016-2020, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// - Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// - Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
// THE POSSIBILITY OF SUCH DAMAGE.
24+
//*****************************************************************************
25+
26+
#include <iostream>
27+
28+
#include "dpnp_fptr.hpp"
29+
#include "dpnp_iface.hpp"
30+
#include "queue_sycl.hpp"
31+
32+
template <typename _KernelNameSpecialization>
33+
class dpnp_arange_c_kernel;
34+
35+
template <typename _DataType>
36+
void dpnp_arange_c(size_t start, size_t step, void* result1, size_t size)
37+
{
38+
// parameter `size` used instead `stop` to avoid dependency on array length calculation algorithm
39+
// TODO: floating point (and negatives) types from `start` and `step`
40+
41+
if (!size)
42+
{
43+
return;
44+
}
45+
46+
cl::sycl::event event;
47+
48+
_DataType* result = reinterpret_cast<_DataType*>(result1);
49+
50+
cl::sycl::range<1> gws(size);
51+
auto kernel_parallel_for_func = [=](cl::sycl::id<1> global_id) {
52+
size_t i = global_id[0];
53+
54+
result[i] = start + i * step;
55+
};
56+
57+
auto kernel_func = [&](cl::sycl::handler& cgh) {
58+
cgh.parallel_for<class dpnp_arange_c_kernel<_DataType>>(gws, kernel_parallel_for_func);
59+
};
60+
61+
event = DPNP_QUEUE.submit(kernel_func);
62+
63+
event.wait();
64+
}
65+
66+
template <typename _KernelNameSpecialization>
67+
class dpnp_full_c_kernel;
68+
69+
template <typename _DataType>
70+
void dpnp_full_c(void* array_in, void* result, const size_t size)
71+
{
72+
dpnp_initval_c<_DataType>(result, array_in, size);
73+
}
74+
75+
void func_map_init_arraycreation(func_map_t& fmap)
76+
{
77+
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_arange_c<int>};
78+
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_arange_c<long>};
79+
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_arange_c<float>};
80+
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_arange_c<double>};
81+
82+
fmap[DPNPFuncName::DPNP_FN_FULL][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_full_c<int>};
83+
fmap[DPNPFuncName::DPNP_FN_FULL][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_full_c<long>};
84+
fmap[DPNPFuncName::DPNP_FN_FULL][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_full_c<float>};
85+
fmap[DPNPFuncName::DPNP_FN_FULL][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_full_c<double>};
86+
87+
return;
88+
}

dpnp/backend/kernels/dpnp_krnl_elemwise.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -254,47 +254,8 @@ static void func_map_init_elemwise_1arg_2type(func_map_t& fmap)
254254

255255
#include <dpnp_gen_1arg_1type_tbl.hpp>
256256

257-
template <typename _KernelNameSpecialization>
258-
class dpnp_arange_c_kernel;
259-
260-
template <typename _DataType>
261-
void dpnp_arange_c(size_t start, size_t step, void* result1, size_t size)
262-
{
263-
// parameter `size` used instead `stop` to avoid dependency on array length calculation algorithm
264-
// TODO: floating point (and negatives) types from `start` and `step`
265-
266-
if (!size)
267-
{
268-
return;
269-
}
270-
271-
cl::sycl::event event;
272-
273-
_DataType* result = reinterpret_cast<_DataType*>(result1);
274-
275-
cl::sycl::range<1> gws(size);
276-
auto kernel_parallel_for_func = [=](cl::sycl::id<1> global_id) {
277-
size_t i = global_id[0];
278-
279-
result[i] = start + i * step;
280-
};
281-
282-
auto kernel_func = [&](cl::sycl::handler& cgh) {
283-
cgh.parallel_for<class dpnp_arange_c_kernel<_DataType>>(gws, kernel_parallel_for_func);
284-
};
285-
286-
event = DPNP_QUEUE.submit(kernel_func);
287-
288-
event.wait();
289-
}
290-
291257
static void func_map_init_elemwise_1arg_1type(func_map_t& fmap)
292258
{
293-
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_arange_c<int>};
294-
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_arange_c<long>};
295-
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_arange_c<float>};
296-
fmap[DPNPFuncName::DPNP_FN_ARANGE][eft_DBL][eft_DBL] = {eft_DBL, (void*)dpnp_arange_c<double>};
297-
298259
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_INT][eft_INT] = {eft_INT, (void*)dpnp_copy_c<int>};
299260
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_LNG][eft_LNG] = {eft_LNG, (void*)dpnp_copy_c<long>};
300261
fmap[DPNPFuncName::DPNP_FN_CONJIGUATE][eft_FLT][eft_FLT] = {eft_FLT, (void*)dpnp_copy_c<float>};

dpnp/backend/src/dpnp_fptr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const DPNPFuncType eft_BOOL = DPNPFuncType::DPNP_FT_BOOL;
6666
/**
6767
* FPTR interface initialization functions
6868
*/
69+
void func_map_init_arraycreation(func_map_t& fmap);
6970
void func_map_init_bitwise(func_map_t& fmap);
7071
void func_map_init_elemwise(func_map_t& fmap);
7172
void func_map_init_fft_func(func_map_t& fmap);

dpnp/backend/src/dpnp_iface_fptr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ static func_map_t func_map_init()
134134
{
135135
func_map_t fmap;
136136

137+
func_map_init_arraycreation(fmap);
137138
func_map_init_bitwise(fmap);
138139
func_map_init_elemwise(fmap);
139140
func_map_init_fft_func(fmap);

dpnp/dpnp_algo/dpnp_algo.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
7575
DPNP_FN_FLOOR
7676
DPNP_FN_FLOOR_DIVIDE
7777
DPNP_FN_FMOD
78+
DPNP_FN_FULL
7879
DPNP_FN_HYPOT
7980
DPNP_FN_INITVAL
8081
DPNP_FN_INV

dpnp/dpnp_algo/dpnp_algo_arraycreation.pyx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ from dpnp.dpnp_algo cimport *
4343
__all__ += [
4444
"dpnp_copy",
4545
"dpnp_diag",
46+
"dpnp_full",
4647
"dpnp_geomspace",
4748
"dpnp_linspace",
4849
"dpnp_logspace",
@@ -89,6 +90,28 @@ cpdef dparray dpnp_diag(v, k):
8990
return result
9091

9192

93+
cpdef dparray dpnp_full(result_shape, value_in, result_dtype):
94+
# Convert string type names (dparray.dtype) to C enum DPNPFuncType
95+
cdef DPNPFuncType dtype_in = dpnp_dtype_to_DPNPFuncType(result_dtype)
96+
97+
# get the FPTR data structure
98+
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_FULL, dtype_in, DPNP_FT_NONE)
99+
100+
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
101+
# Create single-element input array with type given by FPTR data
102+
cdef dparray_shape_type shape_in = (1,)
103+
cdef dparray array_in = dparray(shape_in, dtype=result_type)
104+
array_in[0] = value_in
105+
# Create result array with type given by FPTR data
106+
cdef dparray result = dparray(result_shape, dtype=result_type)
107+
108+
cdef fptr_1in_1out_t func = <fptr_1in_1out_t > kernel_data.ptr
109+
# Call FPTR function
110+
func(array_in.get_data(), result.get_data(), result.size)
111+
112+
return result
113+
114+
92115
cpdef dparray dpnp_geomspace(start, stop, num, endpoint, dtype, axis):
93116
cdef dparray result = dparray(num, dtype=dtype)
94117

dpnp/dpnp_iface_arraycreation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,16 @@ def full(shape, fill_value, dtype=None, order='C'):
611611
[10, 10, 10, 10]
612612
613613
"""
614-
615-
if (not use_origin_backend()):
614+
if not use_origin_backend():
616615
if order not in ('C', 'c', None):
617-
checker_throw_value_error("full", "order", order, 'C')
618-
619-
_dtype = dtype if dtype is not None else dpnp.dtype(type(fill_value))
616+
pass
617+
else:
618+
if dtype is None:
619+
dtype = numpy.array(fill_value).dtype.type
620620

621-
return dpnp_init_val(shape, _dtype, fill_value)
621+
return dpnp_full(shape, fill_value, dtype)
622622

623-
return numpy.full(shape, fill_value, dtype, order)
623+
return call_origin(numpy.full, shape, fill_value, dtype, order)
624624

625625

626626
# numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)

0 commit comments

Comments
 (0)