1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
4 * Copyright (c) 2013,2014,2015,2016,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
8 *
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
31 *
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
34 */
35 #include "gmxpre.h"
36
37 #include "mpi_printer.h"
38
39 #include "config.h"
40
41 #include "gromacs/utility/gmxmpi.h"
42
43 #if GMX_LIB_MPI
44
45 # include <memory>
46 # include <sstream>
47 # include <vector>
48
49 # include <gtest/gtest.h>
50
51 # include "gromacs/utility/classhelpers.h"
52 # include "gromacs/utility/gmxassert.h"
53
54 # define FORWARD_TO_DEFAULT_PRINTER1(MethodName, Param1Type) \
55 virtual void MethodName(const Param1Type& param1) \
56 { \
57 if (rank_ == 0) \
58 { \
59 defaultPrinter_->MethodName(param1); \
60 } \
61 }
62 # define FORWARD_TO_DEFAULT_PRINTER2(MethodName, Param1Type, Param2Type) \
63 virtual void MethodName(const Param1Type& param1, Param2Type param2) \
64 { \
65 if (rank_ == 0) \
66 { \
67 defaultPrinter_->MethodName(param1, param2); \
68 } \
69 }
70
71 namespace
72 {
73
74 class MPIEventForward : public ::testing::TestEventListener
75 {
76 public:
MPIEventForward(TestEventListener * defaultPrinter,int rank,int size)77 MPIEventForward(TestEventListener* defaultPrinter, int rank, int size) :
78 defaultPrinter_(defaultPrinter),
79 rank_(rank),
80 size_(size)
81 {
82 }
83
84 FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramStart, ::testing::UnitTest);
85 FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationStart, ::testing::UnitTest, int);
86 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpStart, ::testing::UnitTest);
87 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpEnd, ::testing::UnitTest);
88 FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseStart, ::testing::TestCase);
89 FORWARD_TO_DEFAULT_PRINTER1(OnTestStart, ::testing::TestInfo);
OnTestPartResult(const::testing::TestPartResult &)90 virtual void OnTestPartResult(const ::testing::TestPartResult& /*result*/)
91 {
92 // Do nothing; all printing is done in OnTestEnd().
93 }
94 virtual void OnTestEnd(const ::testing::TestInfo& test_info);
95 FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseEnd, ::testing::TestCase);
96 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownStart, ::testing::UnitTest);
97 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownEnd, ::testing::UnitTest);
98 FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationEnd, ::testing::UnitTest, int);
99 FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramEnd, ::testing::UnitTest);
100
101 private:
102 const std::unique_ptr<TestEventListener> defaultPrinter_;
103 int rank_;
104 int size_;
105
106 GMX_DISALLOW_COPY_AND_ASSIGN(MPIEventForward);
107 };
108
OnTestEnd(const::testing::TestInfo & test_info)109 void MPIEventForward::OnTestEnd(const ::testing::TestInfo& test_info)
110 {
111 // Serialize printing test results to stdout in rank order by
112 // passing a flag in order from ranks 0 .. (n-1). Rank 0 does not
113 // need to recieve, rank n-1 does not need to send.
114 int timeToPrint = (0 == rank_);
115 if (!timeToPrint)
116 {
117 MPI_Recv(&timeToPrint, 1, MPI_INT, rank_ - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
118 }
119
120 // Now this rank can print
121 int localPassed = true;
122 const ::testing::TestResult* result = test_info.result();
123 if (result->Failed())
124 {
125 // TODO: This sometimes races with the defaultPrinter_, but
126 // unsure why
127 printf("Test failures from rank %d:\n", rank_);
128 for (int i = 0; i < result->total_part_count(); ++i)
129 {
130 /* TODO: A future implementation might forward the
131 contents / output of OnTestPartResult to the master
132 rank to handle all the output there. Currently, the
133 output of the expected and actual behaviour only occurs
134 if the failing ranks include rank 0, and is only
135 indicative of rank 0. */
136 defaultPrinter_->OnTestPartResult(result->GetTestPartResult(i));
137 }
138 localPassed = false;
139 }
140
141 // Pass on the printing token to the next rank
142 if (size_ != rank_ + 1)
143 {
144 MPI_Send(&timeToPrint, 1, MPI_INT, rank_ + 1, 0, MPI_COMM_WORLD);
145 }
146
147 std::vector<int> bDidRankPass(size_);
148 MPI_Gather(&localPassed, 1, MPI_INT, bDidRankPass.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
149
150 if (rank_ == 0)
151 {
152 std::stringstream failingRanks;
153 bool allPassed = true;
154
155 for (size_t i = 0; i != bDidRankPass.size(); ++i)
156 {
157 if (!bDidRankPass[i])
158 {
159 if (!allPassed)
160 {
161 failingRanks << ",";
162 }
163 allPassed = false;
164 failingRanks << i;
165 }
166 }
167
168 defaultPrinter_->OnTestEnd(test_info);
169
170 if (!allPassed)
171 {
172 ::testing::Test::RecordProperty("AllFailingRanks", failingRanks.str().c_str());
173 // This marks the current test failed, and modifies test_info
174 // behind the scenes, so the default printer sees the test as
175 // failed even if localPassed is true.
176 ADD_FAILURE() << "See AllFailingRanks for the rank IDs that failed. Only if "
177 "rank 0 was among those that failed, will there be some "
178 "accompanying information about the failure, and it will "
179 "pertain only to that rank. Run this test manually to get "
180 "more information.";
181 }
182 }
183 }
184
185 } // namespace
186
187 #endif
188
189 //! \cond internal
initMPIOutput()190 void gmx::test::initMPIOutput()
191 {
192 #if GMX_LIB_MPI
193 int size, rank;
194
195 MPI_Comm_size(MPI_COMM_WORLD, &size);
196 if (size == 1)
197 {
198 return;
199 }
200 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
201 ::testing::UnitTest& unit_test = *::testing::UnitTest::GetInstance();
202 ::testing::TestEventListeners& listeners = unit_test.listeners();
203 ::testing::TestEventListener* defprinter = listeners.Release(listeners.default_result_printer());
204 listeners.Append(new MPIEventForward(defprinter, rank, size));
205 if (0 != rank)
206 {
207 /* Permit only rank 0 to write to the single GTest XML file,
208 by removing the generator on the other ranks. This
209 suppresses races when writing that file. */
210 ::testing::TestEventListener* oldlistener = listeners.Release(listeners.default_xml_generator());
211 delete oldlistener;
212 }
213 #endif
214 }
215 //! \endcond
216