22 #include <boost/io/ios_state.hpp>
31 #include <type_traits>
33 #ifdef DEAL_II_HAVE_SYS_RESOURCE_H
34 # include <sys/resource.h>
47 namespace TimerImplementation
56 struct is_duration : std::false_type
62 template <
typename Rep,
typename Period>
63 struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type
73 from_seconds(
const double time)
75 static_assert(is_duration<T>::value,
76 "The template type should be a duration type.");
77 return T(std::lround(T::period::den * (time / T::period::num)));
84 template <
typename Rep,
typename Period>
86 to_seconds(
const std::chrono::duration<Rep, Period> duration)
88 return Period::num *
double(duration.count()) / Period::den;
97 data.
sum = numbers::signaling_nan<double>();
98 data.
min = numbers::signaling_nan<double>();
99 data.
max = numbers::signaling_nan<double>();
100 data.
avg = numbers::signaling_nan<double>();
113 double system_cpu_duration = 0.0;
115 FILETIME cpuTime, sysTime, createTime, exitTime;
116 const auto succeeded = GetProcessTimes(
117 GetCurrentProcess(), &createTime, &exitTime, &sysTime, &cpuTime);
120 system_cpu_duration =
121 (
double)(((
unsigned long long)cpuTime.dwHighDateTime << 32) |
122 cpuTime.dwLowDateTime) /
126 #elif defined(DEAL_II_HAVE_SYS_RESOURCE_H)
128 getrusage(RUSAGE_SELF, &usage);
129 system_cpu_duration = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
134 internal::TimerImplementation::from_seconds<duration>(system_cpu_duration));
139 template <
typename clock_type_>
148 template <
typename clock_type_>
152 current_lap_start_time = clock_type::now();
160 :
Timer(MPI_COMM_SELF, false)
165 Timer::Timer(
const MPI_Comm mpi_communicator,
const bool sync_lap_times_)
167 , mpi_communicator(mpi_communicator)
168 , sync_lap_times(sync_lap_times_)
180 #ifdef DEAL_II_WITH_MPI
212 internal::TimerImplementation::from_seconds<
214 cpu_times.last_lap_time = internal::TimerImplementation::from_seconds<
217 internal::TimerImplementation::to_seconds(
229 return internal::TimerImplementation::to_seconds(
cpu_times.accumulated_time);
239 const double running_time = internal::TimerImplementation::to_seconds(
257 return internal::TimerImplementation::to_seconds(
cpu_times.last_lap_time);
265 wall_clock_type::duration current_elapsed_wall_time;
267 current_elapsed_wall_time = wall_clock_type::now() -
273 return internal::TimerImplementation::to_seconds(current_elapsed_wall_time);
303 : output_frequency(output_frequency)
304 , output_type(output_type)
305 , out_stream(stream, true)
306 , output_is_enabled(true)
307 , mpi_communicator(MPI_COMM_SELF)
315 : output_frequency(output_frequency)
316 , output_type(output_type)
318 , output_is_enabled(true)
319 , mpi_communicator(MPI_COMM_SELF)
325 std::ostream &stream,
328 : output_frequency(output_frequency)
329 , output_type(output_type)
330 , out_stream(stream, true)
331 , output_is_enabled(true)
332 , mpi_communicator(mpi_communicator)
341 : output_frequency(output_frequency)
342 , output_type(output_type)
344 , output_is_enabled(true)
345 , mpi_communicator(mpi_communicator)
352 auto do_exit = [
this]() {
369 #ifdef DEAL_II_WITH_MPI
370 # if __cpp_lib_uncaught_exceptions >= 201411
377 const unsigned int myid =
381 <<
"---------------------------------------------------------\n"
382 <<
"TimerOutput objects finalize timed values printed to the\n"
383 <<
"screen by communicating over MPI in their destructors.\n"
384 <<
"Since an exception is currently uncaught, this\n"
385 <<
"synchronization (and subsequent output) will be skipped\n"
386 <<
"to avoid a possible deadlock.\n"
387 <<
"---------------------------------------------------------"
404 std::lock_guard<std::mutex> lock(
mutex);
406 Assert(section_name.empty() ==
false,
ExcMessage(
"Section string is empty."));
411 ExcMessage(std::string(
"Cannot enter the already active section <") +
412 section_name +
">."));
429 sections[section_name].total_cpu_time = 0;
430 sections[section_name].total_wall_time = 0;
434 sections[section_name].timer.reset();
435 sections[section_name].timer.start();
447 ExcMessage(
"Cannot exit any section because none has been entered!"));
449 std::lock_guard<std::mutex> lock(
mutex);
451 if (!section_name.empty())
454 ExcMessage(
"Cannot delete a section that was never created."));
458 ExcMessage(
"Cannot delete a section that has not been entered."));
463 const std::string actual_section_name =
466 sections[actual_section_name].timer.stop();
467 sections[actual_section_name].total_wall_time +=
468 sections[actual_section_name].timer.last_wall_time();
474 const double cpu_time =
sections[actual_section_name].timer.last_cpu_time();
475 sections[actual_section_name].total_cpu_time += cpu_time;
482 std::string output_time;
483 std::ostringstream cpu;
484 cpu << cpu_time <<
"s";
485 std::ostringstream wall;
486 wall <<
sections[actual_section_name].timer.last_wall_time() <<
"s";
488 output_time =
", CPU time: " + cpu.str();
490 output_time =
", wall time: " + wall.str() +
".";
493 ", CPU/wall time: " + cpu.str() +
" / " + wall.str() +
".";
495 out_stream << actual_section_name << output_time << std::endl;
502 actual_section_name));
507 std::map<std::string, double>
510 std::map<std::string, double> output;
511 for (
const auto §ion :
sections)
515 case TimerOutput::OutputData::total_cpu_time:
516 output[section.first] = section.second.total_cpu_time;
518 case TimerOutput::OutputData::total_wall_time:
519 output[section.first] = section.second.total_wall_time;
521 case TimerOutput::OutputData::n_calls:
522 output[section.first] = section.second.n_calls;
541 unsigned int max_width = 0;
543 max_width =
std::max(max_width,
static_cast<unsigned int>(i.first.size()));
546 max_width =
std::max(max_width + 1,
static_cast<unsigned int>(32));
547 const std::string extra_dash = std::string(max_width - 32,
'-');
548 const std::string extra_space = std::string(max_width - 32,
' ');
561 double check_time = 0.;
563 check_time += i.second.total_cpu_time;
571 <<
"+---------------------------------------------"
572 << extra_dash <<
"+------------"
573 <<
"+------------+\n"
574 <<
"| Total CPU time elapsed since start "
575 << extra_space <<
"|";
576 out_stream << std::setw(10) << std::setprecision(3) << std::right;
579 << extra_space <<
"| "
586 <<
" | % of total |\n";
587 out_stream <<
"+---------------------------------" << extra_dash
588 <<
"+-----------+------------"
592 std::string name_out = i.first;
595 unsigned int pos_non_space = name_out.find_first_not_of(
' ');
596 name_out.erase(0, pos_non_space);
597 name_out.resize(max_width,
' ');
605 out_stream << i.second.total_cpu_time <<
"s |";
612 const double fraction =
614 if (fraction > 0.001)
628 <<
"+---------------------------------" << extra_dash
630 <<
"------------+------------+\n"
636 <<
"Note: The sum of counted times is " << time_gap
637 <<
" seconds larger than the total time.\n"
638 <<
"(Timer function may have introduced too much overhead, or different\n"
639 <<
"section timers may have run at the same time.)" << std::endl;
649 <<
"+---------------------------------------------"
650 << extra_dash <<
"+------------"
651 <<
"+------------+\n"
652 <<
"| Total wallclock time elapsed since start "
653 << extra_space <<
"|";
654 out_stream << std::setw(10) << std::setprecision(3) << std::right;
657 << extra_space <<
"| "
664 out_stream <<
"+---------------------------------" << extra_dash
665 <<
"+-----------+------------"
669 std::string name_out = i.first;
672 unsigned int pos_non_space = name_out.find_first_not_of(
' ');
673 name_out.erase(0, pos_non_space);
674 name_out.resize(max_width,
' ');
682 out_stream << i.second.total_wall_time <<
"s |";
690 const double fraction =
692 if (fraction > 0.001)
706 <<
"+---------------------------------" << extra_dash
708 <<
"------------+------------+\n"
721 double check_time = 0.;
724 check_time += i.second.total_cpu_time;
731 out_stream <<
"\n\n+---------------------------------------------"
733 <<
"------------+------------+"
734 <<
"------------+------------+" <<
'\n'
735 <<
"| Total CPU/wall time elapsed since start "
736 << extra_space <<
"|" << std::setw(10) << std::setprecision(3)
740 << extra_space <<
"|"
743 <<
"\n| Section " << extra_space
745 <<
" CPU time | % of total |"
746 <<
" wall time | % of total |"
747 <<
"\n+---------------------------------" << extra_dash
749 <<
"------------+------------+"
750 <<
"------------+------------+" << std::endl;
754 std::string name_out = i.first;
757 unsigned int pos_non_space = name_out.find_first_not_of(
' ');
758 name_out.erase(0, pos_non_space);
759 name_out.resize(max_width,
' ');
769 out_stream << i.second.total_cpu_time <<
"s |";
776 const double fraction =
778 if (fraction > 0.001)
796 out_stream << i.second.total_wall_time <<
"s |";
804 const double fraction =
806 if (fraction > 0.001)
822 out_stream <<
"+---------------------------------" << extra_dash
824 <<
"------------+------------+"
825 <<
"------------+------------+" << std::endl
831 <<
"Note: The sum of counted times is " << time_gap
832 <<
" seconds larger than the total time.\n"
833 <<
"(Timer function may have introduced too much overhead, or different\n"
834 <<
"section timers may have run at the same time.)" << std::endl;
842 const double quantile)
const
850 Assert(quantile >= 0. && quantile <= 0.5,
851 ExcMessage(
"The quantile must be between 0 and 0.5"));
854 unsigned int max_width = 0;
856 max_width =
std::max(max_width,
static_cast<unsigned int>(i.first.size()));
859 max_width =
std::max(max_width + 1,
static_cast<unsigned int>(17));
860 const std::string extra_dash = std::string(max_width - 17,
'-');
861 const std::string extra_space = std::string(max_width - 17,
' ');
864 const auto print_statistics = [&](
const double given_time) {
866 if (n_ranks == 1 || quantile == 0.)
871 out_stream << std::setw(10) << std::setprecision(4) << std::right;
875 out_stream << std::setw(10) << std::setprecision(4) << std::right;
877 out_stream << std::setw(10) << std::setprecision(4) << std::right;
885 std::vector<double> receive_data(my_rank == 0 ? n_ranks : 0);
886 std::vector<double> result(9);
887 #ifdef DEAL_II_WITH_MPI
888 int ierr = MPI_Gather(&given_time,
901 std::vector<std::pair<double, unsigned int>> data_rank;
902 data_rank.reserve(n_ranks);
903 for (
unsigned int i = 0; i < n_ranks; ++i)
905 data_rank.emplace_back(receive_data[i], i);
906 result[4] += receive_data[i];
908 result[4] /= n_ranks;
909 std::sort(data_rank.begin(), data_rank.end());
911 const unsigned int quantile_index =
912 static_cast<unsigned int>(std::round(quantile * n_ranks));
914 result[0] = data_rank[0].first;
915 result[1] = data_rank[0].second;
916 result[2] = data_rank[quantile_index].first;
917 result[3] = data_rank[quantile_index].second;
918 result[5] = data_rank[n_ranks - 1 - quantile_index].first;
919 result[6] = data_rank[n_ranks - 1 - quantile_index].second;
920 result[7] = data_rank[n_ranks - 1].first;
921 result[8] = data_rank[n_ranks - 1].second;
923 ierr = MPI_Bcast(result.data(), 9, MPI_DOUBLE, 0, mpi_comm);
926 out_stream << std::setw(10) << std::setprecision(4) << std::right;
929 out_stream << static_cast<unsigned int>(result[1])
930 << (n_ranks > 99999 ?
"" :
" ") <<
"|";
931 out_stream << std::setw(10) << std::setprecision(4) << std::right;
934 out_stream << static_cast<unsigned int>(result[3])
935 << (n_ranks > 99999 ?
"" :
" ") <<
"|";
936 out_stream << std::setw(10) << std::setprecision(4) << std::right;
938 out_stream << std::setw(10) << std::setprecision(4) << std::right;
941 out_stream << static_cast<unsigned int>(result[6])
942 << (n_ranks > 99999 ?
"" :
" ") <<
"|";
943 out_stream << std::setw(10) << std::setprecision(4) << std::right;
946 out_stream << static_cast<unsigned int>(result[8])
947 << (n_ranks > 99999 ?
"" :
" ") <<
"|\n";
955 const std::string time_rank_column =
"------------------+";
956 const std::string time_rank_space =
" |";
960 <<
"+------------------------------" << extra_dash <<
"+"
962 << (n_ranks > 1 && quantile > 0. ? time_rank_column :
"")
964 << (n_ranks > 1 && quantile > 0. ? time_rank_column :
"")
965 << time_rank_column <<
'\n'
966 <<
"| Total wallclock time elapsed " << extra_space <<
"|";
972 << (n_ranks > 1 && quantile > 0. ? time_rank_space :
"")
974 << (n_ranks > 1 && quantile > 0. ? time_rank_space :
"")
975 << time_rank_space <<
'\n';
976 out_stream <<
"| Section " << extra_space <<
"| no. calls "
977 <<
"| min time rank |";
978 if (n_ranks > 1 && quantile > 0.)
979 out_stream <<
" " << std::setw(5) << std::setprecision(2) << std::right
980 << quantile <<
"-tile rank |";
982 if (n_ranks > 1 && quantile > 0.)
983 out_stream <<
" " << std::setw(5) << std::setprecision(2) << std::right
984 << 1. - quantile <<
"-tile rank |";
986 out_stream <<
"+------------------------------" << extra_dash <<
"+"
988 << (n_ranks > 1 && quantile > 0. ? time_rank_column :
"")
990 << (n_ranks > 1 && quantile > 0. ? time_rank_column :
"")
991 << time_rank_column <<
'\n';
994 std::string name_out = i.first;
997 unsigned int pos_non_space = name_out.find_first_not_of(
' ');
998 name_out.erase(0, pos_non_space);
999 name_out.resize(max_width,
' ');
1005 print_statistics(i.second.total_wall_time);
1007 out_stream <<
"+------------------------------" << extra_dash <<
"+"
1009 << (n_ranks > 1 && quantile > 0. ? time_rank_column :
"")
1011 << (n_ranks > 1 && quantile > 0. ? time_rank_column :
"")
1012 << time_rank_column <<
'\n';
1035 std::lock_guard<std::mutex> lock(
mutex);
std::ostream & get_stream() const
MPI_Comm mpi_communicator
void print_summary() const
@ cpu_and_wall_times_grouped
OutputFrequency output_frequency
std::map< std::string, Section > sections
void leave_subsection(const std::string §ion_name="")
void print_wall_time_statistics(const MPI_Comm mpi_comm, const double print_quantile=0.) const
std::list< std::string > active_sections
ConditionalOStream out_stream
std::map< std::string, double > get_summary_data(const OutputData kind) const
void enter_subsection(const std::string §ion_name)
TimerOutput(std::ostream &stream, const OutputFrequency output_frequency, const OutputType output_type)
double last_cpu_time() const
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
MPI_Comm mpi_communicator
ClockMeasurements< wall_clock_type > wall_times
ClockMeasurements< cpu_clock_type > cpu_times
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
double last_wall_time() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_WARNING(desc)
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
static ::ExceptionBase & ExcNotImplemented()
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
static const types::blas_int zero
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm mpi_communicator)
static const unsigned int invalid_unsigned_int
static time_point now() noexcept
std::chrono::time_point< CPUClock, duration > time_point
time_point_type current_lap_start_time
typename clock_type::duration duration_type
duration_type accumulated_time
duration_type last_lap_time