Apply clang-format-11
[alexxy/gromacs.git] / src / testutils / mpi_printer.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2016,2019,2021, 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:
77     MPIEventForward(TestEventListener* defaultPrinter, int rank, int size) :
78         defaultPrinter_(defaultPrinter), rank_(rank), size_(size)
79     {
80     }
81
82     FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramStart, ::testing::UnitTest);
83     FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationStart, ::testing::UnitTest, int);
84     FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpStart, ::testing::UnitTest);
85     FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsSetUpEnd, ::testing::UnitTest);
86     FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseStart, ::testing::TestCase);
87     FORWARD_TO_DEFAULT_PRINTER1(OnTestStart, ::testing::TestInfo);
88     virtual void OnTestPartResult(const ::testing::TestPartResult& /*result*/)
89     {
90         // Do nothing; all printing is done in OnTestEnd().
91     }
92     virtual void OnTestEnd(const ::testing::TestInfo& test_info);
93     FORWARD_TO_DEFAULT_PRINTER1(OnTestCaseEnd, ::testing::TestCase);
94     FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownStart, ::testing::UnitTest);
95     FORWARD_TO_DEFAULT_PRINTER1(OnEnvironmentsTearDownEnd, ::testing::UnitTest);
96     FORWARD_TO_DEFAULT_PRINTER2(OnTestIterationEnd, ::testing::UnitTest, int);
97     FORWARD_TO_DEFAULT_PRINTER1(OnTestProgramEnd, ::testing::UnitTest);
98
99 private:
100     const std::unique_ptr<TestEventListener> defaultPrinter_;
101     int                                      rank_;
102     int                                      size_;
103
104     GMX_DISALLOW_COPY_AND_ASSIGN(MPIEventForward);
105 };
106
107 void MPIEventForward::OnTestEnd(const ::testing::TestInfo& test_info)
108 {
109     // Serialize printing test results to stdout in rank order by
110     // passing a flag in order from ranks 0 .. (n-1). Rank 0 does not
111     // need to recieve, rank n-1 does not need to send.
112     int timeToPrint = (0 == rank_);
113     if (!timeToPrint)
114     {
115         MPI_Recv(&timeToPrint, 1, MPI_INT, rank_ - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
116     }
117
118     // Now this rank can print
119     int                          localPassed = true;
120     const ::testing::TestResult* result      = test_info.result();
121     if (result->Failed())
122     {
123         // TODO: This sometimes races with the defaultPrinter_, but
124         // unsure why
125         printf("Test failures from rank %d:\n", rank_);
126         for (int i = 0; i < result->total_part_count(); ++i)
127         {
128             /* TODO: A future implementation might forward the
129                contents / output of OnTestPartResult to the master
130                rank to handle all the output there. Currently, the
131                output of the expected and actual behaviour only occurs
132                if the failing ranks include rank 0, and is only
133                indicative of rank 0. */
134             defaultPrinter_->OnTestPartResult(result->GetTestPartResult(i));
135         }
136         localPassed = false;
137     }
138
139     // Pass on the printing token to the next rank
140     if (size_ != rank_ + 1)
141     {
142         MPI_Send(&timeToPrint, 1, MPI_INT, rank_ + 1, 0, MPI_COMM_WORLD);
143     }
144
145     std::vector<int> bDidRankPass(size_);
146     MPI_Gather(&localPassed, 1, MPI_INT, bDidRankPass.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
147
148     if (rank_ == 0)
149     {
150         std::stringstream failingRanks;
151         bool              allPassed = true;
152
153         for (size_t i = 0; i != bDidRankPass.size(); ++i)
154         {
155             if (!bDidRankPass[i])
156             {
157                 if (!allPassed)
158                 {
159                     failingRanks << ",";
160                 }
161                 allPassed = false;
162                 failingRanks << i;
163             }
164         }
165
166         defaultPrinter_->OnTestEnd(test_info);
167
168         if (!allPassed)
169         {
170             ::testing::Test::RecordProperty("AllFailingRanks", failingRanks.str().c_str());
171             // This marks the current test failed, and modifies test_info
172             // behind the scenes, so the default printer sees the test as
173             // failed even if localPassed is true.
174             ADD_FAILURE() << "See AllFailingRanks for the rank IDs that failed. Only if "
175                              "rank 0 was among those that failed, will there be some "
176                              "accompanying information about the failure, and it will "
177                              "pertain only to that rank. Run this test manually to get "
178                              "more information.";
179         }
180     }
181 }
182
183 } // namespace
184
185 #endif
186
187 //! \cond internal
188 void gmx::test::initMPIOutput()
189 {
190 #if GMX_LIB_MPI
191     int size, rank;
192
193     MPI_Comm_size(MPI_COMM_WORLD, &size);
194     if (size == 1)
195     {
196         return;
197     }
198     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
199     ::testing::UnitTest&           unit_test = *::testing::UnitTest::GetInstance();
200     ::testing::TestEventListeners& listeners = unit_test.listeners();
201     ::testing::TestEventListener* defprinter = listeners.Release(listeners.default_result_printer());
202     listeners.Append(new MPIEventForward(defprinter, rank, size));
203     if (0 != rank)
204     {
205         /* Permit only rank 0 to write to the single GTest XML file,
206            by removing the generator on the other ranks. This
207            suppresses races when writing that file. */
208         ::testing::TestEventListener* oldlistener = listeners.Release(listeners.default_xml_generator());
209         delete oldlistener;
210     }
211 #endif
212 }
213 //! \endcond