Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
comm
test
TimeMonitor
SyncTimeMonitor.cpp
Go to the documentation of this file.
1
// @HEADER
2
// @HEADER
3
4
#include "
Teuchos_UnitTestHarness.hpp
"
5
#include "
Teuchos_TimeMonitor.hpp
"
6
#include "
Teuchos_DefaultComm.hpp
"
7
#include "
Teuchos_CommHelpers.hpp
"
8
#ifdef HAVE_TEUCHOS_MPI
9
# include "
Teuchos_DefaultMpiComm.hpp
"
10
#endif
// HAVE_TEUCHOS_MPI
11
#include <thread>
12
#include <chrono>
13
14
// Check that timers are sync'd
15
TEUCHOS_UNIT_TEST
(
TimeMonitor
, SyncTimeMonitor) {
16
using
Teuchos::SyncTimeMonitor
;
17
using
Teuchos::Comm
;
18
using
Teuchos::outArg
;
19
using
Teuchos::RCP
;
20
using
Teuchos::REDUCE_MAX
;
21
using
Teuchos::reduceAll
;
22
using
Clock = std::chrono::high_resolution_clock;
23
24
RCP<const Comm<int>
> comm =
Teuchos::DefaultComm<int>::getComm
();
25
26
const
int
numProcs = comm->getSize ();
27
TEST_ASSERT
( numProcs > 1 );
28
if
(numProcs < 4) {
29
out <<
"This test requires at least 4 MPI processes."
<< std::endl;
30
return
;
31
}
32
33
const
int
myRank = comm->getRank();
34
35
double
time;
36
{
37
Clock::time_point start_time = Clock::now();
38
{
39
SyncTimeMonitor timer(*
Teuchos::TimeMonitor::getNewTimer
(std::string(
"myTimer"
)), comm.
ptr
());
40
// sleep a second on rank 1 only
41
if
(myRank == 1)
42
std::this_thread::sleep_for (std::chrono::seconds(1));
43
}
44
time = std::chrono::duration_cast<std::chrono::duration<double>>(Clock::now() - start_time).count();
45
}
46
47
std::ostringstream out1;
48
Teuchos::TimeMonitor::summarize
(out1,
false
,
true
,
false
,
Teuchos::Union
,
""
,
true
);
49
std::string outStr = out1.str();
50
int
test = (outStr.find(
"myTimer"
) != std::string::npos);
51
// Check that we took at least a second.
52
int
test2 = (time >= 1.0);
53
test = std::min(test, test2);
54
55
int
gblTest =
false
;
// output argument
56
reduceAll<int, int> (*comm, REDUCE_MAX, test, outArg (gblTest));
57
TEST_EQUALITY
(gblTest, 1);
58
59
}
60
61
// Check that sync'd timers do not hang execution.
62
TEUCHOS_UNIT_TEST
(
TimeMonitor
, HangingSyncTimeMonitor) {
63
using
Teuchos::SyncTimeMonitor
;
64
using
Teuchos::Comm
;
65
using
Teuchos::outArg
;
66
using
Teuchos::RCP
;
67
using
Teuchos::REDUCE_MAX
;
68
using
Teuchos::reduceAll
;
69
70
RCP<const Comm<int>
> comm =
Teuchos::DefaultComm<int>::getComm
();
71
72
const
int
numProcs = comm->getSize ();
73
TEST_ASSERT
( numProcs > 1 );
74
if
(numProcs < 4) {
75
out <<
"This test requires at least 4 MPI processes."
<< std::endl;
76
return
;
77
}
78
79
const
int
myRank = comm->getRank();
80
int
test =
false
;
81
82
try
{
83
// Setup up a sync'd timer and raise an exception on one rank.
84
{
85
SyncTimeMonitor timer(*
Teuchos::TimeMonitor::getNewTimer
(std::string(
"myTimer"
)), comm.
ptr
());
86
if
(myRank == 1)
87
throw
std::runtime_error(
"Test"
);
88
}
89
test =
true
;
90
}
catch
(
const
std::runtime_error& e) {
91
test = (myRank == 1);
92
}
93
94
int
gblTest =
false
;
// output argument
95
reduceAll<int, int> (*comm, REDUCE_MAX, test, outArg (gblTest));
96
TEST_EQUALITY
(gblTest, 1);
97
98
}
Teuchos_CommHelpers.hpp
Teuchos_DefaultComm.hpp
Teuchos_DefaultMpiComm.hpp
Implementation of Teuchos wrappers for MPI.
TEST_ASSERT
#define TEST_ASSERT(v1)
Assert the given statement is true.
Definition
Teuchos_LocalTestingHelpers.hpp:71
TEST_EQUALITY
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
Definition
Teuchos_LocalTestingHelpers.hpp:87
Teuchos_TimeMonitor.hpp
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Teuchos_UnitTestHarness.hpp
Unit testing support.
TEUCHOS_UNIT_TEST
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Definition
Teuchos_UnitTestHelpers.hpp:83
RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:429
RCP::ptr
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
Definition
Teuchos_RCP.hpp:414
Teuchos::Comm
Abstract interface for distributed-memory communication.
Definition
Teuchos_Comm.hpp:314
Teuchos::Comm::reduceAll
void reduceAll(const Comm< Ordinal > &comm, const ValueTypeReductionOp< Ordinal, Packet > &reductOp, const Ordinal count, const Packet sendBuffer[], Packet globalReducts[])
Wrapper for MPI_Allreduce that takes a custom reduction operator.
Definition
Teuchos_CommHelpers.hpp:1433
Teuchos::DefaultComm::getComm
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
Definition
Teuchos_DefaultComm.hpp:212
Teuchos::Ptr::outArg
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Definition
Teuchos_PtrDecl.hpp:227
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition
Teuchos_RCPDecl.hpp:429
Teuchos::SyncTimeMonitor
A TimeMonitor that waits at a MPI barrier before destruction.
Definition
Teuchos_TimeMonitor.hpp:770
Teuchos::TimeMonitor::summarize
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.
Definition
Teuchos_TimeMonitor.cpp:925
Teuchos::TimeMonitor::getNewTimer
static RCP< Time > getNewTimer(const std::string &name)
Return a new timer with the given name (class method).
Definition
Teuchos_TimeMonitor.hpp:210
TimeMonitor
Scope guard for Time, that can compute MPI collective timer statistics.
Definition
Teuchos_TimeMonitor.hpp:179
Teuchos::Union
@ Union
Definition
Teuchos_PerformanceMonitorBase.hpp:63
Teuchos::REDUCE_MAX
@ REDUCE_MAX
Max.
Definition
Teuchos_EReductionType.hpp:74
Generated by
1.17.0