2013-01-16 16 views

回答

2

請參考推力Quict入門指南中的Section Transformations,瞭解如何使用初始化參數編寫仿函數。

struct saxpy_functor 
{ 
    const float a; 

    saxpy_functor(float _a) : a(_a) {} 

    __host__ __device__ 
     float operator()(const float& x, const float& y) const { 
      return a * x + y; 
     } 
}; 
2

這是一個完整的例子。正如@Eric所提到的,所有需要的是定義您自己的功能仿函數,並使用thrust::transform

#include <thrust/sequence.h> 
#include <thrust/device_vector.h> 

class power_functor { 

    double a; 

    public: 

     power_functor(double a_) { a = a_; } 

     __host__ __device__ double operator()(double x) const 
     { 
      return pow(x,a); 
     } 
}; 

void main() { 

    int N = 20; 

    thrust::device_vector<double> d_n(N); 
    thrust::sequence(d_n.begin(), d_n.end()); 

    thrust::transform(d_n.begin(),d_n.end(),d_n.begin(),power_functor(2.)); 

    for (int i=0; i<N; i++) { 
     double val = d_n[i]; 
     printf("Device vector element number %i equal to %f\n",i,val); 
    } 

    getchar(); 
} 
+0

它是不是在故意該方法'操作符()''返回int'而不是'double'? – SomethingSomething

+0

@SomethingSomething這是一個錯誤。固定。謝謝你通知我。 – JackOLantern

相關問題