[英]Using the Thrust library for CUDA for an array reduction
I am using thrust to find the sum of an array ,c, but I keep getting compiler error "error: expresion must have class type" 我使用推力来查找数组的总和,c,但我不断收到编译器错误“错误:表达式必须具有类类型”
float tot = thrust::reduce(c.begin(), c.end());
This is the code line that isn't working, c is a float array and is the element sum of 2 other arrays. 这是不起作用的代码行,c是一个float数组,是另外两个数组的元素和。
Cheers 干杯
It is possible to pass pointers to thrust::reduce
. 可以将指针传递给
thrust::reduce
。 If you have a pointer to an array in host memory, you can do something like this: 如果你有一个指向主机内存中数组的指针,你可以这样做:
float tot = thrust::reduce(c, c + N); // N is the length of c in words
If your pointer is to an array in device memory, you need to cast it as a thrust::device_ptr
first: 如果指针指向设备内存中的数组,则需要先将其转换为
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
。
There's an example for thrust::reduce on the Thrust github page . 在Thrust github页面上有一个关于thrust :: reduce的例子。 You can't call .begin() on a plain old array since it's not an instance of an object, ie it's meaningless.
你不能在一个普通的旧数组上调用.begin(),因为它不是一个对象的实例,即它没有意义。 As an example, it'd be like calling .begin() on the array "b" in the code below.
例如,它就像在下面的代码中调用数组“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.