pycuda icon indicating copy to clipboard operation
pycuda copied to clipboard

Transfer of arrays with different strides

Open mabau opened this issue 8 years ago • 1 comments

Hi,

First, thanks for writing this library! It's a very convenient way to do GPU programming.

I found the following unexpected behaviour when debugging one of my programs that uses numpy arrays in different orders:

import pycuda.autoinit
from pycuda.gpuarray import to_gpu
import numpy as np


# Creating a GPU array in fortran layou
cpu_arr_f = np.zeros([3,4], order='F')
gpu_arr = to_gpu(cpu_arr_f)

# Copying an array in C layout to and from GPU changes it
# pycuda apparently assumes that CPU array has same strides as GPU array without checking it
cpu_arr_c = np.zeros([3,4], order='C')
cpu_arr_c[0,:] = 1

gpu_arr.set(cpu_arr_c)
assert(np.allclose(gpu_arr.get(), cpu_arr_c))

The last assert fails since pycuda apparently assumes that the strides of the source and destination arrays match. Is this the intended behaviour? If yes, I think a warning or a documentation note might be helpful in this case.

mabau avatar May 30 '17 10:05 mabau

Good catch! Thanks for reporting that. I'd be happy to take a PR that adds that check.

inducer avatar May 30 '17 12:05 inducer