perf_reduce_by_key.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <algorithm>
  11. #include <iostream>
  12. #include <numeric>
  13. #include <vector>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/algorithm/fill.hpp>
  16. #include <boost/compute/algorithm/reduce_by_key.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "perf.hpp"
  19. int rand_int()
  20. {
  21. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  22. }
  23. struct unique_key {
  24. int current;
  25. int avgValuesNoPerKey;
  26. unique_key()
  27. {
  28. current = 0;
  29. avgValuesNoPerKey = 512;
  30. }
  31. int operator()()
  32. {
  33. double p = double(1.0) / static_cast<double>(avgValuesNoPerKey);
  34. if((rand() / double(RAND_MAX)) <= p)
  35. return ++current;
  36. return current;
  37. }
  38. } UniqueKey;
  39. int main(int argc, char *argv[])
  40. {
  41. perf_parse_args(argc, argv);
  42. std::cout << "size: " << PERF_N << std::endl;
  43. // setup context and queue for the default device
  44. boost::compute::device device = boost::compute::system::default_device();
  45. boost::compute::context context(device);
  46. boost::compute::command_queue queue(context, device);
  47. std::cout << "device: " << device.name() << std::endl;
  48. // create vector of keys and random values
  49. std::vector<int> host_keys(PERF_N);
  50. std::vector<int> host_values(PERF_N);
  51. std::generate(host_keys.begin(), host_keys.end(), UniqueKey);
  52. std::generate(host_values.begin(), host_values.end(), rand_int);
  53. // create vectors for keys and values on the device and copy the data
  54. boost::compute::vector<int> device_keys(PERF_N, context);
  55. boost::compute::vector<int> device_values(PERF_N,context);
  56. boost::compute::copy(
  57. host_keys.begin(),
  58. host_keys.end(),
  59. device_keys.begin(),
  60. queue
  61. );
  62. boost::compute::copy(
  63. host_values.begin(),
  64. host_values.end(),
  65. device_values.begin(),
  66. queue
  67. );
  68. // vectors for the results
  69. boost::compute::vector<int> device_keys_results(PERF_N, context);
  70. boost::compute::vector<int> device_values_results(PERF_N,context);
  71. typedef boost::compute::vector<int>::iterator iterType;
  72. std::pair<iterType, iterType> result(
  73. device_keys_results.begin(),
  74. device_values_results.begin()
  75. );
  76. // reduce by key
  77. perf_timer t;
  78. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  79. t.start();
  80. result = boost::compute::reduce_by_key(device_keys.begin(),
  81. device_keys.end(),
  82. device_values.begin(),
  83. device_keys_results.begin(),
  84. device_values_results.begin(),
  85. queue);
  86. t.stop();
  87. }
  88. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  89. size_t result_size = std::distance(device_keys_results.begin(), result.first);
  90. if(result_size != static_cast<size_t>(host_keys[PERF_N-1] + 1)){
  91. std::cout << "ERROR: "
  92. << "wrong number of keys" << result_size << "\n" << (host_keys[PERF_N-1] + 1)
  93. << std::endl;
  94. return -1;
  95. }
  96. return 0;
  97. }