2 * This file is part of the GROMACS molecular simulation package.
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.
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.
39 #include <gtest/gtest.h>
41 #include "gromacs/utility/basenetwork.h"
43 #include "testutils/mpitest.h"
45 #include "threadaffinitytest.h"
54 using gmx::test::ThreadAffinityTestHelper;
56 //! Helper to establish test requirements on MPI ranks
57 class RequireEvenRankCountWithAtLeastFourRanks
60 //! Function to require even ranks with at least four ranks
61 static bool conditionSatisfied(const int numRanks)
63 return (numRanks > 2) && (numRanks % 2 == 0);
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";
69 TEST(ThreadAffinityMultiRankTest, PinsWholeNode)
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);
79 TEST(ThreadAffinityMultiRankTest, PinsWithOffsetAndStride)
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);
92 TEST(ThreadAffinityMultiRankTest, PinsTwoNodes)
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);
103 TEST(ThreadAffinityMultiRankTest, DoesNothingWhenDisabled)
105 GMX_MPI_TEST(AllowAnyRankCount);
106 ThreadAffinityTestHelper helper;
107 helper.setAffinityOption(ThreadAffinity::Off);
108 helper.setLogicalProcessorCount(getNumberOfTestMpiRanks());
109 helper.setAffinity(1);
112 TEST(ThreadAffinityMultiRankTest, HandlesTooManyThreadsWithAuto)
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);
122 TEST(ThreadAffinityMultiRankTest, HandlesTooManyThreadsWithForce)
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);
133 class ThreadAffinityHeterogeneousNodesTest : public ::testing::Test
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; }
140 static void setupNodes(ThreadAffinityTestHelper* helper, int coresOnNodeZero, int coresOnOtherNodes)
142 const int node = currentNode();
143 helper->setPhysicalNodeId(node);
144 helper->setLogicalProcessorCount(node == 0 ? coresOnNodeZero : coresOnOtherNodes);
146 static void expectNodeAffinitySet(ThreadAffinityTestHelper* helper, int node, int core)
148 if (currentNode() == node)
150 helper->expectAffinitySet(core);
155 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsOnMasterOnly)
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)
164 helper.expectPinningMessage(false, 1);
166 expectNodeAffinitySet(&helper, 0, indexInNode());
167 helper.setAffinity(1);
170 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsOnNonMasterOnly)
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)
179 helper.expectPinningMessage(false, 1);
180 expectNodeAffinitySet(&helper, currentNode(), indexInNode());
182 helper.setAffinity(1);
185 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesUnknownHardwareOnNonMaster)
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)
195 helper.expectPinningMessage(false, 1);
197 expectNodeAffinitySet(&helper, 0, indexInNode());
198 helper.setAffinity(1);
201 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsAutomaticallyOnMasterOnly)
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)
209 helper.expectPinningMessage(false, 1);
211 expectNodeAffinitySet(&helper, 0, indexInNode());
212 helper.setAffinity(1);
215 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsAutomaticallyOnNonMasterOnly)
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)
223 helper.expectPinningMessage(false, 1);
224 expectNodeAffinitySet(&helper, currentNode(), indexInNode());
226 helper.setAffinity(1);
229 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesInvalidOffsetOnNonMasterOnly)
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)
240 helper.expectPinningMessage(false, 1);
242 expectNodeAffinitySet(&helper, 0, indexInNode() + 2);
243 helper.setAffinity(1);
246 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesInvalidStrideOnNonMasterOnly)
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)
256 helper.expectPinningMessage(true, 2);
258 expectNodeAffinitySet(&helper, 0, 2 * indexInNode());
259 helper.setAffinity(1);