Generalize constraints on MPI rank counts for tests
[alexxy/gromacs.git] / src / gromacs / mdrunutility / tests / threadaffinity_mpi.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,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 #include "gmxpre.h"
36
37 #include <array>
38
39 #include <gtest/gtest.h>
40
41 #include "gromacs/utility/basenetwork.h"
42
43 #include "testutils/mpitest.h"
44
45 #include "threadaffinitytest.h"
46
47 namespace gmx
48 {
49 namespace test
50 {
51 namespace
52 {
53
54 using gmx::test::ThreadAffinityTestHelper;
55
56 //! Helper to establish test requirements on MPI ranks
57 class RequireEvenRankCountWithAtLeastFourRanks
58 {
59 public:
60     //! Function to require even ranks with at least four ranks
61     static bool conditionSatisfied(const int numRanks)
62     {
63         return (numRanks > 2) && (numRanks % 2 == 0);
64     }
65     //! Text to echo when skipping a test that does not satisfy the requirement
66     inline static const char* s_skipReason = "an even rank count of at least four is required";
67 };
68
69 TEST(ThreadAffinityMultiRankTest, PinsWholeNode)
70 {
71     GMX_MPI_TEST(AllowAnyRankCount);
72     ThreadAffinityTestHelper helper;
73     helper.setLogicalProcessorCount(getNumberOfTestMpiRanks());
74     helper.expectPinningMessage(false, 1);
75     helper.expectAffinitySet(gmx_node_rank());
76     helper.setAffinity(1);
77 }
78
79 TEST(ThreadAffinityMultiRankTest, PinsWithOffsetAndStride)
80 {
81     GMX_MPI_TEST(AllowAnyRankCount);
82     ThreadAffinityTestHelper helper;
83     helper.setAffinityOption(ThreadAffinity::On);
84     helper.setOffsetAndStride(1, 2);
85     helper.setLogicalProcessorCount(2 * getNumberOfTestMpiRanks());
86     helper.expectWarningMatchingRegex("Applying core pinning offset 1");
87     helper.expectPinningMessage(true, 2);
88     helper.expectAffinitySet(1 + 2 * gmx_node_rank());
89     helper.setAffinity(1);
90 }
91
92 TEST(ThreadAffinityMultiRankTest, PinsTwoNodes)
93 {
94     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
95     ThreadAffinityTestHelper helper;
96     helper.setPhysicalNodeId(gmx_node_rank() / 2);
97     helper.setLogicalProcessorCount(2);
98     helper.expectPinningMessage(false, 1);
99     helper.expectAffinitySet(gmx_node_rank() % 2);
100     helper.setAffinity(1);
101 }
102
103 TEST(ThreadAffinityMultiRankTest, DoesNothingWhenDisabled)
104 {
105     GMX_MPI_TEST(AllowAnyRankCount);
106     ThreadAffinityTestHelper helper;
107     helper.setAffinityOption(ThreadAffinity::Off);
108     helper.setLogicalProcessorCount(getNumberOfTestMpiRanks());
109     helper.setAffinity(1);
110 }
111
112 TEST(ThreadAffinityMultiRankTest, HandlesTooManyThreadsWithAuto)
113 {
114     GMX_MPI_TEST(AllowAnyRankCount);
115     ThreadAffinityTestHelper helper;
116     const int                threadsPerRank = 2;
117     helper.setLogicalProcessorCount(threadsPerRank * getNumberOfTestMpiRanks() - 1);
118     helper.expectWarningMatchingRegex("Oversubscribing the CPU");
119     helper.setAffinity(threadsPerRank);
120 }
121
122 TEST(ThreadAffinityMultiRankTest, HandlesTooManyThreadsWithForce)
123 {
124     GMX_MPI_TEST(AllowAnyRankCount);
125     ThreadAffinityTestHelper helper;
126     const int                threadsPerRank = 2;
127     helper.setAffinityOption(ThreadAffinity::On);
128     helper.setLogicalProcessorCount(threadsPerRank * getNumberOfTestMpiRanks() - 1);
129     helper.expectWarningMatchingRegex("Oversubscribing the CPU");
130     helper.setAffinity(threadsPerRank);
131 }
132
133 class ThreadAffinityHeterogeneousNodesTest : public ::testing::Test
134 {
135 public:
136     static int  currentNode() { return gmx_node_rank() / 2; }
137     static int  indexInNode() { return gmx_node_rank() % 2; }
138     static bool isMaster() { return gmx_node_rank() == 0; }
139
140     static void setupNodes(ThreadAffinityTestHelper* helper, int coresOnNodeZero, int coresOnOtherNodes)
141     {
142         const int node = currentNode();
143         helper->setPhysicalNodeId(node);
144         helper->setLogicalProcessorCount(node == 0 ? coresOnNodeZero : coresOnOtherNodes);
145     }
146     static void expectNodeAffinitySet(ThreadAffinityTestHelper* helper, int node, int core)
147     {
148         if (currentNode() == node)
149         {
150             helper->expectAffinitySet(core);
151         }
152     }
153 };
154
155 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsOnMasterOnly)
156 {
157     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
158     ThreadAffinityTestHelper helper;
159     helper.setAffinityOption(ThreadAffinity::On);
160     setupNodes(&helper, 2, 1);
161     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", isMaster() || currentNode() == 1);
162     if (currentNode() == 0)
163     {
164         helper.expectPinningMessage(false, 1);
165     }
166     expectNodeAffinitySet(&helper, 0, indexInNode());
167     helper.setAffinity(1);
168 }
169
170 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsOnNonMasterOnly)
171 {
172     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
173     ThreadAffinityTestHelper helper;
174     helper.setAffinityOption(ThreadAffinity::On);
175     setupNodes(&helper, 1, 2);
176     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", currentNode() == 0);
177     if (currentNode() >= 1)
178     {
179         helper.expectPinningMessage(false, 1);
180         expectNodeAffinitySet(&helper, currentNode(), indexInNode());
181     }
182     helper.setAffinity(1);
183 }
184
185 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesUnknownHardwareOnNonMaster)
186 {
187     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
188     ThreadAffinityTestHelper helper;
189     helper.setAffinityOption(ThreadAffinity::On);
190     setupNodes(&helper, 2, 0);
191     helper.expectWarningMatchingRegexIf("No information on available cores",
192                                         isMaster() || currentNode() == 1);
193     if (currentNode() == 0)
194     {
195         helper.expectPinningMessage(false, 1);
196     }
197     expectNodeAffinitySet(&helper, 0, indexInNode());
198     helper.setAffinity(1);
199 }
200
201 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsAutomaticallyOnMasterOnly)
202 {
203     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
204     ThreadAffinityTestHelper helper;
205     setupNodes(&helper, 2, 1);
206     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", isMaster() || currentNode() == 1);
207     if (currentNode() == 0)
208     {
209         helper.expectPinningMessage(false, 1);
210     }
211     expectNodeAffinitySet(&helper, 0, indexInNode());
212     helper.setAffinity(1);
213 }
214
215 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsAutomaticallyOnNonMasterOnly)
216 {
217     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
218     ThreadAffinityTestHelper helper;
219     setupNodes(&helper, 1, 2);
220     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", currentNode() == 0);
221     if (currentNode() >= 1)
222     {
223         helper.expectPinningMessage(false, 1);
224         expectNodeAffinitySet(&helper, currentNode(), indexInNode());
225     }
226     helper.setAffinity(1);
227 }
228
229 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesInvalidOffsetOnNonMasterOnly)
230 {
231     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
232     ThreadAffinityTestHelper helper;
233     helper.setAffinityOption(ThreadAffinity::On);
234     helper.setOffsetAndStride(2, 0);
235     setupNodes(&helper, 4, 2);
236     helper.expectWarningMatchingRegex("Applying core pinning offset 2");
237     helper.expectWarningMatchingRegexIf("Requested offset too large", isMaster() || currentNode() >= 1);
238     if (currentNode() == 0)
239     {
240         helper.expectPinningMessage(false, 1);
241     }
242     expectNodeAffinitySet(&helper, 0, indexInNode() + 2);
243     helper.setAffinity(1);
244 }
245
246 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesInvalidStrideOnNonMasterOnly)
247 {
248     GMX_MPI_TEST(RequireEvenRankCountWithAtLeastFourRanks);
249     ThreadAffinityTestHelper helper;
250     helper.setAffinityOption(ThreadAffinity::On);
251     helper.setOffsetAndStride(0, 2);
252     setupNodes(&helper, 4, 2);
253     helper.expectWarningMatchingRegexIf("Requested stride too large", isMaster() || currentNode() == 1);
254     if (currentNode() == 0)
255     {
256         helper.expectPinningMessage(true, 2);
257     }
258     expectNodeAffinitySet(&helper, 0, 2 * indexInNode());
259     helper.setAffinity(1);
260 }
261
262 } // namespace
263 } // namespace test
264 } // namespace gmx