From eb6d92aec34ca779317d119f41c08319298d99ca Mon Sep 17 00:00:00 2001 From: David Wells Date: Thu, 14 Sep 2017 14:24:29 -0400 Subject: [PATCH] Exit the given subsection in TimerOutput::Scope. The current implementation doesn't work in a multithreaded context. Consider the following sequence of events: Thread 1: start and create Scope scope_1(timer_output, "1") Thread 2: start and create Scope scope_2(timer_output, "2") Thread 1: call ~scope_1() and join Thread 2: call ~scope_2() and join The current implementation of ~Scope() exits the most recent subsection, so when ~scope_1() is called we leave subsection "2" and when ~scope_2() is called we leave subsection "1". We can get around this by always explicitly exiting the subsection in which we started. --- doc/news/changes/minor/20170914DavidWells | 7 +++++++ include/deal.II/base/timer.h | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 doc/news/changes/minor/20170914DavidWells diff --git a/doc/news/changes/minor/20170914DavidWells b/doc/news/changes/minor/20170914DavidWells new file mode 100644 index 0000000000..cec71dcb97 --- /dev/null +++ b/doc/news/changes/minor/20170914DavidWells @@ -0,0 +1,7 @@ +Fixed: The destructor of TimerOutput::Scope will now always exit the subsection +it entered in its constructor, instead of just exiting the last subsection that +was created in the provided TimerOutput object. This fixes timing output +measurements for multithreaded applications and cases where subsections are +nested. +
+(David Wells, 2017/09/14) diff --git a/include/deal.II/base/timer.h b/include/deal.II/base/timer.h index 786c507784..77a532fb23 100644 --- a/include/deal.II/base/timer.h +++ b/include/deal.II/base/timer.h @@ -586,6 +586,12 @@ public: * Reference to the TimerOutput object */ dealii::TimerOutput &timer; + + /** + * Name of the section we need to exit + */ + const std::string section_name; + /** * Do we still need to exit the section we are in? */ @@ -981,9 +987,12 @@ TimerOutput::exit_section (const std::string §ion_name) } inline -TimerOutput::Scope::Scope(dealii::TimerOutput &timer_, const std::string §ion_name) +TimerOutput::Scope::Scope(dealii::TimerOutput &timer_, + const std::string §ion_name_) : - timer(timer_), in(true) + timer(timer_), + section_name(section_name_), + in(true) { timer.enter_section(section_name); } @@ -1006,7 +1015,7 @@ TimerOutput::Scope::stop() if (!in) return; in=false; - timer.exit_section(); + timer.exit_section(section_name); } -- 2.39.5