perf_thrust_reduce_by_key.cu 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <cstdlib>
  12. #include <iostream>
  13. #include <thrust/copy.h>
  14. #include <thrust/device_vector.h>
  15. #include <thrust/generate.h>
  16. #include <thrust/host_vector.h>
  17. #include <thrust/reduce.h>
  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. // create vector of keys and random values
  44. thrust::host_vector<int> host_keys(PERF_N);
  45. thrust::host_vector<int> host_values(PERF_N);
  46. std::generate(host_keys.begin(), host_keys.end(), UniqueKey);
  47. std::generate(host_values.begin(), host_values.end(), rand_int);
  48. // transfer data to the device
  49. thrust::device_vector<int> device_keys = host_keys;
  50. thrust::device_vector<int> device_values = host_values;
  51. // create device vectors for the results
  52. thrust::device_vector<int> device_keys_results(PERF_N);
  53. thrust::device_vector<int> device_values_results(PERF_N);
  54. typedef typename thrust::device_vector<int>::iterator iterType;
  55. thrust::pair<iterType, iterType> result;
  56. perf_timer t;
  57. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  58. t.start();
  59. result = thrust::reduce_by_key(device_keys.begin(),
  60. device_keys.end(),
  61. device_values.begin(),
  62. device_keys_results.begin(),
  63. device_values_results.begin());
  64. cudaDeviceSynchronize();
  65. t.stop();
  66. }
  67. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  68. size_t result_size = thrust::distance(device_keys_results.begin(), result.first);
  69. if(result_size != static_cast<size_t>(host_keys[PERF_N-1] + 1)){
  70. std::cout << "ERROR: "
  71. << "wrong number of keys"
  72. << std::endl;
  73. return -1;
  74. }
  75. return 0;
  76. }