perf_prev_permutation.cpp 2.1 KB

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