• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2018 Stefan Seefeld
3 //
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)
7 
8 #ifndef opencl_benchmark_hpp_
9 #define opencl_benchmark_hpp_
10 #define BOOST_UBLAS_ENABLE_OPENCL
11 
12 #include <boost/numeric/ublas/opencl.hpp>
13 #include "../benchmark.hpp"
14 #include "init.hpp"
15 #include <memory>
16 
17 namespace boost { namespace numeric { namespace ublas { namespace benchmark {
18 namespace opencl {
19 
20 struct base
21 {
baseboost::numeric::ublas::benchmark::opencl::base22   base(compute::device d = compute::system::default_device())
23     : context(d),
24       queue(context, d)
25   {}
26   compute::context context;
27   compute::command_queue queue;
28 };
29 
30 template <typename T, bool C> struct data_factory;
31 template <typename T>
32 struct data_factory<T, true>
33 {
34   typedef T type;
35   typedef std::unique_ptr<type> ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory36   static ptr_type create(long) { return ptr_type(new T());}
37 };
38 template <typename T>
39 struct data_factory<T, false>
40 {
41   typedef T type;
42   typedef std::unique_ptr<type> ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory43   static ptr_type create(long, compute::context) { return ptr_type(new T());}
44 };
45 template <>
46 struct data_factory<void, true>
47 {
48   typedef void type;
49   typedef void *ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory50   static ptr_type create(long) { return 0;}
51 };
52 template <>
53 struct data_factory<void, false>
54 {
55   typedef void type;
56   typedef void *ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory57   static ptr_type create(long, compute::context) { return 0;}
58 };
59 template <typename T>
60 struct data_factory<ublas::vector<T>, true>
61 {
62   typedef ublas::vector<T> type;
63   typedef std::unique_ptr<type> ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory64   static ptr_type create(long l) { return ptr_type(new type(l));}
65 };
66 template <typename T>
67 struct data_factory<ublas::vector<T>, false>
68 {
69   typedef ublas::vector<T, ublas::opencl::storage> type;
70   typedef std::unique_ptr<type> ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory71   static ptr_type create(long l, compute::context c)
72   { return ptr_type(new type(l, c));}
73 };
74 template <typename T, typename L>
75 struct data_factory<ublas::matrix<T, L>, true>
76 {
77   typedef ublas::matrix<T, L> type;
78   typedef std::unique_ptr<type> ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory79   static ptr_type create(long l)
80   { return ptr_type(new type(l, l));}
81 };
82 template <typename T, typename L>
83 struct data_factory<ublas::matrix<T, L>, false>
84 {
85   typedef ublas::matrix<T, L, ublas::opencl::storage> type;
86   typedef std::unique_ptr<type> ptr_type;
createboost::numeric::ublas::benchmark::opencl::data_factory87   static ptr_type create(long l, compute::context c)
88   { return ptr_type(new type(l, l, c));}
89 };
90 
91 template <typename S, bool> class benchmark;
92 
93 template <typename R, typename O>
94 class benchmark<R(O), true> : public base, public ublas::benchmark::benchmark
95 {
96 public:
benchmark(std::string const & name)97   benchmark(std::string const &name)
98     : base(),
99       ublas::benchmark::benchmark(name + " with copy")
100   {}
setup(long l)101   virtual void setup(long l)
102   {
103     r = data_factory<R, true>::create(l);
104     a = data_factory<O, true>::create(l);
105     init(*a, l, 200);
106   }
107   typename data_factory<R, true>::ptr_type r;
108   typename data_factory<O, true>::ptr_type a;
109 };
110 
111 template <typename R, typename O1, typename O2>
112 class benchmark<R(O1, O2), true> : public base, public ublas::benchmark::benchmark
113 {
114 public:
benchmark(std::string const & name)115   benchmark(std::string const &name)
116     : base(),
117       ublas::benchmark::benchmark(name + " with copy")
118   {}
setup(long l)119   virtual void setup(long l)
120   {
121     r = data_factory<R, true>::create(l);
122     a = data_factory<O1, true>::create(l);
123     init(*a, l, 200);
124     b = data_factory<O2, true>::create(l);
125     init(*b, l, 200);
126   }
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;
130 };
131 
132 template <typename R, typename O1, typename O2, typename O3>
133 class benchmark<R(O1, O2, O3), true> : public base, public ublas::benchmark::benchmark
134 {
135 public:
benchmark(std::string const & name)136   benchmark(std::string const &name)
137     : base(),
138       ublas::benchmark::benchmark(name + " with copy")
139   {}
setup(long l)140   virtual void setup(long l)
141   {
142     r = data_factory<R, true>::create(l);
143     a = data_factory<O1, true>::create(l);
144     init(*a, l, 200);
145     b = data_factory<O2, true>::create(l);
146     init(*b, l, 200);
147     c = data_factory<O3, true>::create(l);
148     init(*c, l, 200);
149   }
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;
154 };
155 
156 template <typename R, typename O>
157 class benchmark<R(O), false> : public base, public ublas::benchmark::benchmark
158 {
159 public:
benchmark(std::string const & name)160   benchmark(std::string const &name)
161     : base(),
162       ublas::benchmark::benchmark(name + " w/o copy")
163   {}
setup(long l)164   virtual void setup(long l)
165   {
166     r = data_factory<R, false>::create(l, context);
167     a = data_factory<O, false>::create(l, context);
168     init(*a, l, 200);
169   }
170   typename data_factory<R, false>::ptr_type r;
171   typename data_factory<O, false>::ptr_type a;
172 };
173 
174 template <typename R, typename O1, typename O2>
175 class benchmark<R(O1, O2), false> : public base, public ublas::benchmark::benchmark
176 {
177 public:
benchmark(std::string const & name)178   benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
setup(long l)179   virtual void setup(long l)
180   {
181     r = data_factory<R, false>::create(l, context);
182     a = data_factory<O1, false>::create(l, context);
183     init(*a, l, 200);
184     b = data_factory<O2, false>::create(l, context);
185     init(*b, l, 200);
186   }
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;
190 };
191 
192 template <typename R, typename O1, typename O2, typename O3>
193 class benchmark<R(O1, O2, O3), false> : public base, public ublas::benchmark::benchmark
194 {
195 public:
benchmark(std::string const & name)196   benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
setup(long l)197   virtual void setup(long l)
198   {
199     r = data_factory<R, false>::create(l, context);
200     a = data_factory<O1, false>::create(l, context);
201     init(*a, l, 200);
202     b = data_factory<O2, false>::create(l, context);
203     init(*b, l, 200);
204     c = data_factory<O3, false>::create(l, context);
205     init(*c, l, 200);
206   }
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;
211 };
212 }}}}}
213 
214 #endif
215