perf_thrust_partition.cu 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/partition.h>
  18. #include "perf.hpp"
  19. int rand_int()
  20. {
  21. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  22. }
  23. struct less_than_ten : public thrust::unary_function<bool, int>
  24. {
  25. __device__ bool operator()(int x) const
  26. {
  27. return x < 10;
  28. }
  29. };
  30. int main(int argc, char *argv[])
  31. {
  32. perf_parse_args(argc, argv);
  33. std::cout << "size: " << PERF_N << std::endl;
  34. thrust::host_vector<int> h_vec(PERF_N);
  35. std::generate(h_vec.begin(), h_vec.end(), rand_int);
  36. thrust::device_vector<int> d_vec(PERF_N);
  37. perf_timer t;
  38. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  39. d_vec = h_vec;
  40. t.start();
  41. thrust::partition(
  42. d_vec.begin(), d_vec.end(), less_than_ten()
  43. );
  44. cudaDeviceSynchronize();
  45. t.stop();
  46. }
  47. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  48. return 0;
  49. }