Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_TimerManager.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #include <Zoltan2_TimerManager.hpp>
51 
52 namespace Zoltan2{
53 
54 TimerManager::TimerManager(const RCP<const Comm<int> > &comm,
55  std::ofstream *os, TimerType tt):
56  comm_(comm), myOS_(NULL), fileOS_(os), ttype_(tt),
57  typeSelector_(), timers_(), timerMap_(), stopHint_(-1)
58 {
59  if (fileOS_ == NULL)
60  ttype_ = NO_TIMERS;
61 
62  typeSelector_.reset();
63  typeSelector_.set(ttype_);
64 
65  if (ttype_ == BOTH_TIMERS){
66  typeSelector_.set(MACRO_TIMERS);
67  typeSelector_.set(MICRO_TIMERS);
68  }
69 }
70 
71 TimerManager::TimerManager(const RCP<const Comm<int> > &comm,
72  std::ostream *os, TimerType tt):
73  comm_(comm), myOS_(os), fileOS_(NULL), ttype_(tt),
74  typeSelector_(), timers_(), timerMap_(), stopHint_(-1)
75 {
76  if (myOS_ == NULL)
77  ttype_ = NO_TIMERS;
78 
79  typeSelector_.reset();
80  typeSelector_.set(ttype_);
81 
82  if (ttype_ == BOTH_TIMERS){
83  typeSelector_.set(MACRO_TIMERS);
84  typeSelector_.set(MICRO_TIMERS);
85  }
86 }
87 
89 {
90  if (fileOS_ != NULL){
91  fileOS_->close();
92  fileOS_=NULL;
93  }
94 }
95 
96 void TimerManager::stop(TimerType tt, const std::string &name)
97 {
98  if (!typeSelector_[tt])
99  return;
100 
101  if (stopHint_>0 && timers_[stopHint_]->name() == name){
102  timers_[stopHint_]->stop();
103  stopHint_--;
104  return;
105  }
106 
107  std::map<std::string, int>::iterator curr = timerMap_.find(name);
108  if (curr != timerMap_.end()){
109  timers_[curr->second]->stop();
110  }
111  else{ // Stopping a timer that doesn't exist. Just create it.
112  RCP<Teuchos::Time> newTimer = Teuchos::TimeMonitor::getNewTimer(name);
113  newTimer->reset(); // reset to zero
114  timerMap_[name] = timers_.size();
115  timers_.push_back(newTimer);
116  std::cerr << comm_->getRank() << ": warning, stop with no start" << std::endl;
117  }
118 }
119 
120 void TimerManager::start(TimerType tt, const std::string &name)
121 {
122  if (!typeSelector_[tt])
123  return;
124 
125  std::map<std::string, int>::iterator curr = timerMap_.find(name);
126  int index = -1;
127  if (curr == timerMap_.end()){
128  RCP<Teuchos::Time> newTimer = Teuchos::TimeMonitor::getNewTimer(name);
129  index = timers_.size();
130  timerMap_[name] = index;
131  timers_.push_back(newTimer);
132  }
133  else{
134  index = curr->second;
135  }
136 
137  timers_[index]->start();
138  timers_[index]->incrementNumCalls();
139  stopHint_ = index;
140 }
141 
143 {
144  if (fileOS_)
145  Teuchos::TimeMonitor::summarize(comm_.ptr(), *fileOS_);
146  else if (myOS_)
147  Teuchos::TimeMonitor::summarize(comm_.ptr(), *myOS_);
148 }
149 
151 {
152  print();
153  Teuchos::TimeMonitor::zeroOutTimers();
154  if (fileOS_){
155  fileOS_->close();
156  fileOS_ = NULL;
157  }
158 }
159 
160 } // namespace Zoltan2
Time an algorithm (or other entity) as a whole.
TimerType
The type of timers which should be active.
void print() const
Print out global summary, do not reset timers.
Time the substeps of an entity.
TimerManager(const RCP< const Comm< int > > &comm, std::ofstream *of, TimerType tt)
Constructor for output to a file.
Run both MACRO and MICRO timers.
void printAndResetToZero()
Print out global summary of timers and reset timers to zero.
No timing data will be collected (the default).
Declarations for TimerManager.
void stop(TimerType tt, const std::string &name)
Stop the named timer.
void start(TimerType tt, const std::string &name)
Start the named timer.