1 //=====================================================
2 // File   :  portable_perf_analyzer.hh
3 // Author :  L. Plagne <laurent.plagne@edf.fr)>
4 // Copyright (C) EDF R&D,  mar d�c 3 18:59:35 CET 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 _PORTABLE_PERF_ANALYZER_HH
21 #define _PORTABLE_PERF_ANALYZER_HH
22 
23 #include "utilities.h"
24 #include "timers/portable_timer.hh"
25 
26 template <class Action>
27 class Portable_Perf_Analyzer{
28 public:
Portable_Perf_Analyzer(void)29   Portable_Perf_Analyzer( void ):_nb_calc(1),_nb_init(1),_chronos(){
30     MESSAGE("Portable_Perf_Analyzer Ctor");
31   };
Portable_Perf_Analyzer(const Portable_Perf_Analyzer &)32   Portable_Perf_Analyzer( const Portable_Perf_Analyzer & ){
33     INFOS("Copy Ctor not implemented");
34     exit(0);
35   };
~Portable_Perf_Analyzer(void)36   ~Portable_Perf_Analyzer( void ){
37     MESSAGE("Portable_Perf_Analyzer Dtor");
38   };
39 
40 
41 
eval_mflops(int size)42   inline double eval_mflops(int size)
43   {
44 
45     Action action(size);
46 
47 //     double time_baseline = time_init(action);
48 //     while (time_baseline < MIN_TIME_INIT)
49 //     {
50 //       _nb_init *= 2;
51 //       time_baseline = time_init(action);
52 //     }
53 //
54 //     // optimize
55 //     for (int i=1; i<NB_TRIES; ++i)
56 //       time_baseline = std::min(time_baseline, time_init(action));
57 //
58 //     time_baseline = time_baseline/(double(_nb_init));
59 
60     double time_action = time_calculate(action);
61     while (time_action < MIN_TIME)
62     {
63       _nb_calc *= 2;
64       time_action = time_calculate(action);
65     }
66 
67     // optimize
68     for (int i=1; i<NB_TRIES; ++i)
69       time_action = std::min(time_action, time_calculate(action));
70 
71 //     INFOS("size="<<size);
72 //     INFOS("_nb_init="<<_nb_init);
73 //     INFOS("_nb_calc="<<_nb_calc);
74 
75     time_action = time_action / (double(_nb_calc));
76 
77     action.check_result();
78 
79 
80     double time_baseline = time_init(action);
81     for (int i=1; i<NB_TRIES; ++i)
82       time_baseline = std::min(time_baseline, time_init(action));
83     time_baseline = time_baseline/(double(_nb_init));
84 
85 
86 
87 //     INFOS("time_baseline="<<time_baseline);
88 //     INFOS("time_action="<<time_action);
89 
90     time_action = time_action - time_baseline;
91 
92 //     INFOS("time_corrected="<<time_action);
93 
94     return action.nb_op_base()/(time_action*1000000.0);
95   }
96 
time_init(Action & action)97   inline double time_init(Action & action)
98   {
99     // time measurement
100     _chronos.start();
101     for (int ii=0; ii<_nb_init; ii++)
102       action.initialize();
103     _chronos.stop();
104     return _chronos.user_time();
105   }
106 
107 
time_calculate(Action & action)108   inline double time_calculate(Action & action)
109   {
110     // time measurement
111     _chronos.start();
112     for (int ii=0;ii<_nb_calc;ii++)
113     {
114       action.initialize();
115       action.calculate();
116     }
117     _chronos.stop();
118     return _chronos.user_time();
119   }
120 
get_nb_calc(void)121   unsigned long long get_nb_calc( void )
122   {
123     return _nb_calc;
124   }
125 
126 
127 private:
128   unsigned long long _nb_calc;
129   unsigned long long _nb_init;
130   Portable_Timer _chronos;
131 
132 };
133 
134 #endif //_PORTABLE_PERF_ANALYZER_HH
135