Skip to content

Add np.copy via dpnp #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions numba_dppy/dpnp_glue/dpnp_array_ops_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,54 @@ def dpnp_impl(a):
return dpnp_impl


@overload(stubs.dpnp.copy)
def dpnp_copy_impl(a):
name = "copy"
dpnp_lowering.ensure_dpnp(name)

ret_type = types.void
"""
dpnp source:
https://github.com/IntelPython/dpnp/blob/0.5.1/dpnp/backend/kernels/dpnp_krnl_elemwise.cpp#L213
Function declaration:
void dpnp_copy_c(void* array1_in, void* result1, size_t size)
"""
sig = signature(ret_type, types.voidptr, types.voidptr, types.intp)
dpnp_func = dpnp_ext.dpnp_func("dpnp_" + name, [a.dtype.name, "NONE"], sig)

res_dtype = a.dtype
PRINT_DEBUG = dpnp_lowering.DEBUG

def dpnp_impl(a):
if a.size == 0:
raise ValueError("Passed Empty array")

sycl_queue = dpctl_functions.get_current_queue()

a_usm = dpctl_functions.malloc_shared(a.size * a.itemsize, sycl_queue)
dpctl_functions.queue_memcpy(sycl_queue, a_usm, a.ctypes, a.size * a.itemsize)

out = np.arange(a.size, dtype=res_dtype)
out_usm = dpctl_functions.malloc_shared(out.size * out.itemsize, sycl_queue)

dpnp_func(a_usm, out_usm, a.size)

dpctl_functions.queue_memcpy(
sycl_queue, out.ctypes, out_usm, out.size * out.itemsize
)

dpctl_functions.free_with_queue(a_usm, sycl_queue)
dpctl_functions.free_with_queue(out_usm, sycl_queue)

dpnp_ext._dummy_liveness_func([a.size, out.size])

if PRINT_DEBUG:
print("dpnp implementation")
return out

return dpnp_impl


@overload(stubs.dpnp.sort)
def dpnp_sort_impl(a):
name = "sort"
Expand Down
3 changes: 3 additions & 0 deletions numba_dppy/dpnp_glue/dpnp_fptr_interface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_CBRT
DPNP_FN_CEIL
DPNP_FN_CHOLESKY
DPNP_FN_COPY
DPNP_FN_COPYSIGN
DPNP_FN_CORRELATE
DPNP_FN_COS
Expand Down Expand Up @@ -216,6 +217,8 @@ cdef DPNPFuncName get_DPNPFuncName_from_str(name):
return DPNPFuncName.DPNP_FN_CUMPROD
elif name == "dpnp_sort":
return DPNPFuncName.DPNP_FN_SORT
elif name == "dpnp_copy":
return DPNPFuncName.DPNP_FN_COPY
elif name == "dpnp_take":
return DPNPFuncName.DPNP_FN_TAKE
else:
Expand Down
3 changes: 3 additions & 0 deletions numba_dppy/dpnp_glue/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class zeros_like(Stub):
class full_like(Stub):
pass

class copy(Stub):
pass

class cumsum(Stub):
pass

Expand Down
1 change: 1 addition & 0 deletions numba_dppy/rename_numpy_functions_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"zeros_like": (["numpy"], "zeros_like"),
"full_like": (["numpy"], "full_like"),
# array ops
"copy": (["numpy"], "copy"),
"cumsum": (["numpy"], "cumsum"),
"cumprod": (["numpy"], "cumprod"),
"sort": (["numpy"], "sort"),
Expand Down
13 changes: 13 additions & 0 deletions numba_dppy/tests/test_dpnp_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,19 @@ def f(a, b):
class Testdpnp_array_ops_functions(unittest.TestCase):
tys = [np.int32, np.uint32, np.int64, np.uint64, np.float, np.double]

def test_copy(self):
@njit
def f(a):
c = np.copy(a)
return c

with assert_dpnp_implementaion():
self.assertTrue(
check_for_different_datatypes(
f, np.copy, [10], 1, self.tys, np_all=True
)
)

def test_cumsum(self):
@njit
def f(a):
Expand Down