perf_inner_product.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/inner_product.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. 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. std::vector<int> h1(PERF_N);
  31. std::vector<int> h2(PERF_N);
  32. std::generate(h1.begin(), h1.end(), rand_int);
  33. std::generate(h2.begin(), h2.end(), rand_int);
  34. // create vector on the device and copy the data
  35. boost::compute::vector<int> d1(PERF_N, context);
  36. boost::compute::vector<int> d2(PERF_N, context);
  37. boost::compute::copy(h1.begin(), h1.end(), d1.begin(), queue);
  38. boost::compute::copy(h2.begin(), h2.end(), d2.begin(), queue);
  39. int product = 0;
  40. perf_timer t;
  41. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  42. t.start();
  43. product = boost::compute::inner_product(
  44. d1.begin(), d1.end(), d2.begin(), int(0), queue
  45. );
  46. queue.finish();
  47. t.stop();
  48. }
  49. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  50. // verify product is correct
  51. int host_product = std::inner_product(
  52. h1.begin(), h1.end(), h2.begin(), int(0)
  53. );
  54. if(product != host_product){
  55. std::cout << "ERROR: "
  56. << "device_product (" << product << ") "
  57. << "!= "
  58. << "host_product (" << host_product << ")"
  59. << std::endl;
  60. return -1;
  61. }
  62. return 0;
  63. }