Rename all source files from - to _ for consistency.
[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, 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
48 {
49
50 using gmx::test::ThreadAffinityTestHelper;
51
52 TEST(ThreadAffinityMultiRankTest, PinsWholeNode)
53 {
54     GMX_MPI_TEST(4);
55     ThreadAffinityTestHelper helper;
56     helper.setLogicalProcessorCount(4);
57     helper.expectPinningMessage(false, 1);
58     helper.expectAffinitySet(gmx_node_rank());
59     helper.setAffinity(1);
60 }
61
62 TEST(ThreadAffinityMultiRankTest, PinsWithOffsetAndStride)
63 {
64     GMX_MPI_TEST(4);
65     ThreadAffinityTestHelper helper;
66     helper.setAffinityOption(threadaffON);
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);
73 }
74
75 TEST(ThreadAffinityMultiRankTest, PinsTwoNodes)
76 {
77     GMX_MPI_TEST(4);
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);
84 }
85
86 TEST(ThreadAffinityMultiRankTest, DoesNothingWhenDisabled)
87 {
88     GMX_MPI_TEST(4);
89     ThreadAffinityTestHelper helper;
90     helper.setAffinityOption(threadaffOFF);
91     helper.setLogicalProcessorCount(4);
92     helper.setAffinity(1);
93 }
94
95 TEST(ThreadAffinityMultiRankTest, HandlesTooManyThreadsWithAuto)
96 {
97     GMX_MPI_TEST(4);
98     ThreadAffinityTestHelper helper;
99     helper.setLogicalProcessorCount(6);
100     helper.expectWarningMatchingRegex("Oversubscribing the CPU");
101     helper.setAffinity(2);
102 }
103
104 TEST(ThreadAffinityMultiRankTest, HandlesTooManyThreadsWithForce)
105 {
106     GMX_MPI_TEST(4);
107     ThreadAffinityTestHelper helper;
108     helper.setAffinityOption(threadaffON);
109     helper.setLogicalProcessorCount(6);
110     helper.expectWarningMatchingRegex("Oversubscribing the CPU");
111     helper.setAffinity(2);
112 }
113
114 class ThreadAffinityHeterogeneousNodesTest : public ::testing::Test
115 {
116     public:
117         int currentNode() const { return gmx_node_rank() / 2; }
118         int indexInNode() const { return gmx_node_rank() % 2; }
119         bool isMaster() const { return gmx_node_rank() == 0; }
120
121         void setupNodes(ThreadAffinityTestHelper *helper, std::array<int, 2> cores)
122         {
123             const int node = currentNode();
124             helper->setPhysicalNodeId(node);
125             helper->setLogicalProcessorCount(cores[node]);
126         }
127         void expectNodeAffinitySet(ThreadAffinityTestHelper *helper, int node, int core)
128         {
129             if (currentNode() == node)
130             {
131                 helper->expectAffinitySet(core);
132             }
133         }
134 };
135
136 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsOnMasterOnly)
137 {
138     GMX_MPI_TEST(4);
139     ThreadAffinityTestHelper helper;
140     helper.setAffinityOption(threadaffON);
141     setupNodes(&helper, {{2, 1}});
142     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", isMaster() || currentNode() == 1);
143     if (currentNode() == 0)
144     {
145         helper.expectPinningMessage(false, 1);
146     }
147     expectNodeAffinitySet(&helper, 0, indexInNode());
148     helper.setAffinity(1);
149 }
150
151 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsOnNonMasterOnly)
152 {
153     GMX_MPI_TEST(4);
154     ThreadAffinityTestHelper helper;
155     helper.setAffinityOption(threadaffON);
156     setupNodes(&helper, {{1, 2}});
157     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", currentNode() == 0);
158     if (currentNode() == 1)
159     {
160         helper.expectPinningMessage(false, 1);
161     }
162     expectNodeAffinitySet(&helper, 1, indexInNode());
163     helper.setAffinity(1);
164 }
165
166 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesUnknownHardwareOnNonMaster)
167 {
168     GMX_MPI_TEST(4);
169     ThreadAffinityTestHelper helper;
170     helper.setAffinityOption(threadaffON);
171     setupNodes(&helper, {{2, 0}});
172     helper.expectWarningMatchingRegexIf("No information on available cores", isMaster() || currentNode() == 1);
173     if (currentNode() == 0)
174     {
175         helper.expectPinningMessage(false, 1);
176     }
177     expectNodeAffinitySet(&helper, 0, indexInNode());
178     helper.setAffinity(1);
179 }
180
181 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsAutomaticallyOnMasterOnly)
182 {
183     GMX_MPI_TEST(4);
184     ThreadAffinityTestHelper helper;
185     setupNodes(&helper, {{2, 1}});
186     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", isMaster() || currentNode() == 1);
187     if (currentNode() == 0)
188     {
189         helper.expectPinningMessage(false, 1);
190     }
191     expectNodeAffinitySet(&helper, 0, indexInNode());
192     helper.setAffinity(1);
193 }
194
195 TEST_F(ThreadAffinityHeterogeneousNodesTest, PinsAutomaticallyOnNonMasterOnly)
196 {
197     GMX_MPI_TEST(4);
198     ThreadAffinityTestHelper helper;
199     setupNodes(&helper, {{1, 2}});
200     helper.expectWarningMatchingRegexIf("Oversubscribing the CPU", currentNode() == 0);
201     if (currentNode() == 1)
202     {
203         helper.expectPinningMessage(false, 1);
204     }
205     expectNodeAffinitySet(&helper, 1, indexInNode());
206     helper.setAffinity(1);
207 }
208
209 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesInvalidOffsetOnNonMasterOnly)
210 {
211     GMX_MPI_TEST(4);
212     ThreadAffinityTestHelper helper;
213     helper.setAffinityOption(threadaffON);
214     helper.setOffsetAndStride(2, 0);
215     setupNodes(&helper, {{4, 2}});
216     helper.expectWarningMatchingRegex("Applying core pinning offset 2");
217     helper.expectWarningMatchingRegexIf("Requested offset too large", isMaster() || currentNode() == 1);
218     if (currentNode() == 0)
219     {
220         helper.expectPinningMessage(false, 1);
221     }
222     expectNodeAffinitySet(&helper, 0, indexInNode()+2);
223     helper.setAffinity(1);
224 }
225
226 TEST_F(ThreadAffinityHeterogeneousNodesTest, HandlesInvalidStrideOnNonMasterOnly)
227 {
228     GMX_MPI_TEST(4);
229     ThreadAffinityTestHelper helper;
230     helper.setAffinityOption(threadaffON);
231     helper.setOffsetAndStride(0, 2);
232     setupNodes(&helper, {{4, 2}});
233     helper.expectWarningMatchingRegexIf("Requested stride too large", isMaster() || currentNode() == 1);
234     if (currentNode() == 0)
235     {
236         helper.expectPinningMessage(true, 2);
237     }
238     expectNodeAffinitySet(&helper, 0, 2*indexInNode());
239     helper.setAffinity(1);
240 }
241
242 } // namespace