2015-04-01 36 views
-1

我惊呆了。我收获了很多答案,但仍然无法实现。我试着去实现比较过struct point,这将有助于找到阵极小点,这是到目前为止的代码:最低点的CUDA仿函数

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; 
} 

但THI不编译,它只是喷出大量function "minPointOperator::operator()" cannot be called with the given argument list错误

+0

没有看到一个完整的例子有人point'的'的定义可以尝试编译它不可能说什么是错的,但我敢打赌,0不一个有效的“点”值,也没有办法构建或投射到一个。 – talonmies 2015-04-01 20:44:42

回答

4

的如果你研究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 
$ 
+0

Thx!这是我需要的!我在C/C++愚蠢,所以一直是我的问题... – eocron 2015-04-01 21:13:05