00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00084
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
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 }