Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //g++ g++ boostset.cpp -std=c++14 -lOpenCL -lboost_thread -o bs
- #define CL_TARGET_OPENCL_VERSION 220
- #include <boost/compute.hpp>
- #include <boost/compute/system.hpp>
- #include <boost/compute/algorithm/set_intersection.hpp>
- #include <boost/compute/container/vector.hpp>
- #include "stdc++.h"
- #include "cpu_bench.hpp"
- using namespace std;
- void lcg(vector<unsigned int>& r, int seed, int size, unsigned long a, unsigned long c, unsigned long m){
- if (size == 1){
- r.push_back((a*seed+c)%m);
- return;
- }
- for(int i = 0; i < size; ++i){
- r.push_back(0);
- }
- r[0] = seed;
- for(int i = 1; i < size; ++i){
- r[i] = uint32_t((a*r[i-1]+c)%m);
- }
- r.erase(r.begin());
- }
- void generate_v3_0(int seed, long sizeA, long sizeB, long sizeInter, vector<unsigned int> & a, vector<unsigned int>& b) {
- //hardcoded consts for lcg generator
- //a = 50001
- //c = 49999
- //m = 2500000000
- lcg(a, seed, sizeA+1, 50001, 49999, 2500000000);
- (sizeA!=sizeInter)?lcg(b, a[sizeA-sizeInter-1], sizeB+1, 50001, 49999, 2500000000):lcg(b, seed, sizeB+1, 50001, 49999, 2500000000);
- std::shuffle(a.begin(), a.end(), std::default_random_engine(seed+1));
- std::shuffle(b.begin(), b.end(), std::default_random_engine(seed+2));
- }
- int main(int argc, char* argv[]){
- boost::compute::device device = boost::compute::system::default_device();
- boost::compute::context context(device);
- boost::compute::command_queue queue(context, device);
- int A = 100000;
- int B = 100000;
- int inter = 10000;
- if (argc>1){
- A = stoi(argv[1]);
- B = stoi(argv[2]);
- inter = stoi(argv[3]);
- }
- vector<uint32_t> a;
- vector<uint32_t> b;
- vector<uint32_t> res;
- generate_v3_0(14, A, B, inter, a, b);
- cout << "generated\n";
- boost::compute::vector<uint32_t> c(1000000,context);
- boost::compute::vector<uint32_t>::iterator cEnd;
- auto start_wall = std::chrono::steady_clock::now();
- auto start_time = getCPUTime();
- //sort(a.begin(), a.end());
- //sort(b.begin(), b.end());
- boost::compute::vector<uint32_t> aBoost(a.begin(),a.end(),queue);
- boost::compute::vector<uint32_t> bBoost(b.begin(),b.end(),queue);
- boost::compute::sort(aBoost.begin(), aBoost.end(), queue);
- boost::compute::sort(bBoost.begin(), bBoost.end(), queue);
- auto preprocess_time = getCPUTime();
- auto preprocess_wall = std::chrono::steady_clock::now();
- cEnd = boost::compute::set_intersection(aBoost.begin(), aBoost.end(), bBoost.begin(), bBoost.end(), c.begin(), queue);
- queue.finish();
- auto end_time = getCPUTime();
- auto end_wall = std::chrono::steady_clock::now();
- auto elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(end_wall - start_wall);
- auto elapsedPreproc = std::chrono::duration_cast<std::chrono::microseconds>(preprocess_wall - start_wall);
- cout << "Intersection size: " << distance(c.begin(), cEnd) << endl;
- cout << "Union size: " << A + B - inter << endl;
- cout << "A: " << a.size() << " B: " << b.size() << endl;
- cout << "Ab: " << aBoost.size() << " Bb: " << bBoost.size() << endl;
- std::cout << std::fixed << std::showpoint;
- cout << "\nCPU time: " << setprecision(15) << end_time - start_time;
- cout << "\nCPU preprocess time: " << setprecision(15) << preprocess_time - start_time<< endl;
- cout << setprecision(15) << end_time << endl;
- cout << setprecision(15) << start_time << endl;
- fprintf( stderr, "\nCPU time:%lf\n", (end_time - start_time) );
- fprintf( stderr, "CPU preprocess time:%lf\n", (preprocess_time - start_time) );
- fprintf( stderr, "\nWall time:%lf\n", elapsedTime.count()/1000000.0);
- fprintf( stderr, "Wall preprocess time:%lf\n", elapsedPreproc.count()/1000000.0);
- cout << "\nWALL time: " << setprecision(15) << elapsedTime.count()/1000000 << endl;
- cout << "\nWALL preprocess time: " << setprecision(15) << elapsedPreproc.count()/100000 << endl;
- fprintf( stderr, "%lf ", (end_time - start_time) );
- fprintf( stderr, "%lf ", (preprocess_time - start_time));
- fprintf( stderr, "%lf ", elapsedTime.count()/1000000.0);
- fprintf( stderr, "%lf ", elapsedPreproc.count()/1000000.0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement