简体   繁体   English

如何在CUDA中为我的指针数组分配设备内存?

[英]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.

相关问题 是否可以使用OpenCL分配设备内存并使用指向CUDA中内存的指针? - Can I allocate device memory using OpenCL and use pointers to the memory in CUDA? 我怎么知道指向基类对象的指针数组中元素的类型,它将为派生类分配内存 - how do i know what is the type of an element in an array of pointers to an object of base class, that will allocate memory for derived classes 如何将内存分配给字符指针数组? - How to allocate memory to array of character pointers? 如何将内存静态分配给指向结构的指针数组 - How to Statically Allocate Memory to an Array of Pointers to a Struct 如何分配指针数组并为cuda中的多个内核调用保留它们 - How to allocate Array of Pointers and preserve them for multiple kernel calls in cuda 如何将动态 Memory 分配给 C++ CUDA 中的设备指针变量 - How to allocate Dynamic Memory to Device Pointer variable in C++ CUDA 迭代器是否分配诸如指针之类的内存? - Do the iterators allocate memory such as pointers? CUDA-每次都必须分配和释放内存吗? - CUDA - do I have to allocate and free memory each time? 我如何为二维数组分配 memory? - How do i allocate memory for a 2d array? 我可以在CUDA设备上为包含浮点数数组的对象分配内存吗? - Can I allocate memory on CUDA device for objects containing arrays of float numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM