summaryrefslogtreecommitdiffstats
path: root/bench.cc
blob: 4c58911390d8a41e455d0904ca2c49a93ffd3ba6 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// -*-C++-*-

#define VML_NODEBUG

#include "vecmathlib.h"

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>

#include <sys/time.h>
#include <time.h>

using namespace std;
using namespace vecmathlib;



#ifndef __has_builtin
#  define __has_builtin(x) 0 // Compatibility with non-clang compilers
#endif



typedef unsigned long long ticks;
inline ticks getticks()
{
#if __has_builtin(__builtin_readcyclecounter)
  return __builtin_readcyclecounter();
#elif defined __x86_64__
  ticks a, d;
  asm volatile("rdtsc" : "=a" (a), "=d" (d));
  return a | (d << 32);
#elif defined __powerpc__
  unsigned int tbl, tbu, tbu1;
  do {
    asm volatile("mftbu %0": "=r"(tbu));
    asm volatile("mftb %0": "=r"(tbl));
    asm volatile("mftbu %0": "=r"(tbu1));
  } while (tbu != tbu1);
  return ((unsigned long long)tbu << 32) | tbl;
#else
  timeval tv;
  gettimeofday(&tv, NULL);
  return 1000000ULL * tv.tv_sec + tv.tv_usec;
  // timespec ts;
  // clock_gettime(CLOCK_REALTIME, &ts);
  // return 1000000000ULL * ts.tv_sec + ts.tv_nsec;
#endif
}
inline double elapsed(ticks t1, ticks t0)
{
  return t1-t0;
}

double get_sys_time()
{
  timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + 1.0e-6 * tv.tv_usec;
  // timespec ts;
  // clock_gettime(CLOCK_REALTIME, &ts);
  // return ts.tv_sec + 1.0e-9 * ts.tv_nsec;
}

double measure_tick()
{
  ticks const rstart = getticks();
  double const wstart = get_sys_time();
  while (get_sys_time() - wstart < 0.1) {
    // do nothing, just wait
  }
  ticks const rend = getticks();
  double const wend = get_sys_time();
  assert(wend-wstart >= 0.09);
  return (wend - wstart) / elapsed(rend, rstart);
}



double global_result = 0.0;
template<typename realvec_t>
void save_result(realvec_t result)
{
  for (int i=0; i<realvec_t::size; ++i) {
    global_result += result[i];
  }
}



template<typename T> inline T nop(T x) { return x; }

#define DECLARE_FUNCTOR(FUNC, XMIN, XMAX)                       \
  template<typename T>                                          \
  struct functor_##FUNC {                                       \
    static typename T::real_t get_xmin() { return XMIN; }       \
    static typename T::real_t get_xmax() { return XMAX; }       \
    static const char* name() { return #FUNC; }                 \
    T operator()(T x) { return FUNC(x); }                       \
  }

DECLARE_FUNCTOR(nop, 0.0, 1.0);

DECLARE_FUNCTOR(acos, -0.5, +0.5);
DECLARE_FUNCTOR(acosh, 0.0, 1.0);
DECLARE_FUNCTOR(asin, -0.5, +0.5);
DECLARE_FUNCTOR(asinh, -1.0, +1.0);
DECLARE_FUNCTOR(atan, -1.0, +1.0);
// DECLARE_FUNCTOR(atan2, 0.0);
DECLARE_FUNCTOR(atanh, -0.5, +0.5);
DECLARE_FUNCTOR(cbrt, -1.0, 1.0);
DECLARE_FUNCTOR(ceil, -1.0, +1.0);
// DECLARE_FUNCTOR(copysign, 0.0);
DECLARE_FUNCTOR(cos, 0.0, 1.0);
DECLARE_FUNCTOR(cosh, 0.0, 1.0);
DECLARE_FUNCTOR(exp, 0.0, 1.0);
DECLARE_FUNCTOR(exp10, 0.0, 1.0);
DECLARE_FUNCTOR(exp2, 0.0, 1.0);
DECLARE_FUNCTOR(expm1, 0.0, 1.0);
DECLARE_FUNCTOR(fabs, -1.0, 1.0);
DECLARE_FUNCTOR(floor, -1.0, +1.0);
// DECLARE_FUNCTOR(fdim, 0.0);
// DECLARE_FUNCTOR(fma, 0.0);
// DECLARE_FUNCTOR(fmax, 0.0);
// DECLARE_FUNCTOR(fmin, 0.0);
// DECLARE_FUNCTOR(fmod, 0.0);
// DECLARE_FUNCTOR(frexp, 0.0);
// DECLARE_FUNCTOR(hypot, 0.0);
// DECLARE_FUNCTOR(ilogb, 0.0);
// DECLARE_FUNCTOR(isfinite, 0.0);
// DECLARE_FUNCTOR(isinf, 0.0);
// DECLARE_FUNCTOR(isnan, 0.0);
// DECLARE_FUNCTOR(isnormal, 0.0);
// DECLARE_FUNCTOR(ldexp, 0.0);
// DECLARE_FUNCTOR(ldexp, 0.0);
DECLARE_FUNCTOR(log, 1.0, 2.0);
DECLARE_FUNCTOR(log10, 1.0, 2.0);
DECLARE_FUNCTOR(log1p, 0.0, 1.0);
DECLARE_FUNCTOR(log2, 1.0, 2.0);
// DECLARE_FUNCTOR(nextafter, 0.0);
// DECLARE_FUNCTOR(pow, 0.0);
DECLARE_FUNCTOR(rcp, 1.0, 2.0);
// DECLARE_FUNCTOR(remainder, 0.0);
DECLARE_FUNCTOR(rint, -1.0, +1.0);
DECLARE_FUNCTOR(round, -1.0, +1.0);
DECLARE_FUNCTOR(rsqrt, 0.0, 1.0);
// DECLARE_FUNCTOR(signbit, 0.0);
DECLARE_FUNCTOR(sin, 0.0, 1.0);
DECLARE_FUNCTOR(sinh, -1.0, +1.0);
DECLARE_FUNCTOR(sqrt, 0.0, 1.0);
DECLARE_FUNCTOR(tan, 0.0, 1.0);
DECLARE_FUNCTOR(tanh, -1.0, +1.0);
DECLARE_FUNCTOR(trunc, -1.0, +1.0);



template<typename realvec_t, template<typename> class func_t>
double run_bench()
{
  const int numiters = 10000000;
  
  typedef typename realvec_t::real_t real_t;
  const real_t xmin = func_t<realvec_t>::get_xmin();
  const real_t xmax = func_t<realvec_t>::get_xmax();
  realvec_t x0, dx;
  for (int i=0; i<realvec_t::size; ++i) {
    x0.set_elt(i, xmin + (xmax - xmin) / numiters * i / realvec_t::size);
    dx.set_elt(i, (xmax - xmin) / numiters);
  }
  realvec_t x, y;
  ticks t0, t1;
  double const cycles_per_tick = 1.0; // measure_tick();
  
  func_t<realvec_t> func;
  t0 = getticks();
  x = y = x0;
  for (int n=0; n<numiters; ++n) {
    y += func(x);
    x += dx;
  }
  t1 = getticks();
  save_result(y);
  
  return cycles_per_tick * elapsed(t1,t0) * realvec_t::size / numiters;
}

template<typename realvec_t, template<typename> class func_t>
void bench_type_func()
{
  cout << "   "
       << setw(-5) << func_t<realvec_t>::name() << " "
       << setw(18) << realvec_t::name() << ": " << flush;
  double const cycles = run_bench<realvec_t, func_t>();
  cout << cycles << " cycles\n" << flush;
}

template<template<typename> class func_t>
void bench_func()
{
  cout << "\n"
       << "Benchmarking " << func_t<float_vec>().name() << ":\n";
  
//   bench_type_func<realpseudovec<float,1>, func_t>();
//   // bench_type_func<realbuiltinvec<float,1>, func_t>();
//   bench_type_func<realtestvec<float,1>, func_t>();
// #ifdef VECMATHLIB_HAVE_VEC_FLOAT_1
//   bench_type_func<realvec<float,1>, func_t>();
// #endif
// #ifdef VECMATHLIB_HAVE_VEC_FLOAT_2
//   bench_type_func<realpseudovec<float,2>, func_t>();
//   // bench_type_func<realbuiltinvec<float,2>, func_t>();
//   // bench_type_func<realtestvec<float,2>, func_t>();
//   bench_type_func<realvec<float,2>, func_t>();
// #endif
// #ifdef VECMATHLIB_HAVE_VEC_FLOAT_4
//   bench_type_func<realpseudovec<float,4>, func_t>();
//   // bench_type_func<realbuiltinvec<float,4>, func_t>();
//   // bench_type_func<realtestvec<float,4>, func_t>();
//   bench_type_func<realvec<float,4>, func_t>();
// #endif
// #ifdef VECMATHLIB_HAVE_VEC_FLOAT_8
//   bench_type_func<realpseudovec<float,8>, func_t>();
//   // bench_type_func<realbuiltinvec<float,8>, func_t>();
//   // bench_type_func<realtestvec<float,8>, func_t>();
//   bench_type_func<realvec<float,8>, func_t>();
// #endif
  
//   bench_type_func<realpseudovec<double,1>, func_t>();
//   // bench_type_func<realbuiltinvec<double,1>, func_t>();
//   bench_type_func<realtestvec<double,1>, func_t>();
// #ifdef VECMATHLIB_HAVE_VEC_DOUBLE_1
//   bench_type_func<realvec<double,1>, func_t>();
// #endif
// #ifdef VECMATHLIB_HAVE_VEC_DOUBLE_2
//   bench_type_func<realpseudovec<double,2>, func_t>();
//   // bench_type_func<realbuiltinvec<double,2>, func_t>();
//   // bench_type_func<realtestvec<double,2>, func_t>();
//   bench_type_func<realvec<double,2>, func_t>();
// #endif
#ifdef VECMATHLIB_HAVE_VEC_DOUBLE_4
  bench_type_func<realpseudovec<double,4>, func_t>();
  // bench_type_func<realbuiltinvec<double,4>, func_t>();
  // bench_type_func<realtestvec<double,4>, func_t>();
  bench_type_func<realvec<double,4>, func_t>();
#endif
}

void bench()
{
  bench_func<functor_nop>();
  
  bench_func<functor_acos>();
  bench_func<functor_acosh>();
  bench_func<functor_asin>();
  bench_func<functor_asinh>();
  bench_func<functor_atan>();
  // bench_func<functor_atan2>();
  bench_func<functor_atanh>();
  bench_func<functor_cbrt>();
  bench_func<functor_ceil>();
  // bench_func<functor_copysign>();
  bench_func<functor_cos>();
  bench_func<functor_cosh>();
  bench_func<functor_exp>();
  bench_func<functor_exp10>();
  bench_func<functor_exp2>();
  bench_func<functor_expm1>();
  bench_func<functor_fabs>();
  bench_func<functor_floor>();
  // bench_func<functor_fdim>();
  // bench_func<functor_fma>();
  // bench_func<functor_fmax>();
  // bench_func<functor_fmin>();
  // bench_func<functor_fmod>();
  // bench_func<functor_frexp>();
  // bench_func<functor_hypot>();
  // bench_func<functor_ilogb>();
  // bench_func<functor_isfinite>();
  // bench_func<functor_isinf>();
  // bench_func<functor_isnan>();
  // bench_func<functor_isnormal>();
  // bench_func<functor_ldexp>();
  // bench_func<functor_ldexp>();
  bench_func<functor_log>();
  bench_func<functor_log10>();
  bench_func<functor_log1p>();
  bench_func<functor_log2>();
  // bench_func<functor_nextafter>();
  // bench_func<functor_pow>();
  bench_func<functor_rcp>();
  // bench_func<functor_remainder>();
  bench_func<functor_rint>();
  bench_func<functor_round>();
  bench_func<functor_rsqrt>();
  // bench_func<functor_signbit>();
  bench_func<functor_sin>();
  bench_func<functor_sinh>();
  bench_func<functor_sqrt>();
  bench_func<functor_tan>();
  bench_func<functor_tanh>();
  bench_func<functor_trunc>();
}



int main(int argc, char** argv)
{
  using namespace vecmathlib;

  cout << "Benchmarking math functions:\n";
  
  bench();
  
  // Checking global accumulator to prevent optimisation
  if (! std::isfinite(global_result)) {
    cout << "\n"
         << "WARNING: Global accumulator is not finite\n";
  }
  
  return 0;
}
OpenPOWER on IntegriCloud