inducer / pycuda

CUDA integration for Python, plus shiny features
http://mathema.tician.de/software/pycuda
Other
1.85k stars 288 forks source link

GPUArray.copy() does not copy the strides parameters #403

Closed vincefn closed 1 year ago

vincefn commented 1 year ago

GPUArray.copy() does not copy the strides parameters

For example:

import numpy as np
from scipy.misc import ascent
import matplotlib.pyplot as plt
import pycuda.autoinit
import pycuda.gpuarray as cua

d0 = np.asarray(ascent(), order='F')
d1 = cua.to_gpu(d0)
d2 = d1.copy()
print(d0.strides, d1.strides, d2.strides, cua.empty_like(d1).strides)

plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.imshow(abs(d1.get()))
plt.colorbar()
plt.subplot(122)
plt.imshow(abs(d2.get()))
plt.colorbar()

yields:

(8, 4096) (8, 4096) (4096, 8) (8, 4096)

and the copied array appears indeed flipped due to the missing strides information: image

I guess a simple addition of strides=self.strides in https://github.com/inducer/pycuda/blob/main/pycuda/gpuarray.py#L392 would fix this ?

By comparison, CPUArray.empty_like() copies the strides parameters, and pyOpenCL's array.copy() too.

inducer commented 1 year ago

Thanks for the report! https://gitlab.tiker.net/inducer/pycuda/-/merge_requests/89

vincefn commented 1 year ago

Thanks ! This should also close #154