NeoPZ
benchtinyfad.cc
Go to the documentation of this file.
1 // Emacs will be in -*- Mode: c++ -*-
2 //
3 // ************ DO NOT REMOVE THIS BANNER ****************
4 //
5 // Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
6 // http://www.ann.jussieu.fr/~dicesare
7 //
8 // Tiny benchmark of TinyFad with and without
9 // exression template(ET)
10 //
11 //********************************************************
12 //
13 // KCC 3.3f (kai C++) and egcs 1.1.2 (cygnus)
14 // are OK.
15 // Memory Requirement :
16 // - 45 Mo with egcs
17 // (-Wall -O6 -mcpu=i686 -march=i686 -fstrict-aliasing)
18 // - 32 Mo with KCC
19 // (-w +K3 -O3)
20 //
21 //********************************************************
22 
23 // ANSI C++ include
24 #include <iostream>
25 #include <iomanip>
26 
27 // Fad includes
28 #include <TinyFadET/tfad.h>
29 #include <TinyFad/tinyfad.h>
30 
31 // Include for time computation
32 #include <utils/timer.h>
33 
34 
35 using namespace std;
36 
37 template <int Num> void Bench_4_Op(const TinyFad<Num> & x, const int nloop);
38 
39 
40 int main() {
41 
42  const int nloop = 1000000;//Increase or decrease
43 
44  TinyFad<1> un; TinyFad<2> deux; TinyFad<3> trois;
45  TinyFad<4> quatre; TinyFad<5> cinq; TinyFad<6> six;
46  TinyFad<7> sept; TinyFad<8> huit; TinyFad<9> neuf;TinyFad<10> dix;
47  TinyFad<11> onze; TinyFad<12> douze; TinyFad<13> treize;
48  TinyFad<14> quatorze; TinyFad<15> quinze; TinyFad<16> seize;
49  TinyFad<17> dixsept; TinyFad<18> dixhuit; TinyFad<19> dixneuf;TinyFad<20> vingt;
50 
51 
52  cout << "Clean Cache" << endl;
53  Bench_4_Op(neuf,nloop);
54 
55  cout << "_Size_____TFad______TinyFad_______Hand_______Member___\n" ;
56 
57  Bench_4_Op(un,nloop);
58  Bench_4_Op(deux,nloop);
59  Bench_4_Op(trois,nloop);
60  Bench_4_Op(quatre,nloop);
61  Bench_4_Op(cinq,nloop);
62  Bench_4_Op(six,nloop);
63  Bench_4_Op(sept,nloop);
64  Bench_4_Op(huit,nloop);
65  Bench_4_Op(neuf,nloop);
66  Bench_4_Op(dix,nloop);
67  Bench_4_Op(onze,nloop);
68  Bench_4_Op(douze,nloop);
69  Bench_4_Op(treize,nloop);
70  Bench_4_Op(quatorze,nloop);
71  Bench_4_Op(quinze,nloop);
72  Bench_4_Op(seize,nloop);
73  Bench_4_Op(dixsept,nloop);
74  Bench_4_Op(dixhuit,nloop);
75  Bench_4_Op(dixneuf,nloop);
76  Bench_4_Op(vingt,nloop);
77 
78  return 0;
79 }
80 
81 
82 
83 
84 
85 template <int Num> void Bench_4_Op(const TinyFad<Num> & x, const int nloop){
86  int i,j,k;
87  const int ntest = 3;
88  // int Num = x.size();
89 
90  cout.setf(ios::fixed,ios::floatfield);
91  cout.width(4);
92  cout << Num;
93  cout.flush();
94 
95  float time1 = 0., time2 = 0., time3 = 0., time4 = 0.;
96 
97 
98  TFad<Num> x1(2.f), x2(3.f), x3(5.f), x4(0.f), x5(3.f);
99  if (Num==1)
100  x1.diff(0,Num);
101  else {
102  x1.diff(0,Num);
103  x2.diff(1,Num);
104  }
105 
106  for (j=0; j<ntest; j++){
107 
108  Timer mytime;
109  mytime.start();
110 
111  for (i=0; i<nloop; ++i)
112  x4 = 2.f*(x1*x2)-x3+x3/x5+4.f;
113 
114  mytime.stop();
115  time1 += mytime.elapsedSeconds();
116 
117 
118  TinyFad<Num> y1(2.f), y2(3.f), y3(5.f), y4(0.f), y5(3.f);
119  if (Num==1)
120  y1.diff(0,Num);
121  else {
122  y1.diff(0,Num);
123  y2.diff(1,Num);
124  }
125 
126 
127  mytime.start();
128 
129  for (i=0; i<nloop; ++i)
130  y4 = 2.f*(y1*y2)-y3+y3/y5+4.f;
131 
132  mytime.stop();
133  time2 += mytime.elapsedSeconds();
134 
135 
136  float z1=2., z2=3., z3=5., z4, z5=3.;
137  float *tz1, *tz2, *tz3, *tz4, *tz5;
138  tz1 = new float[Num]; tz2 = new float[Num]; tz3 = new float[Num]; tz4 = new float[Num]; tz5 = new float[Num];
139  for (k=0; k<Num; ++k) {
140  tz5[k] = tz4[k] = tz3[k] = tz1[k] = tz2[k] = float(0.);
141  }
142  tz1[0]=1.;tz2[1]=1.;
143 
144  mytime.start();
145 
146  for (i=0; i<nloop; ++i) {
147  z4 = 2.f*(z1*z2)-z3+z3/z5+4.f;
148  for (k=0; k<Num; ++k)
149  tz4[k] = 2.f*(z2*tz1[k]+z1*tz2[k])-tz3[k]+ (z5*tz3[k] - z3*tz5[k])/(z5*z5);
150  }
151 
152  mytime.stop();
153  time3 += mytime.elapsedSeconds();
154 
155  delete [] tz1; delete [] tz2; delete [] tz3; delete [] tz4; delete [] tz5;
156 
157 
158  mytime.start();
159 
160  for (i=0; i<nloop; ++i) {
161  x4.val() = 2.f*(x1.val()*x2.val())-x3.val()+x3.val()/x5.val()+4.f;
162  for (k=0; k<Num; ++k)
163  x4.fastAccessDx(k) = 2.f*(x1.fastAccessDx(k)*x2.val()+x1.val()*x2.fastAccessDx(k)) - x3.fastAccessDx(k)
164  + (x5.val()*x3.fastAccessDx(k)-x3.val()*x5.fastAccessDx(k))/(x5.val()*x5.val());
165  }
166 
167  mytime.stop();
168  time4 += mytime.elapsedSeconds();
169 
170 
171  }
172 
173 
174  cout << setw(12) << time1 << setw(12) << time2 << setw(12) << time3 << setw(12) << time4 << endl;
175 }
Definition: timer.h:10
void stop()
Definition: timer.h:23
void Bench_4_Op(const TinyFad< Num > &x, const int nloop)
Definition: benchtinyfad.cc:85
void start()
Definition: timer.h:17
int main()
Definition: benchtinyfad.cc:40
double elapsedSeconds()
Definition: timer.h:29
T & fastAccessDx(int i)
Definition: tfad.h:94
f
Definition: test.py:287
Definition: tfad.h:64
clarg::argInt nloop("-l", "Number of loop iterations of the Subst_Backward/Subst_Forward", 1)
const T & val() const
Definition: tfad.h:89