LifeV
LifeChrono.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  @file
28  @brief Chronometer and fake chronometer class class
29 
30  @date 11-12-2010
31  @author
32 
33  @maintainer Radu Popescu <radu.popescu@epfl.ch>
34 */
35 
36 #ifndef LIFE_CHRONO_H
37 #define LIFE_CHRONO_H
38 
39 
40 #include <ctime>
41 
42 #include <Epetra_ConfigDefs.h>
43 #ifdef EPETRA_MPI
44 #include <Epetra_MpiComm.h>
45 #else
46 #include <Epetra_SerialComm.h>
47 #endif
48 
49 
50 #include <lifev/core/LifeV.hpp>
51 
52 namespace LifeV
53 {
54 
55 //! @name LifeChrono - chronometer class
56 /*!
57  This class is used for timing sections of code
58 */
60 {
61 public:
62 
63  //! @name Public Types
64  //@{
65 
68 
69  //@}
70 
71  //! @name Constructor
72  //@{
74  M_t1 (0),
75  M_t2 (0),
76  M_dt (0),
77  M_running (false)
78  {
79  }
80  //@}
81 
82  //! @name Public methods
83  //@{
84  void reset()
85  {
86  M_t1 = 0;
87  M_t2 = 0;
88  M_dt = 0;
89  M_running = false;
90  }
91 
92  //! Start the timer
93  void start()
94  {
95  M_t1 = clock();
96  M_running = true;
97  }
98 
99  //! Stop the timer
100  void stop()
101  {
102  if (M_running)
103  {
104  M_t2 = clock();
105  M_dt += M_t2 - M_t1;
106  M_running = false;
107  }
108  }
109 
110  //! Compute the difference in time between start and stop
112  {
113  if (M_running)
114  {
115  return ( 1. * ( clock() - M_t1 ) ) / CLOCKS_PER_SEC;
116  }
117 
118  return ( 1. * ( M_t2 - M_t1 ) ) / CLOCKS_PER_SEC;
119  }
120 
121  //! Compute the global difference in time between start and stop for all the processes in the communicator
122  /*!
123  * @param comm the global communicator
124  * @return elapsed time
125  */
126  Real globalDiff ( const comm_Type& comm )
127  {
128  Real localDifference;
129  Real globalDifference;
130 
131  if ( M_running )
132  {
133  localDifference = ( 1. * ( clock() - M_t1 ) ) / CLOCKS_PER_SEC;
134  }
135  else
136  {
137  localDifference = ( 1. * ( M_t2 - M_t1 ) ) / CLOCKS_PER_SEC;
138  }
139 
140  comm.MaxAll ( &localDifference, &globalDifference, 1 );
141 
142  return globalDifference;
143  }
144 
145  //! Return a cumulative time difference
147  {
148  if (M_running)
149  {
150  return ( 1. * ( M_dt + clock() - M_t1 ) ) / CLOCKS_PER_SEC;
151  }
152 
153  return ( 1. * M_dt / CLOCKS_PER_SEC );
154  }
155  //@}
156 
157 private:
158  //! @name Private members
159  //@{
160  clock_t M_t1, M_t2, M_dt;
161  bool M_running;
162  //@}
163 };
164 
165 //! @name LifeChronoFake - dummy chronometer class
166 /*!
167  When the diff and diff_cumul methods are called, always
168  returns -1.0
169 */
171 {
172 public:
173  //! @name Constructor
174  //@{
176  {
177  }
178  //@}
179 
180  //! @name Public methods
181  //@{
182  void reset()
183  {
184  }
185  void start()
186  {
187  }
188  void stop()
189  {
190  }
192  {
193  return -1.;
194  };
196  {
197  return -1.;
198  };
199  //@}
200 };
201 
202 }
203 #endif // LIFE_CHRONO_H
Epetra_Comm comm_Type
Definition: LifeChrono.hpp:66
Real globalDiff(const comm_Type &comm)
Compute the global difference in time between start and stop for all the processes in the communicato...
Definition: LifeChrono.hpp:126
void start()
Start the timer.
Definition: LifeChrono.hpp:93
Real diffCumul()
Return a cumulative time difference.
Definition: LifeChrono.hpp:146
void updateInverseJacobian(const UInt &iQuadPt)
std::shared_ptr< comm_Type > commPtr_Type
Definition: LifeChrono.hpp:67
Real diff()
Compute the difference in time between start and stop.
Definition: LifeChrono.hpp:111
double Real
Generic real data.
Definition: LifeV.hpp:175
void stop()
Stop the timer.
Definition: LifeChrono.hpp:100