perf_max_element.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Rastko Anicic <anicic.rastko@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/max_element.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include "perf.hpp"
  17. int rand_int()
  18. {
  19. return static_cast<int>(rand() % 10000000);
  20. }
  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. // create vector of random numbers on the host
  31. std::vector<int> host_vector(PERF_N);
  32. std::generate(host_vector.begin(), host_vector.end(), rand_int);
  33. // create vector on the device and copy the data
  34. boost::compute::vector<int> device_vector(PERF_N, context);
  35. boost::compute::copy(
  36. host_vector.begin(),
  37. host_vector.end(),
  38. device_vector.begin(),
  39. queue
  40. );
  41. boost::compute::vector<int>::iterator device_max_iter
  42. = device_vector.begin();
  43. perf_timer t;
  44. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  45. t.start();
  46. device_max_iter = boost::compute::max_element(
  47. device_vector.begin(), device_vector.end(), queue
  48. );
  49. queue.finish();
  50. t.stop();
  51. }
  52. int device_max = device_max_iter.read(queue);
  53. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  54. std::cout << "max: " << device_max << std::endl;
  55. // verify max is correct
  56. std::vector<int>::iterator host_max_iter
  57. = std::max_element(host_vector.begin(), host_vector.end());
  58. int host_max = *host_max_iter;
  59. if(device_max != host_max){
  60. std::cout << "ERROR: "
  61. << "device_max (" << device_max << ") "
  62. << "!= "
  63. << "host_max (" << host_max << ")"
  64. << std::endl;
  65. return -1;
  66. }
  67. size_t host_max_idx = std::distance(host_vector.begin(), host_max_iter);
  68. size_t device_max_idx = std::distance(device_vector.begin(), device_max_iter);
  69. if(device_max_idx != host_max_idx){
  70. std::cout << "ERROR: "
  71. << "device_max index (" << device_max_idx << ") "
  72. << "!= "
  73. << "host_max index (" << host_max_idx << ")"
  74. << std::endl;
  75. return -1;
  76. }
  77. return 0;
  78. }