Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / hardware / tests / hardwaretopology.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2018,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.
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 /*! \internal \file
36  * \brief
37  * Tests for gmx::HardwareTopology
38  *
39  * \author Erik Lindahl <erik.lindahl@gmail.com>
40  * \ingroup module_hardware
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/hardware/hardwaretopology.h"
45
46 #include "config.h"
47
48 #include <algorithm>
49
50 #include <gtest/gtest.h>
51
52 #include "gromacs/utility/stringutil.h"
53
54 namespace
55 {
56
57 // There is no way we can compare to any reference data since that
58 // depends on the architecture, but we can at least make sure that it
59 // works to execute the tests and that they are self-consistent
60
61 // Although it is not strictly an error, for the very basic execution tests
62 // we also report if we cannot extract the hardware topology on systems
63 // where we expect to be able to. Since this might happen to users, we
64 // provide a bit more information and ask them to mail us in this case.
65
66 TEST(HardwareTopologyTest, Execute)
67 {
68     // There is no way we can compare to any reference data since that
69     // depends on the architecture, but we can at least make sure that it
70     // works to execute the tests
71
72     gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
73
74     // If we cannot even find the number of logical processors we want to flag it
75     EXPECT_GT(hwTop.supportLevel(), gmx::HardwareTopology::SupportLevel::None)
76             << "Cannot determine number of processors. " << std::endl
77             << "GROMACS might still work, but it will likely hurt your performance." << std::endl
78             << "Please mail gmx-developers@gromacs.org so we can try to fix it.";
79 }
80
81 #if GMX_USE_HWLOC
82 TEST(HardwareTopologyTest, HwlocExecute)
83 {
84 #    if defined(__linux__)
85     gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
86
87     // On Linux with hwloc support we should be able to get at least basic information
88     EXPECT_GE(hwTop.supportLevel(), gmx::HardwareTopology::SupportLevel::Basic)
89             << "Cannot determine basic hardware topology from hwloc. GROMACS will still\n"
90             << std::endl
91             << "work, but it might affect your performance for large nodes." << std::endl
92             << "Please mail gmx-developers@gromacs.org so we can try to fix it.";
93 #    endif
94 }
95 #endif
96
97 TEST(HardwareTopologyTest, ProcessorSelfconsistency)
98 {
99     gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
100
101     if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Basic)
102     {
103         SCOPED_TRACE(gmx::formatString("Logical Processor count %d", hwTop.machine().logicalProcessorCount));
104
105         int socketsInMachine = hwTop.machine().sockets.size();
106         int coresPerSocket   = hwTop.machine().sockets[0].cores.size();
107         int hwThreadsPerCore = hwTop.machine().sockets[0].cores[0].hwThreads.size();
108
109         auto logicalProcessors = hwTop.machine().logicalProcessors;
110         for (auto logicalProcessorIt = logicalProcessors.begin();
111              logicalProcessorIt != logicalProcessors.end();
112              ++logicalProcessorIt)
113         {
114             // Check that logical processor information contains
115             // reasonable values.
116             SCOPED_TRACE(gmx::formatString("Socket rank in machine: %d",
117                                            logicalProcessorIt->socketRankInMachine));
118             SCOPED_TRACE(gmx::formatString("Core rank in socket:    %d",
119                                            logicalProcessorIt->coreRankInSocket));
120             SCOPED_TRACE(gmx::formatString("Hw thread rank in core: %d",
121                                            logicalProcessorIt->hwThreadRankInCore));
122             EXPECT_TRUE(logicalProcessorIt->socketRankInMachine >= 0
123                         && logicalProcessorIt->socketRankInMachine < socketsInMachine);
124             EXPECT_TRUE(logicalProcessorIt->coreRankInSocket >= 0
125                         && logicalProcessorIt->coreRankInSocket < coresPerSocket);
126             EXPECT_TRUE(logicalProcessorIt->hwThreadRankInCore >= 0
127                         && logicalProcessorIt->hwThreadRankInCore < hwThreadsPerCore);
128             // Check that logical processor information is distinct
129             // for each logical processor.
130
131             for (auto remainingLogicalProcessorIt = logicalProcessorIt + 1;
132                  remainingLogicalProcessorIt != logicalProcessors.end();
133                  ++remainingLogicalProcessorIt)
134             {
135                 SCOPED_TRACE(gmx::formatString("Other socket rank in machine: %d",
136                                                remainingLogicalProcessorIt->socketRankInMachine));
137                 SCOPED_TRACE(gmx::formatString("Other core rank in socket:    %d",
138                                                remainingLogicalProcessorIt->coreRankInSocket));
139                 SCOPED_TRACE(gmx::formatString("Other hw thread rank in core: %d",
140                                                remainingLogicalProcessorIt->hwThreadRankInCore));
141                 EXPECT_TRUE((logicalProcessorIt->socketRankInMachine != remainingLogicalProcessorIt->socketRankInMachine)
142                             || (logicalProcessorIt->coreRankInSocket != remainingLogicalProcessorIt->coreRankInSocket)
143                             || (logicalProcessorIt->hwThreadRankInCore
144                                 != remainingLogicalProcessorIt->hwThreadRankInCore))
145                         << "This pair of logical processors have the same descriptive information, "
146                            "which is an error";
147             }
148         }
149     }
150 }
151
152 TEST(HardwareTopologyTest, NumaCacheSelfconsistency)
153 {
154     gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
155
156     if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Full)
157     {
158         // Check that numa node id corresponds to rank
159         for (std::size_t i = 0; i < hwTop.machine().numa.nodes.size(); i++)
160         {
161             EXPECT_EQ(hwTop.machine().numa.nodes[i].id, i);
162         }
163
164         // Check that the sum of numa domains is the total processor count
165         int processorsinNumaNudes = 0;
166         for (auto& n : hwTop.machine().numa.nodes)
167         {
168             processorsinNumaNudes += n.logicalProcessorId.size();
169         }
170         EXPECT_EQ(processorsinNumaNudes, hwTop.machine().logicalProcessorCount);
171
172         // Check that every processor is in a numa domain (i.e., that they are unique)
173         std::vector<int> v(hwTop.machine().logicalProcessorCount);
174         for (auto& elem : v)
175         {
176             elem = 0;
177         }
178         for (auto& n : hwTop.machine().numa.nodes)
179         {
180             for (auto& idx : n.logicalProcessorId)
181             {
182                 v[idx] = 1;
183             }
184         }
185         int uniqueProcessorsinNumaNudes = std::count(v.begin(), v.end(), 1);
186         EXPECT_EQ(uniqueProcessorsinNumaNudes, hwTop.machine().logicalProcessorCount);
187
188         // We must have some memory in a numa node
189         for (auto& n : hwTop.machine().numa.nodes)
190         {
191             EXPECT_GT(n.memory, 0);
192         }
193
194         // Check latency matrix size and contents
195         EXPECT_GT(hwTop.machine().numa.baseLatency, 0);
196         EXPECT_GT(hwTop.machine().numa.maxRelativeLatency, 0);
197         // Check number of rows matches # numa nodes
198         EXPECT_EQ(hwTop.machine().numa.relativeLatency.size(), hwTop.machine().numa.nodes.size());
199         for (auto& v2 : hwTop.machine().numa.relativeLatency)
200         {
201             // Check that size of each row matches # numa nodes
202             EXPECT_EQ(v2.size(), hwTop.machine().numa.nodes.size());
203             for (auto& latency : v2)
204             {
205                 // Latency values should be positive
206                 EXPECT_GT(latency, 0);
207             }
208         }
209
210         // We don't check cache fields because these tests depend both
211         // on whether hwloc can detect things correctly, and then
212         // whether GROMACS code packages the results correctly. The
213         // hwloc cache detection is fragile and can report 0 for cache
214         // size, line size or associativity (=unknown), so GROMACS
215         // doesn't test anything related to it.
216         //
217         // TODO Use proper unit tests on mock hardware to test that
218         // HardwareTopology construction is doing its job, rather than
219         // brittle tests that require that hwloc works correctly on
220         // the user's hardware even when GROMACS is barely using the
221         // values returned by hwloc.
222     }
223 }
224
225
226 } // namespace