perf_is_sorted.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/is_sorted.hpp>
  15. #include <boost/compute/algorithm/reverse.hpp>
  16. #include <boost/compute/algorithm/sort.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "perf.hpp"
  19. int main(int argc, char *argv[])
  20. {
  21. perf_parse_args(argc, argv);
  22. std::cout << "size: " << PERF_N << std::endl;
  23. // setup context and queue for the default device
  24. boost::compute::device device = boost::compute::system::default_device();
  25. boost::compute::context context(device);
  26. boost::compute::command_queue queue(context, device);
  27. std::cout << "device: " << device.name() << std::endl;
  28. // create vector of random numbers on the host
  29. std::vector<int> host_vector(PERF_N);
  30. std::generate(host_vector.begin(), host_vector.end(), rand);
  31. // create vector on the device and copy the data
  32. boost::compute::vector<int> device_vector(PERF_N, context);
  33. boost::compute::copy(
  34. host_vector.begin(), host_vector.end(), device_vector.begin(), queue
  35. );
  36. // sort and then reverse the random vector
  37. boost::compute::sort(device_vector.begin(), device_vector.end(), queue);
  38. boost::compute::reverse(device_vector.begin(), device_vector.end(), queue);
  39. perf_timer t;
  40. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  41. t.start();
  42. bool sorted = boost::compute::is_sorted(
  43. device_vector.begin(), device_vector.end(), queue
  44. );
  45. queue.finish();
  46. t.stop();
  47. if(sorted){
  48. std::cerr << "ERROR: is_sorted() returned true" << std::endl;
  49. }
  50. }
  51. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  52. return 0;
  53. }