perf_host_sort.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/timer/timer.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.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> random_vector(PERF_N);
  30. std::generate(random_vector.begin(), random_vector.end(), rand);
  31. // create input vector for gpu
  32. std::vector<int> gpu_vector = random_vector;
  33. // sort vector on gpu
  34. boost::timer::cpu_timer t;
  35. boost::compute::sort(
  36. gpu_vector.begin(), gpu_vector.end(), queue
  37. );
  38. queue.finish();
  39. std::cout << "time: " << t.elapsed().wall / 1e6 << " ms" << std::endl;
  40. // create input vector for host
  41. std::vector<int> host_vector = random_vector;
  42. // sort vector on host
  43. t.start();
  44. std::sort(host_vector.begin(), host_vector.end());
  45. std::cout << "host time: " << t.elapsed().wall / 1e6 << " ms" << std::endl;
  46. // ensure that both sorted vectors are equal
  47. if(!std::equal(gpu_vector.begin(), gpu_vector.end(), host_vector.begin())){
  48. std::cerr << "ERROR: sorted vectors not the same" << std::endl;
  49. return -1;
  50. }
  51. return 0;
  52. }