perf_bolt_saxpy.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <iostream>
  11. #include <algorithm>
  12. #include <vector>
  13. #include <bolt/cl/copy.h>
  14. #include <bolt/cl/device_vector.h>
  15. #include <bolt/cl/transform.h>
  16. #include "perf.hpp"
  17. BOLT_FUNCTOR(saxpy_functor,
  18. struct saxpy_functor
  19. {
  20. float _a;
  21. saxpy_functor(float a) : _a(a) {};
  22. float operator() (const float &x, const float &y) const
  23. {
  24. return _a * x + y;
  25. };
  26. };
  27. )
  28. int main(int argc, char *argv[])
  29. {
  30. perf_parse_args(argc, argv);
  31. std::cout << "size: " << PERF_N << std::endl;
  32. bolt::cl::control ctrl = bolt::cl::control::getDefault();
  33. ::cl::Device device = ctrl.getDevice();
  34. std::cout << "device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
  35. // create host vectors
  36. std::vector<float> host_x(PERF_N);
  37. std::vector<float> host_y(PERF_N);
  38. std::generate(host_x.begin(), host_x.end(), rand);
  39. std::generate(host_y.begin(), host_y.end(), rand);
  40. // create device vectors
  41. bolt::cl::device_vector<float> device_x(PERF_N);
  42. bolt::cl::device_vector<float> device_y(PERF_N);
  43. // transfer data to the device
  44. bolt::cl::copy(host_x.begin(), host_x.end(), device_x.begin());
  45. bolt::cl::copy(host_y.begin(), host_y.end(), device_y.begin());
  46. perf_timer t;
  47. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  48. t.start();
  49. bolt::cl::transform(
  50. device_x.begin(), device_x.end(),
  51. device_y.begin(),
  52. device_y.begin(),
  53. saxpy_functor(2.5f)
  54. );
  55. t.stop();
  56. }
  57. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  58. // transfer data back to host
  59. bolt::cl::copy(device_x.begin(), device_x.end(), host_x.begin());
  60. bolt::cl::copy(device_y.begin(), device_y.end(), host_y.begin());
  61. return 0;
  62. }