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 // @HEADER
3 
10 #include "Teuchos_DefaultComm.hpp"
11 #include <sstream>
12 #include <thread> // std::this_thread::sleep_for;
13 #include <tuple>
14 #include <regex>
15 #include <iterator>
16 #include <limits>
17 
18 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
19 #include "Kokkos_Core.hpp"
20 #endif
21 
22 // ***************************************************
23 // These tests must be run on exactly 4 MPI processes!
24 // ***************************************************
25 
26 TEUCHOS_UNIT_TEST(StackedTimer, minmax_hist)
27 {
28  Teuchos::StackedTimer timer("L0");
29  timer.stopBaseTimer();
30 
32  TEST_ASSERT(comm->getSize() == 4)
33  const int myRank = Teuchos::rank(*comm);
34 
35  if ((myRank == 1) || (myRank == 2)) {
36  timer.start("T0");
37  timer.stop("T0");
38  if(myRank == 1) {
39  timer.start("T0");
40  timer.stop("T0");
41  timer.start("T0");
42  timer.stop("T0");
43  }
44  const_cast<Teuchos::BaseTimer*>(timer.findBaseTimer("L0@T0"))->setAccumulatedTime(Teuchos::as<double>(myRank));
45  } else {
46  timer.start("T1");
47  timer.stop("T1");
48  if (myRank == 3) {
49  timer.start("T1");
50  timer.stop("T1");
51  }
52  const_cast<Teuchos::BaseTimer*>(timer.findBaseTimer("L0@T1"))->setAccumulatedTime(Teuchos::as<double>(myRank));
53  }
54 
55  // Throws since the mpi aggregation has not been called yet
56  TEST_THROW(timer.getMpiAverageTime("L0@T0"),std::runtime_error);
57  TEST_THROW(timer.getMpiAverageCount("L0@T1"),std::runtime_error);
58  TEST_THROW(timer.isTimer("L0@T1"),std::runtime_error);
59 
61 
62  out << "\n### Printing default report ###" << std::endl;
63  options.output_minmax=true;
64  options.output_proc_minmax=true;
65  options.output_histogram=true;
66  options.num_histogram=2;
67  options.output_fraction=false;
68  timer.report(out, comm, options);
69  {
70  std::ostringstream os;
71  timer.report(os, comm, options);
72  if (myRank == 0) {
73  TEST_ASSERT(os.str().find("min=1, max=2, proc min=1, proc max=2") != std::string::npos);
74  TEST_ASSERT(os.str().find("<1, 1>") != std::string::npos);
75  TEST_ASSERT(os.str().find("min=0, max=3, proc min=0, proc max=3") != std::string::npos);
76 
77  constexpr double tol = 10.0*std::numeric_limits<double>::epsilon();
78  TEST_FLOATING_EQUALITY(timer.getMpiAverageTime("L0@T0"),1.5,tol);
79  TEST_FLOATING_EQUALITY(timer.getMpiAverageTime("L0@T1"),1.5,tol);
80  TEST_FLOATING_EQUALITY(timer.getMpiAverageCount("L0@T0"),2.0,tol);
81  TEST_FLOATING_EQUALITY(timer.getMpiAverageCount("L0@T1"),1.5,tol);
82 
83  TEST_THROW(timer.getMpiAverageTime("INCORRECT TIMER NAME"),std::runtime_error);
84  TEST_THROW(timer.getMpiAverageCount("INCORRECT TIMER NAME"),std::runtime_error);
85 
86  TEST_ASSERT(timer.isTimer("L0@T0"));
87  TEST_ASSERT(timer.isTimer("L0@T1"));
88  TEST_ASSERT(!timer.isTimer("INCORRECT TIMER NAME"));
89  }
90  }
91 }
92 
93 // Use our own main to initialize kokkos before calling
94 // runUnitTestsFromMain(). The kokkos space_time_stack profiler seg
95 // faults due to inconsistent push/pop of timers in the teuchos unit
96 // test startup code. By calling initialize here we can use the
97 // space_time_stack profiler with this unit test.
98 int main( int argc, char* argv[] )
99 {
100  // Note that the dtor for GlobalMPISession will call
101  // Kokkos::finalize().
102  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
103 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
104  Kokkos::initialize(argc,argv);
105 #endif
106  {
107  Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout));
108  out.setOutputToRootOnly(0);
109  }
111 
112  auto return_val = Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
113 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
114  if (Kokkos::is_initialized())
115  Kokkos::finalize();
116 #endif
117  return return_val;
118 }
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...