1 //=====================================================
2 // File   :  bench.hh
3 // Author :  L. Plagne <laurent.plagne@edf.fr)>
4 // Copyright (C) EDF R&D,  lun sep 30 14:23:16 CEST 2002
5 //=====================================================
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 //
20 #ifndef BENCH_HH
21 #define BENCH_HH
22 
23 #include "btl.hh"
24 #include "bench_parameter.hh"
25 #include <iostream>
26 #include "utilities.h"
27 #include "size_lin_log.hh"
28 #include "xy_file.hh"
29 #include <vector>
30 #include <string>
31 #include "timers/portable_perf_analyzer.hh"
32 // #include "timers/mixed_perf_analyzer.hh"
33 // #include "timers/x86_perf_analyzer.hh"
34 // #include "timers/STL_perf_analyzer.hh"
35 #ifdef HAVE_MKL
36 extern "C" void cblas_saxpy(const int, const float, const float*, const int, float *, const int);
37 #endif
38 using namespace std;
39 
40 template <template<class> class Perf_Analyzer, class Action>
bench(int size_min,int size_max,int nb_point)41 BTL_DONT_INLINE void bench( int size_min, int size_max, int nb_point )
42 {
43   if (BtlConfig::skipAction(Action::name()))
44     return;
45 
46   string filename="bench_"+Action::name()+".dat";
47 
48   INFOS("starting " <<filename);
49 
50   // utilities
51 
52   std::vector<double> tab_mflops(nb_point);
53   std::vector<int> tab_sizes(nb_point);
54 
55   // matrices and vector size calculations
56   size_lin_log(nb_point,size_min,size_max,tab_sizes);
57 
58   std::vector<int> oldSizes;
59   std::vector<double> oldFlops;
60   bool hasOldResults = read_xy_file(filename, oldSizes, oldFlops, true);
61   int oldi = oldSizes.size() - 1;
62 
63   // loop on matrix size
64   Perf_Analyzer<Action> perf_action;
65   for (int i=nb_point-1;i>=0;i--)
66   {
67     //INFOS("size=" <<tab_sizes[i]<<"   ("<<nb_point-i<<"/"<<nb_point<<")");
68     std::cout << " " << "size = " << tab_sizes[i] << "  " << std::flush;
69 
70     BTL_DISABLE_SSE_EXCEPTIONS();
71     #ifdef HAVE_MKL
72     {
73       float dummy;
74       cblas_saxpy(1,0,&dummy,1,&dummy,1);
75     }
76     #endif
77 
78     tab_mflops[i] = perf_action.eval_mflops(tab_sizes[i]);
79     std::cout << tab_mflops[i];
80 
81     if (hasOldResults)
82     {
83       while (oldi>=0 && oldSizes[oldi]>tab_sizes[i])
84         --oldi;
85       if (oldi>=0 && oldSizes[oldi]==tab_sizes[i])
86       {
87         if (oldFlops[oldi]<tab_mflops[i])
88           std::cout << "\t > ";
89         else
90           std::cout << "\t < ";
91         std::cout << oldFlops[oldi];
92       }
93       --oldi;
94     }
95     std::cout << " MFlops    (" << nb_point-i << "/" << nb_point << ")" << std::endl;
96   }
97 
98   if (!BtlConfig::Instance.overwriteResults)
99   {
100     if (hasOldResults)
101     {
102       // merge the two data
103       std::vector<int> newSizes;
104       std::vector<double> newFlops;
105       unsigned int i=0;
106       unsigned int j=0;
107       while (i<tab_sizes.size() && j<oldSizes.size())
108       {
109         if (tab_sizes[i] == oldSizes[j])
110         {
111           newSizes.push_back(tab_sizes[i]);
112           newFlops.push_back(std::max(tab_mflops[i], oldFlops[j]));
113           ++i;
114           ++j;
115         }
116         else if (tab_sizes[i] < oldSizes[j])
117         {
118           newSizes.push_back(tab_sizes[i]);
119           newFlops.push_back(tab_mflops[i]);
120           ++i;
121         }
122         else
123         {
124           newSizes.push_back(oldSizes[j]);
125           newFlops.push_back(oldFlops[j]);
126           ++j;
127         }
128       }
129       while (i<tab_sizes.size())
130       {
131         newSizes.push_back(tab_sizes[i]);
132         newFlops.push_back(tab_mflops[i]);
133         ++i;
134       }
135       while (j<oldSizes.size())
136       {
137         newSizes.push_back(oldSizes[j]);
138         newFlops.push_back(oldFlops[j]);
139         ++j;
140       }
141       tab_mflops = newFlops;
142       tab_sizes = newSizes;
143     }
144   }
145 
146   // dump the result in a file  :
147   dump_xy_file(tab_sizes,tab_mflops,filename);
148 
149 }
150 
151 // default Perf Analyzer
152 
153 template <class Action>
bench(int size_min,int size_max,int nb_point)154 BTL_DONT_INLINE void bench( int size_min, int size_max, int nb_point ){
155 
156   // if the rdtsc is not available :
157   bench<Portable_Perf_Analyzer,Action>(size_min,size_max,nb_point);
158   // if the rdtsc is available :
159 //    bench<Mixed_Perf_Analyzer,Action>(size_min,size_max,nb_point);
160 
161 
162   // Only for small problem size. Otherwize it will be too long
163 //   bench<X86_Perf_Analyzer,Action>(size_min,size_max,nb_point);
164 //   bench<STL_Perf_Analyzer,Action>(size_min,size_max,nb_point);
165 
166 }
167 
168 #endif
169