std ::向量到CUDA中的数组 [英] std::vector to array in CUDA
问题描述
有一种方法可以将2D向量转换为数组,以便能够在CUDA内核中使用它?
Is there a way to convert a 2D vector into an array to be able to use it in CUDA kernels?
它被声明为:
vector<vector<int>> information;
我想要cudaMalloc并从主机复制到设备,最好的方法是什么?
I want to cudaMalloc and copy from host to device, what would be the best way to do it?
int *d_information;
cudaMalloc((void**)&d_information, sizeof(int)*size);
cudaMemcpy(d_information, information, sizeof(int)*size, cudaMemcpyHostToDevice);
推荐答案
总而言之, CUDA API不支持深度复制,也不知道有关 std :: vector
的任何信息。如果你坚持使用向量的向量作为宿主源,它将需要这样做:
In a word, no there isn't. The CUDA API doesn't support deep copying and also doesn't know anything about std::vector
either. If you insist on having a vector of vectors as a host source, it will require doing something like this:
int *d_information;
cudaMalloc((void**)&d_information, sizeof(int)*size);
int *dst = d_information;
for (std::vector<std::vector<int> >::iterator it = information.begin() ; it != information.end(); ++it) {
int *src = &((*it)[0]);
size _t sz = it->size();
cudaMemcpy(dst, src, sizeof(int)*sz, cudaMemcpyHostToDevice);
dst += sz;
}
[免责声明:在浏览器中编写,未经编译或测试。使用自负的风险]
[disclaimer: written in browser, not compiled or tested. Use at own risk]
这会将主机内存复制到GPU线性内存中的分配,每个向量需要一个副本。如果向量的向量是一个锯齿的数组,你将需要存储一个索引为GPU使用以及。
This would copy the host memory to an allocation in GPU linear memory, requiring one copy for each vector. If the vector of vectors is a "jagged" array, you will want to store an indexing somewhere for the GPU to use as well.
这篇关于std ::向量到CUDA中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!