If I understand what exactly you want, get: Get the index for array A of the maximum value inside it.
If this is true, I suggest you use the traction library:
Here's how you do it:
#include <thrust/device_vector.h> #include <thrust/tuple.h> #include <thrust/reduce.h> #include <thrust/fill.h> #include <thrust/generate.h> #include <thrust/sort.h> #include <thrust/sequence.h> #include <thrust/copy.h> #include <cstdlib> #include <time.h> using namespace thrust; // return the biggest of two tuples template <class T> struct bigger_tuple { __device__ __host__ tuple<T,int> operator()(const tuple<T,int> &a, const tuple<T,int> &b) { if (a > b) return a; else return b; } }; template <class T> int max_index(device_vector<T>& vec) { // create implicit index sequence [0, 1, 2, ... ) counting_iterator<int> begin(0); counting_iterator<int> end(vec.size()); tuple<T,int> init(vec[0],0); tuple<T,int> smallest; smallest = reduce(make_zip_iterator(make_tuple(vec.begin(), begin)), make_zip_iterator(make_tuple(vec.end(), end)), init, bigger_tuple<T>()); return get<1>(smallest); } int main(){ thrust::host_vector<int> h_vec(1024); thrust::sequence(h_vec.begin(), h_vec.end()); // values = indices // transfer data to the device thrust::device_vector<int> d_vec = h_vec; int index = max_index(d_vec); std::cout << "Max index is:" << index <<std::endl; std::cout << "Value is: " << h_vec[index] <<std::endl; return 0; }
source share