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