重载已经定义的运算符

时间:2016-02-28 03:05:52

标签: c++ cuda thrust nvcc

我想重载operator *但是我继续得到函数" operator *(int,int)"已经定义了。我使用的是Thrust库,想在我的内核中使用自己的*。

 __device__ int operator*(int x, int y)
{
    int value = ...                                    
    return value;                                     
}

1 个答案:

答案 0 :(得分:2)

  

我想要做的是使用我自己的*运算符

来使用Thrust减少

thrust::reduce为您提供减少use your own binary operator的机会。应该通过C ++函子提供运算符。

这是一个工作示例,显示使用二元运算符查找一组数字中的最大值:

$ cat t1093.cu
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <iostream>

const int dsize = 10;

struct max_op
{
template <typename T>
  __host__ __device__
  T operator()(const T &lhs, const T &rhs) const
  {
    return (lhs>rhs)?lhs:rhs;
  }
};

int main(){

   thrust::device_vector<int> data(dsize, 1);
   data[2] = 10;
   int result = thrust::reduce(data.begin(), data.end(), 0, max_op());
   std::cout << "max value: " << result << std::endl;
   return 0;
}
$ nvcc -o t1093 t1093.cu
$ ./t1093
max value: 10
$
相关问题