Don't use PME ranks with GPUs and -npme=-1
[gromacs/AngularHB.git] / src / testutils / mpi-printer.cpp
blob334bc74592ce2708f75640b864b41cffbc4da539
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013, 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.
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.
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.
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.
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.
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.
35 #include "mpi-printer.h"
37 #include "config.h"
38 #include "gromacs/utility/gmxmpi.h"
40 #ifdef GMX_LIB_MPI
42 #include <sstream>
43 #include <vector>
45 #include <boost/scoped_ptr.hpp>
46 #include <gtest/gtest.h>
48 #include "gromacs/utility/common.h"
49 #include "gromacs/utility/gmxassert.h"
51 #define FORWARD_TO_DEFAULT_PRINTER1(MethodName, Param1Type) \
52 virtual void MethodName(const Param1Type &param1) { \
53 if (rank_ == 0) { \
54 defaultPrinter_->MethodName(param1); \
55 } \
57 #define FORWARD_TO_DEFAULT_PRINTER2(MethodName, Param1Type, Param2Type) \
58 virtual void MethodName(const Param1Type &param1, Param2Type param2) { \
59 if (rank_ == 0) { \
60 defaultPrinter_->MethodName(param1, param2); \
61 } \
64 namespace
67 class MPIEventForward : public ::testing::TestEventListener
69 public:
70 MPIEventForward(TestEventListener* defaultPrinter, int rank, int size)
71 : defaultPrinter_(defaultPrinter), rank_(rank), size_(size)
75 FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramStart, ::testing::UnitTest);
76 FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationStart, ::testing::UnitTest, int);
77 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpStart, ::testing::UnitTest);
78 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpEnd, ::testing::UnitTest);
79 FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseStart, ::testing::TestCase);
80 FORWARD_TO_DEFAULT_PRINTER1(OnTestStart, ::testing::TestInfo);
81 virtual void OnTestPartResult(const ::testing::TestPartResult & /*result*/)
83 // Do nothing; all printing is done in OnTestEnd().
85 virtual void OnTestEnd(const ::testing::TestInfo &test_info);
86 FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseEnd, ::testing::TestCase);
87 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownStart, ::testing::UnitTest);
88 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownEnd, ::testing::UnitTest);
89 FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationEnd, ::testing::UnitTest, int);
90 FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramEnd, ::testing::UnitTest);
92 private:
93 boost::scoped_ptr<TestEventListener> defaultPrinter_;
94 int rank_;
95 int size_;
97 GMX_DISALLOW_COPY_AND_ASSIGN(MPIEventForward);
100 void MPIEventForward::OnTestEnd(const ::testing::TestInfo &test_info)
102 // Serialize printing test results to stdout in rank order by
103 // passing a flag in order from ranks 0 .. (n-1). Rank 0 does not
104 // need to recieve, rank n-1 does not need to send.
105 int timeToPrint = (0 == rank_);
106 if (!timeToPrint)
108 MPI_Recv(&timeToPrint, 1, MPI_INT, rank_ - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
111 // Now this rank can print
112 int localPassed = true;
113 const ::testing::TestResult *result = test_info.result();
114 if (result->Failed())
116 // TODO: This sometimes races with the defaultPrinter_, but
117 // unsure why
118 printf("Test failures from rank %d:\n", rank_);
119 for (int i = 0; i < result->total_part_count(); ++i)
121 /* TODO: A future implementation might forward the
122 contents / output of OnTestPartResult to the master
123 rank to handle all the output there. Currently, the
124 output of the expected and actual behaviour only occurs
125 if the failing ranks include rank 0, and is only
126 indicative of rank 0. */
127 defaultPrinter_->OnTestPartResult(result->GetTestPartResult(i));
129 localPassed = false;
132 // Pass on the printing token to the next rank
133 if (size_ != rank_ + 1)
135 MPI_Send(&timeToPrint, 1, MPI_INT, rank_ + 1, 0, MPI_COMM_WORLD);
138 std::vector<int> bDidRankPass(size_);
139 MPI_Gather(&localPassed, 1, MPI_INT, &(bDidRankPass[0]), 1, MPI_INT, 0, MPI_COMM_WORLD);
141 if (rank_ == 0)
143 std::stringstream failingRanks;
144 bool allPassed = true;
146 for (size_t i = 0; i != bDidRankPass.size(); ++i)
148 if (!bDidRankPass[i])
150 if (!allPassed)
152 failingRanks << ",";
154 allPassed = false;
155 failingRanks << i;
159 defaultPrinter_->OnTestEnd(test_info);
161 if (!allPassed)
163 ::testing::Test::RecordProperty("AllFailingRanks", failingRanks.str().c_str());
164 // This marks the current test failed, and modifies test_info
165 // behind the scenes, so the default printer sees the test as
166 // failed even if localPassed is true.
167 ADD_FAILURE() <<
168 "See AllFailingRanks for the rank IDs that failed. Only if "
169 "rank 0 was among those that failed, will there be some "
170 "accompanying information about the failure, and it will "
171 "pertain only to that rank. Run this test manually to get "
172 "more information.";
177 } // namespace
179 #endif
181 //! \cond internal
182 void gmx::test::initMPIOutput()
184 #ifdef GMX_LIB_MPI
185 int size, rank;
187 MPI_Comm_size(MPI_COMM_WORLD, &size);
188 if (size == 1)
190 return;
192 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
193 ::testing::UnitTest &unit_test = *::testing::UnitTest::GetInstance();
194 ::testing::TestEventListeners &listeners = unit_test.listeners();
195 ::testing::TestEventListener *defprinter =
196 listeners.Release(listeners.default_result_printer());
197 listeners.Append(new MPIEventForward(defprinter, rank, size));
198 if (0 != rank)
200 /* Permit only rank 0 to write to the single GTest XML file,
201 by removing the generator on the other ranks. This
202 suppresses races when writing that file. */
203 ::testing::TestEventListener *oldlistener = listeners.Release(listeners.default_xml_generator());
204 delete oldlistener;
206 #endif
208 //! \endcond