为thrust Min_element定义谓词时出错,为device_ptr使用zip_iterator



在这个简单的例子中,我试图找到最小值,它还没有被访问。

float *cost=NULL;
cudaMalloc( (void **) &cost, 5 * sizeof(float) );
bool *visited=NULL;
cudaMalloc( (void **) &visited, 5 * sizeof(bool) );
thrust::device_ptr< float > dp_cost( cost );
thrust::device_ptr< bool > dp_visited( visited );
typedef thrust::device_ptr<bool>  BoolIterator;
typedef thrust::device_ptr<float>  ValueIterator;
BoolIterator bools_begin = dp_visited, bools_end = dp_visited +5;
ValueIterator values_begin = dp_cost, values_end = dp_cost +5; 

typedef thrust::tuple<BoolIterator, ValueIterator> IteratorTuple;
typedef thrust::tuple<bool, float> DereferencedIteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> NodePropIterator;
struct nodeProp_comp : public thrust::binary_function<DereferencedIteratorTuple, DereferencedIteratorTuple, bool>
{
    __host__ __device__
    bool operator()( const DereferencedIteratorTuple lhs, const DereferencedIteratorTuple rhs ) const 
    {
        if( !( thrust::get<0>( lhs ) ) && !( thrust::get<0>( rhs ) ) )
        {
            return ( thrust::get<1>( lhs ) < thrust::get<1>( rhs ) );
        }
        else
        {
            return !( thrust::get<0>( lhs ) );
        }
    }
};

NodePropIterator iter_begin (thrust::make_tuple(bools_begin, values_begin));
NodePropIterator iter_end   (thrust::make_tuple(bools_end, values_end));
NodePropIterator min_el_pos = thrust::min_element( iter_begin, iter_end, nodeProp_comp() );
DereferencedIteratorTuple tmp = *min_el_pos;

但是在编译时我得到这个错误。

thrust_min.cu(99):错误:没有重载函数"thrust::min_element"的实例与参数列表匹配参数类型为:(NodePropIterator, NodePropIterator, nodeProp_comp)

编译"/tmp/tmpxft_00005c8e_0000000 -6_thrust_min.cpp1.ii"时发现1个错误。

我编译使用:

nvcc -gencode arch=compute_30,code=sm_30 -G -G thrust_min。cu -Xcompiler -rdynamic,-Wall,-Wextra -lineinfo -o thrust_min

我使用的是gcc版本4.6.3 20120306 (Red Hat 4.6.3-2) (gcc), CUDA 5.

如果在调用min_element期间省略谓词,则不会出现错误…我猜它使用默认的'less'函子。

请帮。

我问了一下这个问题,似乎在c++03中,局部类型(即nodeProp)不能用作模板参数,因为它没有链接。您可能需要回顾这个(与推力无关的)SO问题/答案以进行额外的讨论。

Thrust作为一个模板库,依赖于此。因此,我认为建议将在推力操作中使用的函子放在全局作用域中。

如果你认为还有其他问题在起作用,你可能想要发布一个新的问题和例子。然而,对于你在这个问题上发布的代码,我相信这就是原因,我已经证明了重新排序代码可以解决这个问题。注意,这里真正有问题的是结构定义,而不是类型定义。

最新更新