38ca39ccc9bebb9cefecbbe53a2e7aba9794ac11
[alexxy/gromacs.git] / src / gromacs / mdrunutility / tests / threadaffinitytest.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018,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 #ifndef GMX_MDRUNUTILITY_TESTS_THREADAFFINITYTEST_H
36 #define GMX_MDRUNUTILITY_TESTS_THREADAFFINITYTEST_H
37
38 #include <initializer_list>
39 #include <memory>
40
41 #include <gmock/gmock.h>
42
43 #include "gromacs/hardware/hw_info.h"
44 #include "gromacs/mdrunutility/threadaffinity.h"
45 #include "gromacs/utility/logger.h"
46 #include "gromacs/utility/physicalnodecommunicator.h"
47 #include "gromacs/utility/stringutil.h"
48
49 #include "testutils/loggertest.h"
50
51 struct t_commrec;
52
53 namespace gmx
54 {
55
56 class HardwareTopology;
57
58 namespace test
59 {
60
61 class MockThreadAffinityAccess : public IThreadAffinityAccess
62 {
63     public:
64         MockThreadAffinityAccess();
65         ~MockThreadAffinityAccess() override;
66
67         void setSupported(bool supported) { supported_ = supported; }
68
69         bool isThreadAffinitySupported() const override { return supported_; }
70         MOCK_METHOD1(setCurrentThreadAffinityToCore, bool(int core));
71
72     private:
73         bool supported_;
74 };
75
76 class ThreadAffinityTestHelper
77 {
78     public:
79         ThreadAffinityTestHelper();
80         ~ThreadAffinityTestHelper();
81
82         void setAffinitySupported(bool supported)
83         {
84             affinityAccess_.setSupported(supported);
85         }
86         void setAffinityOption(ThreadAffinity affinityOption)
87         {
88             hwOpt_.threadAffinity = affinityOption;
89         }
90         void setOffsetAndStride(int offset, int stride)
91         {
92             hwOpt_.core_pinning_offset = offset;
93             hwOpt_.core_pinning_stride = stride;
94         }
95
96         void setPhysicalNodeId(int nodeId)
97         {
98             physicalNodeId_ = nodeId;
99         }
100
101         void setLogicalProcessorCount(int logicalProcessorCount);
102
103         void setTotNumThreadsIsAuto(bool isAuto)
104         {
105             hwOpt_.totNumThreadsIsAuto = isAuto;
106         }
107
108         void expectAffinitySet(int core)
109         {
110             EXPECT_CALL(affinityAccess_, setCurrentThreadAffinityToCore(core));
111         }
112         void expectAffinitySet(std::initializer_list<int> cores)
113         {
114             for (int core : cores)
115             {
116                 expectAffinitySet(core);
117             }
118         }
119         void expectAffinitySetThatFails(int core)
120         {
121             using ::testing::Return;
122             EXPECT_CALL(affinityAccess_, setCurrentThreadAffinityToCore(core))
123                 .WillOnce(Return(false));
124         }
125
126         void expectWarningMatchingRegex(const char *re)
127         {
128             expectWarningMatchingRegexIf(re, true);
129         }
130         void expectWarningMatchingRegexIf(const char *re, bool condition)
131         {
132             expectLogMessageMatchingRegexIf(MDLogger::LogLevel::Warning, re, condition);
133         }
134         void expectInfoMatchingRegex(const char *re)
135         {
136             expectInfoMatchingRegexIf(re, true);
137         }
138         void expectInfoMatchingRegexIf(const char *re, bool condition)
139         {
140             expectLogMessageMatchingRegexIf(MDLogger::LogLevel::Info, re, condition);
141         }
142         void expectGenericFailureMessage()
143         {
144             expectGenericFailureMessageIf(true);
145         }
146         void expectGenericFailureMessageIf(bool condition)
147         {
148             expectWarningMatchingRegexIf("NOTE: Thread affinity was not set.", condition);
149         }
150         void expectPinningMessage(bool userSpecifiedStride, int stride)
151         {
152             std::string pattern = formatString("Pinning threads .* %s.* stride of %d",
153                                                userSpecifiedStride ? "user" : "auto",
154                                                stride);
155             expectInfoMatchingRegex(pattern.c_str());
156         }
157         void expectLogMessageMatchingRegexIf(MDLogger::LogLevel level,
158                                              const char *re, bool condition)
159         {
160             if (condition)
161             {
162                 logHelper_.expectEntryMatchingRegex(level, re);
163             }
164         }
165
166         void setAffinity(int numThreadsOnThisRank)
167         {
168             if (hwTop_ == nullptr)
169             {
170                 setLogicalProcessorCount(1);
171             }
172             gmx::PhysicalNodeCommunicator comm(MPI_COMM_WORLD, physicalNodeId_);
173             int numThreadsOnThisNode, indexWithinNodeOfFirstThreadOnThisRank;
174             analyzeThreadsOnThisNode(comm,
175                                      numThreadsOnThisRank,
176                                      &numThreadsOnThisNode,
177                                      &indexWithinNodeOfFirstThreadOnThisRank);
178             gmx_set_thread_affinity(logHelper_.logger(), cr_, &hwOpt_, *hwTop_,
179                                     numThreadsOnThisRank, numThreadsOnThisNode,
180                                     indexWithinNodeOfFirstThreadOnThisRank, &affinityAccess_);
181         }
182
183     private:
184         t_commrec                         *cr_;
185         gmx_hw_opt_t                       hwOpt_;
186         std::unique_ptr<HardwareTopology>  hwTop_;
187         MockThreadAffinityAccess           affinityAccess_;
188         LoggerTestHelper                   logHelper_;
189         int                                physicalNodeId_;
190 };
191
192 } // namespace test
193 } // namespace gmx
194
195 #endif