2013-01-13 44 views

回答

4

似乎很好。

#include <thrust/device_vector.h> 
#include <thrust/scan.h> 
#include <iterator> 

template<class T> 
struct FillMissing 
{ 
    __host__ __device__ T operator()(const T& res, const T& dat) 
    { 
     return dat == T(0) ? res : dat; 
    } 
}; 

int main() 
{ 
    thrust::device_vector<double> vec(7); 
    vec[1] = 2; 
    vec[2] = 1; 
    vec[5] = 3; 

    thrust::inclusive_scan(
      vec.begin(), vec.end(), 
      vec.begin(), 
      FillMissing<double>()); 

    thrust::copy(
      vec.begin(), vec.end(), 
      std::ostream_iterator<double>(std::cout, " ")); 
    std::cout << std::endl; 
} 

输出:

0 2 1 1 1 3 3 
+0

推力:: inclusive_scan'是该运营商关联? –

+0

@ m.s。是。 op(op(a,b),c)== op(a,op(b,c))== if(c!= 0){return c;} else if(b!= 0){return b ; } else {return a; }' – kangshiyin