From 6e3ad506ff69ead0a4be7b354769fd0d81a18b03 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Thu, 2 Apr 2020 22:35:45 -0500 Subject: [PATCH] refactor to use shared pointer --- examples/step-69/step-69.cc | 62 +++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/examples/step-69/step-69.cc b/examples/step-69/step-69.cc index 8beef68072..c201e69dac 100644 --- a/examples/step-69/step-69.cc +++ b/examples/step-69/step-69.cc @@ -506,7 +506,6 @@ namespace Step69 InitialValues initial_values; TimeStepping time_stepping; SchlierenPostprocessor schlieren_postprocessor; - DataOut data_out; vector_type output_vector; @@ -2595,21 +2594,6 @@ namespace Step69 print_head(pcout, "set up time step"); time_stepping.prepare(); schlieren_postprocessor.prepare(); - - data_out.attach_dof_handler(offline_data.dof_handler); - - constexpr auto problem_dimension = - ProblemDescription::problem_dimension; - const auto &component_names = ProblemDescription::component_names; - - for (unsigned int i = 0; i < problem_dimension; ++i) - { - output_vector[i].reinit(offline_data.partitioner); - data_out.add_data_vector(output_vector[i], component_names[i]); - } - - data_out.add_data_vector(schlieren_postprocessor.schlieren, - "schlieren_plot"); } // We will store the current time and state in the variable @@ -2663,7 +2647,7 @@ namespace Step69 // With either the initial state set up, or an interrupted state // restored it is time to enter the main loop: - output(U, base_name + "-solution", t, output_cycle++); + output(U, base_name, t, output_cycle++); print_head(pcout, "enter main loop"); @@ -2697,7 +2681,7 @@ namespace Step69 if (t > output_cycle * output_granularity) { - output(U, base_name + "-solution", t, output_cycle, true); + output(U, base_name, t, output_cycle, true); ++output_cycle; } } @@ -2705,9 +2689,7 @@ namespace Step69 // We wait for any remaining background output thread to finish before // printing a summary and exiting. if (background_thread_state.valid()) - { - background_thread_state.wait(); - } + background_thread_state.wait(); computing_timer.print_summary(); pcout << timer_output.str() << std::endl; @@ -2808,7 +2790,14 @@ namespace Step69 ProblemDescription::problem_dimension; // At this point we make a copy of the state vector, run the schlieren - // postprocessor, and run DataOut::build_patches() + // postprocessor, and run DataOut::build_patches() The actual + // output code is standard: We create a DataOut instance, attach all + // data vectors we want to output and call + // DataOut::build_patches(). There is one twist, however. In order + // to perform asynchronous IO on a background thread we create the + // DataOut object as a shared pointer that we pass on to the + // worker thread to ensure that once we exit this function and the + // worker thread finishes the DataOut object gets destroyed again. for (unsigned int i = 0; i < problem_dimension; ++i) { @@ -2818,8 +2807,20 @@ namespace Step69 schlieren_postprocessor.compute_schlieren(output_vector); - data_out.build_patches(discretization.mapping, - discretization.finite_element.degree - 1); + auto data_out = std::make_shared>(); + + data_out->attach_dof_handler(offline_data.dof_handler); + + const auto &component_names = ProblemDescription::component_names; + + for (unsigned int i = 0; i < problem_dimension; ++i) + data_out->add_data_vector(output_vector[i], component_names[i]); + + data_out->add_data_vector(schlieren_postprocessor.schlieren, + "schlieren_plot"); + + data_out->build_patches(discretization.mapping, + discretization.finite_element.degree - 1); // Next we create a lambda function for the background thread. We capture @@ -2827,7 +2828,7 @@ namespace Step69 // the output function by value so that we have access to them inside // the lambda function. - const auto output_worker = [this, name, t, cycle, checkpoint]() { + const auto output_worker = [this, name, t, cycle, checkpoint, data_out]() { if (checkpoint) { // We checkpoint the current state by doing the precise inverse @@ -2836,10 +2837,10 @@ namespace Step69 const unsigned int i = discretization.triangulation.locally_owned_subdomain(); - std::string name = base_name + "-checkpoint-" + - Utilities::int_to_string(i, 4) + ".archive"; + std::string filename = + name + "-checkpoint-" + Utilities::int_to_string(i, 4) + ".archive"; - std::ofstream file(name, std::ios::binary | std::ios::trunc); + std::ofstream file(filename, std::ios::binary | std::ios::trunc); boost::archive::binary_oarchive oa(file); oa << t << cycle; @@ -2852,9 +2853,10 @@ namespace Step69 cycle, true, DataOutBase::VtkFlags::best_speed); - data_out.set_flags(flags); + data_out->set_flags(flags); - data_out.write_vtu_with_pvtu_record("", name, cycle, mpi_communicator, 6); + data_out->write_vtu_with_pvtu_record( + "", name + "-solution", cycle, mpi_communicator, 6); }; // If the asynchronous writeback option is set we launch a new -- 2.39.5