From a679708be7379017c084e2d58d1cdd84034d4d32 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 5 Jan 2023 12:24:29 -0600 Subject: [PATCH] Preserve strides during copy (closes gh-403) --- pycuda/gpuarray.py | 2 +- test/test_gpuarray.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pycuda/gpuarray.py b/pycuda/gpuarray.py index 6df39063..8228860e 100644 --- a/pycuda/gpuarray.py +++ b/pycuda/gpuarray.py @@ -389,7 +389,7 @@ class GPUArray: return self.get(ary=ary, async_=True, stream=stream) def copy(self): - new = GPUArray(self.shape, self.dtype, self.allocator) + new = GPUArray(self.shape, self.dtype, self.allocator, strides=self.strides) _memcpy_discontig(new, self) return new diff --git a/test/test_gpuarray.py b/test/test_gpuarray.py index fd075dcc..ea641d09 100644 --- a/test/test_gpuarray.py +++ b/test/test_gpuarray.py @@ -1546,6 +1546,16 @@ class TestGPUArray: d2 = d[:, 7:9, :] # non C-contiguous d2.transpose(axes=(1, 0, 2)) # crashes for recent versions + def test_copy_strides(self): + # https://github.com/inducer/pycuda/issues/403 + a = np.random.randn(22, 33).copy(order="f") + a_dev = gpuarray.to_gpu(a) + assert a_dev.strides == a.strides + a_dev_2 = a_dev.copy() + assert a_dev_2.strides == a.strides + a_back = a_dev_2.get() + assert np.array_equal(a_back, a) + if __name__ == "__main__": # make sure that import failures get reported, instead of skipping the tests. -- GitLab