perf_count.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/count.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include "perf.hpp"
  17. int rand_int()
  18. {
  19. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  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. size_t count = 0;
  42. perf_timer t;
  43. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  44. t.start();
  45. count = boost::compute::count(
  46. device_vector.begin(), device_vector.end(), 4, queue
  47. );
  48. queue.finish();
  49. t.stop();
  50. }
  51. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  52. std::cout << "count: " << count << std::endl;
  53. // verify count is correct
  54. size_t host_count = std::count(host_vector.begin(),
  55. host_vector.end(),
  56. 4);
  57. if(count != host_count){
  58. std::cout << "ERROR: "
  59. << "device_count (" << count << ") "
  60. << "!= "
  61. << "host_count (" << host_count << ")"
  62. << std::endl;
  63. return -1;
  64. }
  65. return 0;
  66. }