Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Misc/MsqTimer.cpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Sandia Corporation and Argonne National
5  Laboratory. Under the terms of Contract DE-AC04-94AL85000
6  with Sandia Corporation, the U.S. Government retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
24  pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
25 
26  ***************************************************************** */
27 #include "MsqTimer.hpp"
28 
29 #ifdef MSQ_USE_OLD_IO_HEADERS
30 # include <iostream.h>
31 # include <iomanip.h>
32 #else
33 # include <iostream>
34 # include <iomanip>
35 #endif
36 
37 // Create the global collection of stop watches
39 
40 #ifdef HAVE_TIMES
41 # include <sys/times.h>
42 # include <unistd.h>
43 # include <limits.h>
44 # ifndef CLK_TCK
45 # ifdef _SC_CLK_TCK
46 # define CLK_TCK sysconf(_SC_CLK_TCK)
47 # else
48 # include <sys/param.h>
49 # ifdef HZ
50 # define CLK_TCK HZ
51 # else
52 # error times(3) w/out CLK_TCK. Please report this.
53 # undef HAVE_TIMES
54 # endif
55 # endif
56 # endif
57 #endif
58 
59 #ifdef HAVE_TIMES
60  static inline double now()
61  {
62  tms t;
63  times( &t );
64  return (double)(t.tms_utime + t.tms_stime) / CLK_TCK;
65  }
66 #elif defined(HAVE_CLOCK)
67 # ifdef MSQ_USE_OLD_C_HEADERS
68 # include <time.h>
69 # else
70 # include <ctime>
71 #endif
72  static inline double now()
73  {
74  return (double)msq_stdc::clock() / CLOCKS_PER_SEC;
75  }
76 #endif
77 
78 
79 
81  : atBirth(now())
82 {
84 }
85 
87 {
88  atBirth=now();
89  atLastCheck = atBirth;
90 }
91 
93 {
94  double right_now = now();
95  double rv = right_now - atLastCheck;
96  atLastCheck = right_now;
97  return rv;
98 }
99 
101 {
102  return now() - atBirth;
103 }
104 
106 {
107  if (!isRunning)
108  {
109  isRunning = true;
110  timeAtLastStart=now();
111  ++numStarts;
112  }
113 }
114 
116 {
117  if (isRunning)
118  {
119  isRunning = false;
120  totalTime += now() - timeAtLastStart;
121  }
122 }
123 
125 {
126  isRunning=false;
127  totalTime=0;
128  numStarts=0;
129 }
130 
132 {
133  double rv = totalTime;
134  if (isRunning)
135  rv += now() - timeAtLastStart;
136  return rv;
137 }
138 
140  const msq_std::string &name,
141  bool fail_if_exists)
142 {
143  // Don't allow empty name
144  if (name == "")
145  return 0;
146 
147  Key key = get_key(name);
148 
149  // If the named stopwatch doesn't exist...
150  if (!key)
151  {
152  // See if there is an unused existing stopwatch
153  size_t i;
154  for (i = 0; i < mEntries.size(); i++)
155  {
156  if (mEntries[i].first == "")
157  {
158  mEntries[i].first = name;
159  mEntries[i].second.reset();
160  break;
161  }
162  }
163  // If not, create a new one
164  if (i == mEntries.size())
165  {
166  mEntries.push_back(msq_std::pair<msq_std::string, StopWatch>(name, StopWatch()));
167  }
168  key = i+1;
169  }
170  // If it already existed...
171  else if (fail_if_exists)
172  key = 0;
173 
174  return key;
175 }
176 
177 
179  const msq_std::string &name) const
180 {
181  Key key = 0;
182 
183  for (size_t i = 0; i < mEntries.size(); i++)
184  {
185  if (mEntries[i].first == name)
186  {
187  key = i + 1;
188  break;
189  }
190  }
191 
192  return key;
193 }
194 
197 {
198  // Get rid of anything at the end of the list
199  if (key == mEntries.size())
200  {
201  mEntries.pop_back();
202  while (!mEntries.empty() && mEntries.back().first == "")
203  {
204  mEntries.pop_back();
205  }
206  }
207 
208  else if (key > 0 && key < mEntries.size())
209  {
210  // If in the middle of the list, set its name to ""
211  mEntries[key-1].first = "";
212  }
213 }
214 
215 
218 {
219  if (key > 0 &&
220  key <= mEntries.size() &&
221  mEntries[key-1].first != "")
222  mEntries[key-1].second.start();
223 }
224 
227 {
228  if (key > 0 &&
229  key <= mEntries.size() &&
230  mEntries[key-1].first != "")
231  mEntries[key-1].second.stop();
232 }
233 
236 {
237  if (key > 0 &&
238  key <= mEntries.size())
239  mEntries[key-1].second.reset();
240 }
241 
242 
245 {
246  if (key > 0 &&
247  key <= mEntries.size() &&
248  mEntries[key-1].first != "")
249  return mEntries[key-1].second.total_time();
250  else
251  return 0.0;
252 }
253 
256 {
257  if (key > 0 &&
258  key <= mEntries.size() &&
259  mEntries[key-1].first != "")
260  return mEntries[key-1].second.number_of_starts();
261  else
262  return 0;
263 }
270  msq_std::vector<Key> &sorted_keys)
271 {
272  int num_watches=mEntries.size();
273  int *sorted_indices=new int[num_watches];
274  int i=0;
275  int counter=0;
276  for(i=0;i<num_watches;++i){
277  sorted_indices[i]=0;
278  }
279  double current_max;
280  int index_to_max;
281  //While we haven't added all of the Keys to the vector
282  while(counter<num_watches){
283  current_max=-1;
284  index_to_max=-1;
285  //loop over the times and find the largest remaining
286  for(i=0;i<num_watches;++i){
287  if(mEntries[i].second.total_time()>current_max && sorted_indices[i]==0){
288  current_max=mEntries[i].second.total_time();
289  index_to_max=i;
290  }
291  }
292  //Add the key associated with index_to_max and any subsequent
293  //keys which are associated with a StopWatch that has a total
294  //time equal to current_max;
295  for(i=index_to_max;i<num_watches;++i){
296  if(mEntries[i].second.total_time()>=current_max && sorted_indices[i]==0)
297  {
298  counter++;
299  sorted_indices[i]=counter;
300  sorted_keys.push_back(i+1);
301  }
302  }
303  }
304  //clean up
305  delete[] sorted_indices;
306 }
307 
308 // Originally in MsqMessage.cpp
309 // Moved here and converted to an ostream operator
310 // by J.Kraftcheck, 2004-10-18
311 msq_stdio::ostream& Mesquite::operator<<( msq_stdio::ostream& str,
313 {
314  msq_std::vector<Mesquite::StopWatchCollection::Key> sorted_keys;
315  Mesquite::GlobalStopWatches.get_keys_sorted_by_time(sorted_keys);
316  int number_of_keys=sorted_keys.size();
317  int i =0;
318  str<<"\nTIME | NUM. STARTS | TIMER NAME ("<<number_of_keys<<" timers)\n";
319  for(i=0;i<number_of_keys;++i){
320  str<<msq_stdio::setiosflags(msq_stdio::ios::left)
321  <<msq_stdio::setw(13)
322  <<Mesquite::GlobalStopWatches.total_time(sorted_keys[i])
323  <<" "
324  <<msq_stdio::setw(13)
325  <<Mesquite::GlobalStopWatches.number_of_starts(sorted_keys[i])
326  <<" "
327  <<Mesquite::GlobalStopWatches.get_string(sorted_keys[i])
328  <<msq_stdio::endl;
329  }
330  return str;
331 }
332 
333 
Mesquite::StopWatchCollection GlobalStopWatches
msq_std::string get_string(const Key key)
Gets the string associated with a key.
double since_last_check()
Key get_key(const msq_std::string &name) const
double since_birth() const
Key add(const msq_std::string &name, bool fail_if_exists=true)
void get_keys_sorted_by_time(msq_std::vector< Key > &sorted_keys)
blockLoc i
Definition: read.cpp:79
msq_stdio::ostream & operator<<(msq_stdio::ostream &s, const Matrix3D &A)
double total_time(const Key key) const
static T_Key key
Definition: vinci_lass.c:76
double total_time() const
int number_of_starts(const Key key) const