Hi I am getting an access violation error…..
What might be the problem in my code??
When I change W and H to 10 it works fine.
#define W 100
#define H 100
#define MAX 100000
int** GetImage()
{
int **img = new int*[W];
for(size_t i = 0 ; i < W ; i++ )
img[i] = new int[H];
for(int i=0;i<W;i++)
for(int j=0;j<H;j++)
img[i][j]=255;
return img;
}
int main()
{
int **image = GetImage();
float **dtr = initDistances(image);
//cuda memory allocation
int **devImage;
float **devDt;
int sizei = W*H*sizeof(int);
int sizef = W*H*sizeof(float);
cudaMalloc((void**)&devImage, sizei);
cudaMalloc((void**)&devDt, sizef);
//copy to GPU
cudaMemcpy(devImage, image, sizei, cudaMemcpyHostToDevice); <-- access violation here
cudaMemcpy(devDt, dtr, sizef, cudaMemcpyHostToDevice);
return 0;
}
Your array isn’t contiguous in memory, but you’re trying to copy it as if it were.
To allocate a contiguous array, you’d need to do a single allocation. But you’re allocating an array of pointers and then allocating an array of integers for each of those pointers, so there’s no guarantee that img[0] immediately precedes img[1] in memory. img[0] and img[1] were allocated separately. They could be in completely different places in memory.
Your cudaMemcpy assumes that img[0]-img[W] is one big contiguous block.