2 * This file is part of the GROMACS molecular simulation package.
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.
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 /*! \libinternal \file
37 * Helper functions for MPI tests to make thread-MPI look like real MPI.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_testutils
43 #ifndef GMX_TESTUTILS_MPITEST_H
44 #define GMX_TESTUTILS_MPITEST_H
50 #include <type_traits>
58 * Returns the number of MPI ranks to use for an MPI test.
60 * For thread-MPI builds, this will return the requested number of ranks
61 * even before the thread-MPI threads have been started.
63 * \ingroup module_testutils
65 int getNumberOfTestMpiRanks();
69 * Helper function for GMX_MPI_TEST().
71 * \ingroup module_testutils
73 bool threadMpiTestRunner(std::function<void()> testBody);
76 /*! \brief Implementation of MPI test runner for thread-MPI
78 * See documentation GMX_MPI_TEST */
80 # define GMX_MPI_TEST_INNER \
83 using MyTestClass = std::remove_reference_t<decltype(*this)>; \
84 if (!::gmx::test::threadMpiTestRunner([this]() { this->MyTestClass::TestBody(); })) \
90 # define GMX_MPI_TEST_INNER
93 /*! \brief Declares that this test is an MPI-enabled unit test and
94 * expresses the conditions under which it can run.
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.
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.
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.
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
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.
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.
138 * \ingroup module_testutils
140 #define GMX_MPI_TEST(RankRequirement) \
141 const int numRanks = ::gmx::test::getNumberOfTestMpiRanks(); \
142 if (!RankRequirement::conditionSatisfied(numRanks)) \
144 GTEST_SKIP() << std::string("Test skipped because ") + RankRequirement::s_skipReason; \
149 //! Helper for GMX_MPI_TEST to permit any rank count
150 class AllowAnyRankCount
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";
160 //! Helper for GMX_MPI_TEST to permit only a specific rank count
161 template<int requiredNumRanks>
162 class RequireRankCount
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";
172 //! Helper for GMX_MPI_TEST to permit only a specific rank count
173 template<int minimumNumRanks>
174 class RequireMinimumRankCount
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";