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