1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
|
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 1998 - 2024 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------
#include <deal.II/base/exceptions.h>
#include <deal.II/base/mpi.h>
#include <deal.II/base/signaling_nan.h>
#include <deal.II/base/timer.h>
#include <deal.II/base/utilities.h>
#include <boost/io/ios_state.hpp>
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <type_traits>
#ifdef DEAL_II_HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef DEAL_II_MSVC
# include <windows.h>
#endif
DEAL_II_NAMESPACE_OPEN
namespace internal
{
namespace TimerImplementation
{
namespace
{
/**
* Type trait for checking whether or not a type is a
* std::chrono::duration.
*/
template <typename T>
struct is_duration : std::false_type
{};
/**
* Specialization to get the right truth value.
*/
template <typename Rep, typename Period>
struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type
{};
/**
* Convert a double precision number with units of seconds into a
* specified duration type T. Only valid when T is a
* std::chrono::duration type.
*/
template <typename T>
T
from_seconds(const double time)
{
static_assert(is_duration<T>::value,
"The template type should be a duration type.");
return T(std::lround(T::period::den * (time / T::period::num)));
}
/**
* Convert a given duration into a double precision number with units of
* seconds.
*/
template <typename Rep, typename Period>
double
to_seconds(const std::chrono::duration<Rep, Period> duration)
{
return Period::num * double(duration.count()) / Period::den;
}
/**
* Fill a MinMaxAvg struct with default values.
*/
void
clear_timing_data(Utilities::MPI::MinMaxAvg &data)
{
data.sum = numbers::signaling_nan<double>();
data.min = numbers::signaling_nan<double>();
data.max = numbers::signaling_nan<double>();
data.avg = numbers::signaling_nan<double>();
data.min_index = numbers::invalid_unsigned_int;
data.max_index = numbers::invalid_unsigned_int;
}
} // namespace
} // namespace TimerImplementation
} // namespace internal
CPUClock::time_point
CPUClock::now() noexcept
{
double system_cpu_duration = 0.0;
#ifdef DEAL_II_MSVC
FILETIME cpuTime, sysTime, createTime, exitTime;
const auto succeeded = GetProcessTimes(
GetCurrentProcess(), &createTime, &exitTime, &sysTime, &cpuTime);
if (succeeded)
{
system_cpu_duration =
(double)(((unsigned long long)cpuTime.dwHighDateTime << 32) |
cpuTime.dwLowDateTime) /
1e7;
}
// keep the zero value if GetProcessTimes didn't work
#elif defined(DEAL_II_HAVE_SYS_RESOURCE_H)
rusage usage;
getrusage(RUSAGE_SELF, &usage);
system_cpu_duration = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
#else
DEAL_II_WARNING("Unsupported platform. Porting not finished.")
#endif
return time_point(
internal::TimerImplementation::from_seconds<duration>(system_cpu_duration));
}
template <typename clock_type_>
Timer::ClockMeasurements<clock_type_>::ClockMeasurements()
: current_lap_start_time(clock_type::now())
, accumulated_time(duration_type::zero())
, last_lap_time(duration_type::zero())
{}
template <typename clock_type_>
void
Timer::ClockMeasurements<clock_type_>::reset()
{
current_lap_start_time = clock_type::now();
accumulated_time = duration_type::zero();
last_lap_time = duration_type::zero();
}
Timer::Timer()
: Timer(MPI_COMM_SELF, /*sync_lap_times=*/false)
{}
Timer::Timer(const MPI_Comm mpi_communicator, const bool sync_lap_times_)
: running(false)
, mpi_communicator(mpi_communicator)
, sync_lap_times(sync_lap_times_)
{
reset();
start();
}
void
Timer::start()
{
running = true;
#ifdef DEAL_II_WITH_MPI
if (sync_lap_times)
{
const int ierr = MPI_Barrier(mpi_communicator);
AssertThrowMPI(ierr);
}
#endif
wall_times.current_lap_start_time = wall_clock_type::now();
cpu_times.current_lap_start_time = cpu_clock_type::now();
}
double
Timer::stop()
{
if (running)
{
running = false;
wall_times.last_lap_time =
wall_clock_type::now() - wall_times.current_lap_start_time;
cpu_times.last_lap_time =
cpu_clock_type::now() - cpu_times.current_lap_start_time;
last_lap_wall_time_data =
Utilities::MPI::min_max_avg(internal::TimerImplementation::to_seconds(
wall_times.last_lap_time),
mpi_communicator);
if (sync_lap_times)
{
wall_times.last_lap_time =
internal::TimerImplementation::from_seconds<
decltype(wall_times)::duration_type>(last_lap_wall_time_data.max);
cpu_times.last_lap_time = internal::TimerImplementation::from_seconds<
decltype(cpu_times)::duration_type>(
Utilities::MPI::min_max_avg(
internal::TimerImplementation::to_seconds(
cpu_times.last_lap_time),
mpi_communicator)
.max);
}
wall_times.accumulated_time += wall_times.last_lap_time;
cpu_times.accumulated_time += cpu_times.last_lap_time;
accumulated_wall_time_data =
Utilities::MPI::min_max_avg(internal::TimerImplementation::to_seconds(
wall_times.accumulated_time),
mpi_communicator);
}
return internal::TimerImplementation::to_seconds(cpu_times.accumulated_time);
}
double
Timer::cpu_time() const
{
if (running)
{
const double running_time = internal::TimerImplementation::to_seconds(
cpu_clock_type::now() - cpu_times.current_lap_start_time +
cpu_times.accumulated_time);
return Utilities::MPI::sum(running_time, mpi_communicator);
}
else
{
return Utilities::MPI::sum(internal::TimerImplementation::to_seconds(
cpu_times.accumulated_time),
mpi_communicator);
}
}
double
Timer::last_cpu_time() const
{
return internal::TimerImplementation::to_seconds(cpu_times.last_lap_time);
}
double
Timer::wall_time() const
{
wall_clock_type::duration current_elapsed_wall_time;
if (running)
current_elapsed_wall_time = wall_clock_type::now() -
wall_times.current_lap_start_time +
wall_times.accumulated_time;
else
current_elapsed_wall_time = wall_times.accumulated_time;
return internal::TimerImplementation::to_seconds(current_elapsed_wall_time);
}
double
Timer::last_wall_time() const
{
return internal::TimerImplementation::to_seconds(wall_times.last_lap_time);
}
void
Timer::reset()
{
wall_times.reset();
cpu_times.reset();
running = false;
internal::TimerImplementation::clear_timing_data(last_lap_wall_time_data);
internal::TimerImplementation::clear_timing_data(accumulated_wall_time_data);
}
/* ---------------------------- TimerOutput -------------------------- */
TimerOutput::TimerOutput(std::ostream &stream,
const OutputFrequency output_frequency,
const OutputType output_type)
: output_frequency(output_frequency)
, output_type(output_type)
, out_stream(stream, true)
, output_is_enabled(true)
, mpi_communicator(MPI_COMM_SELF)
{}
TimerOutput::TimerOutput(ConditionalOStream &stream,
const OutputFrequency output_frequency,
const OutputType output_type)
: output_frequency(output_frequency)
, output_type(output_type)
, out_stream(stream)
, output_is_enabled(true)
, mpi_communicator(MPI_COMM_SELF)
{}
TimerOutput::TimerOutput(const MPI_Comm mpi_communicator,
std::ostream &stream,
const OutputFrequency output_frequency,
const OutputType output_type)
: output_frequency(output_frequency)
, output_type(output_type)
, out_stream(stream, true)
, output_is_enabled(true)
, mpi_communicator(mpi_communicator)
{}
TimerOutput::TimerOutput(const MPI_Comm mpi_communicator,
ConditionalOStream &stream,
const OutputFrequency output_frequency,
const OutputType output_type)
: output_frequency(output_frequency)
, output_type(output_type)
, out_stream(stream)
, output_is_enabled(true)
, mpi_communicator(mpi_communicator)
{}
TimerOutput::~TimerOutput()
{
auto do_exit = [this]() {
try
{
while (active_sections.size() > 0)
leave_subsection();
// don't print unless we leave all subsections
if ((output_frequency == summary ||
output_frequency == every_call_and_summary) &&
output_is_enabled == true)
print_summary();
}
catch (...)
{}
};
// avoid communicating with other processes if there is an uncaught
// exception
#ifdef DEAL_II_WITH_MPI
if (std::uncaught_exceptions() > 0 && mpi_communicator != MPI_COMM_SELF)
{
const unsigned int myid =
Utilities::MPI::this_mpi_process(mpi_communicator);
if (myid == 0)
std::cerr
<< "---------------------------------------------------------\n"
<< "TimerOutput objects finalize timed values printed to the\n"
<< "screen by communicating over MPI in their destructors.\n"
<< "Since an exception is currently uncaught, this\n"
<< "synchronization (and subsequent output) will be skipped\n"
<< "to avoid a possible deadlock.\n"
<< "---------------------------------------------------------"
<< std::endl;
}
else
{
do_exit();
}
#else
do_exit();
#endif
}
void
TimerOutput::enter_subsection(const std::string §ion_name)
{
std::lock_guard<std::mutex> lock(mutex);
Assert(section_name.empty() == false, ExcMessage("Section string is empty."));
Assert(std::find(active_sections.begin(),
active_sections.end(),
section_name) == active_sections.end(),
ExcMessage("Cannot enter the already active section <" + section_name +
">."));
if (sections.find(section_name) == sections.end())
{
if (mpi_communicator != MPI_COMM_SELF)
{
// create a new timer for this section. the second argument
// will ensure that we have an MPI barrier before starting
// and stopping a timer, and this ensures that we get the
// maximum run time for this section over all processors.
// The mpi_communicator from TimerOutput is passed to the
// Timer here, so this Timer will collect timing information
// among all processes inside mpi_communicator.
sections[section_name].timer = Timer(mpi_communicator, true);
}
sections[section_name].total_cpu_time = 0;
sections[section_name].total_wall_time = 0;
sections[section_name].n_calls = 0;
}
sections[section_name].timer.reset();
sections[section_name].timer.start();
++sections[section_name].n_calls;
active_sections.push_back(section_name);
}
void
TimerOutput::leave_subsection(const std::string §ion_name)
{
Assert(!active_sections.empty(),
ExcMessage("Cannot exit any section because none has been entered!"));
std::lock_guard<std::mutex> lock(mutex);
if (!section_name.empty())
{
Assert(sections.find(section_name) != sections.end(),
ExcMessage("Cannot delete a section that was never created."));
Assert(std::find(active_sections.begin(),
active_sections.end(),
section_name) != active_sections.end(),
ExcMessage("Cannot delete a section that has not been entered."));
}
// if no string is given, exit the last
// active section.
const std::string actual_section_name =
(section_name.empty() ? active_sections.back() : section_name);
sections[actual_section_name].timer.stop();
sections[actual_section_name].total_wall_time +=
sections[actual_section_name].timer.last_wall_time();
// Get cpu time. On MPI systems, if constructed with an mpi_communicator
// like MPI_COMM_WORLD, then the Timer will sum up the CPU time between
// processors among the provided mpi_communicator. Therefore, no
// communication is needed here.
const double cpu_time = sections[actual_section_name].timer.last_cpu_time();
sections[actual_section_name].total_cpu_time += cpu_time;
// in case we have to print out something, do that here...
if ((output_frequency == every_call ||
output_frequency == every_call_and_summary) &&
output_is_enabled == true)
{
std::string output_time;
std::ostringstream cpu;
cpu << cpu_time << "s";
std::ostringstream wall;
wall << sections[actual_section_name].timer.last_wall_time() << "s";
if (output_type == cpu_times)
output_time = ", CPU time: " + cpu.str();
else if (output_type == wall_times)
output_time = ", wall time: " + wall.str() + ".";
else
output_time =
", CPU/wall time: " + cpu.str() + " / " + wall.str() + ".";
out_stream << actual_section_name << output_time << std::endl;
}
// delete the index from the list of
// active ones
active_sections.erase(std::find(active_sections.begin(),
active_sections.end(),
actual_section_name));
}
std::map<std::string, double>
TimerOutput::get_summary_data(const OutputData kind) const
{
std::map<std::string, double> output;
for (const auto §ion : sections)
{
switch (kind)
{
case TimerOutput::OutputData::total_cpu_time:
output[section.first] = section.second.total_cpu_time;
break;
case TimerOutput::OutputData::total_wall_time:
output[section.first] = section.second.total_wall_time;
break;
case TimerOutput::OutputData::n_calls:
output[section.first] = section.second.n_calls;
break;
default:
DEAL_II_NOT_IMPLEMENTED();
}
}
return output;
}
void
TimerOutput::print_summary() const
{
// we are going to change the precision and width of output below. store the
// old values so the get restored when exiting this function
const boost::io::ios_base_all_saver restore_stream(out_stream.get_stream());
// get the maximum width among all sections
unsigned int max_width = 0;
for (const auto &i : sections)
max_width = std::max(max_width, static_cast<unsigned int>(i.first.size()));
// 32 is the default width until | character
max_width = std::max(max_width + 1, static_cast<unsigned int>(32));
const std::string extra_dash = std::string(max_width - 32, '-');
const std::string extra_space = std::string(max_width - 32, ' ');
if (output_type != cpu_and_wall_times_grouped)
{
// in case we want to write CPU times
if (output_type != wall_times)
{
double total_cpu_time =
Utilities::MPI::sum(timer_all.cpu_time(), mpi_communicator);
// check that the sum of all times is less or equal than the total
// time. otherwise, we might have generated a lot of overhead in this
// function.
double check_time = 0.;
for (const auto &i : sections)
check_time += i.second.total_cpu_time;
const double time_gap = check_time - total_cpu_time;
if (time_gap > 0.0)
total_cpu_time = check_time;
// generate a nice table
out_stream << "\n\n"
<< "+---------------------------------------------"
<< extra_dash << "+------------"
<< "+------------+\n"
<< "| Total CPU time elapsed since start "
<< extra_space << "|";
out_stream << std::setw(10) << std::setprecision(3) << std::right;
out_stream << total_cpu_time << "s | |\n";
out_stream << "| "
<< extra_space << "| "
<< "| |\n";
out_stream << "| Section " << extra_space
<< "| no. calls |";
out_stream << std::setw(10);
out_stream << std::setprecision(3);
out_stream << " CPU time "
<< " | % of total |\n";
out_stream << "+---------------------------------" << extra_dash
<< "+-----------+------------"
<< "+------------+";
for (const auto &i : sections)
{
std::string name_out = i.first;
// resize the array so that it is always of the same size
unsigned int pos_non_space = name_out.find_first_not_of(' ');
name_out.erase(0, pos_non_space);
name_out.resize(max_width, ' ');
out_stream << std::endl;
out_stream << "| " << name_out;
out_stream << "| ";
out_stream << std::setw(9);
out_stream << i.second.n_calls << " |";
out_stream << std::setw(10);
out_stream << std::setprecision(3);
out_stream << i.second.total_cpu_time << "s |";
out_stream << std::setw(10);
if (total_cpu_time != 0)
{
// if run time was less than 0.1%, just print a zero to avoid
// printing silly things such as "2.45e-6%". otherwise print
// the actual percentage
const double fraction =
i.second.total_cpu_time / total_cpu_time;
if (fraction > 0.001)
{
out_stream << std::setprecision(2);
out_stream << fraction * 100;
}
else
out_stream << 0.0;
out_stream << "% |";
}
else
out_stream << 0.0 << "% |";
}
out_stream << std::endl
<< "+---------------------------------" << extra_dash
<< "+-----------+"
<< "------------+------------+\n"
<< std::endl;
if (time_gap > 0.0)
out_stream
<< std::endl
<< "Note: The sum of counted times is " << time_gap
<< " seconds larger than the total time.\n"
<< "(Timer function may have introduced too much overhead, or different\n"
<< "section timers may have run at the same time.)" << std::endl;
}
// in case we want to write out wallclock times
if (output_type != cpu_times)
{
double total_wall_time = timer_all.wall_time();
// now generate a nice table
out_stream << "\n\n"
<< "+---------------------------------------------"
<< extra_dash << "+------------"
<< "+------------+\n"
<< "| Total wallclock time elapsed since start "
<< extra_space << "|";
out_stream << std::setw(10) << std::setprecision(3) << std::right;
out_stream << total_wall_time << "s | |\n";
out_stream << "| "
<< extra_space << "| "
<< "| |\n";
out_stream << "| Section " << extra_space
<< "| no. calls |";
out_stream << std::setw(10);
out_stream << std::setprecision(3);
out_stream << " wall time | % of total |\n";
out_stream << "+---------------------------------" << extra_dash
<< "+-----------+------------"
<< "+------------+";
for (const auto &i : sections)
{
std::string name_out = i.first;
// resize the array so that it is always of the same size
unsigned int pos_non_space = name_out.find_first_not_of(' ');
name_out.erase(0, pos_non_space);
name_out.resize(max_width, ' ');
out_stream << std::endl;
out_stream << "| " << name_out;
out_stream << "| ";
out_stream << std::setw(9);
out_stream << i.second.n_calls << " |";
out_stream << std::setw(10);
out_stream << std::setprecision(3);
out_stream << i.second.total_wall_time << "s |";
out_stream << std::setw(10);
if (total_wall_time != 0)
{
// if run time was less than 0.1%, just print a zero to avoid
// printing silly things such as "2.45e-6%". otherwise print
// the actual percentage
const double fraction =
i.second.total_wall_time / total_wall_time;
if (fraction > 0.001)
{
out_stream << std::setprecision(2);
out_stream << fraction * 100;
}
else
out_stream << 0.0;
out_stream << "% |";
}
else
out_stream << 0.0 << "% |";
}
out_stream << std::endl
<< "+---------------------------------" << extra_dash
<< "+-----------+"
<< "------------+------------+\n"
<< std::endl;
}
}
else
// output_type == cpu_and_wall_times_grouped
{
const double total_wall_time = timer_all.wall_time();
double total_cpu_time =
Utilities::MPI::sum(timer_all.cpu_time(), mpi_communicator);
// check that the sum of all times is less or equal than the total time.
// otherwise, we might have generated a lot of overhead in this function.
double check_time = 0.;
for (const auto &i : sections)
check_time += i.second.total_cpu_time;
const double time_gap = check_time - total_cpu_time;
if (time_gap > 0.0)
total_cpu_time = check_time;
// generate a nice table
out_stream << "\n\n+---------------------------------------------"
<< extra_dash << "+"
<< "------------+------------+"
<< "------------+------------+" << '\n'
<< "| Total CPU/wall time elapsed since start "
<< extra_space << "|" << std::setw(10) << std::setprecision(3)
<< std::right << total_cpu_time << "s | "
<< extra_space << "|" << std::setw(10) << std::setprecision(3)
<< total_wall_time << "s | |"
<< "\n| "
<< extra_space << "|"
<< " | |"
<< " | |"
<< "\n| Section " << extra_space
<< "| no. calls |"
<< " CPU time | % of total |"
<< " wall time | % of total |"
<< "\n+---------------------------------" << extra_dash
<< "+-----------+"
<< "------------+------------+"
<< "------------+------------+" << std::endl;
for (const auto &i : sections)
{
std::string name_out = i.first;
// resize the array so that it is always of the same size
unsigned int pos_non_space = name_out.find_first_not_of(' ');
name_out.erase(0, pos_non_space);
name_out.resize(max_width, ' ');
out_stream << "| " << name_out << "| ";
out_stream << std::setw(9);
out_stream << i.second.n_calls << " |";
if (output_type != wall_times)
{
out_stream << std::setw(10);
out_stream << std::setprecision(3);
out_stream << i.second.total_cpu_time << "s |";
out_stream << std::setw(10);
if (total_cpu_time != 0)
{
// if run time was less than 0.1%, just print a zero to avoid
// printing silly things such as "2.45e-6%". otherwise print
// the actual percentage
const double fraction =
i.second.total_cpu_time / total_cpu_time;
if (fraction > 0.001)
{
out_stream << std::setprecision(2);
out_stream << fraction * 100;
}
else
out_stream << 0.0;
out_stream << "% |";
}
else
out_stream << 0.0 << "% |";
}
if (output_type != cpu_times)
{
out_stream << std::setw(10);
out_stream << std::setprecision(3);
out_stream << i.second.total_wall_time << "s |";
out_stream << std::setw(10);
if (total_wall_time != 0)
{
// if run time was less than 0.1%, just print a zero to avoid
// printing silly things such as "2.45e-6%". otherwise print
// the actual percentage
const double fraction =
i.second.total_wall_time / total_wall_time;
if (fraction > 0.001)
{
out_stream << std::setprecision(2);
out_stream << fraction * 100;
}
else
out_stream << 0.0;
out_stream << "% |";
}
else
out_stream << 0.0 << "% |";
}
out_stream << std::endl;
}
out_stream << "+---------------------------------" << extra_dash
<< "+-----------+"
<< "------------+------------+"
<< "------------+------------+" << std::endl
<< std::endl;
if (output_type != wall_times && time_gap > 0.0)
out_stream
<< std::endl
<< "Note: The sum of counted times is " << time_gap
<< " seconds larger than the total time.\n"
<< "(Timer function may have introduced too much overhead, or different\n"
<< "section timers may have run at the same time.)" << std::endl;
}
}
void
TimerOutput::print_wall_time_statistics(const MPI_Comm mpi_comm,
const double quantile) const
{
// we are going to change the precision and width of output below. store the
// old values so the get restored when exiting this function
const boost::io::ios_base_all_saver restore_stream(out_stream.get_stream());
AssertDimension(sections.size(),
Utilities::MPI::max(sections.size(), mpi_comm));
Assert(quantile >= 0. && quantile <= 0.5,
ExcMessage("The quantile must be between 0 and 0.5"));
// get the maximum width among all sections
unsigned int max_width = 0;
for (const auto &i : sections)
max_width = std::max(max_width, static_cast<unsigned int>(i.first.size()));
// 17 is the default width until | character
max_width = std::max(max_width + 1, static_cast<unsigned int>(17));
const std::string extra_dash = std::string(max_width - 17, '-');
const std::string extra_space = std::string(max_width - 17, ' ');
// function to print data in a nice table
const auto print_statistics = [&](const double given_time) {
const unsigned int n_ranks = Utilities::MPI::n_mpi_processes(mpi_comm);
if (n_ranks == 1 || quantile == 0.)
{
Utilities::MPI::MinMaxAvg data =
Utilities::MPI::min_max_avg(given_time, mpi_comm);
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << data.min << "s ";
out_stream << std::setw(5) << std::right;
out_stream << data.min_index << (n_ranks > 99999 ? "" : " ") << "|";
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << data.avg << "s |";
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << data.max << "s ";
out_stream << std::setw(5) << std::right;
out_stream << data.max_index << (n_ranks > 99999 ? "" : " ") << "|\n";
}
else
{
const unsigned int my_rank = Utilities::MPI::this_mpi_process(mpi_comm);
std::vector<double> receive_data(my_rank == 0 ? n_ranks : 0);
std::vector<double> result(9);
#ifdef DEAL_II_WITH_MPI
int ierr = MPI_Gather(&given_time,
1,
MPI_DOUBLE,
receive_data.data(),
1,
MPI_DOUBLE,
0,
mpi_comm);
AssertThrowMPI(ierr);
if (my_rank == 0)
{
// fill the received data in a pair and sort; on the way, also
// compute the average
std::vector<std::pair<double, unsigned int>> data_rank;
data_rank.reserve(n_ranks);
for (unsigned int i = 0; i < n_ranks; ++i)
{
data_rank.emplace_back(receive_data[i], i);
result[4] += receive_data[i];
}
result[4] /= n_ranks;
std::sort(data_rank.begin(), data_rank.end());
const unsigned int quantile_index =
static_cast<unsigned int>(std::round(quantile * n_ranks));
AssertIndexRange(quantile_index, data_rank.size());
result[0] = data_rank[0].first;
result[1] = data_rank[0].second;
result[2] = data_rank[quantile_index].first;
result[3] = data_rank[quantile_index].second;
result[5] = data_rank[n_ranks - 1 - quantile_index].first;
result[6] = data_rank[n_ranks - 1 - quantile_index].second;
result[7] = data_rank[n_ranks - 1].first;
result[8] = data_rank[n_ranks - 1].second;
}
ierr = MPI_Bcast(result.data(), 9, MPI_DOUBLE, 0, mpi_comm);
AssertThrowMPI(ierr);
#endif
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << result[0] << "s ";
out_stream << std::setw(5) << std::right;
out_stream << static_cast<unsigned int>(result[1])
<< (n_ranks > 99999 ? "" : " ") << "|";
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << result[2] << "s ";
out_stream << std::setw(5) << std::right;
out_stream << static_cast<unsigned int>(result[3])
<< (n_ranks > 99999 ? "" : " ") << "|";
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << result[4] << "s |";
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << result[5] << "s ";
out_stream << std::setw(5) << std::right;
out_stream << static_cast<unsigned int>(result[6])
<< (n_ranks > 99999 ? "" : " ") << "|";
out_stream << std::setw(10) << std::setprecision(4) << std::right;
out_stream << result[7] << "s ";
out_stream << std::setw(5) << std::right;
out_stream << static_cast<unsigned int>(result[8])
<< (n_ranks > 99999 ? "" : " ") << "|\n";
}
};
// in case we want to write out wallclock times
{
const unsigned int n_ranks = Utilities::MPI::n_mpi_processes(mpi_comm);
const std::string time_rank_column = "------------------+";
const std::string time_rank_space = " |";
// now generate a nice table
out_stream << '\n'
<< "+------------------------------" << extra_dash << "+"
<< time_rank_column
<< (n_ranks > 1 && quantile > 0. ? time_rank_column : "")
<< "------------+"
<< (n_ranks > 1 && quantile > 0. ? time_rank_column : "")
<< time_rank_column << '\n'
<< "| Total wallclock time elapsed " << extra_space << "|";
print_statistics(timer_all.wall_time());
out_stream << "| " << extra_space << "|"
<< time_rank_space
<< (n_ranks > 1 && quantile > 0. ? time_rank_space : "")
<< " "
<< (n_ranks > 1 && quantile > 0. ? time_rank_space : "")
<< time_rank_space << '\n';
out_stream << "| Section " << extra_space << "| no. calls "
<< "| min time rank |";
if (n_ranks > 1 && quantile > 0.)
out_stream << " " << std::setw(5) << std::setprecision(2) << std::right
<< quantile << "-tile rank |";
out_stream << " avg time |";
if (n_ranks > 1 && quantile > 0.)
out_stream << " " << std::setw(5) << std::setprecision(2) << std::right
<< 1. - quantile << "-tile rank |";
out_stream << " max time rank |\n";
out_stream << "+------------------------------" << extra_dash << "+"
<< time_rank_column
<< (n_ranks > 1 && quantile > 0. ? time_rank_column : "")
<< "------------+"
<< (n_ranks > 1 && quantile > 0. ? time_rank_column : "")
<< time_rank_column << '\n';
for (const auto &i : sections)
{
std::string name_out = i.first;
// resize the array so that it is always of the same size
unsigned int pos_non_space = name_out.find_first_not_of(' ');
name_out.erase(0, pos_non_space);
name_out.resize(max_width, ' ');
out_stream << "| " << name_out;
out_stream << "| ";
out_stream << std::setw(9);
out_stream << i.second.n_calls << " |";
print_statistics(i.second.total_wall_time);
}
out_stream << "+------------------------------" << extra_dash << "+"
<< time_rank_column
<< (n_ranks > 1 && quantile > 0. ? time_rank_column : "")
<< "------------+"
<< (n_ranks > 1 && quantile > 0. ? time_rank_column : "")
<< time_rank_column << '\n';
}
}
void
TimerOutput::disable_output()
{
output_is_enabled = false;
out_stream.set_condition(false);
}
void
TimerOutput::enable_output()
{
output_is_enabled = true;
out_stream.set_condition(true);
}
void
TimerOutput::reset()
{
std::lock_guard<std::mutex> lock(mutex);
sections.clear();
active_sections.clear();
timer_all.restart();
}
TimerOutput::Scope::~Scope()
{
try
{
stop();
}
catch (...)
{}
}
DEAL_II_NAMESPACE_CLOSE
|