perf_partial_sum.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <numeric>
  13. #include <vector>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/partial_sum.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. using boost::compute::int_;
  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::vector<int_> device_res(PERF_N,context);
  38. boost::compute::copy(
  39. host_vector.begin(),
  40. host_vector.end(),
  41. device_vector.begin(),
  42. queue
  43. );
  44. // sum vector
  45. perf_timer t;
  46. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  47. boost::compute::copy(
  48. host_vector.begin(),
  49. host_vector.end(),
  50. device_vector.begin(),
  51. queue
  52. );
  53. t.start();
  54. boost::compute::partial_sum(
  55. device_vector.begin(),
  56. device_vector.end(),
  57. device_res.begin(),
  58. queue
  59. );
  60. queue.finish();
  61. t.stop();
  62. }
  63. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  64. // verify sum is correct
  65. std::partial_sum(
  66. host_vector.begin(),
  67. host_vector.end(),
  68. host_vector.begin()
  69. );
  70. int device_sum = device_res.back();
  71. int host_sum = host_vector.back();
  72. if(device_sum != host_sum){
  73. std::cout << "ERROR: "
  74. << "device_sum (" << device_sum << ") "
  75. << "!= "
  76. << "host_sum (" << host_sum << ")"
  77. << std::endl;
  78. return -1;
  79. }
  80. return 0;
  81. }