NVIDIA / cuda-python

CUDA Python Low-level Bindings
https://nvidia.github.io/cuda-python/
Other
809 stars 63 forks source link

cudaErrorInvalidMemcpyDirection when supplying value from MemcpyKind to cudaMemcpy2D #40

Closed JonathanCroenen closed 1 year ago

JonathanCroenen commented 1 year ago

I am currently stuck on trying to get cudaMemcpy2D working. It reports the error cudaErrorInvalidMemcpyDirection when supplying the function with any value from MemcpyKind. I have reinstalled and double checked my CUDA and toolkit installs multiple times and have tested on my own and a colleagues machine, so I'm positive they are fine.

A minimal example that shows the problem I am facing is as follows:

from cuda import cuda, cudart
from cuda_utils import cuda_check_errors
import numpy as np

checkCudaErrors(cuda.cuInit(0))
device_id = checkCudaErrors(cuda.cuDeviceGet(0))
context = checkCudaErrors(cuda.cuCtxCreate(0, device_id ))

def allocate_np_array(array: np.ndarray):
    rows, cols = array.shape
    device_ptr, pitch = checkCudaErrors(cudart.cudaMallocPitch(cols * array.itemsize, rows)) 

    checkCudaErrors(
        cudart.cudaMemcpy2D(
            device_ptr,
            pitch,
            array.ctypes.data,
            array.shape[1] * array.itemsize,
            array.shape[1] * array.itemsize,
            array.shape[0],
            cudart.cudaMemcpyKind.cudaMemcpyHostToDevice
        ))

if __name__ == "__main__":
    arr = np.ones((40, 40), dtype=np.float32)
    allocate_np_array(arr)

The checkCudaErrors function is the same one used in this repos examples. The output looks as follows (with filepath removed):

Traceback (most recent call last):
  File "test.py", line 27, in <module>
    allocate_np_array(arr)
  File "test.py", line 13, in allocate_np_array
    cuda_check_errors(
  File "/.../cuda_utils.py", line 22, in checkCudaErrors
    raise RuntimeError("CUDA Error code: {} ({})".format(result[0].value, cuda_get_error_enum(result[0])))
RuntimeError: CUDA Error code: 21 (cudaErrorInvalidMemcpyDirection)

It might be important to note that I am new to CUDA and this is my first real issue post, so don't be afraid to point out my errors in either. Thanks in advance!

vzhurba01 commented 1 year ago

Thanks for the minimal repro. I've updated both main and 11.8.x branches with a fix. PYPI & Conda to be updated at a future release.

You can get this fix by Installing from Source.