使用CUDA的Thrust库进行数组缩减

Igo*_*ren 2 c++ cuda visual-studio-2008 thrust

我使用推力来查找数组的总和,c,但我不断收到编译器错误"错误:表达式必须具有类类型"

float tot = thrust::reduce(c.begin(), c.end());
Run Code Online (Sandbox Code Playgroud)

这是不起作用的代码行,c是一个float数组,是另外两个数组的元素和.

干杯

tal*_*ies 5

可以将指针传递给thrust::reduce.如果你有一个指向主机内存中数组的指针,你可以这样做:

float tot = thrust::reduce(c, c + N); // N is the length of c in words
Run Code Online (Sandbox Code Playgroud)

如果指针指向设备内存中的数组,则需要将其转换为thrust::device_ptr第一个:

thrust::device_ptr<float> cptr = thrust::device_pointer_cast(c);
float tot = thrust::reduce(cptr, cptr + N); // N is the length of c in words
Run Code Online (Sandbox Code Playgroud)