N_Kh
N_Kh

Reputation: 291

Access violation reading location when calling cudaMemcpy2DToArray

I allocated a 2D array in device and want to copy a 2D float array to device. ImgSrc is a Mat type in openCV that I copied the elements of it into a 2D float array named ImgSrc_f.then by using cudaMemcpy2DToArray() I copied my host 2D array(ImgSrc_f) to device 2D array Src. size of 2D arrays are 512X512.

cudaChannelFormatDesc floattex = cudaCreateChannelDesc<float>();
cudaArray *Src;
cudaMallocArray(&Src, &floattex, 512, 512);

float *ImgSrc_f[512];
for (int i=0; i<512; i++)
         ImgSrc_f[i] = (float *)malloc(512 * sizeof(float));
for(int i=0;i<512;i++)
    for(int j=0;j<512;j++)
    {
        ImgSrc_f[i][j]=ImgSrc.at<float>(i,j);
    }
//copy from host memory to device
cudaMemcpy2DToArray(Src, 0, 0,ImgSrc_f,512 * sizeof(float),512 *sizeof(float), 512,cudaMemcpyHostToDevice);

but I got this exception:

Access violation reading location 0x0000000000281000

Upvotes: 1

Views: 644

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

ImgSrc_f does not point to a contiguous 512x512 chunk of memory. Try changing

float *ImgSrc_f[512];
for (int i=0; i<512; i++)
         ImgSrc_f[i] = (float *)malloc(512 * sizeof(float));
for(int i=0;i<512;i++)
    for(int j=0;j<512;j++)
    {
        ImgSrc_f[i][j]=ImgSrc.at<float>(i,j);
    }

to something like

float *ImgSrc_f;
ImgSrc_f = (float *)malloc(512 * 512 * sizeof(float));
for(int i=0;i<512;i++)
    for(int j=0;j<512;j++)
    {
        ImgSrc_f[i * 512 + j]=ImgSrc.at<float>(i,j);
    }

cudaMemcpy2DToArray expects the source pointer to point to a single contiguous block of memory.

Upvotes: 5

Related Questions