summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/numeric/ublas/benchmarks/opencl/benchmark.hpp
blob: fb6075992319e0e35eac13b3d319bfd62286d8ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Copyright (c) 2018 Stefan Seefeld
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or
// copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef opencl_benchmark_hpp_
#define opencl_benchmark_hpp_
#define BOOST_UBLAS_ENABLE_OPENCL

#include <boost/numeric/ublas/opencl.hpp>
#include "../benchmark.hpp"
#include "init.hpp"
#include <memory>

namespace boost { namespace numeric { namespace ublas { namespace benchmark {
namespace opencl {

struct base
{
  base(compute::device d = compute::system::default_device())
    : context(d),
      queue(context, d)
  {}
  compute::context context;
  compute::command_queue queue;
};

template <typename T, bool C> struct data_factory;
template <typename T>
struct data_factory<T, true>
{
  typedef T type;
  typedef std::unique_ptr<type> ptr_type;
  static ptr_type create(long) { return ptr_type(new T());}
};
template <typename T>
struct data_factory<T, false>
{
  typedef T type;
  typedef std::unique_ptr<type> ptr_type;
  static ptr_type create(long, compute::context) { return ptr_type(new T());}
};
template <>
struct data_factory<void, true>
{
  typedef void type;
  typedef void *ptr_type;
  static ptr_type create(long) { return 0;}
};
template <>
struct data_factory<void, false>
{
  typedef void type;
  typedef void *ptr_type;
  static ptr_type create(long, compute::context) { return 0;}
};
template <typename T>
struct data_factory<ublas::vector<T>, true>
{
  typedef ublas::vector<T> type;
  typedef std::unique_ptr<type> ptr_type;
  static ptr_type create(long l) { return ptr_type(new type(l));}
};
template <typename T>
struct data_factory<ublas::vector<T>, false>
{
  typedef ublas::vector<T, ublas::opencl::storage> type;
  typedef std::unique_ptr<type> ptr_type;
  static ptr_type create(long l, compute::context c)
  { return ptr_type(new type(l, c));}
};
template <typename T, typename L>
struct data_factory<ublas::matrix<T, L>, true>
{
  typedef ublas::matrix<T, L> type;
  typedef std::unique_ptr<type> ptr_type;
  static ptr_type create(long l)
  { return ptr_type(new type(l, l));}
};
template <typename T, typename L>
struct data_factory<ublas::matrix<T, L>, false>
{
  typedef ublas::matrix<T, L, ublas::opencl::storage> type;
  typedef std::unique_ptr<type> ptr_type;
  static ptr_type create(long l, compute::context c)
  { return ptr_type(new type(l, l, c));}
};

template <typename S, bool> class benchmark;

template <typename R, typename O>
class benchmark<R(O), true> : public base, public ublas::benchmark::benchmark
{
public:
  benchmark(std::string const &name)
    : base(),
      ublas::benchmark::benchmark(name + " with copy")
  {}
  virtual void setup(long l)
  {
    r = data_factory<R, true>::create(l);
    a = data_factory<O, true>::create(l);
    init(*a, l, 200);
  }
  typename data_factory<R, true>::ptr_type r;
  typename data_factory<O, true>::ptr_type a;
};

template <typename R, typename O1, typename O2>
class benchmark<R(O1, O2), true> : public base, public ublas::benchmark::benchmark
{
public:
  benchmark(std::string const &name)
    : base(),
      ublas::benchmark::benchmark(name + " with copy")
  {}
  virtual void setup(long l)
  {
    r = data_factory<R, true>::create(l);
    a = data_factory<O1, true>::create(l);
    init(*a, l, 200);
    b = data_factory<O2, true>::create(l);
    init(*b, l, 200);
  }
  typename data_factory<R, true>::ptr_type r;
  typename data_factory<O1, true>::ptr_type a;
  typename data_factory<O2, true>::ptr_type b;
};

template <typename R, typename O1, typename O2, typename O3>
class benchmark<R(O1, O2, O3), true> : public base, public ublas::benchmark::benchmark
{
public:
  benchmark(std::string const &name)
    : base(),
      ublas::benchmark::benchmark(name + " with copy")
  {}
  virtual void setup(long l)
  {
    r = data_factory<R, true>::create(l);
    a = data_factory<O1, true>::create(l);
    init(*a, l, 200);
    b = data_factory<O2, true>::create(l);
    init(*b, l, 200);
    c = data_factory<O3, true>::create(l);
    init(*c, l, 200);
  }
  typename data_factory<R, true>::ptr_type r;
  typename data_factory<O1, true>::ptr_type a;
  typename data_factory<O2, true>::ptr_type b;
  typename data_factory<O3, true>::ptr_type c;
};

template <typename R, typename O>
class benchmark<R(O), false> : public base, public ublas::benchmark::benchmark
{
public:
  benchmark(std::string const &name)
    : base(),
      ublas::benchmark::benchmark(name + " w/o copy")
  {}
  virtual void setup(long l)
  {
    r = data_factory<R, false>::create(l, context);
    a = data_factory<O, false>::create(l, context);
    init(*a, l, 200);
  }
  typename data_factory<R, false>::ptr_type r;
  typename data_factory<O, false>::ptr_type a;
};

template <typename R, typename O1, typename O2>
class benchmark<R(O1, O2), false> : public base, public ublas::benchmark::benchmark
{
public:
  benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
  virtual void setup(long l)
  {
    r = data_factory<R, false>::create(l, context);
    a = data_factory<O1, false>::create(l, context);
    init(*a, l, 200);
    b = data_factory<O2, false>::create(l, context);
    init(*b, l, 200);
  }
  typename data_factory<R, false>::ptr_type r;
  typename data_factory<O1, false>::ptr_type a;
  typename data_factory<O2, false>::ptr_type b;
};

template <typename R, typename O1, typename O2, typename O3>
class benchmark<R(O1, O2, O3), false> : public base, public ublas::benchmark::benchmark
{
public:
  benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
  virtual void setup(long l)
  {
    r = data_factory<R, false>::create(l, context);
    a = data_factory<O1, false>::create(l, context);
    init(*a, l, 200);
    b = data_factory<O2, false>::create(l, context);
    init(*b, l, 200);
    c = data_factory<O3, false>::create(l, context);
    init(*c, l, 200);
  }
  typename data_factory<R, false>::ptr_type r;
  typename data_factory<O1, false>::ptr_type a;
  typename data_factory<O2, false>::ptr_type b;
  typename data_factory<O3, false>::ptr_type c;
};
}}}}}

#endif