2D array of int copy from host to device and vice versa How to do it?

Hi, I’ve to copy an array of int from the host to device and tried to use the functions cudaMallocPitch and cudaMemcpy2D but all I obtained is a segmentation fault when I try to modify these data on the device…

This is the code that I wrote:

on the host:

int** vect=new int*[20];

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

		vect[i]=new int[30];

		for (int j=0; j<30; ++j)

			vect[i][j]=i*j;

	}

		

	int** d_vect;

	size_t pitch=sizeof(int);

	cudaMallocPitch((void**) &d_vect, &pitch, 30*sizeof(int), 20);

	cudaMemcpy2D(d_vect, pitch, vect, pitch, 30*sizeof(int), 20, cudaMemcpyHostToDevice);

	dim3 grid(30,20);

	nothing<<< grid, 1 >>>(d_vect);

	cudaMemcpy2D(vect, pitch, d_vect, pitch, 30*sizeof(int), 20, cudaMemcpyDeviceToHost);
__global__ void nothing(int** vect) {

	vect[blockIdx.x][blockIdx.y]=-vect[blockIdx.x][blockIdx.y];

}

I’m sure there’re some error in this code, can you explain me where these are?

I’m not sure I understood well how to do… Many thanks and excuse me for the disturb but it’s my first code with cuda…

The error is in the level of indirection:

cudaMallocPitch() does allocate a “flat” array of pitchheigth bytes (use int instead of int**).