1 /*
2  * Copyright (c) 2001 Matteo Frigo
3  * Copyright (c) 2001 Massachusetts Institute of Technology
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 
22 #include "libbench2/bench.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 
27 void (*report)(const bench_problem *p, double *t, int st);
28 
29 #undef min
30 #undef max /* you never know */
31 
32 struct stats {
33      double min;
34      double max;
35      double avg;
36      double median;
37 };
38 
mkstat(double * t,int st,struct stats * a)39 static void mkstat(double *t, int st, struct stats *a)
40 {
41      int i, j;
42 
43      a->min = t[0];
44      a->max = t[0];
45      a->avg = 0.0;
46 
47      for (i = 0; i < st; ++i) {
48 	  if (t[i] < a->min)
49 	       a->min = t[i];
50 	  if (t[i] > a->max)
51 	       a->max = t[i];
52 	  a->avg += t[i];
53      }
54      a->avg /= (double)st;
55 
56      /* compute median --- silly bubblesort algorithm */
57      for (i = st - 1; i > 1; --i) {
58 	  for (j = 0; j < i - 1; ++j) {
59 	       double t0, t1;
60 	       if ((t0 = t[j]) > (t1 = t[j + 1])) {
61 		    t[j] = t1;
62 		    t[j + 1] = t0;
63 	       }
64 	  }
65      }
66      a->median = t[st / 2];
67 }
68 
report_mflops(const bench_problem * p,double * t,int st)69 void report_mflops(const bench_problem *p, double *t, int st)
70 {
71      struct stats s;
72      mkstat(t, st, &s);
73      ovtpvt("(%g %g %g %g)\n",
74 	    mflops(p, s.max), mflops(p, s.avg),
75 	    mflops(p, s.min), mflops(p, s.median));
76 }
77 
report_time(const bench_problem * p,double * t,int st)78 void report_time(const bench_problem *p, double *t, int st)
79 {
80      struct stats s;
81      UNUSED(p);
82      mkstat(t, st, &s);
83      ovtpvt("(%g %g %g %g)\n", s.min, s.avg, s.max, s.median);
84 }
85 
report_benchmark(const bench_problem * p,double * t,int st)86 void report_benchmark(const bench_problem *p, double *t, int st)
87 {
88      struct stats s;
89      mkstat(t, st, &s);
90      ovtpvt("%.8g %.8g %g\n", mflops(p, s.min), s.min, p->setup_time);
91 }
92 
sprintf_time(double x,char * buf,int buflen)93 static void sprintf_time(double x, char *buf, int buflen)
94 {
95 #ifdef HAVE_SNPRINTF
96 #  define MY_SPRINTF(a, b) snprintf(buf, buflen, a, b)
97 #else
98 #  define MY_SPRINTF(a, b) sprintf(buf, a, b)
99 #endif
100      if (x < 1.0E-6)
101 	  MY_SPRINTF("%.2f ns", x * 1.0E9);
102      else if (x < 1.0E-3)
103 	  MY_SPRINTF("%.2f us", x * 1.0E6);
104      else if (x < 1.0)
105 	  MY_SPRINTF("%.2f ms", x * 1.0E3);
106      else
107 	  MY_SPRINTF("%.2f s", x);
108 #undef MY_SPRINTF
109 }
110 
report_verbose(const bench_problem * p,double * t,int st)111 void report_verbose(const bench_problem *p, double *t, int st)
112 {
113      struct stats s;
114      char bmin[64], bmax[64], bavg[64], bmedian[64], btmin[64];
115      char bsetup[64];
116      int copyp = tensor_sz(p->sz) == 1;
117 
118      mkstat(t, st, &s);
119 
120      sprintf_time(s.min, bmin, 64);
121      sprintf_time(s.max, bmax, 64);
122      sprintf_time(s.avg, bavg, 64);
123      sprintf_time(s.median, bmedian, 64);
124      sprintf_time(time_min, btmin, 64);
125      sprintf_time(p->setup_time, bsetup, 64);
126 
127      ovtpvt("Problem: %s, setup: %s, time: %s, %s: %.8g\n",
128 	    p->pstring, bsetup, bmin,
129 	    copyp ? "fp-move/us" : "``mflops''",
130 	    mflops(p, s.min));
131 
132      if (verbose) {
133 	  ovtpvt("Took %d measurements for at least %s each.\n", st, btmin);
134 	  ovtpvt("Time: min %s, max %s, avg %s, median %s\n",
135 		 bmin, bmax, bavg, bmedian);
136      }
137 }
138