将链表作为数组传递给CUDA

use*_*690 0 arrays cuda linked-list

我正在尝试将一些链接列表形式的数据传输到我的GPGPU.我是否需要进行与节点数量一样多的传输,或者有更好更快的方法吗?

pho*_*oad 5

使用Thrust库时,您可以从迭代器范围生成设备向量.在以下站点中,他们提供了此案例的示例

#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <list>
#include <vector>

int main(void)
{
    // create an STL list with 4 values
    std::list<int> stl_list;

    stl_list.push_back(10);
    stl_list.push_back(20);
    stl_list.push_back(30);
    stl_list.push_back(40);

    // initialize a device_vector with the list
    thrust::device_vector<int> D(stl_list.begin(), stl_list.end());

    // copy a device_vector into an STL vector
    std::vector<int> stl_vector(D.size());
    thrust::copy(D.begin(), D.end(), stl_vector.begin());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

https://github.com/thrust/thrust/wiki/Quick-Start-Guide

看一下标题为"Iterators and Static Dispatching"的部分.

您可以使用STL的算法库执行类似的操作.

std::list<int> stl_list;
stl_list.push_back(10);
...
float *myarray = new float[stl_list.size()];
float *mydevicearray;
CUDA_SAFE_CALL(cudaMalloc(&mydevicearray, sizeof(float)*stl_list.size()));
std::copy(stl_list.begin(), stl_list.end(), myarray);
CUDA_SAFE_CALL(cudaMemcpy(myarray, mydevicearray, sizeof(float)*stl_list.size(), cudaMemcpyHostToDevice));
Run Code Online (Sandbox Code Playgroud)

这两个示例应该只进行一次memcopy操作,因为将内存复制到CUDA设备是昂贵的,并且对列表中的每个元素执行它是不合逻辑的.