perf_comparison_sort.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2016 Jakub Szuppe <j.szuppe@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 <vector>
  13. #include <boost/program_options.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/sort.hpp>
  16. #include <boost/compute/algorithm/is_sorted.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "perf.hpp"
  19. namespace po = boost::program_options;
  20. namespace compute = boost::compute;
  21. int main(int argc, char *argv[])
  22. {
  23. perf_parse_args(argc, argv);
  24. std::cout << "size: " << PERF_N << std::endl;
  25. // setup context and queue for the default device
  26. boost::compute::device device = boost::compute::system::default_device();
  27. boost::compute::context context(device);
  28. boost::compute::command_queue queue(context, device);
  29. std::cout << "device: " << device.name() << std::endl;
  30. using boost::compute::int_;
  31. // create vector of random numbers on the host
  32. std::vector<int_> host_vector(PERF_N);
  33. std::generate(host_vector.begin(), host_vector.end(), rand);
  34. // create vector on the device and copy the data
  35. boost::compute::vector<int_> device_vector(PERF_N, context);
  36. // less function for float
  37. BOOST_COMPUTE_FUNCTION(bool, comp, (int_ a, int_ b),
  38. {
  39. return a < b;
  40. });
  41. // sort vector
  42. perf_timer t;
  43. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  44. boost::compute::copy(
  45. host_vector.begin(),
  46. host_vector.end(),
  47. device_vector.begin(),
  48. queue
  49. );
  50. queue.finish();
  51. t.start();
  52. boost::compute::sort(
  53. device_vector.begin(),
  54. device_vector.end(),
  55. comp,
  56. queue
  57. );
  58. queue.finish();
  59. t.stop();
  60. };
  61. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  62. // verify vector is sorted
  63. if(!boost::compute::is_sorted(device_vector.begin(),
  64. device_vector.end(),
  65. comp,
  66. queue)){
  67. std::cout << "ERROR: is_sorted() returned false" << std::endl;
  68. return -1;
  69. }
  70. return 0;
  71. }