From e44c964e6ffd6d3d8e3bf02f5403a55312f0d471 Mon Sep 17 00:00:00 2001 From: Ryosuke Okuta Date: Sun, 18 Jun 2017 23:02:03 +0900 Subject: [PATCH 1/4] Change default argument of copy method --- cupy/core/core.pyx | 17 ++++++----------- cupy/creation/from_data.py | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/cupy/core/core.pyx b/cupy/core/core.pyx index ae708c0e738..c1759db8704 100644 --- a/cupy/core/core.pyx +++ b/cupy/core/core.pyx @@ -339,7 +339,7 @@ cdef class ndarray: # TODO(okuta): Implement byteswap - cpdef ndarray copy(self, order='C'): + cpdef ndarray copy(self, order='K'): """Returns a copy of the array. This method makes a copy of a given array in the current device. @@ -439,7 +439,7 @@ cdef class ndarray: if strides.size() == shape.size(): newarray = self.view() else: - newarray = self.copy() + newarray = ascontiguousarray(self) strides = _get_strides_for_nocopy_reshape(newarray, shape) if shape.size() != strides.size(): @@ -556,12 +556,7 @@ cdef class ndarray: """ # TODO(beam2d): Support ordering option - if self._c_contiguous: - newarray = self.copy() - else: - newarray = ndarray(self.shape, self.dtype) - elementwise_copy(self, newarray) - + newarray = self.copy(order='C') newarray._shape.assign(1, self.size) newarray._strides.assign(1, self.itemsize) @@ -2626,7 +2621,7 @@ cpdef ndarray _take(ndarray a, indices, li=None, ri=None, ndarray out=None): if li is not None and ri is not None and li == ri: a = rollaxis(a, li) if out is None: - return a[indices].copy() + return ascontiguousarray(a[indices]) else: if out.dtype != a.dtype: raise TypeError('Output dtype mismatch') @@ -3392,14 +3387,14 @@ cpdef inline tuple _mat_to_cublas_contiguous(ndarray a, Py_ssize_t trans): lda = a._shape[0] return a, trans, lda if not a._c_contiguous: - a = a.copy() + a = ascontiguousarray(a) return a, 1 - trans, a._strides[0] // a.itemsize @cython.profile(False) cpdef inline tuple _to_cublas_vector(ndarray a, Py_ssize_t rundim): if a._strides[rundim] < 0: - return a.copy(), 1 + return ascontiguousarray(a), 1 else: return a, a._strides[rundim] // a.itemsize diff --git a/cupy/creation/from_data.py b/cupy/creation/from_data.py index cd3a76ebd78..b55d43399fb 100644 --- a/cupy/creation/from_data.py +++ b/cupy/creation/from_data.py @@ -80,7 +80,7 @@ def ascontiguousarray(a, dtype=None): # TODO(okuta): Implement asmatrix -def copy(a, order='C'): +def copy(a, order='K'): """Creates a copy of a given array on the current device. This function allocates the new array on the current device. If the given From 7179989eaff4e264b6115ba7f136fbfd083db147 Mon Sep 17 00:00:00 2001 From: Ryosuke Okuta Date: Mon, 10 Jul 2017 16:14:42 +0900 Subject: [PATCH 2/4] Fix default order --- cupy/core/core.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cupy/core/core.pyx b/cupy/core/core.pyx index c1759db8704..89094121995 100644 --- a/cupy/core/core.pyx +++ b/cupy/core/core.pyx @@ -339,7 +339,7 @@ cdef class ndarray: # TODO(okuta): Implement byteswap - cpdef ndarray copy(self, order='K'): + cpdef ndarray copy(self, order='C'): """Returns a copy of the array. This method makes a copy of a given array in the current device. From 3289dfcdda47585e997703fb0a5ec161e7f950ac Mon Sep 17 00:00:00 2001 From: Ryosuke Okuta Date: Mon, 10 Jul 2017 16:54:37 +0900 Subject: [PATCH 3/4] Add test --- tests/cupy_tests/creation_tests/test_from_data.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/cupy_tests/creation_tests/test_from_data.py b/tests/cupy_tests/creation_tests/test_from_data.py index 2ff5a1aa53f..5fe177c8b51 100644 --- a/tests/cupy_tests/creation_tests/test_from_data.py +++ b/tests/cupy_tests/creation_tests/test_from_data.py @@ -120,7 +120,7 @@ def test_ascontiguousarray_on_contiguous_array(self): @testing.numpy_cupy_array_equal() def test_copy(self, xp, dtype, order): a = xp.zeros((2, 3, 4), dtype=dtype) - b = a.copy(order=order) + b = xp.copy(a, order=order) a[1] = 1 return b @@ -131,9 +131,16 @@ def test_copy_multigpu(self, dtype, order): with cuda.Device(0): src = cupy.random.uniform(-1, 1, (2, 3)).astype(dtype) with cuda.Device(1): - dst = src.copy(order) + dst = cupy.copy(src, order) testing.assert_allclose(src, dst, rtol=0, atol=0) + @testing.for_CF_orders() + @testing.numpy_cupy_equal() + def test_copy_order(self, xp, order): + a = xp.zeros((2, 3, 4), order=order) + b = xp.copy(a) + return (b.flags.c_contiguous, b.flags.f_contiguous) + @testing.parameterize( *testing.product({ From e6056b16353c0b368492e3abcb53468bc4c9f3c9 Mon Sep 17 00:00:00 2001 From: Ryosuke Okuta Date: Sat, 15 Jul 2017 00:00:58 +0900 Subject: [PATCH 4/4] Use copy --- cupy/core/core.pyx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cupy/core/core.pyx b/cupy/core/core.pyx index 89094121995..dbc6ad13ec2 100644 --- a/cupy/core/core.pyx +++ b/cupy/core/core.pyx @@ -439,7 +439,7 @@ cdef class ndarray: if strides.size() == shape.size(): newarray = self.view() else: - newarray = ascontiguousarray(self) + newarray = self.copy() strides = _get_strides_for_nocopy_reshape(newarray, shape) if shape.size() != strides.size(): @@ -2621,7 +2621,7 @@ cpdef ndarray _take(ndarray a, indices, li=None, ri=None, ndarray out=None): if li is not None and ri is not None and li == ri: a = rollaxis(a, li) if out is None: - return ascontiguousarray(a[indices]) + return a[indices].copy() else: if out.dtype != a.dtype: raise TypeError('Output dtype mismatch') @@ -3387,14 +3387,14 @@ cpdef inline tuple _mat_to_cublas_contiguous(ndarray a, Py_ssize_t trans): lda = a._shape[0] return a, trans, lda if not a._c_contiguous: - a = ascontiguousarray(a) + a = a.copy() return a, 1 - trans, a._strides[0] // a.itemsize @cython.profile(False) cpdef inline tuple _to_cublas_vector(ndarray a, Py_ssize_t rundim): if a._strides[rundim] < 0: - return ascontiguousarray(a), 1 + return a.copy(), 1 else: return a, a._strides[rundim] // a.itemsize