[英]Using the Thrust library for CUDA for an array reduction
我使用推力来查找数组的总和,c,但我不断收到编译器错误“错误:表达式必须具有类类型”
float tot = thrust::reduce(c.begin(), c.end());
这是不起作用的代码行,c是一个float数组,是另外两个数组的元素和。
干杯
可以将指针传递给thrust::reduce
。 如果你有一个指向主机内存中数组的指针,你可以这样做:
float tot = thrust::reduce(c, c + N); // N is the length of c in words
如果指针指向设备内存中的数组,则需要先将其转换为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
c应该是thrust
类型,例如thrust::host_vector
或thrust::device_vector
。
在Thrust github页面上有一个关于thrust :: reduce的例子。 你不能在一个普通的旧数组上调用.begin(),因为它不是一个对象的实例,即它没有意义。 例如,它就像在下面的代码中调用数组“b”上的.begin()。
int main(void)
{
thrust::host_vector<float> a(10);
float b[10];
thrust::fill(a.begin(), a.end(), 1.0);
thrust::fill(b, b+10, 2.0);
cout << "a: " << thrust::reduce(a.begin(), a.end()) << endl;
cout << "b: " << thrust::reduce(b, b+10) << endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.