Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SyncTimeMonitor.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 
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 #include "Teuchos_CommHelpers.hpp"
14 #ifdef HAVE_TEUCHOS_MPI
16 #endif // HAVE_TEUCHOS_MPI
17 #include <thread>
18 #include <chrono>
19 
20 // Check that timers are sync'd
21 TEUCHOS_UNIT_TEST(TimeMonitor, SyncTimeMonitor) {
23  using Teuchos::Comm;
24  using Teuchos::outArg;
25  using Teuchos::RCP;
26  using Teuchos::REDUCE_MAX;
27  using Teuchos::reduceAll;
28  using Clock = std::chrono::high_resolution_clock;
29 
30  RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm ();
31 
32  const int numProcs = comm->getSize ();
33  TEST_ASSERT( numProcs > 1 );
34  if (numProcs < 4) {
35  out << "This test requires at least 4 MPI processes." << std::endl;
36  return;
37  }
38 
39  const int myRank = comm->getRank();
40 
41  double time;
42  {
43  Clock::time_point start_time = Clock::now();
44  {
45  SyncTimeMonitor timer(*Teuchos::TimeMonitor::getNewTimer(std::string("myTimer")), comm.ptr());
46  // sleep a second on rank 1 only
47  if (myRank == 1)
48  std::this_thread::sleep_for (std::chrono::seconds(1));
49  }
50  time = std::chrono::duration_cast<std::chrono::duration<double>>(Clock::now() - start_time).count();
51  }
52 
53  std::ostringstream out1;
54  Teuchos::TimeMonitor::summarize(out1,false,true,false,Teuchos::Union,"",true);
55  std::string outStr = out1.str();
56  int test = (outStr.find("myTimer") != std::string::npos);
57  // Check that we took at least a second.
58  int test2 = (time >= 1.0);
59  test = std::min(test, test2);
60 
61  int gblTest = false; // output argument
62  reduceAll<int, int> (*comm, REDUCE_MAX, test, outArg (gblTest));
63  TEST_EQUALITY(gblTest, 1);
64 
65 }
66 
67 // Check that sync'd timers do not hang execution.
68 TEUCHOS_UNIT_TEST(TimeMonitor, HangingSyncTimeMonitor) {
70  using Teuchos::Comm;
71  using Teuchos::outArg;
72  using Teuchos::RCP;
73  using Teuchos::REDUCE_MAX;
74  using Teuchos::reduceAll;
75 
76  RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm ();
77 
78  const int numProcs = comm->getSize ();
79  TEST_ASSERT( numProcs > 1 );
80  if (numProcs < 4) {
81  out << "This test requires at least 4 MPI processes." << std::endl;
82  return;
83  }
84 
85  const int myRank = comm->getRank();
86  int test = false;
87 
88  try {
89  // Setup up a sync'd timer and raise an exception on one rank.
90  {
91  SyncTimeMonitor timer(*Teuchos::TimeMonitor::getNewTimer(std::string("myTimer")), comm.ptr());
92  if (myRank == 1)
93  throw std::runtime_error("Test");
94  }
95  test = true;
96  } catch (const std::runtime_error& e) {
97  test = (myRank == 1);
98  }
99 
100  int gblTest = false; // output argument
101  reduceAll<int, int> (*comm, REDUCE_MAX, test, outArg (gblTest));
102  TEST_EQUALITY(gblTest, 1);
103 
104 }
#define TEST_ASSERT(v1)
Assert the given statement is true.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#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.
A TimeMonitor that waits at a MPI barrier before destruction.
static RCP< Time > getNewTimer(const std::string &name)
Return a new timer with the given name (class method).
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
Print summary statistics for all timers on the given communicator.
Unit testing support.
Implementation of Teuchos wrappers for MPI.
void reduceAll< int, int >(const Comm< int > &comm, const EReductionType reductType, const int count, const int sendBuffer[], int globalReducts[])
Abstract interface for distributed-memory communication.
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Smart reference counting pointer class for automatic garbage collection.
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.