• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Szuppe <j.szuppe@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 <algorithm>
13 #include <vector>
14 
15 #include <bolt/cl/copy.h>
16 #include <bolt/cl/device_vector.h>
17 #include <bolt/cl/transform.h>
18 
19 #include "perf.hpp"
20 
BOLT_FUNCTOR(saxpy_functor,struct saxpy_functor{ float _a; saxpy_functor(float a) : _a(a) {}; float operator() (const float &x, const float &y) const { return _a * x + y; }; };)21 BOLT_FUNCTOR(saxpy_functor,
22     struct saxpy_functor
23     {
24         float _a;
25         saxpy_functor(float a) : _a(a) {};
26 
27         float operator() (const float &x, const float &y) const
28         {
29             return _a * x + y;
30         };
31     };
32 )
33 
34 int main(int argc, char *argv[])
35 {
36     perf_parse_args(argc, argv);
37 
38     std::cout << "size: " << PERF_N << std::endl;
39 
40     bolt::cl::control ctrl = bolt::cl::control::getDefault();
41     ::cl::Device device = ctrl.getDevice();
42     std::cout << "device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
43 
44     // create host vectors
45     std::vector<float> host_x(PERF_N);
46     std::vector<float> host_y(PERF_N);
47     std::generate(host_x.begin(), host_x.end(), rand);
48     std::generate(host_y.begin(), host_y.end(), rand);
49 
50     // create device vectors
51     bolt::cl::device_vector<float> device_x(PERF_N);
52     bolt::cl::device_vector<float> device_y(PERF_N);
53 
54     // transfer data to the device
55     bolt::cl::copy(host_x.begin(), host_x.end(), device_x.begin());
56     bolt::cl::copy(host_y.begin(), host_y.end(), device_y.begin());
57 
58     perf_timer t;
59     for(size_t trial = 0; trial < PERF_TRIALS; trial++){
60         t.start();
61         bolt::cl::transform(
62             device_x.begin(), device_x.end(),
63             device_y.begin(),
64             device_y.begin(),
65             saxpy_functor(2.5f)
66         );
67         t.stop();
68     }
69     std::cout << "time: " << t.min_time() / 1e6 << " ms" << std::endl;
70 
71     // transfer data back to host
72     bolt::cl::copy(device_x.begin(), device_x.end(), host_x.begin());
73     bolt::cl::copy(device_y.begin(), device_y.end(), host_y.begin());
74 
75     return 0;
76 }
77