的如果你研究reduce的推力文档可能会有所帮助。
注意以下几点:
模板参数
InputIterator为一个Input Iterator的模型和InputIterator的的VALUE_TYPE可转换为T.
T是可分配的模型,并转换为BinaryFunction的first_argument_type和second_argument_type。
这意味着你的init
参数的选择以及返回类型的reduce
不是任意的。它们必须与输入迭代器的类型(实际上)相同(即在这种情况下为point
)。
thrust::reduce
(在这种情况下)不是返回最小元素的索引。它返回最小元素的实际内容,即如果元素是point
,则返回point
,并且init值必须是point
。
要找到最小点的索引(看起来是您的目标),存在各种可能性。一种可能性是将数据与索引数组一起(使用zip_iterator
)一起压缩(例如counting_iterator
),基于该数据进行min-reduction,然后从thrust::reduce
返回的值中提取索引。
但是,我认为更好的办法是什么建议here,即使用thrust::min_element
这将一个迭代符返回最小元素,然后可以很容易地计算指数。
这里有一个样例:
$ 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
$
没有看到一个完整的例子有人point'的'的定义可以尝试编译它不可能说什么是错的,但我敢打赌,0不一个有效的“点”值,也没有办法构建或投射到一个。 – talonmies 2015-04-01 20:44:42