perf_sort_by_key.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@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/compute/system.hpp>
  14. #include <boost/compute/algorithm/sort_by_key.hpp>
  15. #include <boost/compute/algorithm/is_sorted.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/types/fundamental.hpp>
  18. #include "perf.hpp"
  19. int main(int argc, char *argv[])
  20. {
  21. using boost::compute::int_;
  22. using boost::compute::long_;
  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. // create vector of random numbers on the host
  31. std::vector<int_> host_keys(PERF_N);
  32. std::generate(host_keys.begin(), host_keys.end(), rand);
  33. std::vector<long_> host_values(PERF_N);
  34. std::copy(host_keys.begin(), host_keys.end(), host_values.begin());
  35. // create vector on the device and copy the data
  36. boost::compute::vector<int_> device_keys(PERF_N, context);
  37. boost::compute::vector<long_> device_values(PERF_N, context);
  38. perf_timer t;
  39. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  40. boost::compute::copy(
  41. host_keys.begin(), host_keys.end(), device_keys.begin(), queue
  42. );
  43. boost::compute::copy(
  44. host_values.begin(), host_values.end(), device_values.begin(), queue
  45. );
  46. t.start();
  47. // sort vector
  48. boost::compute::sort_by_key(
  49. device_keys.begin(), device_keys.end(), device_values.begin(), queue
  50. );
  51. queue.finish();
  52. t.stop();
  53. }
  54. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  55. // verify keys are sorted
  56. if(!boost::compute::is_sorted(device_keys.begin(), device_keys.end(), queue)){
  57. std::cout << "ERROR: is_sorted() returned false for the keys" << std::endl;
  58. return -1;
  59. }
  60. // verify values are sorted
  61. if(!boost::compute::is_sorted(device_values.begin(), device_values.end(), queue)){
  62. std::cout << "ERROR: is_sorted() returned false for the values" << std::endl;
  63. return -1;
  64. }
  65. return 0;
  66. }