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