perf_thrust_accumulate.cu 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@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 main(int argc, char *argv[])
  20. {
  21. perf_parse_args(argc, argv);
  22. std::cout << "size: " << PERF_N << std::endl;
  23. thrust::host_vector<int> h_vec = generate_random_vector<int>(PERF_N);
  24. // transfer data to the device
  25. thrust::device_vector<int> d_vec = h_vec;
  26. int sum = 0;
  27. perf_timer t;
  28. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  29. t.start();
  30. sum = thrust::reduce(d_vec.begin(), d_vec.end());
  31. cudaDeviceSynchronize();
  32. t.stop();
  33. }
  34. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  35. std::cout << "sum: " << sum << std::endl;
  36. return 0;
  37. }