in the very, very simple code that i am testing for 2d arrays using CUDA, i am getting strange numbers…
i know 1d array structure is better but please let me know if any part is not correct…
many thanks in advance.
====================================
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
global void MatAdd(int **c)
{
int i = threadIdx.x;
int j = threadIdx.y;
c[i][j] = 1;
}
int main(int argc, char argv[])
{
int i, j;
int* crray2d = (int**)malloc(X * sizeof(int*));
int** c_d = (int**)malloc(X * sizeof(int*));
for(i=0; i<X; i++)
{
crray2d[i] = (int*)malloc(Y * sizeof(int));
cudaMemcpy(c_d, crray2d, Y * sizeof(int*), cudaMemcpyHostToDevice);
}
dim3 dimBlock(3,3);
MatAdd<<<1,dimBlock>>>(c_d);
for(i=0;i < X;i++){
cudaMemcpy(crray2d, c_d, Y * sizeof(int*), cudaMemcpyDeviceToHost) ;
}
for(i=0; i<X; i++)
for(j=0; j<Y; j++)
printf("%d ", crray2d[i][j]);
return 0;
}