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/functional.h>
17 #include <thrust/host_vector.h>
18 #include <thrust/transform.h>
19 
20 #include "perf.hpp"
21 
22 struct saxpy_functor : public thrust::binary_function<float,float,float>
23 {
24     const float a;
25 
saxpy_functorsaxpy_functor26     saxpy_functor(float _a) : a(_a) {}
27 
28     __host__ __device__
operator ()saxpy_functor29     float operator()(const float& x, const float& y) const
30     {
31         return a * x + y;
32     }
33 };
34 
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37     perf_parse_args(argc, argv);
38 
39     std::cout << "size: " << PERF_N << std::endl;
40     thrust::host_vector<int> host_x(PERF_N);
41     thrust::host_vector<int> host_y(PERF_N);
42     std::generate(host_x.begin(), host_x.end(), rand);
43     std::generate(host_y.begin(), host_y.end(), rand);
44 
45     // transfer data to the device
46     thrust::device_vector<int> device_x = host_x;
47     thrust::device_vector<int> device_y = host_y;
48 
49     perf_timer t;
50     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
51         t.start();
52         thrust::transform(device_x.begin(), device_x.end(), device_y.begin(), device_y.begin(), saxpy_functor(2.5f));
53         cudaDeviceSynchronize();
54         t.stop();
55     }
56     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
57 
58     // transfer data back to host
59     thrust::copy(device_x.begin(), device_x.end(), host_x.begin());
60     thrust::copy(device_y.begin(), device_y.end(), host_y.begin());
61 
62     return 0;
63 }
64