"invalid copy direction for memcpy" with cudaMemcpy2DFromArray()

Hello everyone!

I assume the topic title is quite self-explanatory, so I’ll just post the simple code which gives me that error (I’m not copying error checking parts, but I check the result of every function call, and the only error I get is the one from cudaMemcpy2DFromArray):


// CUDAPixel is a simple regular struct I defined; test_dst is the destination buffer in the device

CUDAPixel *test_dst;

// I'm going to use cudaMallocPitch to allocate device memory

size_t test_pitch;

// Size of the matrix

int test_width = 10;

int test_height = 5;

// test_src points to the data source on the host; I'm just allocating it, the content is not important for the test

CUDAPixel *test_src = new CUDAPixel[test_width*test_height];

// Allocate device memory

cudaMallocPitch(&test_dst, &test_pitch, test_width*sizeof(CUDAPixel), test_height);

// Copy test_src (regular array in host memory) to test_dst (2D array in device memory)

cudaMemcpy2DFromArray(test_dst, test_pitch, (const struct cudaArray*) test_src, 0, 0, test_width*sizeof(CUDAPixel), test_height, cudaMemcpyHostToDevice);

The last instruction gives me the “invalid copy direction for memcpy” error. Am I missing anything?

Thanks for your help!

cudaMemcpy2DFromArray() is for copying from a CUDA array, which is an opaque data structure (Nvidia has not disclosed it’s layout, so any access has to go through helper functions provided by Nvidia). As these always reside on the device, copying them from the host gives this error.

Just use a normal cudaMemcpy2D() instead.

And what about the pitch value for the host array? Should I set it the exact width in bytes of a row (i.e. assuming no padding or whatever)?



Thank you very much!