2D arrays with cuda confusion

I have a problem that i need to implement it with 2D arrays, but the thing is i’m confused on how to allocate the the arrays, i read a lot about it and i don’t know if its best to

use cudaMalloc, or cudaMallocPitch or even map the 2D array to 1D array and the send it to the GPU, and also i have the same confusion in copying the array so if anyone

knows about which is the best way to do that i would be greatful for your help.

thanks in advance

I suppose I should stop hogging my 2D memory class since so many people seem to need 2D arrays… but I won’t :D

Anywho, the easiest is to flatten the array to 1 dimension, and then to get the 1D index from a 2D type would be some_array[y*width + x] where y and x are the indexes that would be used on the 2D array.

The other way to do it would be the way below, and I typed this in the reply window so don’t expect anything to compile perfectly right away.

int** host;

int** device;

host = new int*[height];

cudaMalloc(device, sizeof(int*)*height);

for(int i = 0; i < width; i++) {

	cudaMalloc(&host[i], sizeof(int)*width);


cudaMemcpy(device, host, cudaMemcpyHostToDevice);

thnx and I really appretiate your help but I found the second way is very disturbing so I already used the first way of flattening the matrix into 1D array and again thnx for the help