perf_exclusive_scan.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Benoit
  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/exclusive_scan.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include "perf.hpp"
  18. int rand_int()
  19. {
  20. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. perf_parse_args(argc, argv);
  25. std::cout << "size: " << PERF_N << std::endl;
  26. // setup context and queue for the default device
  27. boost::compute::device device = boost::compute::system::default_device();
  28. boost::compute::context context(device);
  29. boost::compute::command_queue queue(context, device);
  30. std::cout << "device: " << device.name() << std::endl;
  31. // create vector of random numbers on the host
  32. std::vector<int> host_vector(PERF_N);
  33. std::generate(host_vector.begin(), host_vector.end(), rand_int);
  34. // create vector on the device and copy the data
  35. boost::compute::vector<int> device_vector(PERF_N, context);
  36. boost::compute::vector<int> device_res(PERF_N,context);
  37. boost::compute::copy(
  38. host_vector.begin(),
  39. host_vector.end(),
  40. device_vector.begin(),
  41. queue
  42. );
  43. // sum vector
  44. perf_timer t;
  45. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  46. boost::compute::copy(
  47. host_vector.begin(),
  48. host_vector.end(),
  49. device_vector.begin(),
  50. queue
  51. );
  52. t.start();
  53. boost::compute::exclusive_scan(
  54. device_vector.begin(),
  55. device_vector.end(),
  56. device_res.begin(),
  57. queue
  58. );
  59. queue.finish();
  60. t.stop();
  61. }
  62. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  63. // verify sum is correct
  64. std::partial_sum(
  65. host_vector.begin(),
  66. host_vector.end(),
  67. host_vector.begin()
  68. );
  69. int device_sum = device_res.back();
  70. // when scan is exclusive values are shifted by one on the left
  71. // compared to a inclusive scan
  72. int host_sum = host_vector[host_vector.size()-2];
  73. if(device_sum != host_sum){
  74. std::cout << "ERROR: "
  75. << "device_sum (" << device_sum << ") "
  76. << "!= "
  77. << "host_sum (" << host_sum << ")"
  78. << std::endl;
  79. return -1;
  80. }
  81. return 0;
  82. }