Debian Codegraph @

packages/stk/stk_doc_tests/stk_util/TimerHowToParallel.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 //BEGINTimerParallelTests
36 #include "gtest/gtest.h"
37 #include "stk_unit_test_utils/stringAndNumberComparisons.hpp" // for areStringsEqualWithToleran...
38 #include "stk_util/diag/PrintTimer.hpp" // for printTimersTable
39 #include "stk_util/diag/Timer.hpp" // for Timer, createRootTimer
40 #include "stk_util/diag/TimerMetricTraits.hpp" // for METRICS_ALL
41 #include "stk_util/parallel/Parallel.hpp" // for MPI_Comm_rank, MPI_Comm_size
42 #include <unistd.h> // for usleep
43 #include <iosfwd> // for ostringstream
44 #include <string> // for string
45
46 namespace
47 {
48
49 const double tolerance = 0.10;
50
51 void doWork()
52 {
53 ::usleep(1e5);
54 }
55
56 TEST(StkDiagTimerHowTo, useTimersInParallel)
57 {
58 stk::ParallelMachine communicator = MPI_COMM_WORLD;
59 int numProcs = stk::parallel_machine_size(communicator);
60 if(numProcs == 2)
61 {
62 enum {CHILDMASK1 = 1};
63 stk::diag::TimerSet enabledTimerSet(CHILDMASK1);
64 stk::diag::Timer rootTimer = createRootTimer("totalTestRuntime", enabledTimerSet);
65 rootTimer.start();
66
67 stk::diag::Timer childTimer1("childTimer1", CHILDMASK1, rootTimer);
68
69 {
70 stk::diag::TimeBlockSynchronized timerStartSynchronizedAcrossProcessors(childTimer1, communicator);
71 doWork();
72 }
73
74 std::ostringstream outputStream;
75 bool printTimingsOnlySinceLastPrint = false;
76 stk::diag::printTimersTable(outputStream, rootTimer, stk::diag::METRICS_ALL, printTimingsOnlySinceLastPrint, communicator);
77
78 int procId = stk::parallel_machine_rank(communicator);
79 if(procId == 0)
80 {
81 std::string expectedOutput = " \
82 CPU Time CPU Time CPU Time \
83 Wall Time Wall Time Wall Time \
84 Timer Count Sum (% of System) Min (% of System) Max (% of System) \
85 Sum (% of System) Min (% of System) Max (% of System) \
86 SKIP ----- --------------------- --------------------- SKIP SKIP SKIP --------------------- \
87 totalTestRuntime 2 SKIP SKIP SKIP SKIP SKIP SKIP \
88 00:00:00.200 SKIP 00:00:00.100 SKIP 00:00:00.100 SKIP \
89 childTimer1 2 SKIP SKIP SKIP SKIP SKIP SKIP \
90 00:00:00.200 SKIP 00:00:00.100 SKIP 00:00:00.100 SKIP \
91 \
92 Took SKIP seconds to generate the table above. \
93 ";
94 std::cerr<<expectedOutput<<" : "<<outputStream.str()<<std::endl;
95 using stk::unit_test_util::areStringsEqualWithToleranceForNumbers;
96 EXPECT_TRUE(areStringsEqualWithToleranceForNumbers(expectedOutput, outputStream.str(), tolerance));
97 }
98
99 stk::diag::deleteRootTimer(rootTimer);
100 }
101 }
102
103 }
104 //ENDTimerParallelTests