cuIBM
A GPU-based Immersed Boundary Method code
logger.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <sys/time.h>
10 #include <map>
11 #include <string>
12 #include <iostream>
13 
14 
19 typedef std::map<std::string, double> Event;
20 
21 
26 typedef std::map<std::string, double>::iterator E_iter;
27 
28 
33 class Logger
34 {
35 private:
36  std::ofstream file,
37  stepFile,
38  legendFile;
40  timer,
41  timeStep,
42  memory;
43 
49  double get_time()
50  {
51  struct timeval tv;
52  gettimeofday(&tv, NULL);
53  return double(tv.tv_sec+tv.tv_usec*1e-6);
54  } // get_time
55 
56 public:
57  bool printNow;
58 
62  Logger(){}
63 
69  Logger(std::string folder)
70  {
71  file.open((folder + "/time").c_str());
72  stepFile.open((folder + "/profiling").c_str());
73  legendFile.open((folder + "/profiling_legend").c_str());
74  printNow = false;
75  }
76 
81  {
82  writeLegend();
83  file.close();
84  stepFile.close();
85  }
86 
92  void startTimer(std::string event)
93  {
94  tic[event] = get_time();
95  } // startTimer
96 
103  void stopTimer(std::string event, bool print=false)
104  {
105  double toc = get_time();
106  timeStep[event] += toc - tic[event];
107  timer[event] += toc - tic[event];
108  if(print)
109  std::cout << event << " : " << timer[event] << std::endl;
110  } // stopTimer
111 
117  void eraseTimer(std::string event)
118  {
119  timer.erase(event);
120  } // eraseTimer
121 
125  void resetTimer()
126  {
127  for( E_iter E=timer.begin(); E!=timer.end(); ++E )
128  E->second = 0;
129  } // resetTimer
130 
135  {
136  for (E_iter E=timeStep.begin(); E!=timeStep.end(); ++E )
137  E->second = 0;
138  } // resetTimeStep
139 
146  void allocMemory(std::string event, double bytes)
147  {
148  memory[event] += bytes;
149  } // allocMemory
150 
157  void freeMemory(std::string event, double bytes)
158  {
159  memory[event] -= bytes;
160  } // freeMemory
161 
167  void printTime(std::string event)
168  {
169  std::cout << event << " : " << timer[event] << std::endl;
170  } // printTime
171 
177  void printMemory(std::string event)
178  {
179  std::cout << event << " : " << memory[event] << std::endl;
180  } // printMemory
181 
186  {
187  double totalTime = 0.0;
188  std::cout << std::endl;
189  for( E_iter E=timer.begin(); E!=timer.end(); ++E )
190  {
191  totalTime += E->second;
192  std::cout << std::setw(24) << E->first << std::setw(13) << std::fixed \
193  << std::setprecision(4) << E->second << std::endl;
194  }
195  std::cout << "-------------------------------------" << std::endl;
196  std::cout << std::setw(24) << "TOTAL" << std::setw(13) << std::fixed \
197  << std::setprecision(4) << totalTime << std::endl;
198  } // printAllTime
199 
203  void writeLegend()
204  {
205  for( E_iter E=timer.begin(); E!=timer.end(); ++E )
206  {
207  legendFile << E->first << std::endl;
208  }
209  } // writeLegend
210 
214  void writeTime()
215  {
216  for( E_iter E=timer.begin(); E!=timer.end(); ++E )
217  file << E->first << " " << E->second << std::endl;
218  } // writeTime
219 
225  void writeTimeStep(int n)
226  {
227  stepFile << n << "\t";
228  for ( E_iter E=timeStep.begin(); E!=timeStep.end(); ++E )
229  stepFile << E->second << "\t";
230  stepFile << std::endl;
231  } // writeTimeStep
232 
233 }; // Logger
Monitors the time spent to achieve a certain task.
Definition: logger.h:33
void printAllTime()
Prints time spent for each event as well as the total time.
Definition: logger.h:185
bool printNow
boolean to print or not the time in the terminal
Definition: logger.h:57
Event timeStep
contain the tiem spent times of events for 1 step
Definition: logger.h:39
~Logger()
Destructor.
Definition: logger.h:80
void writeLegend()
Writes the events into a file.
Definition: logger.h:203
void writeTime()
Writes time spent for each event into a file.
Definition: logger.h:214
Logger()
Constructor.
Definition: logger.h:62
Event timer
contain the total time spent of events during the simulation
Definition: logger.h:39
void printTime(std::string event)
Prints the time spent to achieve a event.
Definition: logger.h:167
std::ofstream file
file to write the time spent
Definition: logger.h:36
void resetTimeStep()
Resets the time-step timer.
Definition: logger.h:134
void startTimer(std::string event)
Starts the timer.
Definition: logger.h:92
std::map< std::string, double >::iterator E_iter
Definition: logger.h:26
void eraseTimer(std::string event)
Erases the timer of a given event.
Definition: logger.h:117
void freeMemory(std::string event, double bytes)
Frees memory allocated for a given event.
Definition: logger.h:157
void writeTimeStep(int n)
Writes time spent for an iteration into a file.
Definition: logger.h:225
void resetTimer()
Resets the timer.
Definition: logger.h:125
std::ofstream legendFile
file to write the name of the events
Definition: logger.h:36
void printMemory(std::string event)
Prints the memory allocated for a given event.
Definition: logger.h:177
Event tic
contain the starting time of events
Definition: logger.h:39
Logger(std::string folder)
Constructor overloading. Opens files given the case directory.
Definition: logger.h:69
void stopTimer(std::string event, bool print=false)
Stops the timer.
Definition: logger.h:103
double get_time()
Gets the time of the day.
Definition: logger.h:49
void allocMemory(std::string event, double bytes)
Allocates memory for an event given a size.
Definition: logger.h:146
std::map< std::string, double > Event
Definition: logger.h:19
Event memory
contain info about the memomry allocated for events
Definition: logger.h:39
std::ofstream stepFile
file to write the time spent for 1 step
Definition: logger.h:36