0
votes

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.

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:

terminate called after throwing an instance of 'thrust::system::system_error' what(): invalid argument 1234567

here is the code

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>

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

int main()
{
std::cout<<"1";
thrust::host_vector<int>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<<"2";
thrust::device_vector<int>d_vec(5);
std::cout<<"3";

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

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

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


return 0;
}

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.

1

1 Answers

3
votes

You're not using vectors correctly. push_back() adds an element onto the end of an existing vector. It's clear that you want to replace existing elements.

Also, the thrust algorithm you want is thrust::max_element, not thrust::max

Here's a fully worked code with those issues fixed:

$ cat t1229.cu
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>

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

int main()
{
std::cout<<"1" <<std::endl;
thrust::host_vector<int>h_vec(5);
h_vec[0] = 10;
h_vec[1] = 11;
h_vec[2] = 12;
h_vec[3] = 13;
h_vec[4] = 14;
std::cout<<"2" << std::endl;
thrust::device_vector<int>d_vec(5);
std::cout<<"3" << std::endl;

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

thrust::device_vector<int>::iterator iter=thrust::max_element(d_vec.begin(),d_vec.end());
std::cout<<"6" << std::endl;
unsigned int position = iter - d_vec.begin();
std::cout<<"7" << std::endl;
int max_val = d_vec[position];
std::cout<<"8" << std::endl;

std::cout<<"Max Val= "<<max_val<<" @"<<position<<    std::endl;


return 0;
}
$ nvcc -o t1229 t1229.cu
$ ./t1229
1
2
3
4
5
6
7
8
Max Val= 14 @4
$