perf_bolt_reduce_by_key.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/reduce_by_key.h>
  16. #include "perf.hpp"
  17. int rand_int()
  18. {
  19. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  20. }
  21. struct unique_key {
  22. int current;
  23. int avgValuesNoPerKey;
  24. unique_key()
  25. {
  26. current = 0;
  27. avgValuesNoPerKey = 512;
  28. }
  29. int operator()()
  30. {
  31. double p = double(1.0) / static_cast<double>(avgValuesNoPerKey);
  32. if((rand() / double(RAND_MAX)) <= p)
  33. return ++current;
  34. return current;
  35. }
  36. } UniqueKey;
  37. int main(int argc, char *argv[])
  38. {
  39. perf_parse_args(argc, argv);
  40. std::cout << "size: " << PERF_N << std::endl;
  41. bolt::cl::control ctrl = bolt::cl::control::getDefault();
  42. ::cl::Device device = ctrl.getDevice();
  43. std::cout << "device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
  44. // create vector of keys and random values
  45. std::vector<int> host_keys(PERF_N);
  46. std::vector<int> host_values(PERF_N);
  47. std::generate(host_keys.begin(), host_keys.end(), UniqueKey);
  48. std::generate(host_values.begin(), host_values.end(), rand_int);
  49. // create device vectors for data
  50. bolt::cl::device_vector<int> device_keys(PERF_N);
  51. bolt::cl::device_vector<int> device_values(PERF_N);
  52. // transfer data to the device
  53. bolt::cl::copy(host_keys.begin(), host_keys.end(), device_keys.begin());
  54. bolt::cl::copy(host_values.begin(), host_values.end(), device_values.begin());
  55. // create device vectors for the results
  56. bolt::cl::device_vector<int> device_keys_results(PERF_N);
  57. bolt::cl::device_vector<int> device_values_results(PERF_N);
  58. typedef bolt::cl::device_vector<int>::iterator iterType;
  59. bolt::cl::pair<iterType, iterType> result = {
  60. device_keys_results.begin(),
  61. device_values_results.begin()
  62. };
  63. perf_timer t;
  64. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  65. t.start();
  66. result = bolt::cl::reduce_by_key(device_keys.begin(),
  67. device_keys.end(),
  68. device_values.begin(),
  69. device_keys_results.begin(),
  70. device_values_results.begin());
  71. t.stop();
  72. }
  73. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  74. size_t result_size = bolt::cl::distance(device_keys_results.begin(), result.first);
  75. if(result_size != static_cast<size_t>(host_keys[PERF_N-1] + 1)){
  76. std::cout << "ERROR: "
  77. << "wrong number of keys"
  78. << std::endl;
  79. return -1;
  80. }
  81. return 0;
  82. }