EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
/Users/marc/Developer/EGSnrc/HEN_HOUSE/user_codes/egs_brachy/egs_brachy/timing.h
Go to the documentation of this file.
1/*
2################################################################################
3#
4# egs_brachy timing.h
5# Copyright (C) 2016 Rowan Thomson, Dave Rogers, Randle Taylor, and Marc
6# Chamberland
7#
8# This file is part of egs_brachy
9#
10# egs_brachy is free software: you can redistribute it and/or modify it
11# under the terms of the GNU Affero General Public License as published
12# by the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# egs_brachy is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Affero General Public License for more details:
19# <http://www.gnu.org/licenses/>.
20#
21################################################################################
22#
23# When egs_brachy is used for publications, please cite our paper:
24# M. J. P. Chamberland, R. E. P. Taylor, D. W. O. Rogers, and R. M. Thomson,
25# egs brachy: a versatile and fast Monte Carlo code for brachytherapy,
26# Phys. Med. Biol. 61, 8214-8231 (2016).
27#
28################################################################################
29#
30# Author: Randle Taylor, 2016
31#
32# Contributors: Marc Chamberland
33# Dave Rogers
34# Rowan Thomson
35#
36################################################################################
37*/
38
39#ifndef EGS_BRACHY_TIMER_
40#define EGS_BRACHY_TIMER_
41
42#include <sstream>
43#include <iomanip>
44#include "egs_timer.h"
45
46
47class EB_Timer {
48
49 string name;
50 EGS_Timer timer;
51 EGS_Float start_time;
52 EGS_Float stop_time;
54
55public:
56 EB_Timer(string tname, int level) {
57 nested_level = level;
58 name = tname;
59 start_time = -1;
60 stop_time = -1;
61 }
62
63 void start() {
64 timer.start();
65 start_time = timer.time();
66 }
67
68 void stop() {
69 stop_time = timer.time();
70 }
71
72 EGS_Float getElapsedTime() {
73 return timer.time();
74 }
75
76 EGS_Float getStartTime() {
77 return start_time;
78 }
79
80 EGS_Float getStop() {
81 return stop_time;
82 }
83
84 EGS_Float getDuration() {
85 if (stop_time >= 0 && start_time >=0) {
86 return stop_time - start_time;
87 }
88 return -1;
89 }
90
91 string getName() {
92 return name;
93 }
94
95 bool isStopped() {
96 return getStop() >=0;
97 }
98
99 bool isRunning() {
100 return !isStopped();
101 }
102
103 int getLevel() {
104 return nested_level;
105 }
106
107};
108
109
111
112 vector<EB_Timer *> running_blocks;
113 vector<EB_Timer *> stopped_blocks;
114
115 int level;
116
117public:
119
121 for (vector<EB_Timer *>::iterator it = running_blocks.begin() ; it != running_blocks.end(); ++it) {
122 delete(*it);
123 }
124 running_blocks.clear();
125
126 for (vector<EB_Timer *>::iterator it = stopped_blocks.begin() ; it != stopped_blocks.end(); ++it) {
127 delete(*it);
128 }
129 stopped_blocks.clear();
130 }
131
132 void addTimer(string name) {
133 EB_Timer *timer = new EB_Timer(name, level);
134 timer->start();
135 running_blocks.push_back(timer);
136 level++;
137 }
138
139 void stopTimer() {
140 if (running_blocks.empty()) {
141 level = 0;
142 return;
143 }
144
145 running_blocks.back()->stop();
146
147 vector<EB_Timer *>::iterator back = running_blocks.end();
148 --back;
149 copy(back, running_blocks.end(), back_inserter(stopped_blocks));
150 running_blocks.pop_back();
151 if (running_blocks.empty()) {
152 level = 0;
153 } else {
154 level = running_blocks.back()->getLevel()+1;
155 }
156
157 }
158
159 void outputInfo() {
160 string sep(80, '=');
161 egsInformation("\n\nTiming Blocks\n%s\n", sep.c_str());
162 for (vector<EB_Timer *>::reverse_iterator it = running_blocks.rbegin(); it != running_blocks.rend(); ++it) {
163 string indent(4*((*it)->getLevel()),' ');
164 egsInformation("%s%s is still running\n", indent.c_str(), (*it)->getName().c_str());
165 }
166 for (vector<EB_Timer *>::reverse_iterator it = stopped_blocks.rbegin(); it != stopped_blocks.rend(); ++it) {
167 string indent(4*((*it)->getLevel()),' ');
168
169 stringstream output;
170 output << indent << (*it)->getName();
171
172 stringstream out_time;
173 out_time << std::scientific << std::setprecision(2) << (*it)->getDuration() << " CPU s";
174
175 size_t len = output.str().size() + out_time.str().size();
176 string padding;
177 if (len <= 80) {
178 padding = string(80-len, ' ');
179 }
180 output << padding << out_time.str() ;
181
182 egsInformation("%s\n", output.str().c_str());
183 }
184 }
185
186};
187
188#endif
EGS_Float getDuration()
Definition timing.h:84
string getName()
Definition timing.h:91
string name
Definition timing.h:49
EGS_Float start_time
Definition timing.h:51
EGS_Float stop_time
Definition timing.h:52
bool isRunning()
Definition timing.h:99
EGS_Timer timer
Definition timing.h:50
void start()
Definition timing.h:63
EGS_Float getStop()
Definition timing.h:80
void stop()
Definition timing.h:68
int getLevel()
Definition timing.h:103
EB_Timer(string tname, int level)
Definition timing.h:56
EGS_Float getElapsedTime()
Definition timing.h:72
EGS_Float getStartTime()
Definition timing.h:76
int nested_level
Definition timing.h:53
bool isStopped()
Definition timing.h:95
void stopTimer()
Definition timing.h:139
vector< EB_Timer * > running_blocks
Definition timing.h:112
vector< EB_Timer * > stopped_blocks
Definition timing.h:113
void outputInfo()
Definition timing.h:159
void addTimer(string name)
Definition timing.h:132