16 #include <deal.II/base/timer.h> 17 #include <deal.II/base/exceptions.h> 18 #include <deal.II/base/utilities.h> 25 #if defined(DEAL_II_HAVE_SYS_TIME_H) && defined(DEAL_II_HAVE_SYS_RESOURCE_H) 26 # include <sys/time.h> 27 # include <sys/resource.h> 36 DEAL_II_NAMESPACE_OPEN
44 cumulative_wall_time (0.)
45 #ifdef DEAL_II_WITH_MPI
46 , mpi_communicator (MPI_COMM_SELF)
47 , sync_wall_time (false)
57 #ifdef DEAL_II_WITH_MPI 63 mpi_communicator (mpi_communicator),
78 LARGE_INTEGER freq, time;
79 QueryPerformanceFrequency(&freq);
80 QueryPerformanceCounter(&time);
81 return (
double) time.QuadPart / freq.QuadPart;
87 FILETIME cpuTime, sysTime, createTime, exitTime;
88 if (GetProcessTimes(GetCurrentProcess(), &createTime,
89 &exitTime, &sysTime, &cpuTime))
91 return (
double)(((
unsigned long long)cpuTime.dwHighDateTime << 32)
92 | cpuTime.dwLowDateTime) / 1e6;
106 #ifdef DEAL_II_WITH_MPI 111 #if defined(DEAL_II_HAVE_SYS_TIME_H) && defined(DEAL_II_HAVE_SYS_RESOURCE_H) 115 struct timeval wall_timer;
116 gettimeofday(&wall_timer, NULL);
120 getrusage (RUSAGE_SELF, &usage);
121 start_time = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
123 rusage usage_children;
124 getrusage (RUSAGE_CHILDREN, &usage_children);
125 start_time_children = usage_children.ru_utime.tv_sec + 1.e-6 * usage_children.ru_utime.tv_usec;
127 #elif defined(DEAL_II_MSVC) 132 # error Unsupported platform. Porting not finished. 144 #if defined(DEAL_II_HAVE_SYS_TIME_H) && defined(DEAL_II_HAVE_SYS_RESOURCE_H) 148 getrusage (RUSAGE_SELF, &usage);
149 const double dtime = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
152 rusage usage_children;
153 getrusage (RUSAGE_CHILDREN, &usage_children);
154 const double dtime_children =
155 usage_children.ru_utime.tv_sec + 1.e-6 * usage_children.ru_utime.tv_usec;
158 struct timeval wall_timer;
159 gettimeofday(&wall_timer, NULL);
160 last_lap_time = wall_timer.tv_sec + 1.e-6 * wall_timer.tv_usec
162 #elif defined(DEAL_II_MSVC) 166 # error Unsupported platform. Porting not finished. 169 #ifdef DEAL_II_WITH_MPI 198 #if defined(DEAL_II_HAVE_SYS_TIME_H) && defined(DEAL_II_HAVE_SYS_RESOURCE_H) 200 getrusage (RUSAGE_SELF, &usage);
201 const double dtime = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
203 rusage usage_children;
204 getrusage (RUSAGE_CHILDREN, &usage_children);
205 const double dtime_children =
206 usage_children.ru_utime.tv_sec + 1.e-6 * usage_children.ru_utime.tv_usec;
208 const double running_time = dtime -
start_time + dtime_children
217 #elif defined(DEAL_II_MSVC) 221 # error Unsupported platform. Porting not finished. 236 #if defined(DEAL_II_HAVE_SYS_TIME_H) && defined(DEAL_II_HAVE_SYS_RESOURCE_H) 237 struct timeval wall_timer;
238 gettimeofday(&wall_timer, NULL);
239 return (wall_timer.tv_sec
240 + 1.e-6 * wall_timer.tv_usec
270 output_frequency (output_frequency),
271 output_type (output_type),
272 out_stream (stream, true),
273 output_is_enabled (true)
274 #ifdef DEAL_II_WITH_MPI
285 output_frequency (output_frequency),
286 output_type (output_type),
289 #ifdef DEAL_II_WITH_MPI
295 #ifdef DEAL_II_WITH_MPI 298 std::ostream &stream,
302 output_frequency (output_frequency),
303 output_type (output_type),
306 mpi_communicator (mpi_communicator)
316 output_frequency (output_frequency),
317 output_type (output_type),
320 mpi_communicator (mpi_communicator)
343 Assert (section_name.empty() ==
false,
348 ExcMessage (std::string(
"Cannot enter the already active section <")
349 + section_name +
">."));
353 #ifdef DEAL_II_WITH_MPI 368 sections[section_name].total_cpu_time = 0;
369 sections[section_name].total_wall_time = 0;
373 sections[section_name].timer.reset();
374 sections[section_name].timer.start();
386 ExcMessage(
"Cannot exit any section because none has been entered!"));
390 if (section_name !=
"")
393 ExcMessage (
"Cannot delete a section that was never created."));
396 ExcMessage (
"Cannot delete a section that has not been entered."));
401 const std::string actual_section_name = (section_name ==
"" ?
405 sections[actual_section_name].timer.stop();
406 sections[actual_section_name].total_wall_time
407 +=
sections[actual_section_name].timer.wall_time();
413 const double cpu_time =
sections[actual_section_name].timer();
414 sections[actual_section_name].total_cpu_time += cpu_time;
420 std::string output_time;
421 std::ostringstream cpu;
422 cpu << cpu_time <<
"s";
423 std::ostringstream wall;
424 wall <<
sections[actual_section_name].timer.wall_time() <<
"s";
426 output_time =
", CPU time: " + cpu.str();
428 output_time =
", wall time: " + wall.str() +
".";
430 output_time =
", CPU/wall time: " + cpu.str() +
" / " + wall.str() +
".";
432 out_stream << actual_section_name << output_time
439 actual_section_name));
465 double check_time = 0.;
466 for (std::map<std::string, Section>::const_iterator
468 check_time += i->second.total_cpu_time;
470 const double time_gap = check_time-total_cpu_time;
472 total_cpu_time = check_time;
476 <<
"+---------------------------------------------+------------" 477 <<
"+------------+\n" 478 <<
"| Total CPU time elapsed since start |";
479 out_stream << std::setw(10) << std::setprecision(3) << std::right;
486 out_stream <<
" CPU time " <<
" | % of total |\n";
487 out_stream <<
"+---------------------------------+-----------+------------" 489 for (std::map<std::string, Section>::const_iterator
492 std::string name_out = i->first;
496 unsigned int pos_non_space = name_out.find_first_not_of (
" ");
497 name_out.erase(0, pos_non_space);
498 name_out.resize (32,
' ');
506 out_stream << i->second.total_cpu_time <<
"s |";
509 if (total_cpu_time != 0)
510 out_stream << i->second.total_cpu_time/total_cpu_time * 100 <<
"% |";
515 <<
"+---------------------------------+-----------+" 516 <<
"------------+------------+\n" 521 <<
"Note: The sum of counted times is " << time_gap
522 <<
" seconds larger than the total time.\n" 523 <<
"(Timer function may have introduced too much overhead, or different\n" 524 <<
"section timers may have run at the same time.)" << std::endl;
534 <<
"+---------------------------------------------+------------" 535 <<
"+------------+\n" 536 <<
"| Total wallclock time elapsed since start |";
537 out_stream << std::setw(10) << std::setprecision(3) << std::right;
545 out_stream <<
"+---------------------------------+-----------+------------" 547 for (std::map<std::string, Section>::const_iterator
550 std::string name_out = i->first;
554 unsigned int pos_non_space = name_out.find_first_not_of (
" ");
555 name_out.erase(0, pos_non_space);
556 name_out.resize (32,
' ');
564 out_stream << i->second.total_wall_time <<
"s |";
567 double value = i->second.total_wall_time/total_wall_time * 100;
573 <<
"+---------------------------------+-----------+" 574 <<
"------------+------------+\n" 610 DEAL_II_NAMESPACE_CLOSE
void print_summary() const
double get_lap_time() const
MPI_Comm mpi_communicator
::ExceptionBase & ExcMessage(std::string arg1)
MPI_Comm mpi_communicator
Utilities::MPI::MinMaxAvg mpi_data
bool is_finite(const double x)
T sum(const T &t, const MPI_Comm &mpi_communicator)
#define Assert(cond, exc)
double operator()() const
double start_time_children
TimerOutput(std::ostream &stream, const enum OutputFrequency output_frequency, const enum OutputType output_type)
std::list< std::string > active_sections
std::map< std::string, Section > sections
ConditionalOStream out_stream
double cumulative_wall_time
std::ostream & get_stream() const
void enter_subsection(const std::string §ion_name)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm &mpi_communicator)
OutputFrequency output_frequency
T max(const T &t, const MPI_Comm &mpi_communicator)
void leave_subsection(const std::string §ion_name=std::string())