LifeV
LifeChronoManager.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief
30 
31  @author Antonio Cervone <ant.cervone@gmail.com>
32  @date 2013-07-15
33  */
34 #ifndef LIFECHRONOMANAGER_HPP
35 #define LIFECHRONOMANAGER_HPP
36 
37 #include <Epetra_ConfigDefs.h>
38 #ifdef EPETRA_MPI
39 #include <mpi.h>
40 #include <Epetra_MpiComm.h>
41 #else
42 #include <Epetra_SerialComm.h>
43 #endif
44 
45 #include <lifev/core/LifeV.hpp>
46 #include <lifev/core/util/LifeChrono.hpp>
47 
48 namespace LifeV
49 {
50 
51 //! @name LifeChronoManager - chronometer manager class
52 /*!
53  This class is used for managing multiple chronometers
54 */
55 template <typename TimerType = LifeChrono>
57 {
58 public:
59  typedef TimerType timer_Type;
60  typedef std::map<std::string const, timer_Type*> timerList_Type;
62 
63  /*!
64  * @brief Constructor
65  * @param comm Communicator
66  */
68  M_stringMaxSize ( 16 ),
69  M_comm ( comm )
70  {}
71 
72  /*!
73  * @brief Register a timer
74  * @param name String to be displayed when printing data relative to the timer
75  * @param timer A pointer to the timer to be registered
76  */
77  void add ( std::string const& name, timer_Type* timer );
78 
79  /*!
80  * @brief Print out strings and time diffs for the registered timers
81  * \param out Output stream
82  */
83  void print ( std::ostream& out = std::cout );
84 
85 protected:
89 
90  static UInt const S_printSize = 80;
91  static UInt const S_columnSize = 16;
92 }; // class LifeChronoManager
93 
94 template <typename TimerType>
95 inline void LifeChronoManager<TimerType>::add ( std::string const& name, timer_Type* timer )
96 {
97  UInt const nameSize = name.size();
98  if ( nameSize > M_stringMaxSize )
99  {
100  M_stringMaxSize = nameSize;
101  }
102  M_timerList.insert ( std::make_pair ( name, timer) );
103 } // LifeChronoManager::add
104 
105 template <typename TimerType>
106 inline void LifeChronoManager<TimerType>::print ( std::ostream& out )
107 {
108  bool isLeader = M_comm->MyPID() == 0;
109 
110  std::vector<Real> times ( M_timerList.size() );
111  Real globalTime = 0;
112  UInt count = 0;
113  for ( typename timerList_Type::const_iterator it = M_timerList.begin();
114  it != M_timerList.end(); ++it, count++ )
115  {
116  times[ count ] = it->second->globalDiff ( *M_comm );
117  globalTime += times[ count ];
118  }
119 
120  if ( isLeader )
121  {
122  out << std::string (S_printSize, '=') << std::endl;
123  out << std::setw ( M_stringMaxSize ) << "Name";
124  out << std::setw ( S_columnSize ) << "Time (s)";
125  out << std::setw ( S_columnSize ) << "Perc (%)" << std::endl;
126  out << std::string (S_printSize, '=') << std::endl;
127 
128  count = 0;
129  for ( typename timerList_Type::const_iterator it = M_timerList.begin();
130  it != M_timerList.end(); ++it, count++ )
131  {
132  out << std::setw ( M_stringMaxSize ) << it->first;
133  out << std::setw ( S_columnSize ) << std::fixed << std::setprecision (2) << times[ count ];
134  out << std::setw ( S_columnSize ) << std::fixed << std::setprecision (2) << 100.* times[ count ] / globalTime << std::endl;
135  }
136  out << std::string (S_printSize, '=') << std::endl;
137  out << std::setw ( M_stringMaxSize ) << "Total Time";
138  out << std::setw ( S_columnSize ) << std::fixed << std::setprecision (2) << globalTime;
139  out << std::setw ( S_columnSize ) << std::fixed << std::setprecision (2) << 100. << std::endl;
140  out << std::string (S_printSize, '=') << std::endl;
141  }
142 } // LifeChronoManager::print
143 
144 } // end namespace LifeV
145 
146 #endif // LIFECHRONOMANAGER_HPP
static UInt const S_columnSize
LifeChronoManager(commPtr_Type comm)
Constructor.
void updateInverseJacobian(const UInt &iQuadPt)
void print(std::ostream &out=std::cout)
Print out strings and time diffs for the registered timers.
double Real
Generic real data.
Definition: LifeV.hpp:175
void add(std::string const &name, timer_Type *timer)
Register a timer.
static UInt const S_printSize
std::map< std::string const, timer_Type * > timerList_Type
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
std::shared_ptr< Epetra_Comm const > commPtr_Type