cudamallocpitch and cudamemcpy2d

Hi all

I’m trying to copy a matrix on the GPU and to copy it back on the CPU: my target is learn how to use cudaMallocPitch and cudaMemcpy2D.

This is a part of my code:

[codebox]int **matrixH, *matrixD, **copy;

size_t pitch;

matrixH=(int**)malloc(M*sizeof(int));

copy=(int**)malloc(M*sizeof(int));

for(int i=0; i<M;i++)

{

matrixH[i]=(int*)malloc(N*sizeof(int));

copy[i]=(int*)malloc(N*sizeof(int));

}

//cudaMallocPitch

cudaMallocPitch((void**)&matrixD,&pitch,N*sizeof(int),M);

//copy CPU->GPU

cudaMemcpy2D(matrixD,pitch, matrixH,Nsizeof(int) ,Nsizeof(int),M, cudaMemcpyHostToDevice);

//copy GPU->CPU

cudaMemcpy2D(copy,Nsizeof(int),matrixD,pitch,Nsizeof(int),

M,cudaMemcpyDeviceToHost);[/codebox]

When I call cudaMallocPitch it modifies matrixH’s contents. The values of matrixH and copy should be the same but differs!

can anyone help me?

thanks

Hi all

I’m trying to copy a matrix on the GPU and to copy it back on the CPU: my target is learn how to use cudaMallocPitch and cudaMemcpy2D.

This is a part of my code:

[codebox]int **matrixH, *matrixD, **copy;

size_t pitch;

matrixH=(int**)malloc(M*sizeof(int));

copy=(int**)malloc(M*sizeof(int));

for(int i=0; i<M;i++)

{

matrixH[i]=(int*)malloc(N*sizeof(int));

copy[i]=(int*)malloc(N*sizeof(int));

}

//cudaMallocPitch

cudaMallocPitch((void**)&matrixD,&pitch,N*sizeof(int),M);

//copy CPU->GPU

cudaMemcpy2D(matrixD,pitch, matrixH,Nsizeof(int) ,Nsizeof(int),M, cudaMemcpyHostToDevice);

//copy GPU->CPU

cudaMemcpy2D(copy,Nsizeof(int),matrixD,pitch,Nsizeof(int),

M,cudaMemcpyDeviceToHost);[/codebox]

When I call cudaMallocPitch it modifies matrixH’s contents. The values of matrixH and copy should be the same but differs!

can anyone help me?

thanks