perf_find.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2015 Jakub Szuppe <j.szuppe@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/find.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include "perf.hpp"
  17. // Max integer that can be generated by rand_int() function.
  18. int rand_int_max = 25;
  19. int rand_int()
  20. {
  21. return static_cast<int>((rand() / double(RAND_MAX)) * rand_int_max);
  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. // create vector on the device and copy the data
  36. boost::compute::vector<int> device_vector(PERF_N, context);
  37. boost::compute::copy(
  38. host_vector.begin(),
  39. host_vector.end(),
  40. device_vector.begin(),
  41. queue
  42. );
  43. // trying to find element that isn't in vector (worst-case scenario)
  44. int wanted = rand_int_max + 1;
  45. // device iterator
  46. boost::compute::vector<int>::iterator device_result_it;
  47. perf_timer t;
  48. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  49. t.start();
  50. device_result_it = boost::compute::find(device_vector.begin(),
  51. device_vector.end(),
  52. wanted,
  53. queue);
  54. queue.finish();
  55. t.stop();
  56. }
  57. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  58. // verify if found index is correct by comparing it with std::find() result
  59. size_t host_result_index = std::distance(host_vector.begin(),
  60. std::find(host_vector.begin(),
  61. host_vector.end(),
  62. wanted));
  63. size_t device_result_index = device_result_it.get_index();
  64. if(device_result_index != host_result_index){
  65. std::cout << "ERROR: "
  66. << "device_result_index (" << device_result_index << ") "
  67. << "!= "
  68. << "host_result_index (" << host_result_index << ")"
  69. << std::endl;
  70. return -1;
  71. }
  72. return 0;
  73. }