Generalize constraints on MPI rank counts for tests
[alexxy/gromacs.git] / src / testutils / include / testutils / mpitest.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2019,2020,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 /*! \libinternal \file
36  * \brief
37  * Helper functions for MPI tests to make thread-MPI look like real MPI.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_testutils
42  */
43 #ifndef GMX_TESTUTILS_MPITEST_H
44 #define GMX_TESTUTILS_MPITEST_H
45
46 #include "config.h"
47
48 #include <functional>
49 #include <string>
50 #include <type_traits>
51
52 namespace gmx
53 {
54 namespace test
55 {
56
57 /*! \brief
58  * Returns the number of MPI ranks to use for an MPI test.
59  *
60  * For thread-MPI builds, this will return the requested number of ranks
61  * even before the thread-MPI threads have been started.
62  *
63  * \ingroup module_testutils
64  */
65 int getNumberOfTestMpiRanks();
66
67 //! \cond internal
68 /*! \brief
69  * Helper function for GMX_MPI_TEST().
70  *
71  * \ingroup module_testutils
72  */
73 bool threadMpiTestRunner(std::function<void()> testBody);
74 //! \endcond
75
76 /*! \brief Implementation of MPI test runner for thread-MPI
77  *
78  * See documentation GMX_MPI_TEST */
79 #if GMX_THREAD_MPI
80 #    define GMX_MPI_TEST_INNER                                                                  \
81         do                                                                                      \
82         {                                                                                       \
83             using MyTestClass = std::remove_reference_t<decltype(*this)>;                       \
84             if (!::gmx::test::threadMpiTestRunner([this]() { this->MyTestClass::TestBody(); })) \
85             {                                                                                   \
86                 return;                                                                         \
87             }                                                                                   \
88         } while (0)
89 #else
90 #    define GMX_MPI_TEST_INNER
91 #endif
92
93 /*! \brief Declares that this test is an MPI-enabled unit test and
94  * expresses the conditions under which it can run.
95  *
96  * To write unit tests that run under MPI, you need to do a few things:
97  *  - Put GMX_MPI_TEST(RankRequirement) as the first statement in your
98  *    test body and either declare or use a suitable class as its
99  *    argument to express what requirements exist on the number of MPI
100  *    ranks for this test.
101  *  - Declare your unit test in CMake with gmx_add_mpi_unit_test().
102  *    Note that all tests in the binary should fulfill the conditions above.
103  *
104  * When you do the above, the following will happen:
105  *  - The test will get compiled only if thread-MPI or real MPI is enabled.
106  *  - The test will get executed only when the specified condition on
107  *    the the number of ranks is satisfied.
108  *  - If you are using real MPI, the whole test binary is run under
109  *    MPI and test execution across the processes is synchronized
110  *    (GMX_MPI_TEST() actually has no effect in this case, the
111  *    synchronization is handled at a higher level).
112  *  - If you are using thread-MPI, GMX_MPI_TEST() is required and it
113  *    initializes thread-MPI with the specified number of threads and
114  *    runs the rest of the test on each of the threads.
115  *
116  * \param[in] RankRequirement Class that expresses the necessary
117  *     conditions on the number of MPI ranks for the test to continue.
118  *     If run with unsupported number of ranks, the remainder of the
119  *     test body is skipped, and the GTEST_SKIP() mechanism used to
120  *     report the reason why the number of MPI ranks is unsuitable.
121  *
122  * The RankRequirement class must have two static members; a static
123  * method \c bool conditionSatisfied(const int) that can be passed the
124  * number of ranks present at run time and return whether the test can
125  * run with that number of ranks, and a static const string \c
126  * s_skipReason describing the reason why the test cannot be run, when
127  * that is the case.
128  *
129  * You need to be extra careful for variables in the test fixture, if you use
130  * one: when run under thread-MPI, these will be shared across all the ranks,
131  * while under real MPI, these are naturally different for each process.
132  * Local variables in the test body are private to each rank in both cases.
133  *
134  * Currently, it is not possible to require the use of a single MPI
135  * rank, because that will lead to problems with (at least)
136  * thread-MPI, but such tests can be written as serial tests anyway.
137  *
138  * \ingroup module_testutils
139  */
140 #define GMX_MPI_TEST(RankRequirement)                                                         \
141     const int numRanks = ::gmx::test::getNumberOfTestMpiRanks();                              \
142     if (!RankRequirement::conditionSatisfied(numRanks))                                       \
143     {                                                                                         \
144         GTEST_SKIP() << std::string("Test skipped because ") + RankRequirement::s_skipReason; \
145         return;                                                                               \
146     }                                                                                         \
147     GMX_MPI_TEST_INNER;
148
149 //! Helper for GMX_MPI_TEST to permit any rank count
150 class AllowAnyRankCount
151 {
152 public:
153     /*! \brief Function called by GMX_MPI_CONDITIONAL_TEST to see
154      * whether the test conditions are satisifed */
155     static bool conditionSatisfied(const int /* numRanks */) { return true; }
156     //! Reason to echo when skipping the test
157     inline static const char* s_skipReason = "UNUSED - any rank count satisfies";
158 };
159
160 //! Helper for GMX_MPI_TEST to permit only a specific rank count
161 template<int requiredNumRanks>
162 class RequireRankCount
163 {
164 public:
165     //! Function to require a specific number of ranks
166     static bool conditionSatisfied(const int numRanks) { return numRanks == requiredNumRanks; }
167     //! Text to echo when skipping a test that does not satisfy the requirement
168     inline static const std::string s_skipReason =
169             std::to_string(requiredNumRanks) + " ranks are required";
170 };
171
172 //! Helper for GMX_MPI_TEST to permit only a specific rank count
173 template<int minimumNumRanks>
174 class RequireMinimumRankCount
175 {
176 public:
177     //! Function to require at least the minimum number of ranks
178     static bool conditionSatisfied(const int numRanks) { return numRanks >= minimumNumRanks; }
179     //! Text to echo when skipping a test that does not satisfy the requirement
180     inline static const std::string s_skipReason =
181             std::to_string(minimumNumRanks) + " or more ranks are required";
182 };
183
184
185 } // namespace test
186 } // namespace gmx
187
188 #endif