简体   繁体   English

CUDA:我如何将主机存储的向量传递给Device端int数组?

[英]CUDA: How would I pass a host stored vector to a Device side int array?

I'm currently working with CUDA to process lists of co-ordinates. 我目前正在与CUDA合作处理坐标列表。 I'm thus reading coordinates from a text file to the host and passing those coordinates into an array on the GPU. 因此,我正在从文本文件中读取坐标到主机,并将这些坐标传递到GPU上的数组中。

Unfortunately I do not know in advanced how many coordinates are contained in each text file. 不幸的是,我不知道每个文本文件中包含多少坐标。 I thus use a vector to store the coordinates host side - as non-vector array sizes can not be allocated dynamically. 因此,我使用向量来存储坐标主机端 - 因为无法动态分配非向量数组大小。

As declaring device variables is done on the fly however, I was wondering if there was a way I could pass the values of my host vector into a device side float array? 因为声明设备变量是动态完成的,我想知道是否有一种方法可以将主机向量的值传递到设备端浮点数组中?

Passing data from vector can be done the same way as an array. 从向量传递数据可以与数组相同的方式完成。 Values in a std::vector are stored in contiguous memory. std :: vector中的值存储在连续的内存中。

Is it safe to assume that STL vector storage is always contiguous? 假设STL向量存储始终是连续的是否安全?

As a side note, most scientific storage methods I've come into contact will define up front how many coordinates they are. 作为旁注,我接触过的大多数科学存储方法都会预先确定它们的坐标数量。 If you create this file, I would highly suggest doing so as it will be faster than using std::vector which has to dynamically re-allocate and copy memory when the capacity grows. 如果你创建这个文件,我强烈建议这样做,因为它比使用std :: vector更快,它必须在容量增长时动态地重新分配和复制内存。

Exactly as Christian Sarofeen mentions, you can access raw vector data directly, more info here . 正如Christian Sarofeen提到的那样,您可以直接访问原始vector数据,更多信息请点击此处 The nicest way, in case you are on C++11, is to use data() function for this purpose. 如果您使用的是C ++ 11,最好的方法是使用data()函数来实现此目的。

On the other hand, I am a bit surprised by this statement of yours: 另一方面,我对你的这句话感到有些惊讶:

as non-vector array sizes can not be allocated dynamically 因为非向量数组大小不能动态分配

I have to disagree, this should work for you just fine: 我不同意,这应该对你有用:

int x;
cin >> x;
int* dynamicArray = new int[x];
// Do computations on dynamicArray,
// possibly copy to GPU memory.
// Then deallocate.
delete [] dynamicArray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM