[347] | 1 | #include "timer.hpp" |
---|
[382] | 2 | #include "mpi.hpp" |
---|
[347] | 3 | #include <string> |
---|
| 4 | #include <map> |
---|
[1157] | 5 | #include <iostream> |
---|
| 6 | #include <sstream> |
---|
[1205] | 7 | #include "tracer.hpp" |
---|
[1328] | 8 | using namespace ep_lib; |
---|
[347] | 9 | |
---|
| 10 | namespace xios |
---|
| 11 | { |
---|
[1328] | 12 | //std::map<std::string,CTimer> CTimer::allTimer; |
---|
[1134] | 13 | std::map<std::string,CTimer> *CTimer::allTimer_ptr = 0; |
---|
[347] | 14 | |
---|
[652] | 15 | CTimer::CTimer(const std::string& name_) : name(name_) |
---|
[347] | 16 | { |
---|
[652] | 17 | reset(); |
---|
[347] | 18 | } |
---|
| 19 | |
---|
| 20 | double CTimer::getTime(void) |
---|
| 21 | { |
---|
| 22 | return MPI_Wtime(); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | void CTimer::suspend(void) |
---|
| 26 | { |
---|
| 27 | if (!suspended) |
---|
| 28 | { |
---|
[652] | 29 | traceEnd(name); |
---|
| 30 | cumulatedTime += getTime() - lastTime; |
---|
[347] | 31 | } |
---|
[652] | 32 | suspended = true; |
---|
[347] | 33 | } |
---|
| 34 | |
---|
| 35 | void CTimer::resume(void) |
---|
| 36 | { |
---|
| 37 | if (suspended) |
---|
| 38 | { |
---|
[652] | 39 | lastTime = getTime(); |
---|
| 40 | traceBegin(name); |
---|
[347] | 41 | } |
---|
[652] | 42 | suspended = false; |
---|
[347] | 43 | } |
---|
| 44 | |
---|
| 45 | void CTimer::reset(void) |
---|
| 46 | { |
---|
[652] | 47 | cumulatedTime = 0.; |
---|
| 48 | suspended = true; |
---|
[347] | 49 | } |
---|
| 50 | |
---|
| 51 | double CTimer::getCumulatedTime(void) |
---|
| 52 | { |
---|
[652] | 53 | return cumulatedTime; |
---|
[347] | 54 | } |
---|
| 55 | |
---|
[652] | 56 | CTimer& CTimer::get(const std::string name) |
---|
[347] | 57 | { |
---|
[1328] | 58 | if(allTimer_ptr == NULL) allTimer_ptr = new std::map<std::string,CTimer>; |
---|
[1134] | 59 | |
---|
[1328] | 60 | //std::map<std::string,CTimer>::iterator it = allTimer.find(name); |
---|
| 61 | std::map<std::string,CTimer>::iterator it = allTimer_ptr->find(name); |
---|
| 62 | //if (it == allTimer.end()) |
---|
| 63 | if (it == allTimer_ptr->end()) |
---|
| 64 | it = allTimer_ptr->insert(std::make_pair(name, CTimer(name))).first; |
---|
[1134] | 65 | |
---|
[1328] | 66 | //it = allTimer.insert(std::make_pair(name, CTimer(name))).first; |
---|
[652] | 67 | return it->second; |
---|
[347] | 68 | } |
---|
[1156] | 69 | |
---|
[1205] | 70 | string CTimer::getAllCumulatedTime(void) |
---|
| 71 | { |
---|
| 72 | std::ostringstream strOut ; |
---|
[1328] | 73 | if(allTimer_ptr == 0) allTimer_ptr = new std::map<std::string,CTimer>; |
---|
| 74 | //for(std::map<std::string,CTimer>::iterator it=allTimer.begin();it!=allTimer.end();++it) |
---|
| 75 | for(std::map<std::string,CTimer>::iterator it=allTimer_ptr->begin();it!=allTimer_ptr->end();++it) |
---|
[1205] | 76 | strOut<<"Timer : "<<it->first<<" --> cumulated time : "<<it->second.getCumulatedTime()<<std::endl ; |
---|
| 77 | return strOut.str() ; |
---|
| 78 | } |
---|
[347] | 79 | } |
---|