2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017, 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 #ifndef GMX_MDRUNUTILITY_TESTS_THREADAFFINITYTEST_H
36 #define GMX_MDRUNUTILITY_TESTS_THREADAFFINITYTEST_H
38 #include <initializer_list>
41 #include <gmock/gmock.h>
43 #include "gromacs/hardware/hw_info.h"
44 #include "gromacs/mdrunutility/threadaffinity.h"
45 #include "gromacs/utility/logger.h"
46 #include "gromacs/utility/stringutil.h"
48 #include "testutils/loggertest.h"
55 class HardwareTopology
;
60 class MockThreadAffinityAccess
: public IThreadAffinityAccess
63 MockThreadAffinityAccess();
64 ~MockThreadAffinityAccess();
66 void setSupported(bool supported
) { supported_
= supported
; }
67 void setPhysicalNodeId(int nodeId
) { physicalNodeId_
= nodeId
; }
69 virtual bool isThreadAffinitySupported() const { return supported_
; }
70 virtual int physicalNodeId() const { return physicalNodeId_
; }
71 MOCK_METHOD1(setCurrentThreadAffinityToCore
, bool(int core
));
78 class ThreadAffinityTestHelper
81 ThreadAffinityTestHelper();
82 ~ThreadAffinityTestHelper();
84 void setAffinitySupported(bool supported
)
86 affinityAccess_
.setSupported(supported
);
88 void setAffinityOption(int affinityOption
)
90 hwOpt_
.thread_affinity
= affinityOption
;
92 void setOffsetAndStride(int offset
, int stride
)
94 hwOpt_
.core_pinning_offset
= offset
;
95 hwOpt_
.core_pinning_stride
= stride
;
98 void setPhysicalNodeId(int nodeId
)
100 affinityAccess_
.setPhysicalNodeId(nodeId
);
103 void setLogicalProcessorCount(int logicalProcessorCount
);
105 void expectAffinitySet(int core
)
107 EXPECT_CALL(affinityAccess_
, setCurrentThreadAffinityToCore(core
));
109 void expectAffinitySet(std::initializer_list
<int> cores
)
111 for (int core
: cores
)
113 expectAffinitySet(core
);
116 void expectAffinitySetThatFails(int core
)
118 using ::testing::Return
;
119 EXPECT_CALL(affinityAccess_
, setCurrentThreadAffinityToCore(core
))
120 .WillOnce(Return(false));
123 void expectWarningMatchingRegex(const char *re
)
125 expectWarningMatchingRegexIf(re
, true);
127 void expectWarningMatchingRegexIf(const char *re
, bool condition
)
129 expectLogMessageMatchingRegexIf(MDLogger::LogLevel::Warning
, re
, condition
);
131 void expectInfoMatchingRegex(const char *re
)
133 expectInfoMatchingRegexIf(re
, true);
135 void expectInfoMatchingRegexIf(const char *re
, bool condition
)
137 expectLogMessageMatchingRegexIf(MDLogger::LogLevel::Info
, re
, condition
);
139 void expectGenericFailureMessage()
141 expectGenericFailureMessageIf(true);
143 void expectGenericFailureMessageIf(bool condition
)
145 expectWarningMatchingRegexIf("NOTE: Thread affinity setting failed.", condition
);
147 void expectPinningMessage(bool userSpecifiedStride
, int stride
)
149 std::string pattern
= formatString("Pinning threads .* %s.* stride of %d",
150 userSpecifiedStride
? "user" : "auto",
152 expectInfoMatchingRegex(pattern
.c_str());
154 void expectLogMessageMatchingRegexIf(MDLogger::LogLevel level
,
155 const char *re
, bool condition
)
159 logHelper_
.expectEntryMatchingRegex(level
, re
);
163 void setAffinity(int nthread_local
)
165 if (hwTop_
== nullptr)
167 setLogicalProcessorCount(1);
169 gmx_set_thread_affinity(logHelper_
.logger(), cr_
, &hwOpt_
, *hwTop_
,
170 nthread_local
, &affinityAccess_
);
176 std::unique_ptr
<HardwareTopology
> hwTop_
;
177 MockThreadAffinityAccess affinityAccess_
;
178 LoggerTestHelper logHelper_
;