perf_thrust_set_difference.cu 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <iostream>
  11. #include <iterator>
  12. #include <algorithm>
  13. #include <thrust/device_vector.h>
  14. #include <thrust/host_vector.h>
  15. #include <thrust/set_operations.h>
  16. #include <thrust/sort.h>
  17. #include "perf.hpp"
  18. int rand_int()
  19. {
  20. return static_cast<int>((rand() / double(RAND_MAX)) * 25.0);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. perf_parse_args(argc, argv);
  25. std::cout << "size: " << PERF_N << std::endl;
  26. thrust::host_vector<int> v1(std::floor(PERF_N / 2.0));
  27. thrust::host_vector<int> v2(std::ceil(PERF_N / 2.0));
  28. std::generate(v1.begin(), v1.end(), rand_int);
  29. std::generate(v2.begin(), v2.end(), rand_int);
  30. std::sort(v1.begin(), v1.end());
  31. std::sort(v2.begin(), v2.end());
  32. // transfer data to the device
  33. thrust::device_vector<int> gpu_v1 = v1;
  34. thrust::device_vector<int> gpu_v2 = v2;
  35. thrust::device_vector<int> gpu_v3(PERF_N);
  36. thrust::device_vector<int>::iterator gpu_v3_end;
  37. perf_timer t;
  38. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  39. t.start();
  40. gpu_v3_end = thrust::set_difference(
  41. gpu_v1.begin(), gpu_v1.end(),
  42. gpu_v2.begin(), gpu_v2.end(),
  43. gpu_v3.begin()
  44. );
  45. cudaDeviceSynchronize();
  46. t.stop();
  47. }
  48. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  49. std::cout << "size: " << thrust::distance(gpu_v3.begin(), gpu_v3_end) << std::endl;
  50. return 0;
  51. }