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_MPIContainerComm.hpp"
00034 
00035 
00036 namespace Teuchos {
00037 
00038 
00039 void TimeMonitor::zeroOutTimers()
00040 {
00041   
00042   const Array<RCP<Time> > timers = counters();
00043   
00044   const int numTimers = timers.size();
00045   
00046   for( int i = 0; i < numTimers; ++i ) {
00047     Time &timer = *timers[i];
00048 #ifdef TEUCHOS_DEBUG
00049     TEST_FOR_EXCEPTION(
00050       timer.isRunning(), std::logic_error,
00051       "Teuchos::TimeMonitor::zeroOutTimers():\n\n"
00052       "Error, the timer i = " << i << " with name \"" << timer.name() << "\""
00053       " is current running and not not be set to zero!"
00054       );
00055 #endif
00056     timer.reset();
00057   }
00058   
00059 }
00060 
00061 
00062 void TimeMonitor::summarize(
00063   std::ostream &out,
00064   const bool alwaysWriteLocal,
00065   const bool writeGlobalStats,
00066   const bool writeZeroTimers
00067   )
00068 {
00069 
00070   // 2007/08/17: rabartl: ToDo: Update this function to not print zero timers!
00071   
00072   Array<std::string> localNames(counters().length());
00073   Array<double> localTimings(counters().length());
00074   Array<double> localCallCounts(counters().length());
00075 
00076   for (int i=0; i<counters().length(); i++)
00077   {
00078     localNames[i] = counters()[i]->name();
00079     localTimings[i] = counters()[i]->totalElapsedTime();
00080     localCallCounts[i] = counters()[i]->numCalls();
00081   }
00082   
00083   // Gather timings from all procs, in case some timers have been activated on
00084   // other processors but not on this one.
00085   Array<std::string> names;
00086   Array<Array<double> > data(2);
00087   PerformanceMonitorUtils::synchValues(MPIComm::world(), localNames, 
00088     tuple(localTimings, localCallCounts),
00089     names, data);
00090   
00091   const Array<double>& timings = data[0];
00092   const Array<double>& calls = data[1];
00093   
00094   // Form the table data
00095   MPIComm comm = MPIComm::world();
00096   int np = comm.getNProc();
00097 
00098   int precision = format().precision();
00099 
00100   Array<TableColumn> columnsToWrite;
00101 
00102   TableColumn nameCol(names);
00103   Array<std::string> titles;
00104   titles.append("Timer Name");
00105 
00106   columnsToWrite.append(nameCol);
00107 
00108   Array<int> columnWidths;
00109   columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00110       nameCol));
00111 
00112   if (np==1 || alwaysWriteLocal)
00113   {
00114     TableColumn timeAndCalls(timings, calls, precision, true);
00115     titles.append("Local time (num calls)");
00116     columnsToWrite.append(timeAndCalls);
00117     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00118         timeAndCalls));
00119   }
00120   
00121   if (np > 1 && writeGlobalStats)
00122   {
00123 
00124     titles.append("Min over procs");
00125       
00126     Array<double> minTimings;
00127     PerformanceMonitorUtils::reduce(comm, EMin, timings, minTimings);
00128       
00129     Array<double> minCalls;
00130     PerformanceMonitorUtils::reduce(comm, EMin, calls, minCalls);
00131 
00132     TableColumn timeAndCalls(minTimings, minCalls, precision, true);
00133     columnsToWrite.append(timeAndCalls);
00134       
00135     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00136         timeAndCalls));
00137 
00138   }
00139   
00140   if (np > 1 && writeGlobalStats)
00141   {
00142 
00143     titles.append("Avg over procs");
00144       
00145     Array<double> avgTimings;
00146     PerformanceMonitorUtils::reduce(comm, EAvg, timings, avgTimings);
00147       
00148     Array<double> avgCalls;
00149     PerformanceMonitorUtils::reduce(comm, EAvg, calls, avgCalls);
00150 
00151     TableColumn timeAndCalls(avgTimings, avgCalls, precision, true);
00152     columnsToWrite.append(timeAndCalls);
00153       
00154     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00155         timeAndCalls));
00156 
00157   }
00158   
00159   if (np > 1 && writeGlobalStats)
00160   {
00161 
00162     titles.append("Max over procs");
00163       
00164     Array<double> maxTimings;
00165     PerformanceMonitorUtils::reduce(comm, EMax, timings, maxTimings);
00166       
00167     Array<double> maxCalls;
00168     PerformanceMonitorUtils::reduce(comm, EMax, calls, maxCalls);
00169 
00170     TableColumn timeAndCalls(maxTimings, maxCalls, precision, true);
00171     columnsToWrite.append(timeAndCalls);
00172       
00173     columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00174         timeAndCalls));
00175 
00176   }
00177 
00178   format().setColumnWidths(columnWidths);
00179 
00180   const bool writeOnThisProcessor = ( comm.getRank()==0 || alwaysWriteLocal );
00181   if (writeOnThisProcessor)
00182   {
00183     format().writeWholeTable(out, "TimeMonitor Results",
00184       titles, columnsToWrite);
00185   }
00186 
00187 }
00188 
00189 
00190 } // namespace Tuechos

Generated on Tue Jul 13 09:23:00 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7