Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:05

0001 /**
0002 name=crovella_t688 ; nvcc $name.cu -o /tmp/$name && /tmp/$name
0003 
0004 https://stackoverflow.com/questions/37013191/is-it-possible-to-create-a-thrusts-function-predicate-for-structs-using-a-given
0005 
0006 **/
0007 
0008 #include <thrust/device_vector.h>
0009 #include <thrust/host_vector.h>
0010 #include <thrust/copy.h>
0011 #include <iostream>
0012 
0013 struct my_score {
0014 
0015   int id;
0016   int score;
0017 };
0018 
0019 const int dsize = 10;
0020 
0021 struct copy_func 
0022 {
0023   int threshold;
0024 
0025   copy_func(int thr) : threshold(thr) {};
0026 
0027   __host__ __device__  bool operator()(const my_score &x)
0028   {
0029       return (x.score > threshold);
0030   }
0031 };
0032 
0033 int main(){
0034 
0035   thrust::host_vector<my_score> h_data(dsize);
0036   thrust::device_vector<my_score> d_result(dsize);
0037   int my_threshold = 50;
0038   for (int i = 0; i < dsize; i++)
0039   {
0040        h_data[i].id = i;
0041        h_data[i].score = i * 10;
0042    }
0043 
0044   thrust::device_vector<my_score> d_data = h_data;
0045 
0046   int rsize = thrust::copy_if(d_data.begin(), d_data.end(), d_result.begin(), copy_func(my_threshold)) - d_result.begin();
0047 
0048   std::cout << "There were " << rsize << " entries with a score greater than " << my_threshold << std::endl;
0049 
0050   return 0;
0051 }
0052