Teuchos_TimeMonitor.cpp

Go to the documentation of this file.
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 
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()
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           fprintf(stderr, "%-40s: %g\n", names[i].c_str(), timings[i]);
00067         }
00068     }
00069   else
00070     {
00071       Array<double> minTime(timers_.length());
00072       Array<double> maxTime(timers_.length());
00073       Array<double> avgTime(timers_.length());
00074       gatherTimings(timings, minTime, avgTime, maxTime);
00075       if (rank==0)
00076         {
00077           for (int i=0; i<names.length(); i++)
00078             {
00079               fprintf(stderr, "%-30s: %-12g %-12g %-12g\n", names[i].c_str(), 
00080                       minTime[i], avgTime[i], maxTime[i]);
00081             }
00082         }
00083     }
00084 
00085 }
00086 
00087 void TimeMonitor::gatherTimings(const Array<double>& timings,
00088                                 Array<double>& minTime,
00089                                 Array<double>& avgTime,
00090                                 Array<double>& maxTime)
00091 {
00092 #ifdef HAVE_MPI
00093   int np;
00094   MPI_Comm_size(MPI_COMM_WORLD, &np);
00095 
00096   void* tPtr = (void*) &(timings[0]);
00097   void* minPtr = (void*) &(minTime[0]);
00098   void* avgPtr = (void*) &(avgTime[0]);
00099   void* maxPtr = (void*) &(maxTime[0]);
00100 
00101   int count = (int) timings.length();
00102 
00103   MPI_Allreduce(tPtr, minPtr, count, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
00104   MPI_Allreduce(tPtr, avgPtr, count, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
00105   MPI_Allreduce(tPtr, maxPtr, count, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
00106 
00107   for (int i=0; i<avgTime.length(); i++)
00108     {
00109       avgTime[i] = avgTime[i]/((double) np);
00110     }
00111 #endif
00112 }
00113 

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