Import cmake Modules/FindCUDA.cmake
[gromacs.git] / src / testutils / mpi-printer.cpp
blob1d09b365b2073ecbbfe854d1989a04290ed72886
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2016, 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 "gmxpre.h"
37 #include "mpi-printer.h"
39 #include "config.h"
41 #include "gromacs/utility/gmxmpi.h"
43 #if GMX_LIB_MPI
45 #include <memory>
46 #include <sstream>
47 #include <vector>
49 #include <gtest/gtest.h>
51 #include "gromacs/utility/classhelpers.h"
52 #include "gromacs/utility/gmxassert.h"
54 #define FORWARD_TO_DEFAULT_PRINTER1(MethodName, Param1Type) \
55 virtual void MethodName(const Param1Type &param1) { \
56 if (rank_ == 0) { \
57 defaultPrinter_->MethodName(param1); \
58 } \
60 #define FORWARD_TO_DEFAULT_PRINTER2(MethodName, Param1Type, Param2Type) \
61 virtual void MethodName(const Param1Type &param1, Param2Type param2) { \
62 if (rank_ == 0) { \
63 defaultPrinter_->MethodName(param1, param2); \
64 } \
67 namespace
70 class MPIEventForward : public ::testing::TestEventListener
72 public:
73 MPIEventForward(TestEventListener* defaultPrinter, int rank, int size)
74 : defaultPrinter_(defaultPrinter), rank_(rank), size_(size)
78 FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramStart, ::testing::UnitTest);
79 FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationStart, ::testing::UnitTest, int);
80 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpStart, ::testing::UnitTest);
81 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpEnd, ::testing::UnitTest);
82 FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseStart, ::testing::TestCase);
83 FORWARD_TO_DEFAULT_PRINTER1(OnTestStart, ::testing::TestInfo);
84 virtual void OnTestPartResult(const ::testing::TestPartResult & /*result*/)
86 // Do nothing; all printing is done in OnTestEnd().
88 virtual void OnTestEnd(const ::testing::TestInfo &test_info);
89 FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseEnd, ::testing::TestCase);
90 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownStart, ::testing::UnitTest);
91 FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownEnd, ::testing::UnitTest);
92 FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationEnd, ::testing::UnitTest, int);
93 FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramEnd, ::testing::UnitTest);
95 private:
96 const std::unique_ptr<TestEventListener> defaultPrinter_;
97 int rank_;
98 int size_;
100 GMX_DISALLOW_COPY_AND_ASSIGN(MPIEventForward);
103 void MPIEventForward::OnTestEnd(const ::testing::TestInfo &test_info)
105 // Serialize printing test results to stdout in rank order by
106 // passing a flag in order from ranks 0 .. (n-1). Rank 0 does not
107 // need to recieve, rank n-1 does not need to send.
108 int timeToPrint = (0 == rank_);
109 if (!timeToPrint)
111 MPI_Recv(&timeToPrint, 1, MPI_INT, rank_ - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
114 // Now this rank can print
115 int localPassed = true;
116 const ::testing::TestResult *result = test_info.result();
117 if (result->Failed())
119 // TODO: This sometimes races with the defaultPrinter_, but
120 // unsure why
121 printf("Test failures from rank %d:\n", rank_);
122 for (int i = 0; i < result->total_part_count(); ++i)
124 /* TODO: A future implementation might forward the
125 contents / output of OnTestPartResult to the master
126 rank to handle all the output there. Currently, the
127 output of the expected and actual behaviour only occurs
128 if the failing ranks include rank 0, and is only
129 indicative of rank 0. */
130 defaultPrinter_->OnTestPartResult(result->GetTestPartResult(i));
132 localPassed = false;
135 // Pass on the printing token to the next rank
136 if (size_ != rank_ + 1)
138 MPI_Send(&timeToPrint, 1, MPI_INT, rank_ + 1, 0, MPI_COMM_WORLD);
141 std::vector<int> bDidRankPass(size_);
142 MPI_Gather(&localPassed, 1, MPI_INT, bDidRankPass.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
144 if (rank_ == 0)
146 std::stringstream failingRanks;
147 bool allPassed = true;
149 for (size_t i = 0; i != bDidRankPass.size(); ++i)
151 if (!bDidRankPass[i])
153 if (!allPassed)
155 failingRanks << ",";
157 allPassed = false;
158 failingRanks << i;
162 defaultPrinter_->OnTestEnd(test_info);
164 if (!allPassed)
166 ::testing::Test::RecordProperty("AllFailingRanks", failingRanks.str().c_str());
167 // This marks the current test failed, and modifies test_info
168 // behind the scenes, so the default printer sees the test as
169 // failed even if localPassed is true.
170 ADD_FAILURE() <<
171 "See AllFailingRanks for the rank IDs that failed. Only if "
172 "rank 0 was among those that failed, will there be some "
173 "accompanying information about the failure, and it will "
174 "pertain only to that rank. Run this test manually to get "
175 "more information.";
180 } // namespace
182 #endif
184 //! \cond internal
185 void gmx::test::initMPIOutput()
187 #if GMX_LIB_MPI
188 int size, rank;
190 MPI_Comm_size(MPI_COMM_WORLD, &size);
191 if (size == 1)
193 return;
195 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
196 ::testing::UnitTest &unit_test = *::testing::UnitTest::GetInstance();
197 ::testing::TestEventListeners &listeners = unit_test.listeners();
198 ::testing::TestEventListener *defprinter =
199 listeners.Release(listeners.default_result_printer());
200 listeners.Append(new MPIEventForward(defprinter, rank, size));
201 if (0 != rank)
203 /* Permit only rank 0 to write to the single GTest XML file,
204 by removing the generator on the other ranks. This
205 suppresses races when writing that file. */
206 ::testing::TestEventListener *oldlistener = listeners.Release(listeners.default_xml_generator());
207 delete oldlistener;
209 #endif
211 //! \endcond