OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
timer.C
Go to the documentation of this file.
1 /*
2  *
3  * ##### ##### ###### ###### ### ###
4  * ## ## ## ## ## ## ## ### ##
5  * ## ## ## ## #### #### ## # ##
6  * ## ## ## ## ## ## ## ##
7  * ## ## ## ## ## ## ## ##
8  * ##### ##### ## ###### ## ##
9  *
10  *
11  * OOFEM : Object Oriented Finite Element Code
12  *
13  * Copyright (C) 1993 - 2013 Borek Patzak
14  *
15  *
16  *
17  * Czech Technical University, Faculty of Civil Engineering,
18  * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33  */
34 
35 #include "timer.h"
36 
37 #include <cstdio>
38 
39 #ifndef _WIN32 //_MSC_VER and __MINGW32__ included
40 //for getrusage - user time reporting
41  #include <sys/resource.h>
42 #else
43  #include <ctime>
44 #endif
45 
46 namespace oofem {
47 void Timer :: getUtime(std :: chrono :: duration< double > &answer)
48 {
49 #ifdef _WIN32 //_MSC_VER and __MINGW32__ included
50  clock_t utime = clock();
51  answer = std :: chrono :: seconds(utime / CLOCKS_PER_SEC);
52 #else
53  struct rusage rsg;
54  getrusage(RUSAGE_SELF, & rsg);
55  answer = std :: chrono :: seconds(rsg.ru_utime.tv_sec) + std :: chrono :: microseconds(rsg.ru_utime.tv_usec);
56 #endif
57 }
58 
59 void Timer :: getTime(std :: chrono :: time_point< std :: chrono :: high_resolution_clock > &answer)
60 {
61  answer = std :: chrono :: high_resolution_clock :: now();
62 }
63 
65 {
66  initTimer();
67 }
68 
70 {
71  this->initTimer();
72  this->getTime(start_wtime);
73  this->getUtime(start_utime);
74  running = true;
75 }
76 
78 {
79  this->pauseTimer();
80  running = false;
81 }
82 
84 {
85  this->getTime(end_wtime);
86  this->getUtime(end_utime);
87  running = false;
88  this->updateElapsedTime();
89 }
90 
92 {
93  this->getTime(start_wtime);
94  this->getUtime(start_utime);
95  running = true;
96 }
97 
99 {
100  elapsedWTime = elapsedWTime.zero();
101  elapsedUTime = elapsedUTime.zero();
102  running = false;
103 }
104 
106 {
107  this->updateElapsedTime();
108  return elapsedUTime.count();
109 }
110 
112 {
114  return elapsedWTime.count();
115 }
116 
117 void Timer :: convert2HMS(int &nhrs, int &nmin, int &nsec, double tsec)
118 {
119  long int _nsec = ( long int ) tsec;
120  nhrs = 0;
121  nmin = 0;
122  if ( _nsec > 60 ) {
123  nmin = _nsec / 60;
124  _nsec %= 60;
125  }
126 
127  if ( nmin > 60 ) {
128  nhrs = nmin / 60;
129  nmin %= 60;
130  }
131 
132  nsec = _nsec;
133 }
134 
135 void Timer :: toString(char *buff)
136 {
137  std :: sprintf( buff, "ut: %f.3s, wt: %f.3s", getUtime(), getWtime() );
138 }
139 
141 {
142  if ( running ) {
143  pauseTimer();
144  resumeTimer();
145  }
146 
149 
150  start_utime = end_utime;
151  start_wtime = end_wtime;
152 }
153 
155 {
156  return timers [ t ].getUtime();
157 }
158 
160 {
161  return timers [ t ].getWtime();
162 }
163 
164 void EngngModelTimer :: convert2HMS(int &nhrs, int &nmin, int &nsec, double tsec)
165 {
166  Timer :: convert2HMS(nhrs, nmin, nsec, tsec);
167 }
168 
170 {
171  return timers [ t ].toString(buff);
172 }
173 }
std::chrono::duration< double > elapsedWTime
Accumulated wtime and utime (in seconds) from start.
Definition: timer.h:53
std::chrono::duration< double > elapsedUTime
Definition: timer.h:53
void resumeTimer()
Definition: timer.C:91
void initTimer()
Definition: timer.C:98
std::chrono::time_point< std::chrono::high_resolution_clock > end_wtime
Definition: timer.h:49
std::chrono::duration< double > end_utime
Definition: timer.h:51
std::chrono::time_point< std::chrono::high_resolution_clock > start_wtime
Wall clock time markers.
Definition: timer.h:49
EngngModelTimerType
Enumeration to distinguish different type of timers.
Definition: timer.h:109
void getTime(std::chrono::time_point< std::chrono::high_resolution_clock > &answer)
Platform independent wrapper for wall time.
Definition: timer.C:59
double getWtime(EngngModelTimerType t)
Returns elapsed wall clock time.
Definition: timer.C:159
std::chrono::duration< double > start_utime
User time.
Definition: timer.h:51
static void convert2HMS(int &nhrs, int &nmin, int &nsec, double tsec)
Converts total seconds into hours, mins, and seconds.
Definition: timer.C:164
void updateElapsedTime()
Definition: timer.C:140
void pauseTimer()
Definition: timer.C:83
void toString(char *buff)
Prints receiver state into a string.
Definition: timer.C:135
void toString(EngngModelTimerType t, char *buff)
Printing & formatting.
Definition: timer.C:169
static void convert2HMS(int &nhrs, int &nmin, int &nsec, double tsec)
Converts total seconds into hours, minutes and remaining seconds.
Definition: timer.C:117
double getWtime()
Returns total elapsed wall clock time in seconds.
Definition: timer.C:111
the oofem namespace is to define a context or scope in which all oofem names are defined.
void startTimer()
Definition: timer.C:69
double getUtime(EngngModelTimerType t)
Returns total user time elapsed.
Definition: timer.C:154
double getUtime()
Returns total user time elapsed in seconds.
Definition: timer.C:105
bool running
Flag indicating whether timer is running.
Definition: timer.h:55
void stopTimer()
Definition: timer.C:77

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:31 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011