Problems with CudaMemCpy2D

Hi, I wanted to copy a 2D array from the CPU to the GPU and than back to the CPU.

If the program would do it right, it should display 1 but it displays 2010.

Does anyone see what I did wrong?

Thanking you in anticipation

#include <stdio.h>

#include <cuda.h>

#define m 100

#define n 100

int main(){	

	int a[m][n];

	int *b;

	int i,j;

	size_t pitch;

	

	for(i=0;i<m;i++){

		for(j=0;j<n;j++){

			a[i][j] = 1;

		}

	}

	

	cudaMallocPitch((void**)&b, &pitch, n*sizeof(int), m);

	cudaMemcpy2D(&b, pitch, &a, n*sizeof(int), n*sizeof(int), m, cudaMemcpyHostToDevice);

	a[0][0] = 2010;

	cudaMemcpy2D(&a, n*sizeof(int), &b, pitch, n*sizeof(int), m, cudaMemcpyDeviceToHost);

	printf("%d\n", a[0][0]);	

}

Hi, I wanted to copy a 2D array from the CPU to the GPU and than back to the CPU.

If the program would do it right, it should display 1 but it displays 2010.

Does anyone see what I did wrong?

Thanking you in anticipation

#include <stdio.h>

#include <cuda.h>

#define m 100

#define n 100

int main(){	

	int a[m][n];

	int *b;

	int i,j;

	size_t pitch;

	

	for(i=0;i<m;i++){

		for(j=0;j<n;j++){

			a[i][j] = 1;

		}

	}

	

	cudaMallocPitch((void**)&b, &pitch, n*sizeof(int), m);

	cudaMemcpy2D(&b, pitch, &a, n*sizeof(int), n*sizeof(int), m, cudaMemcpyHostToDevice);

	a[0][0] = 2010;

	cudaMemcpy2D(&a, n*sizeof(int), &b, pitch, n*sizeof(int), m, cudaMemcpyDeviceToHost);

	printf("%d\n", a[0][0]);	

}

This program work as intended here, i.e. it prints “1”. It’s good practice to check the error codes from all API functions. I suspect that CUDA failed to initialize on your system. This can happen for a number of reasons (e.g. a driver mismatch). Below is a version of the code that includes return code checking.

[codebox]#include <stdio.h>

// Macro to catch CUDA errors in CUDA runtime calls

#define CUDA_SAFE_CALL(call) \

do { \

cudaError_t err = call;                                           \

if (cudaSuccess != err) {                                         \

    fprintf (stderr, "Cuda error in file '%s' in line %i : %s.\n",\

             __FILE__, __LINE__, cudaGetErrorString(err) );       \

    exit(EXIT_FAILURE);                                           \

}                                                                 \

} while (0)

#define m 100

#define n 100

int main(){

int a[m][n];

int *b;

int i,j;

size_t pitch;

for(i=0;i<m;i++){

    for(j=0;j<n;j++){

        a[i][j] = 1;

    }

}

CUDA_SAFE_CALL (cudaMallocPitch((void**)&b, &pitch, n*sizeof(int), m));

CUDA_SAFE_CALL (cudaMemcpy2D(&b, pitch, &a, n*sizeof(int), n*sizeof(int),

                             m, cudaMemcpyHostToDevice));

a[0][0] = 2010;

CUDA_SAFE_CALL (cudaMemcpy2D(&a, n*sizeof(int), &b, pitch, n*sizeof(int),

                             m, cudaMemcpyDeviceToHost));

printf("%d\n", a[0][0]);    

}[/codebox]

This program work as intended here, i.e. it prints “1”. It’s good practice to check the error codes from all API functions. I suspect that CUDA failed to initialize on your system. This can happen for a number of reasons (e.g. a driver mismatch). Below is a version of the code that includes return code checking.

[codebox]#include <stdio.h>

// Macro to catch CUDA errors in CUDA runtime calls

#define CUDA_SAFE_CALL(call) \

do { \

cudaError_t err = call;                                           \

if (cudaSuccess != err) {                                         \

    fprintf (stderr, "Cuda error in file '%s' in line %i : %s.\n",\

             __FILE__, __LINE__, cudaGetErrorString(err) );       \

    exit(EXIT_FAILURE);                                           \

}                                                                 \

} while (0)

#define m 100

#define n 100

int main(){

int a[m][n];

int *b;

int i,j;

size_t pitch;

for(i=0;i<m;i++){

    for(j=0;j<n;j++){

        a[i][j] = 1;

    }

}

CUDA_SAFE_CALL (cudaMallocPitch((void**)&b, &pitch, n*sizeof(int), m));

CUDA_SAFE_CALL (cudaMemcpy2D(&b, pitch, &a, n*sizeof(int), n*sizeof(int),

                             m, cudaMemcpyHostToDevice));

a[0][0] = 2010;

CUDA_SAFE_CALL (cudaMemcpy2D(&a, n*sizeof(int), &b, pitch, n*sizeof(int),

                             m, cudaMemcpyDeviceToHost));

printf("%d\n", a[0][0]);    

}[/codebox]

Thank you for your answer.
There is an error:
Cuda error in ‘testsafe.cu’ in line 27 : unspecified driver error.
Line 27 is: CUDA_SAFE_CALL (cudaMemcpy2D(&b, pitch, &a, nsizeof(int), nsizeof(int), m, cudaMemcpyHostToDevice));
I know now that the configuration is wrong an not the code.

Thank you for your answer.
There is an error:
Cuda error in ‘testsafe.cu’ in line 27 : unspecified driver error.
Line 27 is: CUDA_SAFE_CALL (cudaMemcpy2D(&b, pitch, &a, nsizeof(int), nsizeof(int), m, cudaMemcpyHostToDevice));
I know now that the configuration is wrong an not the code.