[英]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.