2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2019,2020, 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"
50 using gmx::test::ThreadAffinityTestHelper
;
52 TEST(ThreadAffinityMultiRankTest
, PinsWholeNode
)
55 ThreadAffinityTestHelper helper
;
56 helper
.setLogicalProcessorCount(4);
57 helper
.expectPinningMessage(false, 1);
58 helper
.expectAffinitySet(gmx_node_rank());
59 helper
.setAffinity(1);
62 TEST(ThreadAffinityMultiRankTest
, PinsWithOffsetAndStride
)
65 ThreadAffinityTestHelper helper
;
66 helper
.setAffinityOption(ThreadAffinity::On
);
67 helper
.setOffsetAndStride(1, 2);
68 helper
.setLogicalProcessorCount(8);
69 helper
.expectWarningMatchingRegex("Applying core pinning offset 1");
70 helper
.expectPinningMessage(true, 2);
71 helper
.expectAffinitySet(1 + 2 * gmx_node_rank());
72 helper
.setAffinity(1);
75 TEST(ThreadAffinityMultiRankTest
, PinsTwoNodes
)
78 ThreadAffinityTestHelper helper
;
79 helper
.setPhysicalNodeId(gmx_node_rank() / 2);
80 helper
.setLogicalProcessorCount(2);
81 helper
.expectPinningMessage(false, 1);
82 helper
.expectAffinitySet(gmx_node_rank() % 2);
83 helper
.setAffinity(1);
86 TEST(ThreadAffinityMultiRankTest
, DoesNothingWhenDisabled
)
89 ThreadAffinityTestHelper helper
;
90 helper
.setAffinityOption(ThreadAffinity::Off
);
91 helper
.setLogicalProcessorCount(4);
92 helper
.setAffinity(1);
95 TEST(ThreadAffinityMultiRankTest
, HandlesTooManyThreadsWithAuto
)
98 ThreadAffinityTestHelper helper
;
99 helper
.setLogicalProcessorCount(6);
100 helper
.expectWarningMatchingRegex("Oversubscribing the CPU");
101 helper
.setAffinity(2);
104 TEST(ThreadAffinityMultiRankTest
, HandlesTooManyThreadsWithForce
)
107 ThreadAffinityTestHelper helper
;
108 helper
.setAffinityOption(ThreadAffinity::On
);
109 helper
.setLogicalProcessorCount(6);
110 helper
.expectWarningMatchingRegex("Oversubscribing the CPU");
111 helper
.setAffinity(2);
114 class ThreadAffinityHeterogeneousNodesTest
: public ::testing::Test
117 static int currentNode() { return gmx_node_rank() / 2; }
118 static int indexInNode() { return gmx_node_rank() % 2; }
119 static bool isMaster() { return gmx_node_rank() == 0; }
121 static void setupNodes(ThreadAffinityTestHelper
* helper
, std::array
<int, 2> cores
)
123 const int node
= currentNode();
124 helper
->setPhysicalNodeId(node
);
125 helper
->setLogicalProcessorCount(cores
[node
]);
127 static void expectNodeAffinitySet(ThreadAffinityTestHelper
* helper
, int node
, int core
)
129 if (currentNode() == node
)
131 helper
->expectAffinitySet(core
);
136 TEST_F(ThreadAffinityHeterogeneousNodesTest
, PinsOnMasterOnly
)
139 ThreadAffinityTestHelper helper
;
140 helper
.setAffinityOption(ThreadAffinity::On
);
141 setupNodes(&helper
, { { 2, 1 } });
142 helper
.expectWarningMatchingRegexIf("Oversubscribing the CPU", isMaster() || currentNode() == 1);
143 if (currentNode() == 0)
145 helper
.expectPinningMessage(false, 1);
147 expectNodeAffinitySet(&helper
, 0, indexInNode());
148 helper
.setAffinity(1);
151 TEST_F(ThreadAffinityHeterogeneousNodesTest
, PinsOnNonMasterOnly
)
154 ThreadAffinityTestHelper helper
;
155 helper
.setAffinityOption(ThreadAffinity::On
);
156 setupNodes(&helper
, { { 1, 2 } });
157 helper
.expectWarningMatchingRegexIf("Oversubscribing the CPU", currentNode() == 0);
158 if (currentNode() == 1)
160 helper
.expectPinningMessage(false, 1);
162 expectNodeAffinitySet(&helper
, 1, indexInNode());
163 helper
.setAffinity(1);
166 TEST_F(ThreadAffinityHeterogeneousNodesTest
, HandlesUnknownHardwareOnNonMaster
)
169 ThreadAffinityTestHelper helper
;
170 helper
.setAffinityOption(ThreadAffinity::On
);
171 setupNodes(&helper
, { { 2, 0 } });
172 helper
.expectWarningMatchingRegexIf("No information on available cores",
173 isMaster() || currentNode() == 1);
174 if (currentNode() == 0)
176 helper
.expectPinningMessage(false, 1);
178 expectNodeAffinitySet(&helper
, 0, indexInNode());
179 helper
.setAffinity(1);
182 TEST_F(ThreadAffinityHeterogeneousNodesTest
, PinsAutomaticallyOnMasterOnly
)
185 ThreadAffinityTestHelper helper
;
186 setupNodes(&helper
, { { 2, 1 } });
187 helper
.expectWarningMatchingRegexIf("Oversubscribing the CPU", isMaster() || currentNode() == 1);
188 if (currentNode() == 0)
190 helper
.expectPinningMessage(false, 1);
192 expectNodeAffinitySet(&helper
, 0, indexInNode());
193 helper
.setAffinity(1);
196 TEST_F(ThreadAffinityHeterogeneousNodesTest
, PinsAutomaticallyOnNonMasterOnly
)
199 ThreadAffinityTestHelper helper
;
200 setupNodes(&helper
, { { 1, 2 } });
201 helper
.expectWarningMatchingRegexIf("Oversubscribing the CPU", currentNode() == 0);
202 if (currentNode() == 1)
204 helper
.expectPinningMessage(false, 1);
206 expectNodeAffinitySet(&helper
, 1, indexInNode());
207 helper
.setAffinity(1);
210 TEST_F(ThreadAffinityHeterogeneousNodesTest
, HandlesInvalidOffsetOnNonMasterOnly
)
213 ThreadAffinityTestHelper helper
;
214 helper
.setAffinityOption(ThreadAffinity::On
);
215 helper
.setOffsetAndStride(2, 0);
216 setupNodes(&helper
, { { 4, 2 } });
217 helper
.expectWarningMatchingRegex("Applying core pinning offset 2");
218 helper
.expectWarningMatchingRegexIf("Requested offset too large", isMaster() || currentNode() == 1);
219 if (currentNode() == 0)
221 helper
.expectPinningMessage(false, 1);
223 expectNodeAffinitySet(&helper
, 0, indexInNode() + 2);
224 helper
.setAffinity(1);
227 TEST_F(ThreadAffinityHeterogeneousNodesTest
, HandlesInvalidStrideOnNonMasterOnly
)
230 ThreadAffinityTestHelper helper
;
231 helper
.setAffinityOption(ThreadAffinity::On
);
232 helper
.setOffsetAndStride(0, 2);
233 setupNodes(&helper
, { { 4, 2 } });
234 helper
.expectWarningMatchingRegexIf("Requested stride too large", isMaster() || currentNode() == 1);
235 if (currentNode() == 0)
237 helper
.expectPinningMessage(true, 2);
239 expectNodeAffinitySet(&helper
, 0, 2 * indexInNode());
240 helper
.setAffinity(1);