15#include <deal.II/base/logstream.h>
16#include <deal.II/base/work_stream.h>
28using namespace dealii;
32 template <
typename HD,
typename PD,
int dim,
typename Number>
34 : ParameterAcceptor(
"/A - TimeLoop")
35 , mpi_ensemble_(mpi_comm,
37 if constexpr (has_n_mpi_ensembles_v<HD>)
38 return HD::n_mpi_ensembles();
42 , hyperbolic_system_(mpi_ensemble_,
"/B - Equation")
43 , parabolic_system_(mpi_ensemble_,
"/B - Equation")
44 , discretization_(mpi_ensemble_,
"/C - Discretization")
45 , offline_data_(mpi_ensemble_, discretization_,
"/D - OfflineData")
46 , initial_values_(mpi_ensemble_,
52 , hyperbolic_module_(mpi_ensemble_,
56 "/F - HyperbolicModule")
57 , parabolic_module_(mpi_ensemble_,
62 "/G - ParabolicModule")
63 , time_integrator_(mpi_ensemble_,
67 "/H - TimeIntegrator")
68 , mesh_adaptor_(mpi_ensemble_,
72 hyperbolic_module_.initial_precomputed(),
73 hyperbolic_module_.alpha(),
75 , solution_transfer_(mpi_ensemble_,
80 , postprocessor_(mpi_ensemble_,
85 , vtu_output_(mpi_ensemble_,
90 hyperbolic_module_.initial_precomputed(),
91 hyperbolic_module_.alpha(),
92 mesh_adaptor_.smoothness_indicators(),
94 , quantities_(mpi_ensemble_,
98 hyperbolic_module_.initial_precomputed(),
100 , error_evaluation_(mpi_ensemble_,
104 hyperbolic_module_.initial_precomputed(),
105 "/L - ErrorEvaluation")
110 add_parameter(
"basename", base_name_,
"Base name for all output files");
112 t_final_ = Number(5.);
113 add_parameter(
"final time", t_final_,
"Final time");
115 enforce_t_final_ =
false;
116 add_parameter(
"enforce final time",
118 "Boolean indicating whether the final time should be "
119 "enforced strictly. If set to true the last time step is "
120 "shortened so that the simulation ends precisely at t_final");
122 timer_granularity_ = Number(0.01);
123 add_parameter(
"timer granularity",
125 "The timer granularity specifies the time interval after "
126 "which compute, output, postprocessing, and mesh adaptation "
127 "routines are run. This \"baseline tick\" is further "
128 "modified by the corresponding \"*_multiplier\" options");
130 enable_output_full_ =
false;
131 add_parameter(
"enable output full",
133 "Write out full pvtu records. The frequency is determined by "
134 "\"timer granularity\" and \"timer output full multiplier\"");
136 enable_output_levelsets_ =
false;
138 "enable output levelsets",
139 enable_output_levelsets_,
140 "Write out levelsets pvtu records. The frequency is determined by "
141 "\"timer granularity\" and \"timer output levelsets multiplier\"");
143 enable_compute_error_ =
false;
145 "enable compute error",
146 enable_compute_error_,
147 "Flag to control whether we compute error norms of the difference to "
148 "an analytic solution. Implemented only for certain initial state "
149 "configurations. The frequency how often errors are logged is "
150 "determined by \"timer granularity\" and \"timer compute error "
153 enable_compute_quantities_ =
false;
155 "enable compute quantities",
156 enable_compute_quantities_,
157 "Flag to control whether we compute quantities of interest. The "
158 "frequency how often quantities are logged is determined by \"timer "
159 "granularity\" and \"timer compute quantities multiplier\"");
161 enable_mesh_adaptivity_ =
false;
163 "enable mesh adaptivity",
164 enable_mesh_adaptivity_,
165 "Flag to control whether we use an adaptive mesh refinement strategy. "
166 "The frequency how often we query MeshAdaptor::analyze() for deciding "
167 "on adapting the mesh is determined by \"timer granularity\" and "
168 "\"timer mesh refinement multiplier\"");
170 timer_output_full_multiplier_ = 1;
171 add_parameter(
"timer output full multiplier",
172 timer_output_full_multiplier_,
173 "Multiplicative modifier applied to \"timer granularity\" "
174 "that determines the full pvtu writeout granularity");
176 timer_output_levelsets_multiplier_ = 1;
177 add_parameter(
"timer output levelsets multiplier",
178 timer_output_levelsets_multiplier_,
179 "Multiplicative modifier applied to \"timer granularity\" "
180 "that determines the levelsets pvtu writeout granularity");
182 timer_compute_error_multiplier_ = 1;
183 add_parameter(
"timer compute error multiplier",
184 timer_compute_error_multiplier_,
185 "Multiplicative modifier applied to \"timer granularity\" "
186 "that determines the writeout granularity for error norms");
188 timer_compute_quantities_multiplier_ = 1;
190 "timer compute quantities multiplier",
191 timer_compute_quantities_multiplier_,
192 "Multiplicative modifier applied to \"timer granularity\" that "
193 "determines the writeout granularity for quantities of interest");
196 add_parameter(
"resume", resume_,
"Resume an interrupted computation");
198 resume_at_time_zero_ =
false;
199 add_parameter(
"resume at time zero",
200 resume_at_time_zero_,
201 "Resume from the latest checkpoint but set the time to t=0.");
203 terminal_update_interval_ = 5;
204 add_parameter(
"terminal update interval",
205 terminal_update_interval_,
206 "Number of seconds after which output statistics are "
207 "recomputed and printed on the terminal. Setting the "
208 "interval to zero disables terminal output.");
210 terminal_correct_for_hypertreadhing_ =
true;
212 "terminal correct for hyperthreading",
213 terminal_correct_for_hypertreadhing_,
214 "If set to true, the CPU throughput is corrected by dividing the total "
215 "consumed CPU time by a factor of 2. This correction is only active if "
216 "the number of threads (per MPI rank) is 2.");
218 checkpoint_update_interval_ = 0;
220 "checkpoint update interval",
221 checkpoint_update_interval_,
222 "Number of seconds after which a new checkpoint is written out to "
223 "disk. Setting the interval to zero disables checkpointing.");
226 add_parameter(
"debug command",
228 "If set to a nonempty string then the host environment's "
229 "command processor is invoked via std::system() with the "
230 "specified string as command parameter.");
232 debug_filename_ =
"";
233 add_parameter(
"debug filename",
235 "If set to a nonempty string then we output the contents of "
236 "this file at the end. This is mainly useful in the "
237 "testsuite to output files we wish to compare");
248 template <
typename HD,
typename PD,
int dim,
typename Number>
252 std::cout <<
"TimeLoop<dim, Number>::run()" << std::endl;
259 base_name_ensemble_ = base_name_;
260 if (mpi_ensemble_.n_ensembles() > 1) {
261 print_info(
"setting up MPI ensemble");
262 unsigned int digits =
263 dealii::Utilities::needed_digits(mpi_ensemble_.n_ensembles() - 1);
264 base_name_ensemble_ +=
266 dealii::Utilities::int_to_string(mpi_ensemble_.ensemble(), digits);
272 if (mpi_ensemble_.world_rank() == 0)
273 logfile_.open(base_name_ +
".log");
275 print_parameters(logfile_);
277 if (enable_compute_error_)
278 error_evaluation_.prepare(base_name_);
285 unsigned int timer_cycle = 0;
289 const auto prepare_compute_kernels = [&]() {
290 print_info(
"preparing compute kernels");
292 discretization_.update_mapping();
293 offline_data_.prepare(problem_dimension, n_precomputed_values);
295 hyperbolic_module_.prepare();
296 parabolic_module_.prepare();
297 time_integrator_.prepare();
298 mesh_adaptor_.prepare( t);
299 postprocessor_.prepare();
300 vtu_output_.prepare();
301 quantities_.prepare(base_name_ensemble_);
302 print_mpi_partition(logfile_);
304 if (mpi_ensemble_.ensemble_rank() == 0)
305 n_global_dofs_ = dealii::Utilities::MPI::sum(
306 offline_data_.dof_handler().n_dofs(),
307 mpi_ensemble_.ensemble_leader_communicator());
312 print_info(
"initializing data structures");
315 print_info(
"resume: reading mesh and loading state vector");
317 read_checkpoint(state_vector,
321 prepare_compute_kernels);
323 if (resume_at_time_zero_) {
330 print_info(
"creating mesh and interpolating initial values");
332 discretization_.prepare(base_name_ensemble_);
334 prepare_compute_kernels();
336 hyperbolic_module_.reinit_state_vector(state_vector);
337 parabolic_module_.reinit_state_vector(state_vector);
340 "time step [X] - interpolate data vectors");
342 initial_values_.get().interpolate_hyperbolic_vector();
348 print_device_information(logfile_);
351 time_integrator_.prepare_state_vector(state_vector, t);
357 Number last_terminal_output = terminal_update_interval_ == Number(0.)
358 ? std::numeric_limits<Number>::max()
360 Number last_checkpoint = checkpoint_update_interval_ == Number(0.)
361 ? std::numeric_limits<Number>::max()
364 print_info(
"entering main loop");
367 constexpr Number relax =
368 Number(1.) - Number(10.) * std::numeric_limits<Number>::epsilon();
371 const auto output_scheduled = [&](
const unsigned int cycle) {
372 const bool do_full_output =
373 (cycle % timer_output_full_multiplier_ == 0) && enable_output_full_;
374 const bool do_levelsets =
375 (cycle % timer_output_levelsets_multiplier_ == 0) &&
376 enable_output_levelsets_;
378 return do_full_output || do_levelsets;
381 unsigned int cycle = 1;
385 std::cout <<
"\n\n### cycle = " << cycle <<
" ###\n\n" << std::endl;
390 if (enable_compute_quantities_) {
392 quantities_.accumulate(state_vector, t);
397 if (t >= relax * timer_cycle * timer_granularity_) {
398 const bool do_compute_error =
399 enable_compute_error_ &&
400 (timer_cycle % timer_compute_error_multiplier_ == 0);
401 const bool do_output_analytic =
402 enable_compute_error_ && output_scheduled(timer_cycle);
404 if (do_compute_error || do_output_analytic) {
406 interpolate_analytic_solution(analytic, t);
408 if (do_compute_error) {
410 error_evaluation_.write_out(state_vector, analytic, t);
413 if (do_output_analytic) {
415 time_integrator_.prepare_state_vector(analytic, t);
418 base_name_ensemble_ +
"-analytic_solution",
424 output(state_vector, base_name_ensemble_ +
"-solution", t, timer_cycle);
426 if (enable_compute_quantities_ &&
427 (timer_cycle % timer_compute_quantities_multiplier_ == 0)) {
429 quantities_.write_out(state_vector, t, timer_cycle);
437 if (t >= relax * t_final_)
442 if (enable_mesh_adaptivity_) {
445 "time step [X] - analyze for mesh adaptation");
447 mesh_adaptor_.analyze(state_vector, t, cycle);
450 if (mesh_adaptor_.need_mesh_adaptation()) {
453 "time step [X] - perform mesh adaptation");
454 print_info(
"performing mesh adaptation");
456 adapt_mesh_and_transfer_state_vector(state_vector,
457 prepare_compute_kernels);
460 time_integrator_.prepare_state_vector(state_vector, t);
466 const auto tau = time_integrator_.step(
470 ? std::min(t_final_, timer_cycle * timer_granularity_)
471 : std::numeric_limits<Number>::max());
475 time_integrator_.prepare_state_vector(state_vector, t);
482 "time step [X] _ - synchronization barriers");
484 Utilities::MPI::max(wall_time, mpi_ensemble_.world_communicator());
489 const bool write_to_log_file =
490 (terminal_update_interval_ != Number(0.)) &&
491 (t >= relax * timer_cycle * timer_granularity_);
493 const bool update_terminal =
494 (wall_time >= last_terminal_output + terminal_update_interval_);
496 if (write_to_log_file || update_terminal) {
498 "time step [X] _ - synchronization barriers");
499 print_cycle_statistics(cycle,
504 last_terminal_output = wall_time;
507 const bool update_checkpoint =
508 (wall_time >= last_checkpoint + checkpoint_update_interval_);
510 if (update_checkpoint) {
513 print_info(
"scheduling checkpointing");
514 write_checkpoint(state_vector, base_name_ensemble_, t, timer_cycle);
515 last_checkpoint = wall_time;
522 if (checkpoint_update_interval_ != Number(0.)) {
525 print_info(
"scheduling checkpointing");
526 write_checkpoint(state_vector, base_name_ensemble_, t, timer_cycle);
533 if (terminal_update_interval_ != Number(0.)) {
534 print_cycle_statistics(cycle,
544 if (enable_compute_error_) {
547 interpolate_analytic_solution(analytic, t);
548 const auto norms = error_evaluation_.compute(state_vector, analytic);
550 logfile_ << std::endl <<
"Computed errors:" << std::endl << std::endl;
551 error_evaluation_.print_summary(logfile_, t, n_global_dofs_, norms);
552 error_evaluation_.print_summary(std::cout, t, n_global_dofs_, norms);
557 if (mpi_ensemble_.world_rank() == 0) {
558 if (debug_command_ !=
"") {
559 auto result [[maybe_unused]] = std::system(debug_command_.c_str());
562 if (debug_filename_ !=
"") {
563 std::ifstream f(debug_filename_);
565 std::cout << f.rdbuf();
578 template <
typename HD,
typename PD,
int dim,
typename Number>
579 template <
typename Callable>
582 const std::string &base_name,
584 unsigned int &timer_cycle,
585 const Callable &prepare_compute_kernels)
588 std::cout <<
"TimeLoop<dim, Number>::read_checkpoint()" << std::endl;
595 discretization_.refinement() = 0;
596 discretization_.prepare(base_name);
597 discretization_.triangulation().load(base_name +
"-checkpoint.mesh");
599 prepare_compute_kernels();
605 std::string name = base_name +
"-checkpoint";
607 unsigned int transfer_handle;
608 if (mpi_ensemble_.ensemble_rank() == 0) {
609 std::string meta = name +
".metadata";
611 std::ifstream file(meta, std::ios::binary);
612 boost::archive::binary_iarchive ia(file);
613 ia >> t >> timer_cycle >> transfer_handle;
617 if constexpr (std::is_same_v<Number, double>)
619 &t, 1, MPI_DOUBLE, 0, mpi_ensemble_.ensemble_communicator());
622 MPI_Bcast(&t, 1, MPI_FLOAT, 0, mpi_ensemble_.ensemble_communicator());
623 AssertThrowMPI(ierr);
625 ierr = MPI_Bcast(&timer_cycle,
629 mpi_ensemble_.ensemble_communicator());
630 AssertThrowMPI(ierr);
632 ierr = MPI_Bcast(&transfer_handle,
636 mpi_ensemble_.ensemble_communicator());
637 AssertThrowMPI(ierr);
641 hyperbolic_module_.reinit_state_vector(state_vector);
642 parabolic_module_.reinit_state_vector(state_vector);
644 solution_transfer_.set_handle(transfer_handle);
645 solution_transfer_.project(state_vector);
646 solution_transfer_.reset_handle();
649 time_integrator_.prepare_state_vector(state_vector, t);
653 template <
typename HD,
typename PD,
int dim,
typename Number>
654 void TimeLoop<HD, PD, dim, Number>::write_checkpoint(
655 const StateVector<Number> &state_vector,
656 const std::string &base_name,
658 const unsigned int &timer_cycle)
661 std::cout <<
"TimeLoop<dim, Number>::write_checkpoint()" << std::endl;
664 solution_transfer_.prepare_projection(state_vector);
665 const auto transfer_handle = solution_transfer_.get_handle();
666 solution_transfer_.reset_handle();
668 std::string name = base_name +
"-checkpoint";
670 if (mpi_ensemble_.ensemble_rank() == 0) {
671 for (
const std::string suffix :
672 {
".mesh",
".mesh_fixed.data",
".mesh.info",
".metadata"})
673 if (std::filesystem::exists(name + suffix))
674 std::filesystem::rename(name + suffix, name + suffix +
"~");
677 const auto &triangulation = discretization_.triangulation();
678 triangulation.save(name +
".mesh");
684 if (mpi_ensemble_.ensemble_rank() == 0) {
685 std::string meta = name +
".metadata";
686 std::ofstream file(meta, std::ios::binary | std::ios::trunc);
687 boost::archive::binary_oarchive oa(file);
688 oa << t << timer_cycle << transfer_handle;
691 const int ierr = MPI_Barrier(mpi_ensemble_.ensemble_communicator());
692 AssertThrowMPI(ierr);
696 template <
typename HD,
typename PD,
int dim,
typename Number>
697 template <
typename Callable>
698 void TimeLoop<HD, PD, dim, Number>::adapt_mesh_and_transfer_state_vector(
699 StateVector<Number> &state_vector,
700 const Callable &prepare_compute_kernels)
703 std::cout <<
"TimeLoop<dim, Number>::adapt_mesh_and_transfer_state_vector()"
707 AssertThrow(mpi_ensemble_.n_ensembles() == 1, dealii::ExcNotImplemented());
713 auto &triangulation = discretization_.triangulation();
714 mesh_adaptor_.mark_cells_for_coarsening_and_refinement(triangulation);
716 triangulation.prepare_coarsening_and_refinement();
718 solution_transfer_.prepare_projection(state_vector);
722 triangulation.execute_coarsening_and_refinement();
723 prepare_compute_kernels();
725 hyperbolic_module_.reinit_state_vector(state_vector);
726 parabolic_module_.reinit_state_vector(state_vector);
728 solution_transfer_.project(state_vector);
729 solution_transfer_.reset_handle();
734 template <
typename HD,
typename PD,
int dim,
typename Number>
735 void TimeLoop<HD, PD, dim, Number>::interpolate_analytic_solution(
736 StateVector<Number> &analytic,
const Number t)
739 std::cout <<
"TimeLoop<dim, Number>::interpolate_analytic_solution()"
743 ComputingTimer::Scope scope(
"time step [X] - interpolate data vectors");
745 hyperbolic_module_.reinit_state_vector(analytic);
746 parabolic_module_.reinit_state_vector(analytic);
747 analytic.U = initial_values_.get().interpolate_hyperbolic_vector(t);
750 hyperbolic_system_.get().fill_precomputed_values(
751 offline_data_, analytic,
false);
752 analytic.precomputed.template view<n_precomputed_values>()
753 .update_ghost_values();
757 template <
typename HD,
typename PD,
int dim,
typename Number>
759 TimeLoop<HD, PD, dim, Number>::output(
const StateVector<Number> &state_vector,
760 const std::string &name,
762 const unsigned int cycle)
765 std::cout <<
"TimeLoop<dim, Number>::output(t = " << t <<
")" << std::endl;
768 const bool do_full_output =
769 (cycle % timer_output_full_multiplier_ == 0) && enable_output_full_;
770 const bool do_levelsets =
771 (cycle % timer_output_levelsets_multiplier_ == 0) &&
772 enable_output_levelsets_;
775 if (!(do_full_output || do_levelsets))
780 ComputingTimer::Scope scope(
"time step [X] - perform vtu output");
781 print_info(
"scheduling output");
783 postprocessor_.compute(state_vector);
790 postprocessor_.reset_bounds();
793 mesh_adaptor_.compute_smoothness_indicators(state_vector);
795 vtu_output_.schedule_output(
796 state_vector, name, t, cycle, do_full_output, do_levelsets);
807 template <
typename HD,
typename PD,
int dim,
typename Number>
808 void TimeLoop<HD, PD, dim, Number>::print_parameters(std::ostream &stream)
810 if (mpi_ensemble_.world_rank() != 0)
819 stream << std::endl <<
"Run time parameters:" << std::endl << std::endl;
820 ParameterAcceptor::prm.print_parameters(
821 stream, ParameterHandler::OutputStyle::ShortPRM);
826 std::ofstream output(base_name_ +
"-parameters.prm");
827 ParameterAcceptor::prm.print_parameters(output, ParameterHandler::ShortPRM);
831 template <
typename HD,
typename PD,
int dim,
typename Number>
832 void TimeLoop<HD, PD, dim, Number>::print_mpi_partition(std::ostream &stream)
841 std::vector<double> values = {
842 (double)offline_data_.n_export_indices(),
843 (double)offline_data_.n_locally_internal(),
844 (double)offline_data_.n_locally_owned(),
845 (double)offline_data_.n_locally_relevant(),
846 (double)offline_data_.n_export_indices() /
847 (double)offline_data_.n_locally_relevant(),
848 (double)offline_data_.n_locally_internal() /
849 (double)offline_data_.n_locally_relevant(),
850 (double)offline_data_.n_locally_owned() /
851 (double)offline_data_.n_locally_relevant()};
855 Utilities::MPI::min_max_avg(values, mpi_ensemble_.world_communicator());
857 if (mpi_ensemble_.world_rank() != 0)
860 std::ostringstream output;
863 dealii::Utilities::needed_digits(mpi_ensemble_.n_world_ranks());
865 const auto print_snippet = [&output, n](
const std::string &name,
866 const auto &values) {
867 output << name <<
": ";
869 output << std::setw(9) << (
unsigned int)values.min
870 <<
" [p" << std::setw(n) << values.min_index <<
"] "
871 << std::setw(9) << (
unsigned int)values.avg <<
" "
872 << std::setw(9) << (
unsigned int)values.max
873 <<
" [p" << std::setw(n) << values.max_index <<
"]";
877 const auto print_percentages = [&output, n](
const auto &percentages) {
878 output << std::endl <<
" ";
879 output <<
" (" << std::setw(3) << std::setprecision(2)
880 << percentages.min * 100 <<
"% )"
881 <<
" [p" << std::setw(n) << percentages.min_index <<
"] "
882 <<
" (" << std::setw(3) << std::setprecision(2)
883 << percentages.avg * 100 <<
"% )"
885 <<
" (" << std::setw(3) << std::setprecision(2)
886 << percentages.max * 100 <<
"% )"
887 <<
" [p" << std::setw(n) << percentages.max_index <<
"]";
890 output << std::endl << std::endl <<
"Partition: ";
891 print_snippet(
"exp", data[0]);
892 print_percentages(data[4]);
894 output << std::endl <<
" ";
895 print_snippet(
"int", data[1]);
896 print_percentages(data[5]);
898 output << std::endl <<
" ";
899 print_snippet(
"own", data[2]);
900 print_percentages(data[6]);
902 output << std::endl <<
" ";
903 print_snippet(
"rel", data[3]);
905 stream << output.str() << std::endl;
909 template <
typename HD,
typename PD,
int dim,
typename Number>
911 TimeLoop<HD, PD, dim, Number>::print_device_information(std::ostream &stream)
917#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP)
924 using Device = std::pair<std::string, int>;
926 const Device local{dealii::Utilities::System::get_hostname(),
927 Kokkos::device_id()};
928 const auto all = dealii::Utilities::MPI::all_gather(
929 mpi_ensemble_.world_communicator(), local);
930 n_devices_ = std::set<Device>(all.begin(), all.end()).size();
934 if (mpi_ensemble_.world_rank() != 0)
937 std::ostringstream output;
939 const auto entry [[maybe_unused]] =
940 [&output](
const std::string &label) -> std::ostream & {
942 <<
" " << std::left << std::setw(22) << label;
948 <<
"Device: " << std::left << std::setw(22) <<
"backend"
949 << Kokkos::DefaultExecutionSpace::name();
956#if defined(KOKKOS_ENABLE_CUDA)
957 const auto &prop = Kokkos::Cuda{}.cuda_device_prop();
959 entry(
"device") << prop.name;
960 entry(
"compute capability") << prop.major <<
"." << prop.minor;
961 entry(
"device id") << Kokkos::device_id() <<
" of "
962 << Kokkos::num_devices();
963 entry(
"devices in use")
964 << n_devices_ <<
" on " << mpi_ensemble_.n_world_ranks() <<
" ranks";
965 entry(
"multiprocessors") << prop.multiProcessorCount;
966 entry(
"warp size") << prop.warpSize <<
" (hardware), " <<
warp_size
967 <<
" (ryujin::warp_size)";
968 entry(
"threads per SM")
969 << prop.maxThreadsPerMultiProcessor <<
" ("
970 << prop.maxThreadsPerMultiProcessor / prop.warpSize <<
" warps)";
971 entry(
"threads per block") << prop.maxThreadsPerBlock;
972 entry(
"concurrency") << Kokkos::DefaultExecutionSpace{}.concurrency();
973 entry(
"registers per SM") << prop.regsPerBlock;
974 entry(
"shared memory per SM")
975 << prop.sharedMemPerMultiprocessor / 1024 <<
" KiB";
976 entry(
"L2 cache") << prop.l2CacheSize / 1024 / 1024 <<
" MiB";
977 entry(
"global memory") << prop.totalGlobalMem / 1024 / 1024 <<
" MiB";
979#elif defined(KOKKOS_ENABLE_HIP)
980 const auto &prop = Kokkos::HIP::hip_device_prop();
982 entry(
"device") << prop.name;
983 entry(
"architecture") << prop.gcnArchName;
984 entry(
"device id") << Kokkos::device_id() <<
" of "
985 << Kokkos::num_devices();
986 entry(
"devices in use")
987 << n_devices_ <<
" on " << mpi_ensemble_.n_world_ranks() <<
" ranks";
988 entry(
"compute units") << prop.multiProcessorCount;
989 entry(
"warp size") << prop.warpSize <<
" (hardware), " <<
warp_size
990 <<
" (ryujin::warp_size)";
991 entry(
"threads per CU")
992 << prop.maxThreadsPerMultiProcessor <<
" ("
993 << prop.maxThreadsPerMultiProcessor / prop.warpSize <<
" warps)";
994 entry(
"threads per block") << prop.maxThreadsPerBlock;
995 entry(
"concurrency") << Kokkos::DefaultExecutionSpace{}.concurrency();
996 entry(
"registers per block") << prop.regsPerBlock;
997 entry(
"shared memory per block") << prop.sharedMemPerBlock / 1024 <<
" KiB";
998 entry(
"L2 cache") << prop.l2CacheSize / 1024 / 1024 <<
" MiB";
999 entry(
"global memory") << prop.totalGlobalMem / 1024 / 1024 <<
" MiB";
1002 stream << output.str() << std::endl;
1006 template <
typename HD,
typename PD,
int dim,
typename Number>
1007 void TimeLoop<HD, PD, dim, Number>::print_info(
const std::string &header)
1009 if (mpi_ensemble_.world_rank() != 0)
1012 std::cout <<
"[INFO] " << header << std::endl;
1016 template <
typename HD,
typename PD,
int dim,
typename Number>
1017 void TimeLoop<HD, PD, dim, Number>::print_head(
const std::string &header,
1018 const std::string &secondary,
1019 std::ostream &stream)
1021 if (mpi_ensemble_.world_rank() != 0)
1024 const int header_size = header.size();
1025 const auto padded_header =
1026 std::string(std::max(0, 34 - header_size) / 2,
' ') + header +
1027 std::string(std::max(0, 35 - header_size) / 2,
' ');
1029 const int secondary_size = secondary.size();
1030 const auto padded_secondary =
1031 std::string(std::max(0, 34 - secondary_size) / 2,
' ') + secondary +
1032 std::string(std::max(0, 35 - secondary_size) / 2,
' ');
1036 stream <<
" ####################################################\n";
1037 stream <<
" #########" << padded_header <<
"#########\n";
1038 stream <<
" #########" << padded_secondary <<
"#########\n";
1039 stream <<
" ####################################################\n";
1040 stream << std::endl;
1045 template <
typename HD,
typename PD,
int dim,
typename Number>
1047 TimeLoop<HD, PD, dim, Number>::print_information(
unsigned int timer_cycle,
1048 Number last_checkpoint,
1049 std::ostream &stream,
1052 static const std::string backend_name = [] {
1053 const auto precision = [] {
1054 if constexpr (std::is_same_v<Number, double>)
1055 return std::string(
"FP64");
1056 else if constexpr (std::is_same_v<Number, float>)
1057 return std::string(
"FP32");
1062 constexpr auto simd_size = VectorizedArray<Number>::size();
1065 return "GPU, " + precision +
", warp size " + std::to_string(
warp_size);
1066 else if constexpr (simd_size == 1)
1067 return "scalar, " + precision;
1069 return "SIMD, " + precision +
", width " + std::to_string(simd_size);
1072 stream <<
"Information: (HYP) " << hyperbolic_system_.get().problem_name();
1073 if constexpr (!ParabolicSystem::is_identity) {
1074 stream <<
"\n (PAR) "
1075 << parabolic_system_.get().problem_name();
1077 stream <<
"\n [" << base_name_ <<
"] ";
1078 if (mpi_ensemble_.n_ensembles() > 1) {
1079 stream << mpi_ensemble_.n_ensembles() <<
" ensembles ";
1081 stream <<
"with " << n_global_dofs_ <<
" Qdofs on "
1082 << mpi_ensemble_.n_world_ranks() <<
" ranks "
1083#if defined(WITH_OPENMP)
1084 <<
"/ " << omp_get_max_threads() <<
" threads "
1085#ifndef WITH_DEAL_II_THREADS
1086 <<
"[serial dealii] "
1088#elif defined(WITH_DEAL_II_THREADS)
1089 <<
"/ " << MultithreadInfo::n_threads() <<
" threads "
1091 <<
"<" << backend_name <<
">\n";
1093 stream <<
" Last output cycle "
1095 <<
" at t = " << timer_granularity_ * (timer_cycle - 1)
1098 if (enable_output_full_)
1100 if (enable_output_levelsets_)
1101 stream <<
"levelsets ";
1102 if (enable_compute_error_)
1104 if (enable_compute_quantities_)
1105 stream <<
"quantities ";
1109 if (checkpoint_update_interval_ != Number(0.)) {
1110 const auto wall_time = Utilities::MPI::min_max_avg(
1112 mpi_ensemble_.world_communicator());
1115 stream <<
" Last checkpoint at FINAL TIME\n";
1117 stream <<
" Last checkpoint at wall time "
1118 << std::setprecision(2) << std::fixed << last_checkpoint
1119 <<
"s (" << std::setprecision(0)
1120 << std::max(0., wall_time.max - last_checkpoint)
1121 <<
"s ago, interval " << checkpoint_update_interval_ <<
"s)\n";
1127 template <
typename HD,
typename PD,
int dim,
typename Number>
1129 TimeLoop<HD, PD, dim, Number>::print_memory_statistics(std::ostream &stream)
1131 Utilities::System::MemoryStats stats;
1132 Utilities::System::get_memory_stats(stats);
1134 Utilities::MPI::MinMaxAvg data = Utilities::MPI::min_max_avg(
1135 stats.VmRSS / 1024., mpi_ensemble_.world_communicator());
1137 if (mpi_ensemble_.world_rank() != 0)
1140 std::ostringstream output;
1143 dealii::Utilities::needed_digits(mpi_ensemble_.n_world_ranks());
1145 output <<
"\nMemory: [MiB]"
1146 << std::setw(8) << data.min
1147 <<
" [p" << std::setw(n) << data.min_index <<
"] "
1148 << std::setw(8) << data.avg <<
" "
1149 << std::setw(8) << data.max
1150 <<
" [p" << std::setw(n) << data.max_index <<
"]";
1152 stream << output.str() << std::endl;
1156 template <
typename HD,
typename PD,
int dim,
typename Number>
1157 void TimeLoop<HD, PD, dim, Number>::print_timers(std::ostream &stream)
1161 const auto equalize = [&]() {
1163 std::max_element(output.begin(),
1165 [](
const auto &left,
const auto &right) {
1166 return left.str().length() < right.str().length();
1168 const auto length = ptr->str().length();
1169 for (
auto &it : output)
1170 it << std::string(length - it.str().length() + 1,
' ');
1173 const auto print_wall_time = [&](
auto &timer,
auto &stream) {
1174 const auto wall_time = Utilities::MPI::min_max_avg(
1175 timer.wall_time(), mpi_ensemble_.world_communicator());
1177 constexpr auto eps = std::numeric_limits<double>::epsilon();
1182 const auto skew_negative = std::max(
1183 100. * (wall_time.min - wall_time.avg) / wall_time.avg - eps, -99.9);
1184 const auto skew_positive = std::min(
1185 100. * (wall_time.max - wall_time.avg) / wall_time.avg + eps, 99.9);
1187 stream << std::setprecision(2) << std::fixed << std::setw(9)
1188 << wall_time.avg <<
"s [sk: " << std::setprecision(1)
1189 << std::setw(5) << std::fixed << skew_negative <<
"%/"
1190 << std::setw(4) << std::fixed << skew_positive <<
"%]";
1192 dealii::Utilities::needed_digits(mpi_ensemble_.n_world_ranks());
1193 stream <<
" [p" << std::setw(n) << wall_time.min_index <<
"/"
1194 << wall_time.max_index <<
"]";
1197 const auto cpu_time_statistics = Utilities::MPI::min_max_avg(
1199 mpi_ensemble_.world_communicator());
1200 const double total_cpu_time = cpu_time_statistics.sum;
1202 const auto print_cpu_time =
1203 [&](
auto &timer,
auto &stream,
bool percentage) {
1204 const auto cpu_time = Utilities::MPI::min_max_avg(
1205 timer.cpu_time(), mpi_ensemble_.world_communicator());
1207 stream << std::setprecision(2) << std::fixed << std::setw(12)
1208 << cpu_time.sum <<
"s ";
1211 stream <<
"(" << std::setprecision(1) << std::setw(4)
1212 << 100. * cpu_time.sum / total_cpu_time <<
"%)";
1215 auto jt = output.begin();
1216 for (
auto &it : ComputingTimer::timers())
1217 *jt++ <<
" " << it.first;
1220 jt = output.begin();
1221 for (
auto &it : ComputingTimer::timers())
1222 print_wall_time(it.second, *jt++);
1225 jt = output.begin();
1226 bool compute_percentages =
false;
1227 for (
auto &it : ComputingTimer::timers()) {
1228 print_cpu_time(it.second, *jt++, compute_percentages);
1229 if (it.first.starts_with(
"time loop"))
1230 compute_percentages =
true;
1234 if (mpi_ensemble_.world_rank() != 0)
1237 stream << std::endl <<
"Timer statistics:\n";
1238 for (
auto &it : output)
1239 stream << it.str() << std::endl;
1243 template <
typename HD,
typename PD,
int dim,
typename Number>
1244 void TimeLoop<HD, PD, dim, Number>::print_throughput(
unsigned int cycle,
1246 std::ostream &stream,
1253 static struct Data {
1254 unsigned int cycle = 0;
1256 double cpu_time_sum = 0.;
1257 double cpu_time_avg = 0.;
1258 double cpu_time_min = 0.;
1259 double cpu_time_max = 0.;
1260 double wall_time = 0.;
1261 double device_time_sum = 0.;
1262 } previous, current;
1264 static double time_per_second_exp = 0.;
1271 current.cycle = cycle;
1274 const auto wall_time_statistics = Utilities::MPI::min_max_avg(
1276 mpi_ensemble_.world_communicator());
1277 current.wall_time = wall_time_statistics.max;
1279 const auto cpu_time_statistics = Utilities::MPI::min_max_avg(
1281 mpi_ensemble_.world_communicator());
1282 current.cpu_time_sum = cpu_time_statistics.sum;
1283 current.cpu_time_avg = cpu_time_statistics.avg;
1284 current.cpu_time_min = cpu_time_statistics.min;
1285 current.cpu_time_max = cpu_time_statistics.max;
1288 const auto device_time_statistics = Utilities::MPI::min_max_avg(
1290 current.device_time_sum = device_time_statistics.sum;
1299 double delta_cycles = current.cycle - previous.cycle;
1300 const double cycles_per_second =
1301 delta_cycles / (current.wall_time - previous.wall_time);
1303 const auto efficiency = time_integrator_.efficiency();
1304 const auto n_dofs =
static_cast<double>(n_global_dofs_);
1306 double wall_m_dofs_per_sec = delta_cycles * n_dofs * efficiency / 1.e6 /
1307 (current.wall_time - previous.wall_time);
1309 const double delta_time =
1310 (current.t - previous.t) / (current.cycle - previous.cycle);
1311 const double time_per_second =
1312 (current.t - previous.t) / (current.wall_time - previous.wall_time);
1316 std::ostringstream output;
1319 output << std::endl;
1321 output <<
"Throughput:" << std::endl;
1326 const double delta_wall_time = current.wall_time - previous.wall_time;
1327 const double delta_device_time =
1328 current.device_time_sum - previous.device_time_sum;
1330 const double utilization =
1331 delta_device_time / (n_devices_ * delta_wall_time);
1333 const double device_m_dofs_per_sec =
1334 delta_cycles * n_dofs * efficiency / 1.e6 / delta_device_time;
1336 << std::setprecision(4) << std::fixed << device_m_dofs_per_sec
1337 <<
" MQ/s in compute kernels (on "
1338 << n_devices_ << (n_devices_ == 1 ?
" device)" :
" devices)")
1342 << std::setprecision(2) << std::fixed << delta_device_time
1344 << std::setprecision(2) << std::fixed << delta_wall_time
1346 << std::setprecision(1) << std::fixed << 100. * utilization
1347 <<
"% utilization ]" << std::endl;
1352 double cpu_m_dofs_per_sec = delta_cycles * n_dofs * efficiency / 1.e6 /
1353 (current.cpu_time_sum - previous.cpu_time_sum);
1356 const bool fudge_cpu_timings = terminal_correct_for_hypertreadhing_ &&
1357#if defined(WITH_OPENMP)
1358 (omp_get_max_threads() == 2);
1359#elif defined(WITH_DEAL_II_THREADS)
1360 (MultithreadInfo::n_threads() == 2);
1365 if (fudge_cpu_timings)
1366 cpu_m_dofs_per_sec *= 2.;
1368 double cpu_time_skew = (current.cpu_time_max - current.cpu_time_min -
1369 previous.cpu_time_max + previous.cpu_time_min) /
1372 cpu_time_skew = std::max(0., cpu_time_skew);
1374 const double cpu_time_skew_percentage =
1375 cpu_time_skew * delta_cycles /
1376 (current.cpu_time_avg - previous.cpu_time_avg);
1379 << (fudge_cpu_timings ?
"CPU*: " :
"CPU : ")
1380 << std::setprecision(4) << std::fixed << cpu_m_dofs_per_sec
1382 << std::scientific << 1. / cpu_m_dofs_per_sec * 1.e-6
1383 <<
" s/Qdof/substep)" << std::endl;
1385 output <<
" [cpu time skew: "
1386 << std::setprecision(2) << std::scientific << cpu_time_skew
1388 << std::setprecision(1) << std::setw(4) << std::setfill(
' ') << std::fixed
1389 << 100. * cpu_time_skew_percentage
1390 <<
"%)]" << std::endl;
1394 << std::setprecision(4) << std::fixed << wall_m_dofs_per_sec
1396 << std::scientific << 1. / wall_m_dofs_per_sec * 1.e-6
1397 <<
" s/Qdof/substep) ("
1398 << std::setprecision(2) << std::fixed << cycles_per_second
1399 <<
" cycles/s)" << std::endl;
1401 const auto &scheme = time_integrator_.time_stepping_scheme();
1403 << Patterns::Tools::Convert<TimeSteppingScheme>::to_string(scheme)
1405 << std::setprecision(2) << std::fixed << hyperbolic_module_.cfl()
1407 << std::setprecision(0) << std::fixed << hyperbolic_module_.n_restarts()
1409 << std::setprecision(0) << std::fixed << parabolic_module_.n_restarts()
1411 << std::setprecision(0) << std::fixed << hyperbolic_module_.n_warnings()
1413 << std::setprecision(0) << std::fixed << parabolic_module_.n_warnings()
1415 << std::setprecision(0) << std::fixed << hyperbolic_module_.n_corrections()
1417 << std::setprecision(0) << std::fixed << parabolic_module_.n_corrections()
1418 <<
" corr) ]" << std::endl;
1420 if constexpr (!ParabolicSystem::is_identity)
1421 parabolic_module_.print_solver_statistics(output);
1423 output <<
" [ dt = "
1424 << std::scientific << std::setprecision(2) << delta_time
1427 <<
" dt/s) ]" << std::endl;
1432 time_per_second_exp = 0.8 * time_per_second_exp + 0.2 * time_per_second;
1433 auto eta =
static_cast<unsigned int>(std::max(t_final_ - t, Number(0.)) /
1434 time_per_second_exp);
1436 output <<
"\n ETA : ";
1438 const unsigned int days = eta / (24 * 3600);
1440 output << days <<
" d ";
1444 const unsigned int hours = eta / 3600;
1446 output << hours <<
" h ";
1450 const unsigned int minutes = eta / 60;
1451 output << minutes <<
" min";
1453 output <<
" (terminal update every "
1454 << std::setprecision(2) << std::fixed << terminal_update_interval_
1457 if (mpi_ensemble_.world_rank() != 0)
1460 stream << output.str() << std::endl;
1464 template <
typename HD,
typename PD,
int dim,
typename Number>
1465 void TimeLoop<HD, PD, dim, Number>::print_cycle_statistics(
1468 unsigned int timer_cycle,
1469 Number last_checkpoint,
1470 bool write_to_logfile,
1473 std::ostringstream output;
1477 std::ostringstream primary;
1479 primary <<
"FINAL (cycle " << Utilities::int_to_string(cycle, 6) <<
")";
1481 primary <<
"Cycle " << Utilities::int_to_string(cycle, 6)
1482 <<
" (" << std::fixed << std::setprecision(1)
1483 << t / t_final_ * 100 <<
"%)";
1486 std::ostringstream secondary;
1487 secondary <<
"at time t = " << std::setprecision(8) << std::fixed << t;
1489 print_head(primary.str(), secondary.str(), output);
1493 print_information(timer_cycle, last_checkpoint, output, final_time);
1494 print_memory_statistics(output);
1495 print_timers(output);
1496 print_throughput(cycle, t, output, final_time);
1499 if (mpi_ensemble_.world_rank() != 0)
1503 std::cout <<
"\033[2J\033[H";
1505 std::cout << output.str() << std::flush;
1507 if (write_to_logfile) {
1508 logfile_ <<
"\n" << output.str() << std::flush;
static dealii::Timer & timer(const std::string §ion)
static const std::map< std::string, dealii::Timer > & timers()
TimeLoop(const MPI_Comm &mpi_comm)
constexpr unsigned int warp_size
constexpr bool have_separate_memory_spaces
void debug_poison_invalid_values(StateVector< Number > &state_vector, const OfflineData &offline_data)
void print_revision_and_version(std::ostream &stream)
MultiComponentVector< Number > U