perf_bolt_max_element.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/max_element.h>
  16. #include "perf.hpp"
  17. int rand_int()
  18. {
  19. return static_cast<int>(rand() % 10000000);
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. perf_parse_args(argc, argv);
  24. std::cout << "size: " << PERF_N << std::endl;
  25. bolt::cl::control ctrl = bolt::cl::control::getDefault();
  26. ::cl::Device device = ctrl.getDevice();
  27. std::cout << "device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
  28. // create host vector
  29. std::vector<int> host_vec = generate_random_vector<int>(PERF_N);
  30. // create device vectors
  31. bolt::cl::device_vector<int> device_vec(PERF_N);
  32. // transfer data to the device
  33. bolt::cl::copy(host_vec.begin(), host_vec.end(), device_vec.begin());
  34. bolt::cl::device_vector<int>::iterator max_iter = device_vec.begin();
  35. perf_timer t;
  36. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  37. t.start();
  38. max_iter = bolt::cl::max_element(device_vec.begin(), device_vec.end());
  39. t.stop();
  40. }
  41. int device_max = *max_iter;
  42. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  43. std::cout << "max: " << device_max << std::endl;
  44. // verify max is correct
  45. int host_max = *std::max_element(host_vec.begin(), host_vec.end());
  46. if(device_max != host_max){
  47. std::cout << "ERROR: "
  48. << "device_max (" << device_max << ") "
  49. << "!= "
  50. << "host_max (" << host_max << ")"
  51. << std::endl;
  52. return -1;
  53. }
  54. return 0;
  55. }