1
2 // loop1 generated by makeloops.py Thu Jun 30 16:44:56 2011
3
4 #include <blitz/vector2.h>
5 #include <blitz/array.h>
6 #include <random/uniform.h>
7 #include <blitz/benchext.h>
8
9 #ifdef BZ_HAVE_VALARRAY
10 #define BENCHMARK_VALARRAY
11 #endif
12
13 #ifdef BENCHMARK_VALARRAY
14 #include <valarray>
15 #endif
16
17 namespace blitz {
18 extern void sink();
19 }
20
21 using namespace blitz;
22 using namespace std;
23
24 #if defined(BZ_FORTRAN_SYMBOLS_WITH_TRAILING_UNDERSCORES)
25 #define loop1_f77 loop1_f77_
26 #define loop1_f77overhead loop1_f77overhead_
27 #define loop1_f90 loop1_f90_
28 #define loop1_f90overhead loop1_f90overhead_
29 #elif defined(BZ_FORTRAN_SYMBOLS_WITH_DOUBLE_TRAILING_UNDERSCORES)
30 #define loop1_f77 loop1_f77__
31 #define loop1_f77overhead loop1_f77overhead__
32 #define loop1_f90 loop1_f90__
33 #define loop1_f90overhead loop1_f90overhead__
34 #elif defined(BZ_FORTRAN_SYMBOLS_CAPS)
35 #define loop1_f77 LOOP1_F77
36 #define loop1_f77overhead LOOP1_F77OVERHEAD
37 #define loop1_f90 LOOP1_F90
38 #define loop1_f90overhead LOOP1_F90OVERHEAD
39 #endif
40
41 extern "C" {
42 void loop1_f77(const int& N, double* x, double* y);
43 void loop1_f77overhead(const int& N, double* x, double* y);
44 void loop1_f90(const int& N, double* x, double* y);
45 void loop1_f90overhead(const int& N, double* x, double* y);
46
47 }
48
49 void VectorVersion(BenchmarkExt<int>& bench);
50 void ArrayVersion(BenchmarkExt<int>& bench);
51 void ArrayVersion_unaligned(BenchmarkExt<int>& bench);
52 void ArrayVersion_misaligned(BenchmarkExt<int>& bench);
53 void ArrayVersion_index(BenchmarkExt<int>& bench);
54 void doTinyVectorVersion(BenchmarkExt<int>& bench);
55 void F77Version(BenchmarkExt<int>& bench);
56 #ifdef FORTRAN_90
57 void F90Version(BenchmarkExt<int>& bench);
58 #endif
59 #ifdef BENCHMARK_VALARRAY
60 void ValarrayVersion(BenchmarkExt<int>& bench);
61 #endif
62
63 const int numSizes = 80;
64 const bool runvector=false; // no point as long as Vector is Array<1>
65
main()66 int main()
67 {
68 int numBenchmarks = 5;
69 if (runvector) numBenchmarks++;
70 #ifdef BENCHMARK_VALARRAY
71 numBenchmarks++;
72 #endif
73 #ifdef FORTRAN_90
74 numBenchmarks++;
75 #endif
76
77 BenchmarkExt<int> bench("loop1: $x = sqrt($y)", numBenchmarks);
78
79 bench.setNumParameters(numSizes);
80
81 Array<int,1> parameters(numSizes);
82 Array<long,1> iters(numSizes);
83 Array<double,1> flops(numSizes);
84
85 parameters=pow(pow(2.,0.25),tensor::i)+tensor::i;
86 flops = 1 * parameters;
87 iters = 100000000L / flops;
88 iters = where(iters<2, 2, iters);
89 cout << iters << endl;
90
91 bench.setParameterVector(parameters);
92 bench.setIterations(iters);
93 bench.setOpsPerIteration(flops);
94 bench.setDependentVariable("flops");
95 bench.beginBenchmarking();
96
97
98
99 ArrayVersion(bench);
100 ArrayVersion_unaligned(bench);
101 ArrayVersion_misaligned(bench);
102 ArrayVersion_index(bench);
103 //doTinyVectorVersion(bench);
104 F77Version(bench);
105 #ifdef FORTRAN_90
106 F90Version(bench);
107 #endif
108 #ifdef BENCHMARK_VALARRAY
109 ValarrayVersion(bench);
110 #endif
111
112 if(runvector)
113 VectorVersion(bench);
114
115 bench.endBenchmarking();
116
117 bench.saveMatlabGraph("loop1.m");
118 return 0;
119 }
120
121 template<class T>
initializeRandomDouble(T * data,int numElements,int stride=1)122 void initializeRandomDouble(T* data, int numElements, int stride = 1)
123 {
124 ranlib::Uniform<T> rnd;
125
126 for (int i=0; i < numElements; ++i)
127 data[size_t(i*stride)] = rnd.random();
128 }
129
130 template<class T>
initializeRandomDouble(valarray<T> & data,int numElements,int stride=1)131 void initializeRandomDouble(valarray<T>& data, int numElements, int stride = 1)
132 {
133 ranlib::Uniform<T> rnd;
134
135 for (int i=0; i < numElements; ++i)
136 data[size_t(i*stride)] = rnd.random();
137 }
138
VectorVersion(BenchmarkExt<int> & bench)139 void VectorVersion(BenchmarkExt<int>& bench)
140 {
141 bench.beginImplementation("Vector<T>");
142
143 while (!bench.doneImplementationBenchmark())
144 {
145 int N = bench.getParameter();
146 long iters = bench.getIterations();
147
148 cout << bench.currentImplementation() << ": N = " << N << endl;
149
150 Vector<double> x(N);
151 initializeRandomDouble(x.data(), N);
152 Vector<double> y(N);
153 initializeRandomDouble(y.data(), N);
154
155
156 bench.start();
157 for (long i=0; i < iters; ++i)
158 {
159 x = sqrt(y);
160 sink();
161 }
162 bench.stop();
163
164 bench.startOverhead();
165 for (long i=0; i < iters; ++i) {
166 sink();
167 }
168
169 bench.stopOverhead();
170 }
171
172 bench.endImplementation();
173 }
174
175
ArrayVersion(BenchmarkExt<int> & bench)176 void ArrayVersion(BenchmarkExt<int>& bench)
177 {
178 bench.beginImplementation("Array<T,1>");
179
180 while (!bench.doneImplementationBenchmark())
181 {
182 int N = bench.getParameter();
183 long iters = bench.getIterations();
184
185 cout << bench.currentImplementation() << ": N = " << N << endl;
186
187 Array<double,1> x(N);
188 initializeRandomDouble(x.dataFirst(), N);
189 Array<double,1> y(N);
190 initializeRandomDouble(y.dataFirst(), N);
191
192
193 bench.start();
194 for (long i=0; i < iters; ++i)
195 {
196 x = sqrt(y);
197 sink();
198 }
199 bench.stop();
200
201 bench.startOverhead();
202 for (long i=0; i < iters; ++i) {
203 sink();
204 }
205
206 bench.stopOverhead();
207 }
208
209 bench.endImplementation();
210 }
211
212
ArrayVersion_index(BenchmarkExt<int> & bench)213 void ArrayVersion_index(BenchmarkExt<int>& bench)
214 {
215 bench.beginImplementation("Array<T,1> (indexexpr.)");
216
217 while (!bench.doneImplementationBenchmark())
218 {
219 int N = bench.getParameter();
220 long iters = bench.getIterations();
221
222 cout << bench.currentImplementation() << ": N = " << N << endl;
223
224 Array<double,1> x(N);
225 initializeRandomDouble(x.dataFirst(), N);
226 Array<double,1> y(N);
227 initializeRandomDouble(y.dataFirst(), N);
228
229
230 bench.start();
231 for (long i=0; i < iters; ++i)
232 {
233 x = sqrt(y(tensor::i));;
234 sink();
235 }
236 bench.stop();
237
238 bench.startOverhead();
239 for (long i=0; i < iters; ++i) {
240 sink();
241 }
242
243 bench.stopOverhead();
244 }
245
246 bench.endImplementation();
247 }
248
ArrayVersion_unaligned(BenchmarkExt<int> & bench)249 void ArrayVersion_unaligned(BenchmarkExt<int>& bench)
250 {
251 bench.beginImplementation("Array<T,1> (unal.)");
252
253 while (!bench.doneImplementationBenchmark())
254 {
255 int N = bench.getParameter();
256 long iters = bench.getIterations();
257
258 cout << bench.currentImplementation() << ": N = " << N << endl;
259
260
261 Array<double,1> xfill(N+1);
262 Array<double,1> x(xfill(Range(1,N)));
263 initializeRandomDouble(x.dataFirst(), N);
264
265 Array<double,1> yfill(N+1);
266 Array<double,1> y(yfill(Range(1,N)));
267 initializeRandomDouble(y.dataFirst(), N);
268
269
270 bench.start();
271 for (long i=0; i < iters; ++i)
272 {
273 x = sqrt(y);
274 sink();
275 }
276 bench.stop();
277
278 bench.startOverhead();
279 for (long i=0; i < iters; ++i) {
280 sink();
281 }
282
283 bench.stopOverhead();
284 }
285
286 bench.endImplementation();
287 }
288
ArrayVersion_misaligned(BenchmarkExt<int> & bench)289 void ArrayVersion_misaligned(BenchmarkExt<int>& bench)
290 {
291 bench.beginImplementation("Array<T,1> (misal.)");
292
293 while (!bench.doneImplementationBenchmark())
294 {
295 int N = bench.getParameter();
296 long iters = bench.getIterations();
297
298 cout << bench.currentImplementation() << ": N = " << N << endl;
299
300
301 Array<double,1> xfill(N+2);
302 Array<double,1> x(xfill(Range(0,N+0-1)));
303 initializeRandomDouble(x.dataFirst(), N);
304
305 Array<double,1> yfill(N+2);
306 Array<double,1> y(yfill(Range(1,N+1-1)));
307 initializeRandomDouble(y.dataFirst(), N);
308
309
310 bench.start();
311 for (long i=0; i < iters; ++i)
312 {
313 x = sqrt(y);
314 sink();
315 }
316 bench.stop();
317
318 bench.startOverhead();
319 for (long i=0; i < iters; ++i) {
320 sink();
321 }
322
323 bench.stopOverhead();
324 }
325
326 bench.endImplementation();
327 }
328
329 #ifdef BENCHMARK_VALARRAY
ValarrayVersion(BenchmarkExt<int> & bench)330 void ValarrayVersion(BenchmarkExt<int>& bench)
331 {
332 bench.beginImplementation("valarray<T>");
333
334 while (!bench.doneImplementationBenchmark())
335 {
336 int N = bench.getParameter();
337 cout << bench.currentImplementation() << ": N = " << N << endl;
338
339 long iters = bench.getIterations();
340
341 valarray<double> x(N);
342 initializeRandomDouble(x, N);
343 valarray<double> y(N);
344 initializeRandomDouble(y, N);
345
346
347 bench.start();
348 for (long i=0; i < iters; ++i)
349 {
350 x = sqrt(y);
351 sink();
352 }
353 bench.stop();
354
355 bench.startOverhead();
356 for (long i=0; i < iters; ++i) {
357 sink();
358 }
359 bench.stopOverhead();
360 }
361
362 bench.endImplementation();
363 }
364 #endif
365
F77Version(BenchmarkExt<int> & bench)366 void F77Version(BenchmarkExt<int>& bench)
367 {
368 bench.beginImplementation("Fortran 77");
369
370 while (!bench.doneImplementationBenchmark())
371 {
372 int N = bench.getParameter();
373 cout << bench.currentImplementation() << ": N = " << N << endl;
374
375 int iters = bench.getIterations();
376
377 double* x = new double[N];
378 initializeRandomDouble(x, N);
379 double* y = new double[N];
380 initializeRandomDouble(y, N);
381
382
383 bench.start();
384 for (int iter=0; iter < iters; ++iter)
385 loop1_f77(N, x, y);
386 bench.stop();
387
388 bench.startOverhead();
389 for (int iter=0; iter < iters; ++iter)
390 loop1_f77overhead(N, x, y);
391
392 bench.stopOverhead();
393
394 delete [] x;
395 delete [] y;
396
397 }
398
399 bench.endImplementation();
400 }
401
402 #ifdef FORTRAN_90
F90Version(BenchmarkExt<int> & bench)403 void F90Version(BenchmarkExt<int>& bench)
404 {
405 bench.beginImplementation("Fortran 90");
406
407 while (!bench.doneImplementationBenchmark())
408 {
409 int N = bench.getParameter();
410 cout << bench.currentImplementation() << ": N = " << N << endl;
411
412 int iters = bench.getIterations();
413
414 double* x = new double[N];
415 initializeRandomDouble(x, N);
416 double* y = new double[N];
417 initializeRandomDouble(y, N);
418
419
420 bench.start();
421 for (int iter=0; iter < iters; ++iter)
422 loop1_f90(N, x, y);
423 bench.stop();
424
425 bench.startOverhead();
426 for (int iter=0; iter < iters; ++iter)
427 loop1_f90overhead(N, x, y);
428
429 bench.stopOverhead();
430 delete [] x;
431 delete [] y;
432
433 }
434
435 bench.endImplementation();
436 }
437 #endif
438
439