[英]How do I allocate device memory to my array of pointers, in CUDA?
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. 现在,我要将“对”复制到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: 内核原型接收d_pair为:
__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];
基本上,我想复制Pair *对[n]; as such to "d_pair".
就像“d_pair”一样。 How do I do this?
我该怎么做呢?
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的数组(或Vector):
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.
您的副本使用相同的(sizeof(Pair)+ sizeof(Pair *))* n)表达式,但数组对是(n * sizeof(Pair *)),因此您正在复制未定义的内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.