SWE
/import/home/rettenbs/src/SWE/src/tools/Logger.hh
Go to the documentation of this file.
00001 
00029 #ifndef LOGGER_HPP_
00030 #define LOGGER_HPP_
00031 
00032 #if (defined USEMPI && !defined CUDA)
00033 #include <mpi.h>
00034 #endif
00035 
00036 #include <string>
00037 #include <iostream>
00038 #include <ctime>
00039 
00040 namespace tools {
00041   class Logger;
00042 }
00043 
00044 class tools::Logger {
00045 //private:
00046 
00052   std::ostream& timeCout() {
00053     //get current time
00054     time_t rawTime;
00055     time(&rawTime);
00056 
00057     //convert time to a human readable format
00058     std::string humanReadableTime = ctime(&rawTime);
00059 
00060     //remove new-line character
00061     humanReadableTime.erase(humanReadableTime.end() - 1);
00062 
00063     //return the stream
00064     return std::cout << humanReadableTime;
00065   }
00066 
00068   int processRank;
00069 
00071   const std::string programName;
00072 
00074   const std::string welcomeMessage;
00075 
00077   const std::string copyRights;
00078 
00080   const std::string finishMessage;
00081 
00083   const std::string midDelimiter;
00084 
00086   const std::string largeDelimiter;
00087 
00089   const std::string indentation;
00090 
00092 #if (defined USEMPI && !defined CUDA)
00093   double cpuClock;
00094 #else
00095   clock_t cpuClock;
00096 #endif
00097 
00099 #if (defined USEMPI && !defined CUDA)
00100   double cpuCommClock;
00101 #else
00102   clock_t cpuCommClock;
00103 #endif
00104 
00106   double cpuTime;
00107 
00109   double cpuCommTime;
00110 
00112   double wallClockTime;
00113 
00120   void printNumber1d( const int i_nX,
00121                       const std::string i_quantity ) {
00122     std::cout << "Number of " << i_quantity << ": " << i_nX << std::endl;
00123   }
00124 
00132   void printNumber2d( const int i_nX,
00133                       const int i_nY,
00134                       const std::string i_quantity ) {
00135     std::cout << "Number of " << i_quantity << ": " << i_nX << " * " << i_nY
00136               << " = " << i_nX*i_nY << std::endl;
00137   }
00138 
00139   public:
00156     Logger( const int i_processRank = 0,
00157             const std::string i_programName = "SWE",
00158             const std::string i_welcomeMessage = "Welcome to",
00159             const std::string i_copyRights =  "\n\nSWE Copyright (C) 2012-2013\n"
00160                                               "  Technische Universitaet Muenchen\n"
00161                                               "  Department of Informatics\n"
00162                                               "  Chair of Scientific Computing\n"
00163                                               "  http://www5.in.tum.de/SWE\n"
00164                                               "\n"
00165                                               "SWE comes with ABSOLUTELY NO WARRANTY.\n"
00166                                               "SWE is free software, and you are welcome to redistribute it\n"
00167                                               "under certain conditions.\n"
00168                                               "Details can be found in the file \'gpl.txt\'.",
00169             const std::string i_finishMessage = "finished successfully.",
00170             const std::string i_midDelimiter = "\n------------------------------------------------------------------\n",
00171             const std::string i_largeDelimiter = "\n*************************************************************\n",
00172             const std::string i_indentation = "\t" ):
00173             processRank(i_processRank),
00174             programName(i_programName),
00175             welcomeMessage(i_welcomeMessage),
00176             copyRights(i_copyRights),
00177             finishMessage(i_finishMessage),
00178             midDelimiter(i_midDelimiter),
00179             largeDelimiter(i_largeDelimiter),
00180             indentation(i_indentation) {
00181       //set time to zero
00182       cpuTime = cpuCommTime = wallClockTime = 0.;
00183 
00184 #ifndef USEMPI
00185       // Since we have one static logger, we do not know the MPI rank in this
00186       // constructor. When using MPI, the process rank has to be set first,
00187       // before printing the welcome message.
00188           printWelcomeMessage();
00189 #endif
00190     }
00191 
00196     virtual ~Logger() {
00197       #ifndef USEMPI
00198         printFinishMessage();
00199       #endif
00200       std::cout.flush();
00201     }
00202 
00206     void printWelcomeMessage() {
00207       if(processRank == 0) {
00208         std::cout << largeDelimiter
00209                   << welcomeMessage << " "
00210                   << programName
00211                   << copyRights
00212                   << largeDelimiter;
00213       }
00214     }
00215 
00219     void printFinishMessage() {
00220       if(processRank == 0) {
00221         std::cout << largeDelimiter
00222                   << programName << " "
00223                   << finishMessage
00224                   << largeDelimiter;
00225       }
00226     }
00227 
00233     std::ostream& cout() {
00234       return timeCout() << indentation
00235         #ifdef USEMPI
00236         <<  "process " << processRank << " - "
00237         #endif
00238         ;
00239     }
00240 
00246     void setProcessRank( const int i_processRank ) {
00247       processRank = i_processRank;
00248     }
00249 
00250 
00256     void printString(const std::string i_string) {
00257       if (processRank == 0 )
00258       timeCout() << indentation
00259                 << i_string << std::endl;
00260     }
00261 
00269     void printNumberOfProcesses( const int i_numberOfProcesses,
00270                                  const std::string i_processesName="MPI processes" ) {
00271       if (processRank == 0 )
00272       timeCout() << indentation
00273                 << "Number of " << i_processesName << ": "
00274                 << i_numberOfProcesses << std::endl;
00275     }
00276 
00285     void printNumberOfCells( const int i_nX,
00286                              const int i_nY,
00287                              const std::string i_cellMessage="cells") {
00288       if(processRank == 0) {
00289         timeCout() << indentation;
00290         printNumber2d(i_nX, i_nY, i_cellMessage);
00291       }
00292     }
00293 
00300     void printNumberOfCellsPerProcess( const int i_nX, const int i_nY ) {
00301       timeCout() << indentation << "process " << processRank << " - ";
00302       printNumber2d(i_nX, i_nY, "cells");
00303     }
00304 
00312     void printCellSize( const float i_dX, const float i_dY, const std::string i_unit="m" ) {
00313       if(processRank == 0) {
00314         timeCout() << indentation
00315                    <<"Cell size: " << i_dX << i_unit <<" * " << i_dY << i_unit
00316                    << " = " << i_dX*i_dY << " " << i_unit << "^2" << std::endl;
00317       }
00318     }
00319 
00320 
00328     void printNumberOfBlocks( const int i_nX, const int i_nY ) {
00329       if(processRank == 0) {
00330         timeCout() << indentation;
00331         printNumber2d(i_nX, i_nY, "blocks");
00332       }
00333     }
00334 
00339     void printStartMessage( const std::string i_startMessage = "Everything is set up, starting the simulation." ) {
00340       if(processRank == 0) {
00341         std::cout << midDelimiter;
00342         timeCout() << indentation << i_startMessage;
00343         std::cout << midDelimiter;
00344       }
00345     }
00346 
00353     void printSimulationTime( const float i_time,
00354                               const std::string i_simulationTimeMessage = "Simulation at time" ) {
00355       if(processRank == 0) {
00356         timeCout() << indentation
00357                   << i_simulationTimeMessage << ": " << i_time << " seconds." << std::endl;
00358       }
00359     }
00360 
00369     void printOutputFileCreation( const std::string i_fileName,
00370                                   const int i_blockX , const int i_blockY,
00371                                   const std::string i_fileType = "netCDF" ) {
00372       timeCout() << indentation << "process " << processRank << " - "
00373                 << "creating " << i_fileType << " file " << i_fileName << " for block " << i_blockX << ", " << i_blockY << "." << std::endl;
00374     }
00375 
00382     void printOutputTime( const float i_time,
00383                           const std::string i_outputTimeMessage="Writing output file at time" ) {
00384       if(processRank == 0) {
00385         timeCout() << indentation
00386                   << i_outputTimeMessage << ": " << i_time << " seconds" << std::endl;
00387       }
00388     }
00389 
00395     void printStatisticsMessage( const std::string i_statisticsMessage="Simulation finished. Printing statistics for each process." ) {
00396       if(processRank == 0) {
00397         std::cout << midDelimiter;
00398         timeCout() << indentation << i_statisticsMessage;
00399         std::cout << midDelimiter;
00400       }
00401     }
00402 
00413     void printSolverStatistics( const long i_firstSolverCounter,
00414                                 const long i_secondSolverCounter,
00415                                 const int i_blockX=0,
00416                                 const int i_blockY=0,
00417                                 const std::string i_firstSolverName="f-Wave solver",
00418                                 const std::string i_secondSolverName="Augemented Riemann solver" ) {
00419       timeCout() << indentation << "process " << processRank << " - "
00420                  << "Solver Statistics for block " << i_blockX << ", " << i_blockY << ":"<< std::endl;
00421       timeCout() << indentation << "process " << processRank << " - " << indentation
00422                  << "Times the " << i_firstSolverName << " was used: " << i_firstSolverCounter << std::endl;
00423       timeCout() << indentation << "process " << processRank << " - " << indentation
00424                  << "Times the " << i_secondSolverName << " was used: " << i_secondSolverCounter << std::endl;
00425       timeCout() << indentation << "process " << processRank << " - " << indentation
00426                  << "In Total: " << i_firstSolverCounter + i_secondSolverCounter << std::endl;
00427     }
00428 
00432     void updateCpuTime() {
00433 #if (defined USEMPI && !defined CUDA)
00434       cpuTime += MPI_Wtime() - cpuClock;
00435 #else
00436       cpuTime += (clock() - cpuClock)/(double)CLOCKS_PER_SEC;
00437 #endif
00438     }
00439 
00443     void updateCpuCommunicationTime() {
00444 #if (defined USEMPI && !defined CUDA)
00445       cpuCommTime += MPI_Wtime() - cpuCommClock;
00446 #else
00447       cpuCommTime += (clock() - cpuCommClock)/(double)CLOCKS_PER_SEC;
00448 #endif
00449     }
00450 
00451     void resetCpuClockToCurrentTime() {
00452 #if (defined USEMPI && !defined CUDA)
00453       cpuClock = MPI_Wtime();
00454 #else
00455       cpuClock = clock();
00456 #endif
00457     }
00458 
00459     void resetCpuCommunicationClockToCurrentTime() {
00460 #if (defined USEMPI && !defined CUDA)
00461       cpuCommClock = MPI_Wtime();
00462 #else
00463       cpuCommClock = clock();
00464 #endif
00465     }
00466 
00472     void initWallClockTime( const double i_wallClockTime ) {
00473       wallClockTime = i_wallClockTime;
00474     }
00475 
00481     void printWallClockTime( const double i_wallClockTime,
00482                              const std::string i_wallClockTimeMessage = "wall clock time" ) {
00483       timeCout() << indentation << "process " << processRank << " - "
00484                  << i_wallClockTimeMessage << ": "
00485                  << i_wallClockTime - wallClockTime
00486                  << " seconds"<< std::endl;
00487     }
00488 
00494     void printCpuTime(const std::string i_cpuTimeMessage = "CPU time" ) {
00495       timeCout() << indentation << "process " << processRank << " - "
00496                 << i_cpuTimeMessage << ": "
00497                 << cpuTime << " seconds"<< std::endl;
00498     }
00499 
00505     void printCpuCommunicationTime( const std::string i_cpuCommunicationTimeMessage = "CPU + communication time" ) {
00506       timeCout() << indentation << "process " << processRank << " - "
00507                 << i_cpuCommunicationTimeMessage << ": "
00508                 << cpuCommTime << " seconds"<< std::endl;
00509     }
00510 
00517     void printIterationsDone(unsigned int i_iterations, std::string i_iterationMessage = "iterations done")
00518     {
00519         if (processRank == 0) {
00520                 timeCout() << indentation << i_iterations
00521                         << ' ' << i_iterationMessage << std::endl;
00522         }
00523     }
00524 
00525   public:
00527     static Logger logger;
00528 };
00529 
00530 
00531 #endif /* LOGGER_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends