perf_set_difference.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #include <algorithm>
  11. #include <iostream>
  12. #include <numeric>
  13. #include <vector>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/set_difference.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include "perf.hpp"
  18. int rand_int()
  19. {
  20. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. perf_parse_args(argc, argv);
  25. std::cout << "size: " << PERF_N << std::endl;
  26. // setup context and queue for the default device
  27. boost::compute::device device = boost::compute::system::default_device();
  28. boost::compute::context context(device);
  29. boost::compute::command_queue queue(context, device);
  30. std::cout << "device: " << device.name() << std::endl;
  31. // create vectors of random numbers on the host
  32. std::vector<int> v1(std::floor(PERF_N / 2.0));
  33. std::vector<int> v2(std::ceil(PERF_N / 2.0));
  34. std::generate(v1.begin(), v1.end(), rand_int);
  35. std::generate(v2.begin(), v2.end(), rand_int);
  36. std::sort(v1.begin(), v1.end());
  37. std::sort(v2.begin(), v2.end());
  38. // create vectors on the device and copy the data
  39. boost::compute::vector<int> gpu_v1(std::floor(PERF_N / 2.0), context);
  40. boost::compute::vector<int> gpu_v2(std::ceil(PERF_N / 2.0), context);
  41. boost::compute::copy(
  42. v1.begin(), v1.end(), gpu_v1.begin(), queue
  43. );
  44. boost::compute::copy(
  45. v2.begin(), v2.end(), gpu_v2.begin(), queue
  46. );
  47. boost::compute::vector<int> gpu_v3(PERF_N, context);
  48. boost::compute::vector<int>::iterator gpu_v3_end;
  49. perf_timer t;
  50. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  51. t.start();
  52. gpu_v3_end = boost::compute::set_difference(
  53. gpu_v1.begin(), gpu_v1.end(),
  54. gpu_v2.begin(), gpu_v2.end(),
  55. gpu_v3.begin(), queue
  56. );
  57. queue.finish();
  58. t.stop();
  59. }
  60. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  61. std::cout << "size: " << std::distance(gpu_v3.begin(), gpu_v3_end) << std::endl;
  62. return 0;
  63. }