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 #include "Teuchos_TimeMonitor.hpp"
00030 #include "Teuchos_TableColumn.hpp"
00031 #include "Teuchos_TableFormat.hpp"
00032 #include "Teuchos_MPISession.hpp"
00033 #include "Teuchos_MPIContainerComm.hpp"
00034 #include "Teuchos_ConfigDefs.hpp"
00035 using namespace Teuchos;
00036 
00037 
00038 
00039 void TimeMonitor::summarize(ostream &out, bool alwaysWriteLocal,
00040                             bool writeGlobalStats)
00041 {
00042   Array<string> localNames(counters().length());
00043   Array<double> localTimings(counters().length());
00044   Array<double> localCallCounts(counters().length());
00045 
00046   for (int i=0; i<counters().length(); i++)
00047     {
00048       localNames[i] = counters()[i]->name();
00049       localTimings[i] = counters()[i]->totalElapsedTime();
00050       localCallCounts[i] = counters()[i]->numCalls();
00051     }
00052   
00053   /* Gather timings from all procs, in case some timers have been activated
00054    * on other processors but not on this one.  */
00055   Array<string> names;
00056   Array<Array<double> > data(2);
00057   PerformanceMonitorUtils::synchValues(MPIComm::world(), localNames, 
00058                                        tuple(localTimings, localCallCounts),
00059                                        names, data);
00060   
00061   const Array<double>& timings = data[0];
00062   const Array<double>& calls = data[1];
00063 
00064 
00065   
00066   /* form the table data */
00067   MPIComm comm = MPIComm::world();
00068   int np = comm.getNProc();
00069 
00070   int precision = format().precision();
00071 
00072   Array<TableColumn> columnsToWrite;
00073 
00074   TableColumn nameCol(names);
00075   Array<string> titles;
00076   titles.append("Timer Name");
00077 
00078   columnsToWrite.append(nameCol);
00079 
00080   Array<int> columnWidths;
00081   columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00082                                                           nameCol));
00083   if (np==1 || alwaysWriteLocal)
00084     {
00085       TableColumn timeAndCalls(timings, calls, precision, true);
00086       titles.append("Local time (num calls)");
00087       columnsToWrite.append(timeAndCalls);
00088       columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00089                                                               timeAndCalls));
00090     }
00091   
00092   if (np > 1 && writeGlobalStats)
00093     {
00094       titles.append("Min over procs");
00095       
00096       Array<double> minTimings;
00097       PerformanceMonitorUtils::reduce(comm, EMin, timings, minTimings);
00098       
00099       Array<double> minCalls;
00100       PerformanceMonitorUtils::reduce(comm, EMin, calls, minCalls);
00101 
00102       TableColumn timeAndCalls(minTimings, minCalls, precision, true);
00103       columnsToWrite.append(timeAndCalls);
00104       
00105       columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00106                                                               timeAndCalls));
00107     }
00108   
00109   if (np > 1 && writeGlobalStats)
00110     {
00111       titles.append("Avg over procs");
00112       
00113       Array<double> avgTimings;
00114       PerformanceMonitorUtils::reduce(comm, EAvg, timings, avgTimings);
00115       
00116       Array<double> avgCalls;
00117       PerformanceMonitorUtils::reduce(comm, EAvg, calls, avgCalls);
00118 
00119       TableColumn timeAndCalls(avgTimings, avgCalls, precision, true);
00120       columnsToWrite.append(timeAndCalls);
00121       
00122       columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00123                                                               timeAndCalls));
00124     }
00125   
00126   if (np > 1 && writeGlobalStats)
00127     {
00128       titles.append("Max over procs");
00129       
00130       Array<double> maxTimings;
00131       PerformanceMonitorUtils::reduce(comm, EMax, timings, maxTimings);
00132       
00133       Array<double> maxCalls;
00134       PerformanceMonitorUtils::reduce(comm, EMax, calls, maxCalls);
00135 
00136       TableColumn timeAndCalls(maxTimings, maxCalls, precision, true);
00137       columnsToWrite.append(timeAndCalls);
00138       
00139       columnWidths.append(format().computeRequiredColumnWidth(titles[titles.size()-1], 
00140                                                               timeAndCalls));
00141     }
00142 
00143   format().setColumnWidths(columnWidths);
00144 
00145   RefCountPtr<std::ostream> outPtr = rcp(&out, false);
00146   bool writeOnThisProcessor = ( comm.getRank()==0 || alwaysWriteLocal );
00147   if (writeOnThisProcessor)
00148     {
00149       format().writeWholeTable(outPtr, "TimeMonitor Results",
00150                                titles, columnsToWrite);
00151     }
00152 }

Generated on Thu Sep 18 12:30:31 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1