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_MPISession.hpp"
00031 #include "Teuchos_ConfigDefs.hpp"
00032 
00033 using namespace Teuchos;
00034 
00035 Array<RefCountPtr<Time> > TimeMonitor::timers_;
00036 
00037 RefCountPtr<Time> TimeMonitor::getNewTimer(const string& name)
00038 {
00039   RefCountPtr<Time> rtn = rcp(new Time(name), true);
00040   timers_.append(rtn);
00041   return rtn;
00042 }
00043 
00044 
00045 void TimeMonitor::summarize(ostream &out)
00046 {
00047   Array<string> names(timers_.length());
00048   Array<double> timings(timers_.length());
00049 
00050   for (int i=0; i<timers_.length(); i++)
00051     {
00052       names[i] = timers_[i]->name();
00053       timings[i] = timers_[i]->totalElapsedTime();
00054     }
00055 
00056   int np=1;
00057   int rank=0;
00058 #ifdef HAVE_MPI
00059   MPI_Comm_size(MPI_COMM_WORLD, &np);
00060   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00061 #endif
00062   if (np==1)
00063     {
00064       for (int i=0; i<names.length(); i++)
00065         {
00066           out << std::left << std::setw(40) << names[i]
00067               << ": " << timings[i] << endl;
00068         }
00069     }
00070   else
00071     {
00072       Array<double> minTime(timers_.length());
00073       Array<double> maxTime(timers_.length());
00074       Array<double> avgTime(timers_.length());
00075       gatherTimings(timings, minTime, avgTime, maxTime);
00076       if (rank==0)
00077         {
00078           for (int i=0; i<names.length(); i++)
00079             {
00080               out << std::left << std::setw(30) << names[i]
00081                   << ": " << std::left << std::setw(12) << minTime[i]
00082                   << " "  << std::left << std::setw(12) << avgTime[i]
00083                   << " "  << std::left << std::setw(12) << maxTime[i] 
00084                   << endl;
00085             }
00086         }
00087     }
00088 
00089 }
00090 
00091 void TimeMonitor::gatherTimings(const Array<double>& timings,
00092                                 Array<double>& minTime,
00093                                 Array<double>& avgTime,
00094                                 Array<double>& maxTime)
00095 {
00096 #ifdef HAVE_MPI
00097   int np;
00098   MPI_Comm_size(MPI_COMM_WORLD, &np);
00099 
00100   void* tPtr = (void*) &(timings[0]);
00101   void* minPtr = (void*) &(minTime[0]);
00102   void* avgPtr = (void*) &(avgTime[0]);
00103   void* maxPtr = (void*) &(maxTime[0]);
00104 
00105   int count = (int) timings.length();
00106 
00107   MPI_Allreduce(tPtr, minPtr, count, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
00108   MPI_Allreduce(tPtr, avgPtr, count, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
00109   MPI_Allreduce(tPtr, maxPtr, count, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
00110 
00111   for (int i=0; i<avgTime.length(); i++)
00112     {
00113       avgTime[i] = avgTime[i]/((double) np);
00114     }
00115 #endif
00116 }
00117 

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