Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stacked_timer4.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #include "Teuchos_TimeMonitor.hpp"
15 #include "Teuchos_StackedTimer.hpp"
16 #include "Teuchos_DefaultComm.hpp"
17 #include <sstream>
18 #include <thread> // std::this_thread::sleep_for;
19 #include <tuple>
20 #include <regex>
21 #include <iterator>
22 #include <limits>
23 
24 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
25 #include "Kokkos_Core.hpp"
26 #endif
27 
28 // ***************************************************
29 // These tests must be run on exactly 4 MPI processes!
30 // ***************************************************
31 
32 TEUCHOS_UNIT_TEST(StackedTimer, minmax_hist)
33 {
34  Teuchos::StackedTimer timer("L0");
35  timer.stopBaseTimer();
36 
38  TEST_ASSERT(comm->getSize() == 4)
39  const int myRank = Teuchos::rank(*comm);
40 
41  if ((myRank == 1) || (myRank == 2)) {
42  timer.start("T0");
43  timer.stop("T0");
44  if(myRank == 1) {
45  timer.start("T0");
46  timer.stop("T0");
47  timer.start("T0");
48  timer.stop("T0");
49  }
50  const_cast<Teuchos::BaseTimer*>(timer.findBaseTimer("L0@T0"))->setAccumulatedTime(Teuchos::as<double>(myRank));
51  } else {
52  timer.start("T1");
53  timer.stop("T1");
54  if (myRank == 3) {
55  timer.start("T1");
56  timer.stop("T1");
57  }
58  const_cast<Teuchos::BaseTimer*>(timer.findBaseTimer("L0@T1"))->setAccumulatedTime(Teuchos::as<double>(myRank));
59  }
60 
61  // Throws since the mpi aggregation has not been called yet
62  TEST_THROW(timer.getMpiAverageTime("L0@T0"),std::runtime_error);
63  TEST_THROW(timer.getMpiAverageCount("L0@T1"),std::runtime_error);
64  TEST_THROW(timer.isTimer("L0@T1"),std::runtime_error);
65 
67 
68  out << "\n### Printing default report ###" << std::endl;
69  options.output_minmax=true;
70  options.output_proc_minmax=true;
71  options.output_histogram=true;
72  options.num_histogram=2;
73  options.output_fraction=false;
74  timer.report(out, comm, options);
75  {
76  std::ostringstream os;
77  timer.report(os, comm, options);
78  if (myRank == 0) {
79  TEST_ASSERT(os.str().find("min=1, max=2, proc min=1, proc max=2") != std::string::npos);
80  TEST_ASSERT(os.str().find("<1, 1>") != std::string::npos);
81  TEST_ASSERT(os.str().find("min=0, max=3, proc min=0, proc max=3") != std::string::npos);
82 
83  constexpr double tol = 10.0*std::numeric_limits<double>::epsilon();
84  TEST_FLOATING_EQUALITY(timer.getMpiAverageTime("L0@T0"),1.5,tol);
85  TEST_FLOATING_EQUALITY(timer.getMpiAverageTime("L0@T1"),1.5,tol);
86  TEST_FLOATING_EQUALITY(timer.getMpiAverageCount("L0@T0"),2.0,tol);
87  TEST_FLOATING_EQUALITY(timer.getMpiAverageCount("L0@T1"),1.5,tol);
88 
89  TEST_THROW(timer.getMpiAverageTime("INCORRECT TIMER NAME"),std::runtime_error);
90  TEST_THROW(timer.getMpiAverageCount("INCORRECT TIMER NAME"),std::runtime_error);
91 
92  TEST_ASSERT(timer.isTimer("L0@T0"));
93  TEST_ASSERT(timer.isTimer("L0@T1"));
94  TEST_ASSERT(!timer.isTimer("INCORRECT TIMER NAME"));
95  }
96  }
97 }
98 
99 // Use our own main to initialize kokkos before calling
100 // runUnitTestsFromMain(). The kokkos space_time_stack profiler seg
101 // faults due to inconsistent push/pop of timers in the teuchos unit
102 // test startup code. By calling initialize here we can use the
103 // space_time_stack profiler with this unit test.
104 int main( int argc, char* argv[] )
105 {
106  // Note that the dtor for GlobalMPISession will call
107  // Kokkos::finalize().
108  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
109 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
110  Kokkos::initialize(argc,argv);
111 #endif
112  {
113  Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout));
114  out.setOutputToRootOnly(0);
115  }
117 
118  auto return_val = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
119 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
120  if (Kokkos::is_initialized())
121  Kokkos::finalize();
122 #endif
123  return return_val;
124 }
bool isTimer(const std::string &flat_timer_name)
#define TEST_ASSERT(v1)
Assert the given statement is true.
void stop(const std::string &name, const bool pop_kokkos_profiling_region=true)
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
double getMpiAverageCount(const std::string &flat_timer_name)
Initialize, finalize, and query the global MPI session.
Unit testing support.
static int runUnitTestsFromMain(int argc, char *argv[])
Run the unit tests from main() passing in (argc, argv).
void start(const std::string name, const bool push_kokkos_profiling_region=true)
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
The basic timer used internally, uses std::chrono::high_resolution_clock.
Unit testing support.
static void setGloballyReduceTestResult(const bool globallyReduceUnitTestResult)
Set if the unit tests should reduce pass/fail across processes.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
const BaseTimer * findBaseTimer(const std::string &name) const
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Assert the relative floating-point equality of rel_error(v1,v2) &lt;= tol.
int main(int argc, char *argv[])
double getMpiAverageTime(const std::string &flat_timer_name)
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
void report(std::ostream &os)
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Smart reference counting pointer class for automatic garbage collection.
This class allows one to push and pop timers on and off a stack.
Common capabilities for collecting and reporting performance data collectively across MPI processes...