C++ 极小点的CUDA函子
我惊呆了。我读了很多答案,但还是没能找到答案。我试图在C++ 极小点的CUDA函子,c++,c,cuda,thrust,C++,C,Cuda,Thrust,我惊呆了。我读了很多答案,但还是没能找到答案。我试图在结构点上实现comparator,这将有助于找到数组中的最小点,这是迄今为止的代码: struct minPointOperator { __device__ __host__ point operator()(const point& x, const point& y) const { return x.value > y.value ? y : x; } }; int
结构点上实现comparator,这将有助于找到数组中的最小点,这是迄今为止的代码:
struct minPointOperator
{
__device__ __host__ point operator()(const point& x, const point& y) const
{
return x.value > y.value ? y : x;
}
};
int findBestPointIndx(point* dev_points, int pointCount)
{
thrust::device_ptr<point> points(dev_points);
int id = thrust::reduce(points, points+pointCount, 0, minPointOperator());
return id;
}
struct minpoint运算符
{
__设备\uuuuuuuuuuu主机\uuuuuuuuu点运算符()(常数点&x,常数点&y)常数
{
返回x.value>y.value?y:x;
}
};
int findBestPointIndx(点*dev_点,int pointCount)
{
推力:设备测试点(开发点);
int id=推力::减少(点,点+点计数,0,minPointOperator());
返回id;
}
但是它没有编译,只是抛出了大量的函数“minPointOperator::operator()”不能用给定的参数列表调用
注意以下几点:
模板参数
InputIterator是输入迭代器的模型,InputIterator的值类型可转换为T
T是可赋值的模型,可转换为BinaryFunction的第一个参数类型和第二个参数类型
这意味着您对init
参数的选择以及reduce
的返回类型不是任意的。它们必须与输入迭代器的类型(有效)相同(即本例中的点)
推力::减少
(在本例中)不返回最小元素的索引。它返回最小元素的实际内容,即如果元素为点
,则返回点
,并且初始值必须为点
类型
要找到最小点的索引(似乎是您的目标),有各种可能性。一种可能是(使用zip\u迭代器
)将数据与索引数组(例如counting\u迭代器
)压缩在一起,在此基础上进行最小缩减,然后从推力::缩减
返回的值中提取索引
然而,我认为一个更好的方法是建议的方法,即使用它将一个interator返回到最小元素,然后可以使用它轻松地计算索引
下面是一个成功的例子:
$ cat t683.cu
#include <thrust/device_ptr.h>
#include <thrust/extrema.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>
struct point{
int value;
int data;
};
struct minPointOperator
{
__device__ __host__ bool operator()(const point& x, const point& y) const
{
return (x.value < y.value);
}
};
int findBestPointIndx(point* dev_points, int pointCount)
{
thrust::device_ptr<point> points(dev_points);
int id = thrust::min_element(points, points+pointCount, minPointOperator()) - points;
return id;
}
int main(){
point data[4] = { {4,5}, {0,7}, {2,3}, {6,1} };
thrust::host_vector<point> h_data(data, data + 4);
thrust::device_vector<point> d_data = h_data;
int min_point = findBestPointIndx(thrust::raw_pointer_cast(d_data.data()), 4);
std::cout << "min index: " << min_point << std::endl;
return 0;
}
$ nvcc t683.cu -o t683
$ ./t683
min index: 1
$
$cat t683.cu
#包括
#包括
#包括
#包括
#包括
结构点{
int值;
int数据;
};
结构minpoint运算符
{
__设备\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
{
返回值(x值 std::cout在一个完整的示例中,如果没有看到点的定义,可能有人会尝试编译它,不可能说什么是错误的,但我敢打赌0不是一个有效的点
值,也没有方法构造或转换为一个。