How do I allocate and transfer(to and from Host) 2D arrays in device memory in Cuda?
+1
A:
I found a solution to this problem. I didn't have to flatten the array.
The inbuild cudaMallocPitch() function did the job. And I could transfer the array to and from device using cudaMemcpy2D() function.
for eg,
cudaMallocPitch((void**) &array, &pitch, a*sizeof(float), b);
This creates a 2D array of size a*b with the pitch as passed in as parameter.
The following code creates a 2D array and loops over the elements. It compiles readily, you may use it.
#include<stdio.h>
#include<cuda.h>
#define height 50
#define width 50
// Device code
__global__ void kernel(float* devPtr, int pitch)
{
for (int r = 0; r < height; ++r) {
float* row = (float*)((char*)devPtr + r * pitch);
for (int c = 0; c < width; ++c) {
float element = row[c];
}
}
}
//Host Code
int main()
{
float* devPtr;
size_t pitch;
cudaMallocPitch((void**)&devPtr, &pitch, width * sizeof(float), height);
kernel<<<100, 512>>>(devPtr, pitch);
return 0;
}
Gitmo
2009-06-26 10:51:49
A:
How did you declare the parameters in the function as int **d_A where d_A[][] is the 2-D device array that we need? When i do that it doesnt clearly know the dimensions and gives some wierd answers. I too used cudaMallocPitch and cudaMalloc2D but this was causing me trouble.
Please advise
avinash
2010-05-14 16:40:56
I don't think you can use 2D array of the form d_A[][]. You would have to use a double pointer. Declare an array of pointers. And make each element of the array point to an array of pointers again. HOpe that answers your query.
Gitmo
2010-05-15 12:32:43