Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4Timer class implementation << 27 // 23 // 28 // Author: P.Kent, 21.08.95 - First implementa << 24 // $Id: G4Timer.cc,v 1.13 2004/11/22 08:14:35 gcosmo Exp $ 29 // Revision: G.Cosmo, 29.04.97 - Added timings << 25 // GEANT4 tag $Name: geant4-07-00-cand-03 $ 30 // ------------------------------------------- << 26 // >> 27 // >> 28 // ---------------------------------------------------------------------- >> 29 // class G4Timer >> 30 // >> 31 // Implementation >> 32 // 29.04.97 G.Cosmo Added timings for Windows systems 31 33 32 #include "G4Timer.hh" 34 #include "G4Timer.hh" 33 #include "G4ios.hh" 35 #include "G4ios.hh" 34 #include "G4Exception.hh" << 35 36 36 #include <iomanip> << 37 #undef times >> 38 >> 39 // Global error function >> 40 void G4Exception(const char* s=0); 37 41 38 #if defined(IRIX6_2) 42 #if defined(IRIX6_2) 39 # if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE << 43 # if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE_EXTENDED==1) 40 # define __vfork vfork 44 # define __vfork vfork 41 # endif 45 # endif 42 #endif 46 #endif 43 47 44 #ifdef WIN32 48 #ifdef WIN32 45 # include <sys/types.h> 49 # include <sys/types.h> 46 # include <windows.h> 50 # include <windows.h> 47 51 48 // extract milliseconds time unit << 52 // extract milliseconds time unit 49 G4int sysconf(G4int a) << 53 int sysconf(int a){ 50 { << 54 if( a == _SC_CLK_TCK ) return 1000; 51 if(a == _SC_CLK_TCK) << 55 else return 0; 52 return 1000; << 56 } 53 else << 57 54 return 0; << 58 static clock_t filetime2msec( FILETIME* t ){ 55 } << 59 56 << 60 return (clock_t)((((float)t->dwHighDateTime)*429496.7296)+ 57 static clock_t filetime2msec(FILETIME* t) << 61 (((float)t->dwLowDateTime)*.0001) ); 58 { << 62 } 59 return (clock_t)((((G4float) t->dwHighDateTi << 63 60 (((G4float) t->dwLowDateTim << 64 61 } << 65 clock_t times(struct tms * t){ 62 << 66 FILETIME ct = {0,0}, et = {0,0}, st = {0,0}, ut = {0,0}, rt = {0,0}; 63 clock_t times(struct tms* t) << 67 SYSTEMTIME realtime; 64 { << 68 65 FILETIME ct = { 0, 0 }, et = { 0, 0 }, st = << 69 GetSystemTime( &realtime ); 66 rt = { 0, 0 }; << 70 SystemTimeToFileTime( &realtime, &rt ); // get real time in 10^-9 sec 67 SYSTEMTIME realtime; << 71 if( t != 0 ){ 68 << 72 GetProcessTimes( GetCurrentProcess(), &ct, &et, &st, &ut);// get process time in 10^-9 sec 69 GetSystemTime(&realtime); << 73 t->tms_utime = t->tms_cutime = filetime2msec(&ut); 70 SystemTimeToFileTime(&realtime, &rt); // ge << 74 t->tms_stime = t->tms_cstime = filetime2msec(&st); 71 if(t != 0) << 75 } 72 { << 76 return filetime2msec(&rt); 73 GetProcessTimes(GetCurrentProcess(), &ct, << 77 } 74 // get process time in 10^-9 sec << 75 t->tms_utime = t->tms_cutime = filetime2ms << 76 t->tms_stime = t->tms_cstime = filetime2ms << 77 } << 78 return filetime2msec(&rt); << 79 } << 80 #endif /* WIN32 */ 78 #endif /* WIN32 */ 81 79 82 // Print timer status on std::ostream << 80 // Print timer status n std::ostream 83 // << 81 std::ostream& operator << (std::ostream& os, const G4Timer& t) 84 std::ostream& operator<<(std::ostream& os, con << 85 { 82 { 86 // so fixed doesn't propagate << 83 if (t.IsValid()) 87 std::stringstream ss; << 84 { 88 ss << std::fixed; << 85 os << "User=" << t.GetUserElapsed() 89 if(t.IsValid()) << 86 << "s Real=" << t.GetRealElapsed() 90 { << 87 << "s Sys=" << t.GetSystemElapsed() << "s"; 91 ss << "User=" << t.GetUserElapsed() << "s << 88 } 92 << "s Sys=" << t.GetSystemElapsed() << << 89 else 93 #ifdef G4MULTITHREADED << 90 { 94 // avoid possible FPE error << 91 os << "User=****s Real=****s Sys=****s"; 95 if(t.GetRealElapsed() > 1.0e-6) << 92 } 96 { << 93 return os; 97 G4double cpu_util = (t.GetUserElapsed() << 98 t.GetRealElapsed() * << 99 ss << std::setprecision(1); << 100 ss << " [Cpu=" << std::setprecision(1) < << 101 } << 102 #endif << 103 } << 104 else << 105 { << 106 ss << "User=****s Real=****s Sys=****s"; << 107 } << 108 os << ss.str(); << 109 << 110 return os; << 111 } 94 } 112 95 113 G4double G4Timer::GetRealElapsed() const 96 G4double G4Timer::GetRealElapsed() const 114 { 97 { 115 if(!fValidTimes) << 98 if (!fValidTimes) 116 { << 99 { 117 G4Exception("G4Timer::GetRealElapsed()", " << 100 G4Exception("G4Timer::GetRealElapsed - Timer not stopped or times not recorded"); 118 "Timer not stopped or times no << 101 } 119 } << 102 G4double diff=fEndRealTime-fStartRealTime; 120 std::chrono::duration<G4double> diff = fEndR << 103 return diff/sysconf(_SC_CLK_TCK); 121 return diff.count(); << 122 } 104 } 123 105 >> 106 124 G4double G4Timer::GetSystemElapsed() const 107 G4double G4Timer::GetSystemElapsed() const 125 { 108 { 126 if(!fValidTimes) << 109 if (!fValidTimes) 127 { << 110 { 128 G4Exception("G4Timer::GetSystemElapsed()", << 111 G4Exception("G4Timer::GetSystemElapsed - Timer not stopped or times not recorded"); 129 FatalException, "Timer not sto << 112 } 130 } << 113 G4double diff=fEndTimes.tms_stime-fStartTimes.tms_stime; 131 G4double diff = fEndTimes.tms_stime - fStart << 114 return diff/sysconf(_SC_CLK_TCK); 132 return diff / sysconf(_SC_CLK_TCK); << 133 } 115 } 134 116 135 G4double G4Timer::GetUserElapsed() const 117 G4double G4Timer::GetUserElapsed() const 136 { 118 { 137 if(!fValidTimes) << 119 if (!fValidTimes) 138 { << 120 { 139 G4Exception("G4Timer::GetUserElapsed()", " << 121 G4Exception("G4Timer::GetUserElapsed - Timer not stopped or times not recorded"); 140 "Timer not stopped or times no << 122 } 141 } << 123 G4double diff=fEndTimes.tms_utime-fStartTimes.tms_utime; 142 G4double diff = fEndTimes.tms_utime - fStart << 124 return diff/sysconf(_SC_CLK_TCK); 143 return diff / sysconf(_SC_CLK_TCK); << 144 } 125 } >> 126 145 127