Zoltan2 Version of the Day
Zoltan2_TimerManager.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #include <Zoltan2_TimerManager.hpp>
00051 
00052 namespace Zoltan2{
00053 
00054 TimerManager::TimerManager(const RCP<const Comm<int> > &comm, 
00055   std::ofstream *os, TimerType tt):
00056   comm_(comm), myOS_(NULL), fileOS_(os), ttype_(tt), 
00057   typeSelector_(), timers_(), timerMap_(), stopHint_(-1) 
00058 {
00059   if (fileOS_ == NULL)
00060     ttype_ = NO_TIMERS;
00061 
00062   typeSelector_.reset();
00063   typeSelector_.set(ttype_);
00064 
00065   if (ttype_ == BOTH_TIMERS){
00066     typeSelector_.set(MACRO_TIMERS);
00067     typeSelector_.set(MICRO_TIMERS);
00068   }
00069 }
00070 
00071 TimerManager::TimerManager(const RCP<const Comm<int> > &comm, 
00072   std::ostream *os, TimerType tt):
00073   comm_(comm), myOS_(os), fileOS_(NULL), ttype_(tt),
00074   typeSelector_(), timers_(), timerMap_(), stopHint_(-1) 
00075 {
00076   if (myOS_ == NULL)
00077     ttype_ = NO_TIMERS;
00078 
00079   typeSelector_.reset();
00080   typeSelector_.set(ttype_);
00081 
00082   if (ttype_ == BOTH_TIMERS){
00083     typeSelector_.set(MACRO_TIMERS);
00084     typeSelector_.set(MICRO_TIMERS);
00085   }
00086 }
00087 
00088 TimerManager::~TimerManager()
00089 {
00090   if (fileOS_ != NULL){
00091     fileOS_->close();
00092     fileOS_=NULL;
00093   }
00094 }
00095 
00096 void TimerManager::stop(TimerType tt, const string &name)
00097 {
00098   if (!typeSelector_[tt])
00099     return;
00100 
00101   if (stopHint_>0 && timers_[stopHint_]->name() == name){
00102     timers_[stopHint_]->stop();
00103     stopHint_--;
00104     return;
00105   }
00106 
00107   std::map<string, int>::iterator curr = timerMap_.find(name);
00108   if (curr != timerMap_.end()){
00109     timers_[curr->second]->stop();
00110   }
00111   else{  // Stopping a timer that doesn't exist.  Just create it.
00112     RCP<Teuchos::Time> newTimer = Teuchos::TimeMonitor::getNewTimer(name);
00113     newTimer->reset();     // reset to zero
00114     timerMap_[name] = timers_.size();
00115     timers_.push_back(newTimer);
00116     cerr << comm_->getRank() << ": warning, stop with no start" << endl;
00117   }
00118 }
00119 
00120 void TimerManager::start(TimerType tt, const string &name)
00121 {
00122   if (!typeSelector_[tt])
00123     return;
00124 
00125   std::map<string, int>::iterator curr = timerMap_.find(name);
00126   int index = -1;
00127   if (curr == timerMap_.end()){
00128     RCP<Teuchos::Time> newTimer = Teuchos::TimeMonitor::getNewTimer(name);
00129     index = timers_.size();
00130     timerMap_[name] = index;
00131     timers_.push_back(newTimer);
00132   }
00133   else{
00134     index = curr->second;
00135   }
00136 
00137   timers_[index]->start();
00138   timers_[index]->incrementNumCalls();
00139   stopHint_ = index;
00140 }
00141 
00142 void TimerManager::print() const
00143 {
00144   if (fileOS_)
00145     Teuchos::TimeMonitor::summarize(comm_.ptr(), *fileOS_);
00146   else if (myOS_)
00147     Teuchos::TimeMonitor::summarize(comm_.ptr(), *myOS_);
00148 }
00149 
00150 void TimerManager::printAndResetToZero() 
00151 {
00152   print();
00153   Teuchos::TimeMonitor::zeroOutTimers();
00154   if (fileOS_){
00155     fileOS_->close();
00156     fileOS_ = NULL;
00157   }
00158 }
00159 
00160 }  // namespace Zoltan2