Advertisement
HellFinger

Untitled

Apr 23rd, 2022
929
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //g++ g++ boostset.cpp -std=c++14 -lOpenCL -lboost_thread -o bs
  2. #define CL_TARGET_OPENCL_VERSION 220
  3. #include <boost/compute.hpp>
  4. #include <boost/compute/system.hpp>
  5. #include <boost/compute/algorithm/set_intersection.hpp>
  6. #include <boost/compute/container/vector.hpp>
  7. #include "stdc++.h"
  8. #include "cpu_bench.hpp"
  9. using namespace std;
  10.  
  11. void lcg(vector<unsigned int>& r, int seed, int size, unsigned long a, unsigned long c, unsigned long m){
  12.   if (size == 1){
  13.     r.push_back((a*seed+c)%m);
  14.     return;
  15.   }
  16.   for(int i = 0; i < size; ++i){
  17.     r.push_back(0);
  18.   }
  19.   r[0] = seed;
  20.   for(int i = 1; i < size; ++i){
  21.     r[i] = uint32_t((a*r[i-1]+c)%m);
  22.   }
  23.   r.erase(r.begin());
  24. }
  25.  
  26.  
  27.  
  28. void generate_v3_0(int seed, long sizeA, long sizeB, long sizeInter, vector<unsigned int> & a, vector<unsigned int>& b) {
  29.     //hardcoded consts for lcg generator
  30.     //a = 50001
  31.     //c = 49999
  32.     //m = 2500000000
  33.    
  34.    
  35.     lcg(a, seed, sizeA+1, 50001, 49999, 2500000000);
  36.     (sizeA!=sizeInter)?lcg(b, a[sizeA-sizeInter-1], sizeB+1, 50001, 49999, 2500000000):lcg(b, seed, sizeB+1, 50001, 49999, 2500000000);
  37.    
  38.     std::shuffle(a.begin(), a.end(), std::default_random_engine(seed+1));
  39.     std::shuffle(b.begin(), b.end(), std::default_random_engine(seed+2));
  40. }
  41.  
  42. int main(int argc, char* argv[]){
  43.  
  44.  
  45.     boost::compute::device device = boost::compute::system::default_device();
  46.     boost::compute::context context(device);
  47.     boost::compute::command_queue queue(context, device);
  48.  
  49.     int A = 100000;
  50.   int B = 100000;
  51.   int inter = 10000;
  52.  
  53.   if (argc>1){
  54.     A = stoi(argv[1]);
  55.     B = stoi(argv[2]);
  56.     inter = stoi(argv[3]);
  57.   }
  58.  
  59.     vector<uint32_t> a;
  60.     vector<uint32_t> b;
  61.     vector<uint32_t> res;
  62.    
  63.    
  64.     generate_v3_0(14, A, B, inter, a, b);
  65.     cout << "generated\n";
  66.    
  67.    
  68.  
  69.     boost::compute::vector<uint32_t> c(1000000,context);
  70.  
  71.     boost::compute::vector<uint32_t>::iterator cEnd;
  72.  
  73.     auto start_wall = std::chrono::steady_clock::now();
  74.     auto start_time = getCPUTime();
  75.  
  76.     //sort(a.begin(), a.end());
  77.     //sort(b.begin(), b.end());
  78.  
  79.     boost::compute::vector<uint32_t> aBoost(a.begin(),a.end(),queue);
  80.     boost::compute::vector<uint32_t> bBoost(b.begin(),b.end(),queue);
  81.  
  82.     boost::compute::sort(aBoost.begin(), aBoost.end(), queue);
  83.     boost::compute::sort(bBoost.begin(), bBoost.end(), queue);
  84.  
  85.  
  86.     auto preprocess_time = getCPUTime();
  87.     auto preprocess_wall = std::chrono::steady_clock::now();
  88.  
  89.  
  90.     cEnd = boost::compute::set_intersection(aBoost.begin(), aBoost.end(), bBoost.begin(), bBoost.end(), c.begin(), queue);
  91.    
  92.  
  93.     queue.finish();
  94.  
  95.     auto end_time = getCPUTime();
  96.     auto end_wall = std::chrono::steady_clock::now();
  97.  
  98.     auto elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(end_wall - start_wall);
  99.     auto elapsedPreproc = std::chrono::duration_cast<std::chrono::microseconds>(preprocess_wall - start_wall);
  100.     cout << "Intersection size: " << distance(c.begin(), cEnd) << endl;
  101.     cout << "Union size: " << A + B - inter << endl;
  102.     cout << "A: " << a.size() << " B: " << b.size() << endl;
  103.     cout << "Ab: " << aBoost.size() << " Bb: " << bBoost.size() << endl;
  104.    
  105.     std::cout << std::fixed << std::showpoint;
  106.     cout << "\nCPU time: " << setprecision(15) << end_time - start_time;
  107.     cout << "\nCPU preprocess time: " << setprecision(15) << preprocess_time - start_time<< endl;
  108.    
  109.     cout << setprecision(15) << end_time << endl;
  110.     cout << setprecision(15) << start_time << endl;
  111.     fprintf( stderr, "\nCPU time:%lf\n", (end_time - start_time) );
  112.     fprintf( stderr, "CPU preprocess time:%lf\n", (preprocess_time - start_time) );
  113.     fprintf( stderr, "\nWall time:%lf\n", elapsedTime.count()/1000000.0);
  114.     fprintf( stderr, "Wall preprocess time:%lf\n", elapsedPreproc.count()/1000000.0);
  115.    
  116.    
  117.    
  118.    
  119.     cout << "\nWALL time: " << setprecision(15) << elapsedTime.count()/1000000 << endl;
  120.     cout << "\nWALL preprocess time: " << setprecision(15) << elapsedPreproc.count()/100000 << endl;
  121.    
  122.     fprintf( stderr, "%lf ", (end_time - start_time) );
  123.     fprintf( stderr, "%lf ",  (preprocess_time - start_time));
  124.     fprintf( stderr, "%lf ", elapsedTime.count()/1000000.0);
  125.     fprintf( stderr, "%lf ",  elapsedPreproc.count()/1000000.0);
  126.     return 0;
  127. }
Advertisement
RAW Paste Data Copied
Advertisement