Maximum value of thrust device_vector
<p>i am trying to find the maximum value and it's location of a thrust::device_vecotr.
the mechanism below can save the position of the maximum value, however, i couldn't find the max_val.</p>

<p>i have cout statements to track the running order and where it crashes. it seems to be it crash on this line
    int max_val = *iter;
it shows this result:</p>

<blockquote>
  <p>terminate called after throwing an instance of 'thrust::system::system_error'
    what():  invalid argument
  1234567</p>
</blockquote>

<p>here is the code</p>

<pre><code>#include &lt;thrust/device_vector.h&gt;
#include &lt;thrust/host_vector.h&gt;
#include &lt;thrust/reduce.h&gt;
#include &lt;thrust/extrema.h&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;

template &lt;typename Vector&gt;
void print_vector(const std::string&amp; name, const Vector&amp; v)
{
  typedef typename Vector::value_type T;
  std::cout &lt;&lt; "  " &lt;&lt; std::setw(20) &lt;&lt; name &lt;&lt; "  ";
  thrust::copy(v.begin(), v.end(),     std::ostream_iterator&lt;T&gt;(std::cout, " "));
  std::cout &lt;&lt; std::endl;
}

int main()
{
std::cout&lt;&lt;"1";
thrust::host_vector&lt;int&gt;h_vec(5);
h_vec.push_back(10);
h_vec.push_back(11);
h_vec.push_back(12);
h_vec.push_back(13);
h_vec.push_back(14);
std::cout&lt;&lt;"2";
thrust::device_vector&lt;int&gt;d_vec(5);
std::cout&lt;&lt;"3";

thrust::copy_n(h_vec.begin(),5,d_vec.begin());
std::cout&lt;&lt;"4";
//  print_vector("D_Vec",d_vec);
std::cout&lt;&lt;"5";

thrust::device_vector&lt;int&gt;::iterator iter=thrust::max(d_vec.begin(),d_vec.end());
std::cout&lt;&lt;"6";
unsigned int position = iter - d_vec.begin();
std::cout&lt;&lt;"7";
int max_val = *iter;
std::cout&lt;&lt;"8";

std::cout&lt;&lt;"Max Val= "&lt;&lt;14&lt;&lt;" @"&lt;&lt;position&lt;&lt;    std::endl;


return 0;
}
</code></pre>

<p>Help .. please. also, if there is a better way to extract the maximum value and its position in device_vector using THRUST library it is more than appreciated.</p>