2016-05-08 68 views
0

我已經使用不使用推力設備向量的普通CUDA代碼編寫了內核。內核輸出存儲在設備數組中的一些結果,比如說數組X.我想現在對X進行縮減。是否有一種方法可以在不先將X複製到推力的情況下使用thrust :: reduction函數: device_vector變量?Cuda推力不使用device_vectors?

回答

3

傳統的做法是將設備指針包裝到thrust::device_ptr中,並將其傳遞給推力算法。 Thrust中的基於標籤的模板模型將確保由於調用中提供的輸入序列的類型而導致設備執行結果。

#include <thrust/device_ptr.h> 
#include <thrust/reduce.h> 

int* X; 
cudaMalloc((void **)&X, sizeof(int) * size_t(N)); 

// Do stuff with X 

thrust::device_ptr X_ptr(X); 
int result = thrust::reduce(X_ptr, X_ptr+N); 

從推力1.7起,引入了執行策略的概念。這消除了使用device_ptr明確包裝設備地址的需要。因此,你可以用它來thrust::device政策,表明輸入迭代器是在設備上,你選擇哪種方式來做到這一點有點像

#include <thrust/reduce.h> 
#include <thrust/execution_policy.h> 

int* X; 
cudaMalloc((void **)&X, sizeof(int) * size_t(N)); 

// Do stuff with X 
int result = thrust::reduce(thrust::device, X, X+N); 

應該由你有什麼版本的推力來引導和代碼風格你比較喜歡。