我想重载运算符*,但我不断收到函数“operator *(int,int)”已经定义。我正在使用Thrust库,并希望在我的内核中使用我自己的*。重载已定义的运算符
__device__ int operator*(int x, int y)
{
int value = ...
return value;
}
我想重载运算符*,但我不断收到函数“operator *(int,int)”已经定义。我正在使用Thrust库,并希望在我的内核中使用我自己的*。重载已定义的运算符
__device__ int operator*(int x, int y)
{
int value = ...
return value;
}
我想要做的是减少使用推力的降低与我自己的运算符*
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
$
非常感谢。我可以为CUB做同样的事吗?我试图以同样的方式定义我自己的操作员扫描Cub,但没有奏效。 – Mems
对于某些算法实现,您可以在CUB中执行类似的操作。在[cub文档](https://nvlabs.github.io/cub/structcub_1_1_device_reduce.html#)中提供了用于将最少运算符(非常类似于上面的max运算符)传递给'cub :: DeviceReduce :: Reduce'的示例代码。 aa4adabeb841b852a7a5ecf4f99a2daeb)。它也可以为'cub :: DeviceScan :: InclusiveScan'完成,示例代码在这里提供(https://nvlabs.github.io/cub/structcub_1_1_device_scan.html#af27a73a9a8daef1b4fe5a16233932e30)。 –
我试过CUB自定义运算符的例子,它的工作原理,但问题是它只是为块大小的数组工作;。这少于1024个元素。我正在使用DeviceScan,但它始终挂起以用于较大的输入。 – Mems
什么是编译器? – Nik
@IlDivinCodino我正在使用NVCC – Mems
如何在纯C++中执行此操作?你能给个例子吗?运算符是否重载[需要某种类的类定义](https://isocpp.org/wiki/faq/intrinsic-types#intrinsics-and-operator-overloading)?要么操作符重载必须是类成员函数,要么操作符重载必须将类作为其参数之一? –