perf_thrust_count.cu 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <iostream>
  12. #include <vector>
  13. #include <thrust/count.h>
  14. #include <thrust/host_vector.h>
  15. #include <thrust/device_vector.h>
  16. #include "perf.hpp"
  17. int rand_int()
  18. {
  19. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. perf_parse_args(argc, argv);
  24. std::cout << "size: " << PERF_N << std::endl;
  25. // create vector of random numbers on the host
  26. thrust::host_vector<int> host_vector(PERF_N);
  27. thrust::generate(host_vector.begin(), host_vector.end(), rand_int);
  28. thrust::device_vector<int> v = host_vector;
  29. size_t count = 0;
  30. perf_timer t;
  31. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  32. t.start();
  33. count = thrust::count(v.begin(), v.end(), 4);
  34. cudaDeviceSynchronize();
  35. t.stop();
  36. }
  37. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  38. std::cout << "count: " << count << std::endl;
  39. return 0;
  40. }