Skip to content

Add dtype and manipulation functions #1566

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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 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
1 change: 0 additions & 1 deletion dpnp/dparray.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ cdef class dparray:
cdef void * get_data(self)

cpdef item(self, id=*)
cpdef dparray astype(self, dtype, order=*, casting=*, subok=*, copy=*)
137 changes: 33 additions & 104 deletions dpnp/dparray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ from libcpp cimport bool as cpp_bool

import numpy

from dpnp.dpnp_algo import (
dpnp_astype,
dpnp_flatten,
)

# to avoid interference with Python internal functions
from dpnp.dpnp_iface import asnumpy
from dpnp.dpnp_iface import asnumpy, astype
from dpnp.dpnp_iface import get_dpnp_descriptor as iface_get_dpnp_descriptor
from dpnp.dpnp_iface import prod as iface_prod
from dpnp.dpnp_iface import sum as iface_sum
Expand Down Expand Up @@ -110,7 +105,9 @@ from dpnp.dpnp_iface_logic import ( # TODO do the same as for iface_sum
)
from dpnp.dpnp_iface_manipulation import (
copyto,
ravel,
repeat,
reshape,
squeeze,
transpose,
)
Expand Down Expand Up @@ -584,11 +581,10 @@ cdef class dparray:

Parameters
----------
order: {'C', 'F', 'A', 'K'}, optional
order: {'C', 'F'}, optional
'C' means to flatten in row-major (C-style) order.
'F' means to flatten in column-major (Fortran- style) order.
'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise.
'K' means to flatten a in the order the elements occur in memory. The default is 'C'.
The default is 'C'.

Returns
-------
Expand All @@ -601,42 +597,18 @@ cdef class dparray:

"""

if not utils.use_origin_backend(self):
c_order, fortran_order = self.flags.c_contiguous, self.flags.f_contiguous

if order not in {'C', 'F', 'A', 'K'}:
pass
elif order == 'K' and not c_order and not fortran_order:
# skip dpnp backend if both C-style and Fortran-style order not found in flags
pass
else:
if order == 'K':
# either C-style or Fortran-style found in flags
order = 'C' if c_order else 'F'
elif order == 'A':
order = 'F' if fortran_order else 'C'

if order == 'F':
return self.transpose().reshape(self.size)

self_desc = iface_get_dpnp_descriptor(self)
return dpnp_flatten(self_desc).get_pyobj()

result = dp2nd_array(self).flatten(order=order)

return nd2dp_array(result)
return ravel(self, order=order)

def ravel(self, order='C'):
"""
Return a contiguous flattened array.

Parameters
----------
order: {'C', 'F', 'A', 'K'}, optional
order: {'C', 'F'}, optional
'C' means to flatten in row-major (C-style) order.
'F' means to flatten in column-major (Fortran- style) order.
'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise.
'K' means to flatten a in the order the elements occur in memory. The default is 'C'.
The default is 'C'.

Returns
-------
Expand All @@ -654,8 +626,8 @@ cdef class dparray:
:obj:`dpnp.ravel`, :obj:`dpnp.flat`

"""
# TODO: don't copy the input array
return self.flatten(order=order)

return ravel(self, order=order)

def reshape(self, d0, *dn, order=b'C'):
"""Change the shape of the array.
Expand All @@ -665,39 +637,7 @@ cdef class dparray:

"""

if order is not b'C':
utils.checker_throw_value_error("dparray::reshape", "order", order, b'C')

if dn:
if not isinstance(d0, int):
msg_tmpl = "'{}' object cannot be interpreted as an integer"
raise TypeError(msg_tmpl.format(type(d0).__name__))
shape = [d0, *dn]
else:
shape = d0

cdef long shape_it = 0
cdef tuple shape_tup = utils._object_to_tuple(shape)
cdef size_previous = self.size

cdef long size_new = 1
cdef shape_type_c shape_new
shape_new.reserve(len(shape_tup))

for shape_it in shape_tup:
if shape_it < 0:
utils.checker_throw_value_error("dparray::reshape", "shape", shape_it, ">=0")

shape_new.push_back(shape_it)
size_new *= shape_it

if size_new != size_previous:
utils.checker_throw_value_error("dparray::reshape", "shape", size_new, size_previous)

self._dparray_shape = shape_new
self._dparray_size = size_new

return self
return reshape(self, d0, *dn, order=b'C')

def repeat(self, *args, **kwds):
""" Repeat elements of an array.
Expand Down Expand Up @@ -870,47 +810,36 @@ cdef class dparray:
def __truediv__(self, other):
return divide(self, other)

cpdef dparray astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
"""Copy the array with data type casting.
def astype(self, dtype, order='K', casting='unsafe', subok=True, copy=True):
"""
Copy the array with data type casting.

Args:
dtype: Target type.
order ({'C', 'F', 'A', 'K'}): Row-major (C-style) or column-major (Fortran-style) order.
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
And when ``order`` is 'K', it keeps strides as closely as possible.
copy (bool): If it is False and no cast happens, then this method returns the array itself.
Otherwise, a copy is returned.
Parameters
----------
dtype : dtype
Target data type.
order : {'C', 'F', 'A', 'K'}
Row-major (C-style) or column-major (Fortran-style) order.
When ``order`` is 'A', it uses 'F' if ``a`` is column-major and uses 'C' otherwise.
And when ``order`` is 'K', it keeps strides as closely as possible.
copy : bool
If it is False and no cast happens, then this method returns the array itself.
Otherwise, a copy is returned.

Returns:
Returns
-------
out : dpnp.array
If ``copy`` is False and no cast is required, then the array itself is returned.
Otherwise, it returns a (possibly casted) copy of the array.

.. note::
This method currently does not support `order``, `casting``, ``copy``, and ``subok`` arguments.

.. seealso:: :meth:`numpy.ndarray.astype`
Limitations
-----------
Parameter `subok` is supported with default value.
Otherwise the function will be executed sequentially on CPU.

"""

if casting is not 'unsafe':
pass
elif subok is not True:
pass
elif copy is not True:
pass
elif order is not 'K':
pass
elif self.dtype == numpy.complex128 or dtype == numpy.complex128:
pass
elif self.dtype == numpy.complex64 or dtype == numpy.complex64:
pass
else:
self_desc = iface_get_dpnp_descriptor(self)
return dpnp_astype(self_desc, dtype).get_pyobj()

result = dp2nd_array(self).astype(dtype=dtype, order=order, casting=casting, subok=subok, copy=copy)

return nd2dp_array(result)
return astype(self, dtype, order=order, casting=casting, subok=subok, copy=copy)

def conj(self):
"""
Expand Down
1 change: 0 additions & 1 deletion dpnp/dpnp_algo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

set(dpnp_algo_pyx_deps
${CMAKE_CURRENT_SOURCE_DIR}/dpnp_algo_linearalgebra.pxi
${CMAKE_CURRENT_SOURCE_DIR}/dpnp_algo_manipulation.pxi
${CMAKE_CURRENT_SOURCE_DIR}/dpnp_algo_counting.pxi
${CMAKE_CURRENT_SOURCE_DIR}/dpnp_algo_statistics.pxi
${CMAKE_CURRENT_SOURCE_DIR}/dpnp_algo_trigonometric.pxi
Expand Down
10 changes: 0 additions & 10 deletions dpnp/dpnp_algo/dpnp_algo.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_ARGSORT
DPNP_FN_ARGSORT_EXT
DPNP_FN_ASTYPE
DPNP_FN_ASTYPE_EXT
DPNP_FN_CBRT
DPNP_FN_CBRT_EXT
DPNP_FN_CHOLESKY
Expand Down Expand Up @@ -103,7 +102,6 @@ cdef extern from "dpnp_iface_fptr.hpp" namespace "DPNPFuncName": # need this na
DPNP_FN_FILL_DIAGONAL
DPNP_FN_FILL_DIAGONAL_EXT
DPNP_FN_FLATTEN
DPNP_FN_FLATTEN_EXT
DPNP_FN_FMOD
DPNP_FN_FMOD_EXT
DPNP_FN_FULL
Expand Down Expand Up @@ -351,9 +349,6 @@ ctypedef c_dpctl.DPCTLSyclEventRef(*dpnp_reduction_c_t)(c_dpctl.DPCTLSyclQueueRe
const long*,
const c_dpctl.DPCTLEventVectorRef)

cpdef dpnp_descriptor dpnp_astype(dpnp_descriptor x1, dtype)
cpdef dpnp_descriptor dpnp_flatten(dpnp_descriptor x1)


"""
Internal functions
Expand Down Expand Up @@ -391,11 +386,6 @@ cpdef dpnp_descriptor dpnp_maximum(dpnp_descriptor x1_obj, dpnp_descriptor x2_ob
cpdef dpnp_descriptor dpnp_minimum(dpnp_descriptor x1_obj, dpnp_descriptor x2_obj, object dtype=*,
dpnp_descriptor out=*, object where=*)

"""
Array manipulation routines
"""
cpdef dpnp_descriptor dpnp_repeat(dpnp_descriptor array1, repeats, axes=*)


"""
Statistics functions
Expand Down
93 changes: 0 additions & 93 deletions dpnp/dpnp_algo/dpnp_algo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ import operator
import numpy

__all__ = [
"dpnp_astype",
"dpnp_flatten",
"dpnp_queue_initialize",
]

Expand All @@ -65,7 +63,6 @@ include "dpnp_algo_counting.pxi"
include "dpnp_algo_indexing.pxi"
include "dpnp_algo_linearalgebra.pxi"
include "dpnp_algo_logic.pxi"
include "dpnp_algo_manipulation.pxi"
include "dpnp_algo_mathematical.pxi"
include "dpnp_algo_searching.pxi"
include "dpnp_algo_sorting.pxi"
Expand All @@ -74,96 +71,6 @@ include "dpnp_algo_statistics.pxi"
include "dpnp_algo_trigonometric.pxi"


ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_astype_t)(c_dpctl.DPCTLSyclQueueRef,
const void *, void * , const size_t,
const c_dpctl.DPCTLEventVectorRef)
ctypedef c_dpctl.DPCTLSyclEventRef(*fptr_dpnp_flatten_t)(c_dpctl.DPCTLSyclQueueRef,
void *, const size_t, const size_t,
const shape_elem_type * , const shape_elem_type * ,
void *, const size_t, const size_t,
const shape_elem_type * , const shape_elem_type * ,
const long * ,
const c_dpctl.DPCTLEventVectorRef)


cpdef utils.dpnp_descriptor dpnp_astype(utils.dpnp_descriptor x1, dtype):
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)
cdef DPNPFuncType param2_type = dpnp_dtype_to_DPNPFuncType(dtype)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_ASTYPE_EXT, param1_type, param2_type)

x1_obj = x1.get_array()

# ceate result array with type given by FPTR data
cdef shape_type_c result_shape = x1.shape
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
kernel_data.return_type,
None,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef fptr_dpnp_astype_t func = <fptr_dpnp_astype_t > kernel_data.ptr
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref, x1.get_data(), result.get_data(), x1.size, NULL)

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef utils.dpnp_descriptor dpnp_flatten(utils.dpnp_descriptor x1):
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(x1.dtype)

cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_FLATTEN_EXT, param1_type, param1_type)

cdef shape_type_c x1_shape = x1.shape
cdef shape_type_c x1_strides = utils.strides_to_vector(x1.strides, x1_shape)

x1_obj = x1.get_array()

# ceate result array with type given by FPTR data
cdef shape_type_c result_shape = (x1.size,)
cdef utils.dpnp_descriptor result = utils.create_output_descriptor(result_shape,
kernel_data.return_type,
None,
device=x1_obj.sycl_device,
usm_type=x1_obj.usm_type,
sycl_queue=x1_obj.sycl_queue)

result_sycl_queue = result.get_array().sycl_queue

cdef c_dpctl.SyclQueue q = <c_dpctl.SyclQueue> result_sycl_queue
cdef c_dpctl.DPCTLSyclQueueRef q_ref = q.get_queue_ref()

cdef shape_type_c result_strides = utils.strides_to_vector(result.strides, result_shape)

cdef fptr_dpnp_flatten_t func = <fptr_dpnp_flatten_t > kernel_data.ptr
cdef c_dpctl.DPCTLSyclEventRef event_ref = func(q_ref,
result.get_data(),
result.size,
result.ndim,
result_shape.data(),
result_strides.data(),
x1.get_data(),
x1.size,
x1.ndim,
x1_shape.data(),
x1_strides.data(),
NULL,
NULL) # dep_events_ref

with nogil: c_dpctl.DPCTLEvent_WaitAndThrow(event_ref)
c_dpctl.DPCTLEvent_Delete(event_ref)

return result


cpdef dpnp_queue_initialize():
"""
Initialize SYCL queue which will be used for any library operations.
Expand Down
Loading