Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cudaErrorInvalidMemcpyDirection when supplying value from MemcpyKind to cudaMemcpy2D #40

Closed
JonathanCroenen opened this issue Dec 9, 2022 · 1 comment

Comments

@JonathanCroenen
Copy link

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 added a commit that referenced this issue Dec 9, 2022
vzhurba01 added a commit that referenced this issue Dec 9, 2022
@vzhurba01
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants