Skip to content
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

Asanyarray ascontiguousarray asfarray copy funcs #265

Merged
merged 11 commits into from
Nov 21, 2020
170 changes: 85 additions & 85 deletions dpnp/backend/backend_iface_fptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,91 +59,91 @@
*/
enum class DPNPFuncName : size_t
{
DPNP_FN_NONE, /**< Very first element of the enumeration */
DPNP_FN_ABSOLUTE, /**< Used in numpy.absolute() implementation */
DPNP_FN_ADD, /**< Used in numpy.add() implementation */
DPNP_FN_ARANGE, /**< Used in numpy.arange() implementation */
DPNP_FN_ARCCOS, /**< Used in numpy.arccos() implementation */
DPNP_FN_ARCCOSH, /**< Used in numpy.arccosh() implementation */
DPNP_FN_ARCSIN, /**< Used in numpy.arcsin() implementation */
DPNP_FN_ARCSINH, /**< Used in numpy.arcsinh() implementation */
DPNP_FN_ARCTAN, /**< Used in numpy.arctan() implementation */
DPNP_FN_ARCTAN2, /**< Used in numpy.arctan2() implementation */
DPNP_FN_ARCTANH, /**< Used in numpy.arctanh() implementation */
DPNP_FN_ARGMAX, /**< Used in numpy.argmax() implementation */
DPNP_FN_ARGMIN, /**< Used in numpy.argmin() implementation */
DPNP_FN_ARGSORT, /**< Used in numpy.argsort() implementation */
DPNP_FN_BETA, /**< Used in numpy.random.beta() implementation */
DPNP_FN_BINOMIAL, /**< Used in numpy.random.binomial() implementation */
DPNP_FN_BITWISE_AND, /**< Used in numpy.bitwise_and() implementation */
DPNP_FN_BITWISE_OR, /**< Used in numpy.bitwise_or() implementation */
DPNP_FN_BITWISE_XOR, /**< Used in numpy.bitwise_xor() implementation */
DPNP_FN_CBRT, /**< Used in numpy.cbrt() implementation */
DPNP_FN_CEIL, /**< Used in numpy.ceil() implementation */
DPNP_FN_CHISQUARE, /**< Used in numpy.random.chisquare() implementation */
DPNP_FN_CHOLESKY, /**< Used in numpy.linalg.cholesky() implementation */
DPNP_FN_COPYSIGN, /**< Used in numpy.copysign() implementation */
DPNP_FN_CORRELATE, /**< Used in numpy.correlate() implementation */
DPNP_FN_COS, /**< Used in numpy.cos() implementation */
DPNP_FN_COSH, /**< Used in numpy.cosh() implementation */
DPNP_FN_COV, /**< Used in numpy.cov() implementation */
DPNP_FN_DEGREES, /**< Used in numpy.degrees() implementation */
DPNP_FN_DET, /**< Used in numpy.linalg.det() implementation */
DPNP_FN_DIVIDE, /**< Used in numpy.divide() implementation */
DPNP_FN_DOT, /**< Used in numpy.dot() implementation */
DPNP_FN_EIG, /**< Used in numpy.linalg.eig() implementation */
DPNP_FN_EIGVALS, /**< Used in numpy.linalg.eigvals() implementation */
DPNP_FN_EXP, /**< Used in numpy.exp() implementation */
DPNP_FN_EXP2, /**< Used in numpy.exp2() implementation */
DPNP_FN_EXPM1, /**< Used in numpy.expm1() implementation */
DPNP_FN_EXPONENTIAL, /**< Used in numpy.random.exponential() implementation */
DPNP_FN_FABS, /**< Used in numpy.fabs() implementation */
DPNP_FN_FFT_FFT, /**< Used in numpy.fft.fft() implementation */
DPNP_FN_FLOOR, /**< Used in numpy.floor() implementation */
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() implementation */
DPNP_FN_FMOD, /**< Used in numpy.fmod() implementation */
DPNP_FN_GAMMA, /**< Used in numpy.random.gamma() implementation */
DPNP_FN_GAUSSIAN, /**< Used in numpy.random.randn() implementation */
DPNP_FN_HYPOT, /**< Used in numpy.hypot() implementation */
DPNP_FN_INVERT, /**< Used in numpy.invert() implementation */
DPNP_FN_LEFT_SHIFT, /**< Used in numpy.left_shift() implementation */
DPNP_FN_LOG, /**< Used in numpy.log() implementation */
DPNP_FN_LOG10, /**< Used in numpy.log10() implementation */
DPNP_FN_LOG2, /**< Used in numpy.log2() implementation */
DPNP_FN_LOG1P, /**< Used in numpy.log1p() implementation */
DPNP_FN_MATMUL, /**< Used in numpy.matmul() implementation */
DPNP_FN_MATRIX_RANK, /**< Used in numpy.linalg.matrix_rank() implementation */
DPNP_FN_MAX, /**< Used in numpy.max() implementation */
DPNP_FN_MAXIMUM, /**< Used in numpy.maximum() implementation */
DPNP_FN_MEAN, /**< Used in numpy.mean() implementation */
DPNP_FN_MEDIAN, /**< Used in numpy.median() implementation */
DPNP_FN_MIN, /**< Used in numpy.min() implementation */
DPNP_FN_MINIMUM, /**< Used in numpy.minimum() implementation */
DPNP_FN_MODF, /**< Used in numpy.modf() implementation */
DPNP_FN_MULTIPLY, /**< Used in numpy.multiply() implementation */
DPNP_FN_NEGATIVE_BINOMIAL, /**< Used in numpy.random.negative_binomial() implementation */
DPNP_FN_POWER, /**< Used in numpy.random.power() implementation */
DPNP_FN_PROD, /**< Used in numpy.prod() implementation */
DPNP_FN_RADIANS, /**< Used in numpy.radians() implementation */
DPNP_FN_REMAINDER, /**< Used in numpy.remainder() implementation */
DPNP_FN_RECIP, /**< Used in numpy.recip() implementation */
DPNP_FN_RIGHT_SHIFT, /**< Used in numpy.right_shift() implementation */
DPNP_FN_SIGN, /**< Used in numpy.sign() implementation */
DPNP_FN_SIN, /**< Used in numpy.sin() implementation */
DPNP_FN_SINH, /**< Used in numpy.sinh() implementation */
DPNP_FN_SORT, /**< Used in numpy.sort() implementation */
DPNP_FN_SQRT, /**< Used in numpy.sqrt() implementation */
DPNP_FN_SQUARE, /**< Used in numpy.square() implementation */
DPNP_FN_STD, /**< Used in numpy.std() implementation */
DPNP_FN_SUBTRACT, /**< Used in numpy.subtract() implementation */
DPNP_FN_SUM, /**< Used in numpy.sum() implementation */
DPNP_FN_TAN, /**< Used in numpy.tan() implementation */
DPNP_FN_TANH, /**< Used in numpy.tanh() implementation */
DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() implementation */
DPNP_FN_TRUNC, /**< Used in numpy.trunc() implementation */
DPNP_FN_UNIFORM, /**< Used in numpy.random.uniform() implementation */
DPNP_FN_VAR, /**< Used in numpy.var() implementation */
DPNP_FN_LAST /**< The latest element of the enumeration */
DPNP_FN_NONE, /**< Very first element of the enumeration */
DPNP_FN_ABSOLUTE, /**< Used in numpy.absolute() implementation */
DPNP_FN_ADD, /**< Used in numpy.add() implementation */
DPNP_FN_ARANGE, /**< Used in numpy.arange() implementation */
DPNP_FN_ARCCOS, /**< Used in numpy.arccos() implementation */
DPNP_FN_ARCCOSH, /**< Used in numpy.arccosh() implementation */
DPNP_FN_ARCSIN, /**< Used in numpy.arcsin() implementation */
DPNP_FN_ARCSINH, /**< Used in numpy.arcsinh() implementation */
DPNP_FN_ARCTAN, /**< Used in numpy.arctan() implementation */
DPNP_FN_ARCTAN2, /**< Used in numpy.arctan2() implementation */
DPNP_FN_ARCTANH, /**< Used in numpy.arctanh() implementation */
DPNP_FN_ARGMAX, /**< Used in numpy.argmax() implementation */
DPNP_FN_ARGMIN, /**< Used in numpy.argmin() implementation */
DPNP_FN_ARGSORT, /**< Used in numpy.argsort() implementation */
DPNP_FN_BETA, /**< Used in numpy.random.beta() implementation */
DPNP_FN_BINOMIAL, /**< Used in numpy.random.binomial() implementation */
DPNP_FN_BITWISE_AND, /**< Used in numpy.bitwise_and() implementation */
DPNP_FN_BITWISE_OR, /**< Used in numpy.bitwise_or() implementation */
DPNP_FN_BITWISE_XOR, /**< Used in numpy.bitwise_xor() implementation */
DPNP_FN_CBRT, /**< Used in numpy.cbrt() implementation */
DPNP_FN_CEIL, /**< Used in numpy.ceil() implementation */
DPNP_FN_CHISQUARE, /**< Used in numpy.random.chisquare() implementation */
DPNP_FN_CHOLESKY, /**< Used in numpy.linalg.cholesky() implementation */
DPNP_FN_COPYSIGN, /**< Used in numpy.copysign() implementation */
DPNP_FN_CORRELATE, /**< Used in numpy.correlate() implementation */
DPNP_FN_COS, /**< Used in numpy.cos() implementation */
DPNP_FN_COSH, /**< Used in numpy.cosh() implementation */
DPNP_FN_COV, /**< Used in numpy.cov() implementation */
DPNP_FN_DEGREES, /**< Used in numpy.degrees() implementation */
DPNP_FN_DET, /**< Used in numpy.linalg.det() implementation */
DPNP_FN_DIVIDE, /**< Used in numpy.divide() implementation */
DPNP_FN_DOT, /**< Used in numpy.dot() implementation */
DPNP_FN_EIG, /**< Used in numpy.linalg.eig() implementation */
DPNP_FN_EIGVALS, /**< Used in numpy.linalg.eigvals() implementation */
DPNP_FN_EXP, /**< Used in numpy.exp() implementation */
DPNP_FN_EXP2, /**< Used in numpy.exp2() implementation */
DPNP_FN_EXPM1, /**< Used in numpy.expm1() implementation */
DPNP_FN_EXPONENTIAL, /**< Used in numpy.random.exponential() implementation */
DPNP_FN_FABS, /**< Used in numpy.fabs() implementation */
DPNP_FN_FFT_FFT, /**< Used in numpy.fft.fft() implementation */
DPNP_FN_FLOOR, /**< Used in numpy.floor() implementation */
DPNP_FN_FLOOR_DIVIDE, /**< Used in numpy.floor_divide() implementation */
DPNP_FN_FMOD, /**< Used in numpy.fmod() implementation */
DPNP_FN_GAMMA, /**< Used in numpy.random.gamma() implementation */
DPNP_FN_GAUSSIAN, /**< Used in numpy.random.randn() implementation */
DPNP_FN_HYPOT, /**< Used in numpy.hypot() implementation */
DPNP_FN_INVERT, /**< Used in numpy.invert() implementation */
DPNP_FN_LEFT_SHIFT, /**< Used in numpy.left_shift() implementation */
DPNP_FN_LOG, /**< Used in numpy.log() implementation */
DPNP_FN_LOG10, /**< Used in numpy.log10() implementation */
DPNP_FN_LOG2, /**< Used in numpy.log2() implementation */
DPNP_FN_LOG1P, /**< Used in numpy.log1p() implementation */
DPNP_FN_MATMUL, /**< Used in numpy.matmul() implementation */
DPNP_FN_MATRIX_RANK, /**< Used in numpy.linalg.matrix_rank() implementation */
DPNP_FN_MAX, /**< Used in numpy.max() implementation */
DPNP_FN_MAXIMUM, /**< Used in numpy.maximum() implementation */
DPNP_FN_MEAN, /**< Used in numpy.mean() implementation */
DPNP_FN_MEDIAN, /**< Used in numpy.median() implementation */
DPNP_FN_MIN, /**< Used in numpy.min() implementation */
DPNP_FN_MINIMUM, /**< Used in numpy.minimum() implementation */
DPNP_FN_MODF, /**< Used in numpy.modf() implementation */
DPNP_FN_MULTIPLY, /**< Used in numpy.multiply() implementation */
DPNP_FN_NEGATIVE_BINOMIAL, /**< Used in numpy.random.negative_binomial() implementation */
DPNP_FN_POWER, /**< Used in numpy.random.power() implementation */
DPNP_FN_PROD, /**< Used in numpy.prod() implementation */
DPNP_FN_RADIANS, /**< Used in numpy.radians() implementation */
DPNP_FN_REMAINDER, /**< Used in numpy.remainder() implementation */
DPNP_FN_RECIP, /**< Used in numpy.recip() implementation */
DPNP_FN_RIGHT_SHIFT, /**< Used in numpy.right_shift() implementation */
DPNP_FN_SIGN, /**< Used in numpy.sign() implementation */
DPNP_FN_SIN, /**< Used in numpy.sin() implementation */
DPNP_FN_SINH, /**< Used in numpy.sinh() implementation */
DPNP_FN_SORT, /**< Used in numpy.sort() implementation */
DPNP_FN_SQRT, /**< Used in numpy.sqrt() implementation */
DPNP_FN_SQUARE, /**< Used in numpy.square() implementation */
DPNP_FN_STD, /**< Used in numpy.std() implementation */
DPNP_FN_SUBTRACT, /**< Used in numpy.subtract() implementation */
DPNP_FN_SUM, /**< Used in numpy.sum() implementation */
DPNP_FN_TAN, /**< Used in numpy.tan() implementation */
DPNP_FN_TANH, /**< Used in numpy.tanh() implementation */
DPNP_FN_TRANSPOSE, /**< Used in numpy.transpose() implementation */
DPNP_FN_TRUNC, /**< Used in numpy.trunc() implementation */
DPNP_FN_UNIFORM, /**< Used in numpy.random.uniform() implementation */
DPNP_FN_VAR, /**< Used in numpy.var() implementation */
DPNP_FN_LAST /**< The latest element of the enumeration */
};

/**
Expand Down
3 changes: 2 additions & 1 deletion dpnp/backend/custom_kernels_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ void func_map_init_random(func_map_t& fmap)
fmap[DPNPFuncName::DPNP_FN_GAUSSIAN][eft_DBL][eft_DBL] = {eft_DBL, (void*)custom_rng_gaussian_c<double>};
fmap[DPNPFuncName::DPNP_FN_GAUSSIAN][eft_FLT][eft_FLT] = {eft_FLT, (void*)custom_rng_gaussian_c<float>};

fmap[DPNPFuncName::DPNP_FN_NEGATIVE_BINOMIAL][eft_INT][eft_INT] = {eft_INT, (void*)custom_rng_negative_binomial_c<int>};
fmap[DPNPFuncName::DPNP_FN_NEGATIVE_BINOMIAL][eft_INT][eft_INT] = {eft_INT,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not look like these changes belong to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a result of applying of code style tool... The correct way is to apply code style tool during work on changes, but if someone missed it, then these changes occur when I apply code style. Sometimes it is hard to split these changes in new and old code. We should improve our CI to automatic check/apply of code style.

(void*)custom_rng_negative_binomial_c<int>};

fmap[DPNPFuncName::DPNP_FN_UNIFORM][eft_DBL][eft_DBL] = {eft_DBL, (void*)custom_rng_uniform_c<double>};
fmap[DPNPFuncName::DPNP_FN_UNIFORM][eft_FLT][eft_FLT] = {eft_FLT, (void*)custom_rng_uniform_c<float>};
Expand Down
71 changes: 56 additions & 15 deletions dpnp/dparray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ cimport numpy
cimport dpnp.dpnp_utils as utils


# initially copyed from original
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyed->copied

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alexander-Makaryev it looks like it is related to #89

cdef class _flagsobj:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don not understand why we need this. It is better to reuse this from NumPy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to get own behavior while we have no a full enough support of features like f-contiguous memory layout, strides, views (sharing the memory) etc. In fact at this moment we just return a constant values of flags and it will be easier to modify our own class "step by step". We can replace it with original Numpy class in future, when it will be possible to reuse original functionality. Possibly I am wrong)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alexander-Makaryev We "return a constant values" because we support one data state only at this time.
It is questionable we will support diffrent states.
Using Numpy Flags is also possible in "one state"- just construct it based on "constant values".
I think this is ambiguous code what is better to handle via "fallback" because it is not related to SYCL

aligned: bool
updateifcopy: bool
writeable: bool
writebackifcopy: bool
@property
def behaved(self) -> bool: ...

@property
def c_contiguous(self) -> bool:
return True

@property
def carray(self) -> bool: ...
@property
def contiguous(self) -> bool: ...

@property
def f_contiguous(self) -> bool:
return False

@property
def farray(self) -> bool: ...
@property
def fnc(self) -> bool: ...
@property
def forc(self) -> bool: ...
@property
def fortran(self) -> bool: ...
@property
def num(self) -> int: ...

@property
def owndata(self) -> bool:
return True

def __getitem__(self, key: str) -> bool: ...
def __setitem__(self, key: str, value: bool) -> None: ...


cdef class dparray:
"""Multi-dimensional array using USM interface for an Intel GPU device.

Expand Down Expand Up @@ -134,7 +175,7 @@ cdef class dparray:
"""

string = "<DPNP DParray:name={}".format(self.__class__.__name__)
string += ": mem=0x{:x}".format(< size_t > self._dparray_data)
string += ": mem=0x{:x}".format( < size_t > self._dparray_data)
string += ": size={}".format(self.size)
string += ": shape={}".format(self.shape)
string += ": type={}".format(self.dtype)
Expand Down Expand Up @@ -199,7 +240,7 @@ cdef class dparray:
self._dparray_shape = newshape # TODO strides, enpty dimentions and etc.

@property
def flags(self):
def flags(self) -> _flagsobj:
"""Object containing memory-layout information.

It only contains ``c_contiguous``, ``f_contiguous``, and ``owndata`` attributes.
Expand All @@ -209,7 +250,7 @@ cdef class dparray:

"""

return (True, False, False)
return _flagsobj()

@property
def strides(self):
Expand Down Expand Up @@ -271,7 +312,7 @@ cdef class dparray:
def __array_interface__(self):
# print(f"====__array_interface__====self._dparray_data={ < size_t > self._dparray_data}")
interface_dict = {
"data": (< size_t > self._dparray_data, False), # last parameter is "Writable"
"data": ( < size_t > self._dparray_data, False), # last parameter is "Writable"
"strides": self.strides,
"descr": None,
"typestr": self.dtype.str,
Expand Down Expand Up @@ -339,15 +380,15 @@ cdef class dparray:
raise utils.checker_throw_index_error("__getitem__", lin_idx, self.size)

if self.dtype == numpy.float64:
return (< double * > self._dparray_data)[lin_idx]
return ( < double * > self._dparray_data)[lin_idx]
elif self.dtype == numpy.float32:
return (< float * > self._dparray_data)[lin_idx]
return ( < float * > self._dparray_data)[lin_idx]
elif self.dtype == numpy.int64:
return (< long * > self._dparray_data)[lin_idx]
return ( < long * > self._dparray_data)[lin_idx]
elif self.dtype == numpy.int32:
return (< int * > self._dparray_data)[lin_idx]
return ( < int * > self._dparray_data)[lin_idx]
elif self.dtype == numpy.bool:
return (< cpp_bool * > self._dparray_data)[lin_idx]
return ( < cpp_bool * > self._dparray_data)[lin_idx]

utils.checker_throw_type_error("__getitem__", self.dtype)

Expand All @@ -364,15 +405,15 @@ cdef class dparray:
raise utils.checker_throw_index_error("__setitem__", lin_idx, self.size)

if self.dtype == numpy.float64:
(< double * > self._dparray_data)[lin_idx] = <double > value
( < double * > self._dparray_data)[lin_idx] = <double > value
elif self.dtype == numpy.float32:
(< float * > self._dparray_data)[lin_idx] = <float > value
( < float * > self._dparray_data)[lin_idx] = <float > value
elif self.dtype == numpy.int64:
(< long * > self._dparray_data)[lin_idx] = <long > value
( < long * > self._dparray_data)[lin_idx] = <long > value
elif self.dtype == numpy.int32:
(< int * > self._dparray_data)[lin_idx] = <int > value
( < int * > self._dparray_data)[lin_idx] = <int > value
elif self.dtype == numpy.bool:
(< cpp_bool * > self._dparray_data)[lin_idx] = < cpp_bool > value
( < cpp_bool * > self._dparray_data)[lin_idx] = < cpp_bool > value
else:
utils.checker_throw_type_error("__setitem__", self.dtype)

Expand Down Expand Up @@ -423,7 +464,7 @@ cdef class dparray:
"""

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

if order not in {'C', 'F', 'A', 'K'}:
pass
Expand Down
Loading