[英]Can I allocate device memory using OpenCL and use pointers to the memory in CUDA?
[英]How do I allocate device memory to my array of pointers, in CUDA?
我的主機上有以下數據結構:
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
現在,我要將“對”復制到GPU。 所以,我聲明了以下指針:
Pair **d_pair;
並使用以下內容分配內存:
cudaMalloc((void**)d_pair,(sizeof(Pair)+sizeof(Pair*))*n);
現在,我從主機復制到設備:
cudaMempy(d_pair,pair,(sizeof(Pair)+sizeof(Pair*))*n),cudaMemcpyHostToDevice);
內核原型接收d_pair為:
__global__ my_kernel(Pair* d_pair[], ... ){
...
}
上述聲明序列是否應按預期工作? 如果沒有,我做了哪些修改? 基本上,我想復制Pair *對[n]; 就像“d_pair”一樣。 我該怎么做呢?
它不起作用:您發送的是指針數組,但不是對象本身。 你需要一個Pair的數組(或Vector):
Pair pairs[n];
接着 :
Pair *d_pair;
cudaMalloc((void**)&d_pair,sizeof(Pair)*n);
cudaMempy(d_pair,pairs,sizeof(Pair)*n,cudaMemcpyHostToDevice);
順便說一下,這個:
cudaMempy(d_pair,pair,(sizeof(Pair)+sizeof(Pair*))*n),cudaMemcpyHostToDevice);
沒有意義,你為指針和一對分配空間。 您的副本使用相同的(sizeof(Pair)+ sizeof(Pair *))* n)表達式,但數組對是(n * sizeof(Pair *)),因此您正在復制未定義的內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.