Hi Everyone,
I’m just trying to get a feel for transferring/copying arrays to and from the GPU, and so I am starting with small, square matrices (eg. 2x2, 100x100). However, when I am using the real data I have, it will be jagged arrays with varying lengths of rows.
I implemented a global function myFun() such that it takes a 1D array and doubles the values inside of it. Like so:
[codebox]///// Kernel.cu /////
global void call(int *src)
{
for (int i = 0; i < 3; i++)
{
src[i] *= 2;
}
}
extern “C” void thisFun()
{
int numElements = 3;
int memSize = numElements * sizeof(int);
int *h_a, *d_a;
h_a = (int *)malloc(memSize);
h_a[0] = 1;
h_a[1] = 2;
h_a[2] = 3;
cudaMalloc((void **)&d_a,memSize);
cudaMemcpy(d_a,h_a,memSize,cudaMemcpyHostToDevice);
call<<<1,1>>>(d_a);
cudaThreadSynchronize();
cudaMemcpy(h_a,d_a,memSize,cudaMemcpyDeviceToHost);
}[/codebox]
Now realize, this code works just fine. However, when I go from this to accept 2D arrays, the code runs, but the values are not updated (i.e. it tells me the initial values of the host array rather than the doubled values).
Here’s what I have:
[codebox]///// Kernel.cu /////
global void call(int **src)
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
src[i][j] *= 2;
}
extern “C” void thisFun()
{
int numRows = 2;
int numCols = 3;
int numElements = 6;
int memSize = numElements * sizeof(int);
int **h_a, **d_a;
h_a = (int **)malloc(numRows * sizeof(int *));
h_a[0] = (int *)malloc(numCols * sizeof(int));
h_a[1] = (int *)malloc(numCols * sizeof(int));
h_a[0][0] = 1;
h_a[0][1] = 2;
h_a[0][2] = 3;
h_a[1][0] = 4;
h_a[1][1] = 5;
h_a[1][2] = 6;
cudaMalloc((void **)&d_a,memSize);
cudaMemcpy(d_a,h_a,memSize,cudaMemcpyHostToDevice);
call<<<1,1>>>(d_a);
cudaThreadSynchronize();
cudaMemcpy(h_a,d_a,memSize,cudaMemcpyDeviceToHost);
}[/codebox]
So, this compiles and runs, but it doesn’t give me the updated values. This is “basically” the same thing as the 1D version, but I don’t know why it doesn’t update the values. I do the cudaMemcpy back as I think it should be done.
Thanks,
Daniel