perf_thrust_rotate.cu 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/host_vector.h>
  16. #include "perf.hpp"
  17. int main(int argc, char *argv[])
  18. {
  19. perf_parse_args(argc, argv);
  20. std::cout << "size: " << PERF_N << std::endl;
  21. thrust::host_vector<int> h_vec = generate_random_vector<int>(PERF_N);
  22. // transfer data to the device
  23. thrust::device_vector<int> d_vec;
  24. size_t rotate_distance = PERF_N / 2;
  25. perf_timer t;
  26. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  27. d_vec = h_vec;
  28. t.start();
  29. // there is no thrust::rotate() so we implement it manually with copy()
  30. thrust::device_vector<int> tmp(d_vec.begin(), d_vec.begin() + rotate_distance);
  31. thrust::copy(d_vec.begin() + rotate_distance, d_vec.end(), d_vec.begin());
  32. thrust::copy(tmp.begin(), tmp.end(), d_vec.begin() + rotate_distance);
  33. cudaDeviceSynchronize();
  34. t.stop();
  35. }
  36. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  37. // transfer data back to host
  38. thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
  39. return 0;
  40. }