2 // Copyright (c) 2018 Stefan Seefeld
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or
6 // copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef opencl_benchmark_hpp_
9 #define opencl_benchmark_hpp_
10 #define BOOST_UBLAS_ENABLE_OPENCL
12 #include <boost/numeric/ublas/opencl.hpp>
13 #include "../benchmark.hpp"
17 namespace boost { namespace numeric { namespace ublas { namespace benchmark {
22 base(compute::device d = compute::system::default_device())
26 compute::context context;
27 compute::command_queue queue;
30 template <typename T, bool C> struct data_factory;
32 struct data_factory<T, true>
35 typedef std::unique_ptr<type> ptr_type;
36 static ptr_type create(long) { return ptr_type(new T());}
39 struct data_factory<T, false>
42 typedef std::unique_ptr<type> ptr_type;
43 static ptr_type create(long, compute::context) { return ptr_type(new T());}
46 struct data_factory<void, true>
49 typedef void *ptr_type;
50 static ptr_type create(long) { return 0;}
53 struct data_factory<void, false>
56 typedef void *ptr_type;
57 static ptr_type create(long, compute::context) { return 0;}
60 struct data_factory<ublas::vector<T>, true>
62 typedef ublas::vector<T> type;
63 typedef std::unique_ptr<type> ptr_type;
64 static ptr_type create(long l) { return ptr_type(new type(l));}
67 struct data_factory<ublas::vector<T>, false>
69 typedef ublas::vector<T, ublas::opencl::storage> type;
70 typedef std::unique_ptr<type> ptr_type;
71 static ptr_type create(long l, compute::context c)
72 { return ptr_type(new type(l, c));}
74 template <typename T, typename L>
75 struct data_factory<ublas::matrix<T, L>, true>
77 typedef ublas::matrix<T, L> type;
78 typedef std::unique_ptr<type> ptr_type;
79 static ptr_type create(long l)
80 { return ptr_type(new type(l, l));}
82 template <typename T, typename L>
83 struct data_factory<ublas::matrix<T, L>, false>
85 typedef ublas::matrix<T, L, ublas::opencl::storage> type;
86 typedef std::unique_ptr<type> ptr_type;
87 static ptr_type create(long l, compute::context c)
88 { return ptr_type(new type(l, l, c));}
91 template <typename S, bool> class benchmark;
93 template <typename R, typename O>
94 class benchmark<R(O), true> : public base, public ublas::benchmark::benchmark
97 benchmark(std::string const &name)
99 ublas::benchmark::benchmark(name + " with copy")
101 virtual void setup(long l)
103 r = data_factory<R, true>::create(l);
104 a = data_factory<O, true>::create(l);
107 typename data_factory<R, true>::ptr_type r;
108 typename data_factory<O, true>::ptr_type a;
111 template <typename R, typename O1, typename O2>
112 class benchmark<R(O1, O2), true> : public base, public ublas::benchmark::benchmark
115 benchmark(std::string const &name)
117 ublas::benchmark::benchmark(name + " with copy")
119 virtual void setup(long l)
121 r = data_factory<R, true>::create(l);
122 a = data_factory<O1, true>::create(l);
124 b = data_factory<O2, true>::create(l);
127 typename data_factory<R, true>::ptr_type r;
128 typename data_factory<O1, true>::ptr_type a;
129 typename data_factory<O2, true>::ptr_type b;
132 template <typename R, typename O1, typename O2, typename O3>
133 class benchmark<R(O1, O2, O3), true> : public base, public ublas::benchmark::benchmark
136 benchmark(std::string const &name)
138 ublas::benchmark::benchmark(name + " with copy")
140 virtual void setup(long l)
142 r = data_factory<R, true>::create(l);
143 a = data_factory<O1, true>::create(l);
145 b = data_factory<O2, true>::create(l);
147 c = data_factory<O3, true>::create(l);
150 typename data_factory<R, true>::ptr_type r;
151 typename data_factory<O1, true>::ptr_type a;
152 typename data_factory<O2, true>::ptr_type b;
153 typename data_factory<O3, true>::ptr_type c;
156 template <typename R, typename O>
157 class benchmark<R(O), false> : public base, public ublas::benchmark::benchmark
160 benchmark(std::string const &name)
162 ublas::benchmark::benchmark(name + " w/o copy")
164 virtual void setup(long l)
166 r = data_factory<R, false>::create(l, context);
167 a = data_factory<O, false>::create(l, context);
170 typename data_factory<R, false>::ptr_type r;
171 typename data_factory<O, false>::ptr_type a;
174 template <typename R, typename O1, typename O2>
175 class benchmark<R(O1, O2), false> : public base, public ublas::benchmark::benchmark
178 benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
179 virtual void setup(long l)
181 r = data_factory<R, false>::create(l, context);
182 a = data_factory<O1, false>::create(l, context);
184 b = data_factory<O2, false>::create(l, context);
187 typename data_factory<R, false>::ptr_type r;
188 typename data_factory<O1, false>::ptr_type a;
189 typename data_factory<O2, false>::ptr_type b;
192 template <typename R, typename O1, typename O2, typename O3>
193 class benchmark<R(O1, O2, O3), false> : public base, public ublas::benchmark::benchmark
196 benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
197 virtual void setup(long l)
199 r = data_factory<R, false>::create(l, context);
200 a = data_factory<O1, false>::create(l, context);
202 b = data_factory<O2, false>::create(l, context);
204 c = data_factory<O3, false>::create(l, context);
207 typename data_factory<R, false>::ptr_type r;
208 typename data_factory<O1, false>::ptr_type a;
209 typename data_factory<O2, false>::ptr_type b;
210 typename data_factory<O3, false>::ptr_type c;