Reputation: 4401
I have the following data structures on my host:
typedef struct point{
int x;
int y;
}Point;
typedef struct pair{
Point i;
Point j;
float cost;
}Pair;
Pair* pairs[n]; // allocates an array of pointers to pair
Now, I've to copy "pairs" to the GPU. So, I declare the following pointer:
Pair **d_pair;
and allocate the memory using the following:
cudaMalloc((void**)d_pair,(sizeof(Pair)+sizeof(Pair*))*n);
Now, I copy from host to device:
cudaMempy(d_pair,pair,(sizeof(Pair)+sizeof(Pair*))*n),cudaMemcpyHostToDevice);
The kernel prototype receives d_pair as:
__global__ my_kernel(Pair* d_pair[], ... ){
...
}
Should the above sequence of statements work as intended? If not, what modifications I make? Basically, I want to copy Pair* pairs[n]; as such to "d_pair". How do I do this?
Upvotes: 0
Views: 1467
Reputation: 1628
It won't work: you are sending an array of pointer, but not the objects themselves. You need to have an array (or a Vector) of Pair:
Pair pairs[n];
And then :
Pair *d_pair;
cudaMalloc((void**)&d_pair,sizeof(Pair)*n);
cudaMempy(d_pair,pairs,sizeof(Pair)*n,cudaMemcpyHostToDevice);
By the way, this:
cudaMempy(d_pair,pair,(sizeof(Pair)+sizeof(Pair*))*n),cudaMemcpyHostToDevice);
is non sense, you allocate space for a pointer AND a Pair. Your copy use the same (sizeof(Pair)+sizeof(Pair*))*n) expression but the array pairs is (n*sizeof(Pair*)), so you are copying undefined memory.
Upvotes: 2