Teuchos_TimeMonitor.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 
00030 #include "Teuchos_TimeMonitor.hpp"
00031 #include "Teuchos_TableColumn.hpp"
00032 #include "Teuchos_TableFormat.hpp"
00033 #include "Teuchos_MPISession.hpp"
00034 #include "Teuchos_MPIContainerComm.hpp"
00035 
00036 
00037 namespace Teuchos {
00038 
00039 
00040 void TimeMonitor::zeroOutTimers()
00041 {
00042   
00043   const Array<RCP<Time> > timers = counters();
00044   
00045   const int numTimers = timers.size();
00046   
00047   for( int i = 0; i < numTimers; ++i ) {
00048     Time &timer = *timers[i];
00049 #ifdef TEUCHOS_DEBUG
00050     TEST_FOR_EXCEPTION(
00051       timer.isRunning(), std::logic_error,
00052       "Teuchos::TimeMonitor::zeroOutTimers():\n\n"
00053       "Error, the timer i = " << i << " with name \"" << timer.name() << "\""
00054       " is current running and not not be set to zero!"
00055       );
00056 #endif
00057     timer.reset();
00058   }
00059   
00060 }
00061 
00062 
00063 void TimeMonitor::summarize(
00064   std::ostream &out,
00065   const bool alwaysWriteLocal,
00066   const bool writeGlobalStats,
00067   const bool writeZeroTimers
00068   )
00069 {
00070 
00071   // 2007/08/17: rabartl: ToDo: Update this function to not print zero timers!
00072   
00073   Array<std::string> localNames(counters().length());
00074   Array<double> localTimings(counters().length());
00075   Array<double> localCallCounts(counters().length());
00076 
00077   for (int i=0; i<counters().length(); i++)
00078   {
00079     localNames[i] = counters()[i]->name();
00080     localTimings[i] = counters()[i]->totalElapsedTime();
00081     localCallCounts[i] = counters()[i]->numCalls();
00082   }
00083   
00084   // Gather timings from all procs, in case some timers have been activated on
00085   // other processors but not on this one.
00086   Array<std::string> names;
00087   Array<Array<double> > data(2);
00088   PerformanceMonitorUtils::synchValues(MPIComm::world(), localNames, 
00089     tuple(localTimings, localCallCounts),
00090     names, data);
00091   
00092   const Array<double>& timings = data[0];
00093   const Array<double>& calls = data[1];
00094   
00095   // Form the table data
00096   MPIComm comm = MPIComm::world();
00097   int np = comm.getNProc();
00098 
00099   int precision = format().precision();
00100 
00101   Array<TableColumn> columnsToWrite;
00102 
00103   TableColumn nameCol(names);
00104   Array<std::string> titles;
00105   titles.append("Timer Name");
00106 
00107   columnsToWrite.append(nameCol);
00108 
00109   Array<int> columnWidths;
00110   columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00111       nameCol));
00112 
00113   if (np==1 || alwaysWriteLocal)
00114   {
00115     TableColumn timeAndCalls(timings, calls, precision, true);
00116     titles.append("Local time (num calls)");
00117     columnsToWrite.append(timeAndCalls);
00118     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00119         timeAndCalls));
00120   }
00121   
00122   if (np > 1 && writeGlobalStats)
00123   {
00124 
00125     titles.append("Min over procs");
00126       
00127     Array<double> minTimings;
00128     PerformanceMonitorUtils::reduce(comm, EMin, timings, minTimings);
00129       
00130     Array<double> minCalls;
00131     PerformanceMonitorUtils::reduce(comm, EMin, calls, minCalls);
00132 
00133     TableColumn timeAndCalls(minTimings, minCalls, precision, true);
00134     columnsToWrite.append(timeAndCalls);
00135       
00136     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00137         timeAndCalls));
00138 
00139   }
00140   
00141   if (np > 1 && writeGlobalStats)
00142   {
00143 
00144     titles.append("Avg over procs");
00145       
00146     Array<double> avgTimings;
00147     PerformanceMonitorUtils::reduce(comm, EAvg, timings, avgTimings);
00148       
00149     Array<double> avgCalls;
00150     PerformanceMonitorUtils::reduce(comm, EAvg, calls, avgCalls);
00151 
00152     TableColumn timeAndCalls(avgTimings, avgCalls, precision, true);
00153     columnsToWrite.append(timeAndCalls);
00154       
00155     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00156         timeAndCalls));
00157 
00158   }
00159   
00160   if (np > 1 && writeGlobalStats)
00161   {
00162 
00163     titles.append("Max over procs");
00164       
00165     Array<double> maxTimings;
00166     PerformanceMonitorUtils::reduce(comm, EMax, timings, maxTimings);
00167       
00168     Array<double> maxCalls;
00169     PerformanceMonitorUtils::reduce(comm, EMax, calls, maxCalls);
00170 
00171     TableColumn timeAndCalls(maxTimings, maxCalls, precision, true);
00172     columnsToWrite.append(timeAndCalls);
00173       
00174     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00175         timeAndCalls));
00176 
00177   }
00178 
00179   format().setColumnWidths(columnWidths);
00180 
00181   const bool writeOnThisProcessor = ( comm.getRank()==0 || alwaysWriteLocal );
00182   if (writeOnThisProcessor)
00183   {
00184     format().writeWholeTable(out, "TimeMonitor Results",
00185       titles, columnsToWrite);
00186   }
00187 
00188 }
00189 
00190 
00191 } // namespace Tuechos

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7