From: bangerth Date: Mon, 12 Jan 2009 19:34:01 +0000 (+0000) Subject: Use data structures better adjusted to the task. Use proper doxygen comment format. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d248a3ebaf60b1e0360091ab71855742b1662d4b;p=dealii-svn.git Use data structures better adjusted to the task. Use proper doxygen comment format. git-svn-id: https://svn.dealii.org/trunk@18188 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/timer.h b/deal.II/base/include/base/timer.h index a42f49fdb0..698c66438d 100644 --- a/deal.II/base/include/base/timer.h +++ b/deal.II/base/include/base/timer.h @@ -15,8 +15,10 @@ #include #include + #include -#include +#include +#include DEAL_II_NAMESPACE_OPEN @@ -172,6 +174,7 @@ class Timer +//TODO: The following class is not thread-safe /** * This class can be used to generate formatted output from time * measurements of different subsections in a program. It is possible to @@ -189,59 +192,112 @@ class Timer class TimerOutput { public: - // Sets whether to generate output every - // time we exit a section, just in the - // end, or both. + /** + * Sets whether to generate output every + * time we exit a section, just in the + * end, or both. + */ enum OutputFrequency {every_call, summary, every_call_and_summary} - output_frequency; + output_frequency; - // Sets whether to show CPU times, wall - // times, or both CPU and wall times. + /** + * Sets whether to show CPU times, wall + * times, or both CPU and wall times. + */ enum OutputType {cpu_times, wall_times, cpu_and_wall_times} - output_type; - - // Constructor that takes std::cout as - // output stream. + output_type; + + /** + * Constructor that takes std::cout as + * output stream. + */ TimerOutput (std::ostream &stream, const enum OutputFrequency output_frequency, const enum OutputType output_type); - // Constructor that takes a - // ConditionalOStream to write output to. + /** + * Constructor that takes a + * ConditionalOStream to write output to. + */ TimerOutput (ConditionalOStream &stream, const enum OutputFrequency output_frequency, const enum OutputType output_type); - // Destructor. Calls print_summary() in - // case the option for writing the - // summary output is set. + /** + * Destructor. Calls print_summary() in + * case the option for writing the + * summary output is set. + */ ~TimerOutput(); - // Open a section by given a string name - // of it. In case the name already - // exists, that section is done once - // again. + /** + * Open a section by given a string name + * of it. In case the name already + * exists, that section is done once + * again. + */ void enter_section (const std::string §ion_name); - // Leave a section. If no name is given, - // the last section that was entered is - // left. + /** + * Leave a section. If no name is given, + * the last section that was entered is + * left. + */ void exit_section (const std::string §ion_name = std::string()); - // Print a formatted table that - // summarizes the time consumed in the - // various sections. + /** + * Print a formatted table that + * summarizes the time consumed in the + * various sections. + */ void print_summary (); private: + /** + * A timer object for the overall + * run time. If we are using MPI, + * this timer also accumulates + * over all MPI processes. + */ Timer timer_all; - std::vector section_timers; - std::vector section_names; - std::vector section_total_cpu_times; - std::vector section_total_wall_times; - std::vector section_n_calls; + + /** + * A structure that groups all + * information that we collect + * about each of the sections. + */ + struct Section + { + Timer timer; + double total_cpu_time; + double total_wall_time; + unsigned int n_calls; + }; + + /** + * A list of all the sections and + * their information. + */ + std::map sections; + + /** + * The stream object to which we + * are to output. + */ ConditionalOStream out_stream; - std::vector active_sections; + + /** + * A list of the sections that + * have been entered and not + * exited. The list is kept in + * the order in which sections + * have been entered, but + * elements may be removed in the + * middle if an argument is given + * to the exit_section() + * function. + */ + std::list active_sections; }; DEAL_II_NAMESPACE_CLOSE diff --git a/deal.II/base/source/timer.cc b/deal.II/base/source/timer.cc index f20c70dfff..c01913ef7b 100644 --- a/deal.II/base/source/timer.cc +++ b/deal.II/base/source/timer.cc @@ -179,58 +179,22 @@ TimerOutput::enter_section (const std::string §ion_name) Assert (section_name.empty() == false, ExcMessage ("Section string is empty.")); - unsigned int this_section_number = numbers::invalid_unsigned_int; - - // check whether the requested section - // already exists - for (unsigned int i=0; i::iterator position_to_delete - = active_sections.begin() + active_index_to_delete; - active_sections.erase(position_to_delete); + active_sections.erase (std::find (active_sections.begin(), active_sections.end(), + section_name)); } @@ -355,10 +305,10 @@ TimerOutput::print_summary () // generated a lot of overhead in this // function. double check_time = 0.; - for (unsigned int i=0; i::const_iterator + i = sections.begin(); i!=sections.end(); ++i) + check_time += i->second.total_cpu_time; + if (check_time > total_cpu_time) { total_cpu_time = check_time; @@ -382,9 +332,10 @@ TimerOutput::print_summary () out_stream << " CPU time " << " | % of total |\n"; out_stream << "+---------------------------------------------+------------" << "+------------+"; - for (unsigned int i=0; i::const_iterator + i = sections.begin(); i!=sections.end(); ++i) { - std::string name_out = section_names[i]; + std::string name_out = i->first; // resize the array so that it is always // of the same size @@ -395,13 +346,13 @@ TimerOutput::print_summary () out_stream << "| " << name_out; out_stream << "| "; std::cout.width(9); - out_stream << section_n_calls[i] << " |"; + out_stream << i->second.n_calls << " |"; std::cout.width(10); std::cout.precision(3); - out_stream << section_total_cpu_times[i] << "s |"; + out_stream << i->second.total_cpu_time << "s |"; std::cout.width(10); std::cout.precision(2); - out_stream << section_total_cpu_times[i]/total_cpu_time * 100 << "% |"; + out_stream << i->second.total_cpu_time/total_cpu_time * 100 << "% |"; } out_stream << std::endl << "+---------------------------------------------+" @@ -420,10 +371,10 @@ TimerOutput::print_summary () // generated a lot of overhead in this // function. double check_time = 0.; - for (unsigned int i=0; i::const_iterator + i = sections.begin(); i!=sections.end(); ++i) + check_time += i->second.total_wall_time; + if (check_time > total_wall_time) { total_wall_time = check_time; @@ -449,9 +400,10 @@ TimerOutput::print_summary () out_stream << " CPU time " << " | % of total |\n"; out_stream << "+---------------------------------------------+------------" << "+------------+"; - for (unsigned int i=0; i::const_iterator + i = sections.begin(); i!=sections.end(); ++i) { - std::string name_out = section_names[i]; + std::string name_out = i->first; // resize the array so that it is always // of the same size @@ -462,13 +414,13 @@ TimerOutput::print_summary () out_stream << "| " << name_out; out_stream << "| "; std::cout.width(9); - out_stream << section_n_calls[i] << " |"; + out_stream << i->second.n_calls << " |"; std::cout.width(10); std::cout.precision(3); - out_stream << section_total_wall_times[i] << "s |"; + out_stream << i->second.total_wall_time << "s |"; std::cout.width(10); std::cout.precision(2); - out_stream << section_total_wall_times[i]/total_wall_time * 100 << "% |"; + out_stream << i->second.total_wall_time/total_wall_time * 100 << "% |"; } out_stream << std::endl << "+---------------------------------------------+"