Question

I am currently trying to use the Thrust library and I am having issues with the return types.

  device_vector<int> input(5);

  input[0] = 0;
  input[1] = 2;
  input[2] = 5;
  input[3] = 7;
  input[4] = 8;

  pair<int*, int*> result= minmax_element(input.begin(), input.end());

gives the error:

 error : no suitable conversion function from "const thrust::detail::normal_iterator<thrust::device_ptr<int>>" to "int *" exists

Could someone please explain to me how work out what the return type should be?

according to the documentation the return type is

thrust::pair<ForwardIterator,ForwardIterator>

However this not not working for me, could someone please explain!

Thanks!

Was it helpful?

Solution

When naming a template parameter, ForwardIterator does not name any particular type. For our purposes, we can think of it as a placeholder for the type of iterator given to thrust::minmax_element:

template<typename ForwardIterator>
  thrust::pair<ForwardIterator,ForwardIterator> minmax_element(ForwardIterator first, ForwardIterator last);

So minmax_element returns a pair of whatever type of iterator is given to it as arguments.

In your case, minmax_element returns a pair of device_vector<int>::iterators. You can make your code compile by making result the appropriate pair:

device_vector<int> input(5);

input[0] = 0;
input[1] = 2;
input[2] = 5;
input[3] = 7;
input[4] = 8;

// we can use a typedef for brevity
typedef pair<device_vector<int>::iterator, device_vector<int>::iterator> result_type;

result_type result = minmax_element(input.begin(), input.end());

// check the results
assert(0 == *result.first);
assert(8 == *result.second);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top