XmtVirtualFunctionsAttachments: virtual_test.cpp

File virtual_test.cpp, 9.7 KB (added by gemacke, 6 years ago)
Line 
1/****************************************************************************/
2/*! \file virtual_test.cpp
3
4    \brief Tests the cost of using pointer indirection for objects and for
5           using virtual functions.
6
7    \author Greg Mackey (gemacke@sandia.gov)
8
9    \date 4/25/2012
10*/
11/****************************************************************************/
12
13#include <mtgl/util.hpp>
14
15using namespace mtgl;
16
17class Polygon {
18public:
19  Polygon() : height(0), width(0) {}
20
21  unsigned long height_times_width() const { return height * width; }
22
23  void set_dimensions(unsigned long h, unsigned long w)
24  {
25    height = h;
26    width = w;
27  }
28
29protected:
30  unsigned long height;
31  unsigned long width;
32};
33
34class Triangle : public Polygon {
35public:
36  unsigned long area() const { return height * width / 2; }
37
38  #pragma mta no inline
39  unsigned long no_inline_area() const { return height * width / 2; }
40};
41
42class Square : public Polygon {
43public:
44  unsigned long area() const { return height * width; }
45
46  #pragma mta no inline
47  unsigned long no_inline_area() const { return height * width; }
48};
49
50class VirtualPolygon {
51public:
52  VirtualPolygon() : height(0), width(0) {}
53
54  unsigned long height_times_width() const { return height * width; }
55
56  void set_dimensions(unsigned long h, unsigned long w)
57  {
58    height = h;
59    width = w;
60  }
61
62  virtual unsigned long area() const { return 0; }
63
64protected:
65  unsigned long height;
66  unsigned long width;
67};
68
69class VirtualTriangle : public VirtualPolygon {
70public:
71  unsigned long area() const { return height * width / 2; }
72};
73
74class VirtualSquare : public VirtualPolygon {
75public:
76  unsigned long area() const { return height * width; }
77};
78
79int main(int argc, char* argv[])
80{
81  if (argc < 2)
82  {
83    printf("Usage: %s <size>\n", argv[0]);
84    exit(1);
85  }
86
87  unsigned long size = strtoul(argv[1], NULL, 10);
88
89  mt_timer timer;
90
91  printf("Square Array:\n");
92
93#ifdef __MTA__
94  // Primes the pump on the XMT so that timings are correct.
95  int* temp = new int[size];
96
97  #pragma mta assert nodep
98  for (unsigned long i = 0; i < size; ++i) temp[i] = 0;
99
100  delete [] temp;
101#endif
102
103  #pragma mta fence
104  timer.start();
105
106  #pragma mta assert par_newdelete
107  Square* squares = new Square[size];
108
109  #pragma mta fence
110  timer.stop();
111
112  printf("                Square Init: %9.6lf\n", timer.getElapsedSeconds());
113
114  #pragma mta fence
115  timer.start();
116
117  #pragma mta assert nodep
118  for (unsigned long i = 0; i < size; ++i) squares[i].set_dimensions(2, 1);
119
120  #pragma mta fence
121  timer.stop();
122
123  printf("           set_dimensions(): %9.6lf\n", timer.getElapsedSeconds());
124
125  #pragma mta fence
126  timer.start();
127
128  unsigned long sum = 0;
129  #pragma mta assert nodep
130  for (unsigned long i = 0; i < size; ++i)
131  {
132    sum += squares[i].height_times_width();
133  }
134
135  #pragma mta fence
136  timer.stop();
137
138  printf("       height_times_width(): %9.6lf    %lu\n",
139         timer.getElapsedSeconds(), sum);
140
141  #pragma mta fence
142  timer.start();
143
144  sum = 0;
145  for (unsigned long i = 0; i < size; ++i)
146  {
147    sum += squares[i].area();
148  }
149
150  #pragma mta fence
151  timer.stop();
152
153  printf("                     area(): %9.6lf    %lu\n",
154         timer.getElapsedSeconds(), sum);
155
156  #pragma mta fence
157  timer.start();
158
159  #pragma mta assert par_newdelete
160  delete [] squares;
161
162  #pragma mta fence
163  timer.stop();
164
165  printf("              Square Delete: %9.6lf\n", timer.getElapsedSeconds());
166
167  printf("\nSquare Pointer Array:\n");
168
169  #pragma mta fence
170  timer.start();
171
172  Square** squares_ptr = new Square*[size];
173  #pragma mta assert nodep
174  for (unsigned long i = 0; i < size; ++i) squares_ptr[i] = new Square;
175
176  #pragma mta fence
177  timer.stop();
178
179  printf("                Square Init: %9.6lf\n", timer.getElapsedSeconds());
180
181  #pragma mta fence
182  timer.start();
183
184  #pragma mta assert nodep
185  for (unsigned long i = 0; i < size; ++i) squares_ptr[i]->set_dimensions(2, 1);
186
187  #pragma mta fence
188  timer.stop();
189
190  printf("           set_dimensions(): %9.6lf\n", timer.getElapsedSeconds());
191
192  #pragma mta fence
193  timer.start();
194
195  sum = 0;
196  #pragma mta assert nodep
197  for (unsigned long i = 0; i < size; ++i)
198  {
199    sum += squares_ptr[i]->height_times_width();
200  }
201
202  #pragma mta fence
203  timer.stop();
204
205  printf("       height_times_width(): %9.6lf    %lu\n",
206         timer.getElapsedSeconds(), sum);
207
208  #pragma mta fence
209  timer.start();
210
211  sum = 0;
212  for (unsigned long i = 0; i < size; ++i)
213  {
214    sum += squares_ptr[i]->area();
215  }
216
217  #pragma mta fence
218  timer.stop();
219
220  printf("                     area(): %9.6lf    %lu\n",
221         timer.getElapsedSeconds(), sum);
222
223  #pragma mta fence
224  timer.start();
225
226  for (unsigned long i = 0; i < size; ++i) delete squares_ptr[i];
227  delete [] squares_ptr;
228
229  #pragma mta fence
230  timer.stop();
231
232  printf("              Square Delete: %9.6lf\n", timer.getElapsedSeconds());
233
234  printf("\nPolygon Array Normal Inheritance:\n");
235
236  Polygon** polys = new Polygon*[size];
237
238  #pragma mta fence
239  timer.start();
240
241  #pragma mta assert nodep
242  for (unsigned long i = 0; i < size; i += 2) polys[i] = new Square;
243
244  #pragma mta fence
245  timer.stop();
246
247  printf("                Square Init: %9.6lf\n", timer.getElapsedSeconds());
248
249  #pragma mta fence
250  timer.start();
251
252  #pragma mta assert nodep
253  for (unsigned long i = 1; i < size; i += 2) polys[i] = new Triangle;
254
255  #pragma mta fence
256  timer.stop();
257
258  printf("              Triangle Init: %9.6lf\n", timer.getElapsedSeconds());
259
260  #pragma mta fence
261  timer.start();
262
263  #pragma mta assert nodep
264  for (unsigned long i = 0; i < size; ++i) polys[i]->set_dimensions(2, 1);
265
266  #pragma mta fence
267  timer.stop();
268
269  printf("           set_dimensions(): %9.6lf\n", timer.getElapsedSeconds());
270
271  #pragma mta fence
272  timer.start();
273
274  sum = 0;
275  #pragma mta assert nodep
276  for (unsigned long i = 0; i < size; ++i)
277  {
278    sum += polys[i]->height_times_width();
279  }
280
281  #pragma mta fence
282  timer.stop();
283
284  printf("       height_times_width(): %9.6lf    %lu\n",
285         timer.getElapsedSeconds(), sum);
286
287  #pragma mta fence
288  timer.start();
289
290  sum = 0;
291  #pragma mta assert nodep
292  for (unsigned long i = 0; i < size; i += 2)
293  {
294    sum += static_cast<Square*>(polys[i])->area();
295  }
296
297  #pragma mta fence
298  timer.stop();
299
300  printf("              Square area(): %9.6lf    %lu\n",
301         timer.getElapsedSeconds(), sum);
302
303  #pragma mta fence
304  timer.start();
305
306  sum = 0;
307  #pragma mta assert nodep
308  for (unsigned long i = 1; i < size; i += 2)
309  {
310    sum += static_cast<Triangle*>(polys[i])->area();
311  }
312
313  #pragma mta fence
314  timer.stop();
315
316  printf("            Triangle area(): %9.6lf    %lu\n",
317         timer.getElapsedSeconds(), sum);
318
319#ifdef __MTA__
320  #pragma mta fence
321  timer.start();
322
323  sum = 0;
324  #pragma mta assert parallel
325  for (unsigned long i = 0; i < size; i += 2)
326  {
327    sum += static_cast<Square*>(polys[i])->no_inline_area();
328  }
329
330  #pragma mta fence
331  timer.stop();
332
333  printf("    No Inline Square area(): %9.6lf    %lu\n",
334         timer.getElapsedSeconds(), sum);
335
336  #pragma mta fence
337  timer.start();
338
339  sum = 0;
340  #pragma mta assert parallel
341  for (unsigned long i = 1; i < size; i += 2)
342  {
343    sum += static_cast<Triangle*>(polys[i])->no_inline_area();
344  }
345
346  #pragma mta fence
347  timer.stop();
348
349  printf("  No Inline Triangle area(): %9.6lf    %lu\n",
350         timer.getElapsedSeconds(), sum);
351#endif
352
353  #pragma mta fence
354  timer.start();
355
356  for (unsigned long i = 0; i < size; ++i) delete polys[i];
357  delete [] polys;
358
359  #pragma mta fence
360  timer.stop();
361
362  printf("                Poly Delete: %9.6lf\n", timer.getElapsedSeconds());
363
364  printf("\nPolygon Array Virtual Inheritance:\n");
365
366  VirtualPolygon** vpolys = new VirtualPolygon*[size];
367
368  #pragma mta fence
369  timer.start();
370
371  #pragma mta assert nodep
372  for (unsigned long i = 0; i < size; i += 2) vpolys[i] = new VirtualSquare;
373
374  #pragma mta fence
375  timer.stop();
376
377  printf("                Square Init: %9.6lf\n", timer.getElapsedSeconds());
378
379  #pragma mta fence
380  timer.start();
381
382  #pragma mta assert nodep
383  for (unsigned long i = 1; i < size; i += 2) vpolys[i] = new VirtualTriangle;
384
385  #pragma mta fence
386  timer.stop();
387
388  printf("              Triangle Init: %9.6lf\n", timer.getElapsedSeconds());
389
390  #pragma mta fence
391  timer.start();
392
393  #pragma mta assert nodep
394  for (unsigned long i = 0; i < size; ++i) vpolys[i]->set_dimensions(2, 1);
395
396  #pragma mta fence
397  timer.stop();
398
399  printf("           set_dimensions(): %9.6lf\n", timer.getElapsedSeconds());
400
401  #pragma mta fence
402  timer.start();
403
404  sum = 0;
405  #pragma mta assert nodep
406  for (unsigned long i = 0; i < size; ++i)
407  {
408    sum += vpolys[i]->height_times_width();
409  }
410
411  #pragma mta fence
412  timer.stop();
413
414  printf("       height_times_width(): %9.6lf    %lu\n",
415         timer.getElapsedSeconds(), sum);
416
417  #pragma mta fence
418  timer.start();
419
420  sum = 0;
421  #pragma mta assert parallel
422  for (unsigned long i = 0; i < size; ++i)
423  {
424    sum += vpolys[i]->area();
425  }
426
427  #pragma mta fence
428  timer.stop();
429
430  printf("                     area(): %9.6lf    %lu\n",
431         timer.getElapsedSeconds(), sum);
432
433  #pragma mta fence
434  timer.start();
435
436  sum = 0;
437  #pragma mta assert parallel
438  for (unsigned long i = 0; i < size; i += 2)
439  {
440    sum += static_cast<VirtualSquare*>(vpolys[i])->area();
441  }
442
443  #pragma mta fence
444  timer.stop();
445
446  printf("              Square area(): %9.6lf    %lu\n",
447         timer.getElapsedSeconds(), sum);
448
449  #pragma mta fence
450  timer.start();
451
452  sum = 0;
453  #pragma mta assert parallel
454  for (unsigned long i = 1; i < size; i += 2)
455  {
456    sum += static_cast<VirtualTriangle*>(vpolys[i])->area();
457  }
458
459  #pragma mta fence
460  timer.stop();
461
462  printf("            Triangle area(): %9.6lf    %lu\n",
463         timer.getElapsedSeconds(), sum);
464
465  #pragma mta fence
466  timer.start();
467
468  for (unsigned long i = 0; i < size; ++i) delete vpolys[i];
469  delete [] vpolys;
470
471  #pragma mta fence
472  timer.stop();
473
474  printf("                Poly Delete: %9.6lf\n", timer.getElapsedSeconds());
475
476  return 0;
477}