/usr/include/trilinos/Teuchos_PerformanceMonitorBase.hpp is in libtrilinos-teuchos-dev 12.12.1-5.
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 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef TEUCHOS_PERFORMANCEMONITORBASE_H
#define TEUCHOS_PERFORMANCEMONITORBASE_H
/*! \file Teuchos_PerformanceMonitorBase.hpp
\brief Provides common capabilities for collecting and reporting
performance data across processors
*/
#include "Teuchos_ConfigDefs.hpp"
#include "Teuchos_Array.hpp"
#include "Teuchos_Comm.hpp"
#include "Teuchos_RCP.hpp"
#include "Teuchos_TableFormat.hpp"
#include <cstdlib> // atexit
namespace Teuchos
{
/// \brief Set operation type for \c mergeCounterNames() to perform.
///
/// The \c mergeCounterNames() function merges sets of counter names
/// over all MPI processes in a communicator. Different MPI
/// processes may have created different sets of counters. This
/// enum allows the caller to specify how mergeCounterNames() picks
/// the global set of timers.
enum ECounterSetOp { Intersection, Union };
/// \brief Merge counter names over all processors.
///
/// Different MPI processes may have created different sets of
/// counters. Use this function to reconcile the sets among
/// processes, either by computing their intersection or their
/// union. This is done using a reduction to MPI Rank 0 (relative
/// to the given communicator) and a broadcast to all processes
/// participating in the communicator. We use a
/// reduce-and-broadcast rather than just a reduction, so that all
/// participating processes can use the resulting list of global
/// names as lookup keys for computing global statistics.
///
/// \param comm [in] Communicator over which to merge.
///
/// \param localNames [in] The calling MPI process' list of (local)
/// counter names.
///
/// \param globalNames [out] On output, on each MPI process: the
/// results of merging the counter names.
///
/// \param setOp [in] If Intersection, globalNames on output
/// contains the intersection of all sets of counter names. If
/// Union, globalNames on output contains the union of all sets of
/// counter names.
void
mergeCounterNames (const Comm<int>& comm,
const Array<std::string>& localNames,
Array<std::string>& globalNames,
const ECounterSetOp setOp);
/// \class PerformanceMonitorBase
///
/// \brief Common capabilities for collecting and reporting
/// performance data across processors.
///
/// PerformanceMonitorBase is templated on a counter type T (which
/// might be a timer or a flop counter). The common capability of the
/// counter type is a counter for the number of calls. Derived counter
/// types may supply additional features.
///
/// PerformanceMonitorBase's constructor increments its counter's
/// call count. Subclasses of PerformanceMonitorBase may do more
/// upon construction or destruction; for example, TimeMonitor
/// starts its timer on construction and stops it on destruction.
///
/// This class keeps a static list of all counters created using the
/// getNewCounter() method during the course of a run. Counts from
/// this list can then be printed out at the end of the run.
/// Subclasses of PerformanceMonitorBase, such as TimeMonitor, may
/// use this list to do things like compute global timer statistics
/// over all the MPI processes.
///
/// PerformanceMonitorBase requires that the counter type T provide at
/// least the following interface:
///
/// \code
/// // Constructor taking an std::string argument (the counter name).
/// T (const std::string&);
///
/// // Return the name of the counter.
/// const std::string& name () const;
///
/// // Add one to the number of calls (the number of times the counter
/// // was started).
/// void incrementNumCalls ();
///
/// // Return the number of calls (see incrementNumCalls () above).
/// int numCalls () const;
///
/// // Indicate whether the counter is already running.
/// bool isRunning () const;
/// \endcode
///
template <class T>
class PerformanceMonitorBase
{
public:
/** \brief Construct with a counter. */
PerformanceMonitorBase(T& counter_in, bool reset=false)
: counter_(counter_in), isRecursiveCall_(counter_.isRunning())
{
(void) reset; // get rid of "unused parameter" warning
counter_.incrementNumCalls ();
}
/// \brief Destructor.
///
/// The destructor for the base class does nothing. We provide a
/// virtual destructor for memory safety of derived classes.
virtual ~PerformanceMonitorBase() {}
/** \brief Create a new counter with the specified name and add it
* to a global set of counters of this type.
*
* If the counter already exists, just return the existing
* counter. If the counter doesn't already exist, create a new
* counter with that name and return it.
*
* New counters should usually be created in this way rather than
* through a direct constructor call. This lets
* PerformanceMonitorBase keep track of them, so that methods like
* summarize() and report() know about them. Timers created in
* other ways are not included in the reports printed by these
* methods.
*/
static RCP<T> getNewCounter (const std::string& name);
private:
/// \brief Free the singleton returned by format().
///
/// \warning Only for use as atexit() handler.
///
/// \warning This method is not reentrant. In particular, if
/// multiple threads call this method at the same time, they
/// might manage to double-delete format_. This could only
/// happen if the format() method below is called twice by
/// different threads.
static void freeTableFormat () {
if (format_ != NULL) {
delete format_;
format_ = NULL;
}
}
/// \brief Free the singleton returned by counters().
///
/// \warning Only for use as atexit() handler.
///
/// \warning This method is not reentrant. In particular, if
/// multiple threads call this method at the same time, they
/// might manage to double-delete counters_. This could only
/// happen if the counters() method below is called twice by
/// different threads.
static void freeCounters () {
if (counters_ != NULL) {
delete counters_;
counters_ = NULL;
}
}
public:
/// \brief Table format that will be used to print a summary of
/// timer results.
///
/// \warning This method is not reentrant. In particular, if
/// multiple threads call this method at the same time, they
/// might manage to double-register the atexit() handler for
/// format_. This could only happen if this method is called
/// twice by different threads.
static TableFormat& format ()
{
if (format_ == NULL) {
format_ = new TableFormat ();
// It _is_ possible for atexit() to fail (e.g., because it ran
// out of memory for storing callbacks). We could throw an
// exception here in that case, but I think it's better just
// to let the minor memory leak happen.
(void) atexit (freeTableFormat);
}
TEUCHOS_TEST_FOR_EXCEPTION(
format_ == NULL, std::logic_error, "Teuchos::PerformanceMonitorBase::"
"format: Should never get here! format_ is NULL.");
return *format_;
}
/// \brief Return the first counter with the given name, or null if none.
///
/// It is currently possible to create multiple counters with the
/// same name using \c getNewCounter(). If multiple counters with
/// the given name exist, this method simply returns the first in
/// the list. Do not rely on the ability to create multiple
/// counters with the same name; this may go away in the future.
static RCP<T>
lookupCounter (const std::string& name);
/// \brief "Forget" about all counters created with getNewCounter().
///
/// This removes all counters from the current set of counters (as
/// would be returned by counters()).
static void clearCounters ();
/// \brief "Forget" about all counters created with getNewCounter().
///
/// This removes all counters from the current list of counters
/// (as would be returned by counters()).
///
/// \warning This method is DEPRECATED, because the name is
/// inaccurate (the template parameter of PerformanceMonitorBase
/// may be any kind of performance counter, not just a timer).
/// Use clearCounters() instead.
static TEUCHOS_DEPRECATED void clearTimers ();
/// \brief "Forget" about any counters with the given name.
///
/// If one or more counters with the given name was created using
/// getNewCounter(), calling this method with that name will
/// remove them from the global list of counters.
static void clearCounter (const std::string& name);
/// \brief "Forget" about any counters with the given name.
///
/// If one or more counters with the given name was created using
/// getNewCounter(), calling this method with that name will
/// remove them from the global list of counters.
///
/// \warning This method is DEPRECATED, because the name is
/// inaccurate (the template parameter of PerformanceMonitorBase
/// may be any kind of performance counter, not just a timer).
/// Use clearCounter() instead.
static TEUCHOS_DEPRECATED void clearTimer (const std::string& name);
protected:
//! Constant access to the instance's counter reference.
const T& counter() const { return counter_; }
//! Nonconstant access to the instance's counter reference.
T& counter() { return counter_; }
/// \brief Whether we are currently in a recursive call of the counter.
///
/// Subclasses of PerformanceMonitorBase may use this information
/// to control whether to start or stop the given counter. This
/// matters in cases such as timing, where we don't want to start
/// and stop timers multiple times within a single call stack.
bool isRecursiveCall() const { return isRecursiveCall_; }
/// \brief Array of all counters that were created with
/// getNewCounter() on the calling (MPI) process.
///
/// \warning This method is not reentrant.
static std::map<std::string, RCP<T> >& counters ()
{
if (counters_ == NULL) {
counters_ = new std::map<std::string, RCP<T> > ();
// It _is_ possible for atexit() to fail (e.g., because it ran
// out of memory for storing callbacks). We could throw an
// exception here in that case, but I think it's better just
// to let the minor memory leak happen.
(void) atexit (freeCounters);
}
TEUCHOS_TEST_FOR_EXCEPTION(
counters_ == NULL, std::logic_error, "Teuchos::PerformanceMonitorBase::"
"counters: Should never get here! counters_ is NULL.");
return *counters_;
}
private:
//! Singleton object returned by format().
static TableFormat* format_;
//! Singleton object returned by counters().
static std::map<std::string, RCP<T> >* counters_;
//! Reference to the counter being wrapped.
T& counter_;
//! Whether we are currently in a recursive call of the counter.
bool isRecursiveCall_;
};
template<class T>
TableFormat*
PerformanceMonitorBase<T>::format_ = NULL;
template<class T>
std::map<std::string, RCP<T> >*
PerformanceMonitorBase<T>::counters_ = NULL;
template<class T>
RCP<T>
PerformanceMonitorBase<T>::getNewCounter (const std::string& name)
{
typedef std::map<std::string, RCP<T> > map_type;
typedef typename map_type::iterator iter_type;
map_type& ctrs = counters ();
iter_type it = ctrs.find (name);
RCP<T> newCounter = null;
if (it == ctrs.end ()) {
newCounter = rcp (new T (name));
#ifdef HAVE_TEUCHOS_DEBUG
const bool wasNotThere = ctrs.insert (std::make_pair (name, newCounter)).second;
TEUCHOS_TEST_FOR_EXCEPTION(
! wasNotThere, std::logic_error,
"getNewCounter: insert() claims that timer \"" << name << "\" was "
"already there in the map, even though find() claims that it was not. "
"Please report this bug to the Teuchos developers.");
#else
// Use the returned iterator to optimize insertion.
ctrs.insert (it, std::make_pair (name, newCounter));
#endif // HAVE_TEUCHOS_DEBUG
} else {
newCounter = it->second;
#ifdef HAVE_TEUCHOS_DEBUG
TEUCHOS_TEST_FOR_EXCEPTION(
it->second.is_null (), std::logic_error,
"getNewCounter: Timer \"" << name << "\" was already there in the map, "
"but looking it up by name resulted in a null timer. "
"Please report this bug to the Teuchos developers.");
TEUCHOS_TEST_FOR_EXCEPTION(
name != it->second->name (), std::logic_error,
"getNewCounter: Timer \"" << name << "\" was already there in the map, "
"but looking it up by name resulted in a timer with a different name \""
<< it->second->name () << "\". Please report this bug to the Teuchos "
"developers.");
#endif // HAVE_TEUCHOS_DEBUG
}
#ifdef HAVE_TEUCHOS_DEBUG
TEUCHOS_TEST_FOR_EXCEPTION(
newCounter.is_null (), std::logic_error,
"getNewCounter: At end of method, when creating timer \"" << name
<< "\", newCounter is null. Please report this bug to the Teuchos "
"developers.");
#endif // HAVE_TEUCHOS_DEBUG
return newCounter;
}
template<class T>
RCP<T>
PerformanceMonitorBase<T>::lookupCounter (const std::string& name)
{
typedef std::map<std::string, RCP<T> > map_type;
typedef typename map_type::iterator iter_type;
map_type& ctrs = counters ();
iter_type it = ctrs.find (name);
if (it == ctrs.end ()) {
return null;
} else {
return it->second;
}
}
template<class T>
void
PerformanceMonitorBase<T>::clearCounter (const std::string& name)
{
counters ().erase (name);
}
template<class T>
void
PerformanceMonitorBase<T>::clearTimer (const std::string& name)
{
clearCounter (name);
}
template<class T>
void
PerformanceMonitorBase<T>::clearTimers ()
{
clearCounters ();
}
template<class T>
void
PerformanceMonitorBase<T>::clearCounters ()
{
counters ().clear ();
}
} // namespace Teuchos
#endif // TEUCHOS_PERFORMANCEMONITORBASE_H
|