perf_thrust_merge.cu 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/merge.h>
  16. #include <thrust/sort.h>
  17. #include "perf.hpp"
  18. int main(int argc, char *argv[])
  19. {
  20. perf_parse_args(argc, argv);
  21. std::cout << "size: " << PERF_N << std::endl;
  22. thrust::host_vector<int> v1(std::floor(PERF_N / 2.0));
  23. thrust::host_vector<int> v2(std::ceil(PERF_N / 2.0));
  24. std::generate(v1.begin(), v1.end(), rand);
  25. std::generate(v2.begin(), v2.end(), rand);
  26. std::sort(v1.begin(), v1.end());
  27. std::sort(v2.begin(), v2.end());
  28. // transfer data to the device
  29. thrust::device_vector<int> gpu_v1 = v1;
  30. thrust::device_vector<int> gpu_v2 = v2;
  31. thrust::device_vector<int> gpu_v3(PERF_N);
  32. perf_timer t;
  33. for(size_t trial = 0; trial < PERF_TRIALS; trial++){
  34. t.start();
  35. thrust::merge(
  36. gpu_v1.begin(), gpu_v1.end(),
  37. gpu_v2.begin(), gpu_v2.end(),
  38. gpu_v3.begin()
  39. );
  40. cudaDeviceSynchronize();
  41. t.stop();
  42. }
  43. std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
  44. thrust::host_vector<int> check_v3 = gpu_v3;
  45. thrust::host_vector<int> v3(PERF_N);
  46. std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
  47. bool ok = std::equal(check_v3.begin(), check_v3.end(), v3.begin());
  48. if(!ok){
  49. std::cerr << "ERROR: merged ranges different" << std::endl;
  50. return -1;
  51. }
  52. return 0;
  53. }