/usr/include/deal.II/base/timer.h is in libdeal.ii-dev 6.3.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | //---------------------------------------------------------------------------
// $Id: timer.h 21358 2010-06-24 23:38:14Z bangerth $
// Version: $Name$
//
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__timer_h
#define __deal2__timer_h
#include <base/config.h>
#include <base/conditional_ostream.h>
#include <base/thread_management.h>
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
#include <mpi.h>
#include <base/utilities.h>
#endif
#include <string>
#include <list>
#include <map>
DEAL_II_NAMESPACE_OPEN
/**
* This is a very simple class which provides information about both the
* CPU time and the wallclock time elapsed since the timer was started
* last time. Information is retrieved from the system on the basis of
* clock cycles since last time the computer was booted for the CPU
* time. On a SUN workstation, this information is exact to about a
* microsecond.
*
*
* <h3>Usage</h3>
*
* Use of this class is as you might expect by looking at the member
* functions:
* @code
* Time timer;
* timer.start ();
*
* // do some complicated computations here
* ...
*
* timer.stop ();
*
* std::cout << "Elapsed CPU time: " << timer() << " seconds.";
* std::cout << "Elapsed wall time: " << timer.wall_time() << " seconds.";
*
* // reset timer for the next thing it shall do
* timer.reset();
* @endcode
*
* Alternatively, you can also restart the timer instead of resetting
* it. The times between successive calls to start()/ stop() will then be
* accumulated. The usage of this class is also explained in the
* step-12 and step-29 tutorial programs.
*
* @note Implementation of this class is system
* dependent. Unfortunately, it does not work with multithreading
* right now. In this case, we would like to sum up the time needed by
* all children.
*
* @ingroup utilities
* @author G. Kanschat, W. Bangerth
*/
//TODO:[?] make class work with multithreading as well. better docs.
class Timer
{
public:
/**
* Constructor. Starts the timer at 0 sec.
*/
Timer ();
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
/**
* Constructor that takes an MPI
* communicator as input. A timer
* constructed this way will sum up the
* CPU times over all processors in the
* MPI network when requested by the
* operator ().
*
* Starts the timer at 0 sec.
*
* If @p sync_wall_time is true, the wall
* time is synchronized between all CPUs
* using a MPI_Barrier() and a collective
* operation. Note that this only works
* if you stop() the timer before
* querying for the wall time. The time
* for the MPI operations are not
* included in the timing but may slow
* down your program.
*
* This constructor is only available
* if the deal.II compiler is an MPI
* compiler.
*/
Timer (MPI_Comm mpi_communicator,
bool sync_wall_time = false);
/**
* Structure to save collective data
* measured by this timer class. Queried
* by get_data() or printed with
* print_data() after calling stop().
*/
struct TimeMinMaxAvg
{
double sum;
double min;
double max;
unsigned int min_index;
unsigned int max_index;
double avg;
/**
* Set the time values to @p
* val, and the MPI rank to
* the given @p rank.
*/
void set(const double val,
const unsigned int rank)
{
sum = min = max = val;
min_index = max_index = rank;
}
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
/**
* Given two structures
* indicating timer
* information, do the
* reduction by choosing the
* one with the longer time
* interval. This is a max
* operation and therefore a
* reduction.
*
* Arguments are passed as
* void pointers to satisfy
* the MPI requirement of
* reduction operations.
*/
static void max_reduce ( const void * in_lhs_,
void * inout_rhs_,
int * len,
MPI_Datatype * );
#endif
};
/**
* Returns a reference to the data
* structure with global timing
* information. Filled after calling
* stop().
*/
const TimeMinMaxAvg & get_data() const;
/**
* Prints the data to the given stream.
*/
template <class STREAM>
void print_data(STREAM & stream) const;
#endif
/**
* Re-start the timer at the point where
* it was stopped. This way a cumulative
* measurement of time is possible.
*/
void start ();
/**
* Sets the current time as next
* starting time and return the
* elapsed time in seconds.
*/
double stop ();
/**
* Stop the timer if necessary and reset
* the elapsed time to zero.
*/
void reset ();
/**
* Access to the current CPU time
* without disturbing time
* measurement. The elapsed time is
* returned in units of seconds.
*/
double operator() () const;
/**
* Access to the current wall time
* without disturbing time
* measurement. The elapsed time is
* returned in units of seconds.
*/
double wall_time () const;
private:
/**
* Value of the user time when start()
* was called the last time or when the
* object was created and no stop() was
* issued in between.
*/
double start_time;
/**
* Similar to #start_time, but
* needed for children threads
* in multithread mode. Value of
* the user time when start()
* was called the last time or
* when the object was created
* and no stop() was issued in
* between.
*
* For some reason (error in
* operating system?) the
* function call
* <tt>getrusage(RUSAGE_CHILDREN,.)</tt>
* gives always 0 (at least
* on Solaris7). Hence the
* Timer class still does not
* yet work for multithreading
* mode.
*/
double start_time_children;
/**
* Value of the wall time when start()
* was called the last time or when the
* object was created and no stop() was
* issued in between.
*/
double start_wall_time;
/**
* Accumulated time for all previous
* start()/stop() cycles. The time for
* the present cycle is not included.
*/
double cumulative_time;
/**
* Accumulated wall time for all
* previous start()/stop() cycles. The
* wall time for the present cycle is
* not included.
*/
double cumulative_wall_time;
/**
* Store whether the timer is presently
* running.
*/
bool running;
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
/**
* Store whether the timer is presently
* running.
*/
MPI_Comm mpi_communicator;
/**
* Store whether the wall time is
* synchronized between machines.
*/
bool sync_wall_time;
TimeMinMaxAvg mpi_data;
#endif
};
//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
* create several sections that perform certain aspects of the program. A
* section can be entered several times. By changing the options in
* OutputFrequency and OutputType, the user can choose whether output should
* be generated every time a section is joined or just in the end of the
* program. Moreover, it is possible to show CPU times, wall times or both.
*
* <h3>Usage</h3>
*
* Use of this class could be as follows:
* @code
* TimerOuput timer (std::cout, TimerOutput::summary,
* TimerOutput::wall_times);
*
* timer.enter_subsection ("Setup dof system");
* setup_dofs();
* timer.leave_subsection();
*
* timer.enter_subsection ("Assemble");
* assemble_system_1();
* timer.leave_subsection();
*
* timer.enter_subsection ("Solve");
* solve_system_1();
* timer.leave_subsection();
*
* timer.enter_subsection ("Assemble");
* assemble_system_2();
* timer.leave_subsection();
*
* timer.enter_subsection ("Solve");
* solve_system_2();
* timer.leave_subsection();
*
* // do something else...
* @endcode
* When run, this program will return an output like this:
* @code
* +---------------------------------------------+------------+------------+
* | Total wallclock time elapsed since start | 88.8s | |
* | | | |
* | Section | no. calls | wall time | % of total |
* +---------------------------------+-----------+------------+------------+
* | Assemble | 2 | 19.7s | 22% |
* | Solve | 2 | 3.03s | 3.4% |
* | Setup dof system | 1 | 3.97s | 4.5% |
* +---------------------------------+-----------+------------+------------+
* @endcode
* The output will see that we entered the assembly and solve section
* twice, and reports how much time we spent there. Moreover, the class
* measures the total time spent from start to termination of the TimerOutput
* object. In this case, we did a lot of other stuff, so that the time
* proportions of the functions we measured are far away from 100 precent.
*
* @ingroup utilities
* @author M. Kronbichler, 2009.
*/
class TimerOutput
{
public:
/**
* 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;
/**
* 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.
*/
TimerOutput (std::ostream &stream,
const enum OutputFrequency output_frequency,
const enum OutputType output_type);
/**
* Constructor that takes a
* ConditionalOStream to write output to.
*/
TimerOutput (ConditionalOStream &stream,
const enum OutputFrequency output_frequency,
const enum OutputType output_type);
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
/**
* Constructor that takes an MPI
* communicator as input. A timer
* constructed this way will sum up the
* CPU times over all processors in the
* MPI network for calculating the CPU
* time.
*
* Meant for using std::cout as output
* stream.
*/
TimerOutput (MPI_Comm mpi_comm,
std::ostream &stream,
const enum OutputFrequency output_frequency,
const enum OutputType output_type);
/**
* Constructor that takes an MPI
* communicator as input. A timer
* constructed this way will sum up the
* CPU times over all processors in the
* MPI network for calculating the CPU
* time.
*
* Constructor that takes a
* ConditionalOStream to write output to.
*/
TimerOutput (MPI_Comm mpi_comm,
ConditionalOStream &stream,
const enum OutputFrequency output_frequency,
const enum OutputType output_type);
#endif
/**
* 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.
*/
void enter_subsection (const std::string §ion_name);
/**
* Same as @p enter_subsection.
*/
void enter_section (const std::string §ion_name);
/**
* Leave a section. If no name is given,
* the last section that was entered is
* left.
*/
void leave_subsection (const std::string §ion_name = std::string());
/**
* Same as @p leave_subsection.
*/
void exit_section (const std::string §ion_name = std::string());
/**
* Print a formatted table that
* summarizes the time consumed in the
* various sections.
*/
void print_summary () const;
/**
* By calling this function, all output
* can be disabled. This function
* together with enable_output() can be
* useful if one wants to control the
* output in a flexible way without
* putting a lot of <tt>if</tt> clauses
* in the program.
*/
void disable_output ();
/**
* This function re-enables output of
* this class if it was previously
* disabled with disable_output(). This
* function together with
* disable_output() can be useful if
* one wants to control the output in a
* flexible way without putting a lot
* of <tt>if</tt> clauses in the
* program.
*/
void enable_output ();
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;
/**
* 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<std::string, Section> sections;
/**
* The stream object to which we
* are to output.
*/
ConditionalOStream out_stream;
/**
* A boolean variable that sets whether
* output of this class is currently on
* or off.
*/
bool output_is_enabled;
/**
* 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<std::string> active_sections;
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
/**
* Store whether the timer is presently
* running.
*/
MPI_Comm mpi_communicator;
#endif
/**
* A lock that makes sure that this
* class gives reasonable results even
* when used with several threads.
*/
Threads::ThreadMutex mutex;
};
/* ---------------- inline functions ----------------- */
#ifdef DEAL_II_COMPILER_SUPPORTS_MPI
const Timer::TimeMinMaxAvg & Timer::get_data() const
{
return mpi_data;
}
template <class STREAM>
void Timer::print_data(STREAM & stream) const
{
unsigned int my_id = dealii::Utilities::System::get_this_mpi_process(mpi_communicator);
if (my_id==0)
stream << mpi_data.max << " wall,"
<< " max @" << mpi_data.max_index
<< ", min=" << mpi_data.min << " @" << mpi_data.min_index
<< ", avg=" << mpi_data.avg
<< std::endl;
}
#endif
inline
void
TimerOutput::enter_section (const std::string §ion_name)
{
enter_subsection(section_name);
}
inline
void
TimerOutput::exit_section (const std::string §ion_name)
{
leave_subsection(section_name);
}
DEAL_II_NAMESPACE_CLOSE
#endif
|