In the simple example I tried to find the min value, which is not yet visited.
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;
But on compilation i get this error.
thrust_min.cu(99): error: no instance of overloaded function "thrust::min_element" matches the argument list argument types are: (NodePropIterator, NodePropIterator, nodeProp_comp)
1 error detected in the compilation of "/tmp/tmpxft_00005c8e_00000000-6_thrust_min.cpp1.ii".
I compile using :
nvcc -gencode arch=compute_30,code=sm_30 -G -g thrust_min.cu -Xcompiler -rdynamic,-Wall,-Wextra -lineinfo -o thrust_min
I am using gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC), CUDA 5.
I get no error if I omit the predicate during the call to min_element ... which uses the default 'less' functor i guess.
Please help.
I asked around about this, and it seems that, in c++03, a local type (i.e., nodeProp
) can't be used as a template parameter because it has no linkage. You may want to review this (non-thrust related) SO question/answer for additional discussion.
Thrust, being a template library, depends on this. So I think the recommendation is to put your functors that are used in thrust operations at global scope.
If you think there are other issues at play, you may want to post a new question with examples. However for the code you've posted in this question, I believe this is the reason, and I've demonstrated that reordering the code fixes the issue. Note the struct definition is really what is at issue here, not the typedefs.