MpdCodeTimer.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /// \class MpdCodeTimer
  2. ///
  3. /// brief MPD code timing utility class.
  4. /// \author Maria Ilieva (LHEP, JINR, Dubna)
  5. #include "MpdCodeTimer.h"
  6. #include "FairTask.h"
  7. #include <TStopwatch.h>
  8. #include <TObjString.h>
  9. //#include "mkl.h"
  10. //#include "mkl_lapacke.h"
  11. #ifdef _OPENMP
  12. #include "omp.h"
  13. omp_lock_t geoManagerLock;
  14. #endif
  15. #include <iostream>
  16. #include <cstdlib>
  17. using std::cout;
  18. using std::endl;
  19. //using std::pair;
  20. MpdCodeTimer* MpdCodeTimer::fgCT = 0x0;
  21. //__________________________________________________________________________
  22. MpdCodeTimer::MpdCodeTimer(const char *name, const char *title)
  23. : FairTask(name)
  24. {
  25. /// Constructor
  26. fgCT = this;
  27. }
  28. //__________________________________________________________________________
  29. MpdCodeTimer* MpdCodeTimer::Instance()
  30. {
  31. /// Get pointer to the code timer singleton object
  32. if (!fgCT){
  33. fgCT = new MpdCodeTimer;
  34. #ifdef _OPENMP
  35. omp_init_lock(&geoManagerLock);
  36. #endif
  37. // automatic destroy is supposed
  38. std::atexit(DestroyInstance);
  39. }
  40. return fgCT;
  41. }
  42. //__________________________________________________________________________
  43. MpdCodeTimer* MpdCodeTimer::Instance(const char *name, const char *title)
  44. {
  45. /// Get pointer to the Kalman filter reconstructor singleton object
  46. if (!fgCT){
  47. fgCT = new MpdCodeTimer(name, title);
  48. #ifdef _OPENMP
  49. omp_init_lock(&geoManagerLock);
  50. #endif
  51. // automatic destroy is supposed
  52. std::atexit(DestroyInstance);
  53. }
  54. return fgCT;
  55. }
  56. //__________________________________________________________________________
  57. MpdCodeTimer::~MpdCodeTimer()
  58. {
  59. /// Destructor
  60. fTimeMap.Clear();
  61. fgCT = NULL;
  62. }
  63. //__________________________________________________________________________
  64. InitStatus MpdCodeTimer::Init() {
  65. fTimeMap.SetOwner(kTRUE);
  66. fTimeMap.SetOwnerValue(kTRUE);
  67. return kSUCCESS;
  68. }
  69. //__________________________________________________________________________
  70. InitStatus MpdCodeTimer::ReInit()
  71. {
  72. return kSUCCESS;
  73. }
  74. //__________________________________________________________________________
  75. void MpdCodeTimer::Reset()
  76. {
  77. ///
  78. }
  79. //__________________________________________________________________________
  80. void MpdCodeTimer::Register()
  81. {
  82. ///
  83. }
  84. //__________________________________________________________________________
  85. void MpdCodeTimer::Finish()
  86. {
  87. ///
  88. Print();
  89. }
  90. //__________________________________________________________________________
  91. void MpdCodeTimer::Exec(Option_t * option)
  92. {
  93. ///
  94. }
  95. //__________________________________________________________________________
  96. void MpdCodeTimer::Start(const TString classname, const TString method)
  97. {
  98. /// Start the stopwatch. Create new if it does not exist.
  99. TString classMeth = classname;
  100. classMeth += "::";
  101. classMeth += method;
  102. if (fTimeMap.FindObject(classMeth) == 0x0) {
  103. // Create new
  104. TStopwatch *sw = new TStopwatch;
  105. fTimeMap.Add(new TObjString(classMeth),sw);
  106. sw->Start();
  107. } else {
  108. (static_cast<TStopwatch*>(fTimeMap(classMeth)))->Start(kFALSE);
  109. }
  110. }
  111. //__________________________________________________________________________
  112. void MpdCodeTimer::Stop(const TString classname, const TString method)
  113. {
  114. /// Stop timer
  115. if (fgCT == NULL) return;
  116. TString classMeth = classname;
  117. classMeth += "::";
  118. classMeth += method;
  119. if (fTimeMap.FindObject(classMeth) == 0x0) return;
  120. (static_cast<TStopwatch*>(fTimeMap(classMeth)))->Stop();
  121. }
  122. //__________________________________________________________________________
  123. void MpdCodeTimer::Print()
  124. {
  125. ///
  126. //TFile = new File("time.log","");
  127. TIterator* it = fTimeMap.MakeIterator();
  128. TObjString *classMeth = NULL;
  129. TObjArray tmp;
  130. while ( (classMeth = static_cast<TObjString*>(it->Next()) ) ) {
  131. tmp.Add(classMeth);
  132. }
  133. tmp.Sort();
  134. Int_t nEntries = tmp.GetEntriesFast();
  135. cout << " \n *** MpdCodeTimer info *** " << endl;
  136. for (Int_t i = 0; i < nEntries; ++i) {
  137. classMeth = static_cast<TObjString*> (tmp.UncheckedAt(i));
  138. cout << classMeth->String() << " ";
  139. //(static_cast<TStopwatch*>(fTimeMap.GetValue(classMeth)))->Print();
  140. Print(static_cast<TStopwatch*>(fTimeMap.GetValue(classMeth)),"");
  141. }
  142. }
  143. //__________________________________________________________________________
  144. void MpdCodeTimer::Print(TStopwatch *sw, Option_t *opt)
  145. {
  146. /// Print TStopwatch information (adapted from ROOT)
  147. Double_t realt = const_cast<TStopwatch*>(sw)->RealTime();
  148. Double_t cput = const_cast<TStopwatch*>(sw)->CpuTime();
  149. Int_t hours = Int_t(realt / 3600);
  150. realt -= hours * 3600;
  151. Int_t min = Int_t(realt / 60);
  152. realt -= min * 60;
  153. Int_t sec = Int_t(realt);
  154. if (realt < 0) realt = 0;
  155. if (cput < 0) cput = 0;
  156. if (opt && *opt == 'm') {
  157. Printf("Real time %d:%02d:%06.3f, CP time %.3f, %d slices", hours, min, realt, cput, sw->Counter());
  158. } else if (opt && *opt == 'u') {
  159. Printf("Real time %d:%02d:%09.6f, CP time %.3f, %d slices", hours, min, realt, cput, sw->Counter());
  160. } else {
  161. Printf("Real time %d:%02d:%02d, CP time %.3f, %d slices", hours, min, sec, cput, sw->Counter());
  162. }
  163. }
  164. ClassImp(MpdCodeTimer)