Fix self-consistency tests of hwloc data structures
[gromacs.git] / src / gromacs / hardware / tests / hardwaretopology.cpp
blob61afde793559aa24612ca948c58980197879e310
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,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.
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 /*! \internal \file
36 * \brief
37 * Tests for gmx::HardwareTopology
39 * \author Erik Lindahl <erik.lindahl@gmail.com>
40 * \ingroup module_hardware
42 #include "gmxpre.h"
44 #include "gromacs/hardware/hardwaretopology.h"
46 #include "config.h"
48 #include <algorithm>
50 #include <gtest/gtest.h>
52 #include "gromacs/utility/stringutil.h"
54 namespace
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
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.
66 TEST(HardwareTopologyTest, Execute)
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
72 gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
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.";
81 #if GMX_USE_HWLOC
82 TEST(HardwareTopologyTest, HwlocExecute)
84 #if defined(__linux__)
85 gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
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" << std::endl
90 << "work, but it might affect your performance for large nodes." << std::endl
91 << "Please mail gmx-developers@gromacs.org so we can try to fix it.";
92 #endif
94 #endif
96 TEST(HardwareTopologyTest, ProcessorSelfconsistency)
98 gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
100 if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Basic)
102 SCOPED_TRACE(gmx::formatString("Logical Processor count %d", hwTop.machine().logicalProcessorCount));
104 int socketsInMachine = hwTop.machine().sockets.size();
105 int coresPerSocket = hwTop.machine().sockets[0].cores.size();
106 int hwThreadsPerCore = hwTop.machine().sockets[0].cores[0].hwThreads.size();
108 auto logicalProcessors = hwTop.machine().logicalProcessors;
109 for (auto logicalProcessorIt = logicalProcessors.begin();
110 logicalProcessorIt != logicalProcessors.end();
111 ++logicalProcessorIt)
113 // Check that logical processor information contains
114 // reasonable values.
115 SCOPED_TRACE(gmx::formatString("Socket rank in machine: %d", logicalProcessorIt->socketRankInMachine));
116 SCOPED_TRACE(gmx::formatString("Core rank in socket: %d", logicalProcessorIt->coreRankInSocket));
117 SCOPED_TRACE(gmx::formatString("Hw thread rank in core: %d", logicalProcessorIt->hwThreadRankInCore));
118 EXPECT_TRUE(logicalProcessorIt->socketRankInMachine >= 0 && logicalProcessorIt->socketRankInMachine < socketsInMachine);
119 EXPECT_TRUE(logicalProcessorIt->coreRankInSocket >= 0 && logicalProcessorIt->coreRankInSocket < coresPerSocket);
120 EXPECT_TRUE(logicalProcessorIt->hwThreadRankInCore >= 0 && logicalProcessorIt->hwThreadRankInCore < hwThreadsPerCore);
121 // Check that logical processor information is distinct
122 // for each logical processor.
124 for (auto remainingLogicalProcessorIt = logicalProcessorIt + 1;
125 remainingLogicalProcessorIt != logicalProcessors.end();
126 ++remainingLogicalProcessorIt)
128 SCOPED_TRACE(gmx::formatString("Other socket rank in machine: %d", remainingLogicalProcessorIt->socketRankInMachine));
129 SCOPED_TRACE(gmx::formatString("Other core rank in socket: %d", remainingLogicalProcessorIt->coreRankInSocket));
130 SCOPED_TRACE(gmx::formatString("Other hw thread rank in core: %d", remainingLogicalProcessorIt->hwThreadRankInCore));
131 EXPECT_TRUE((logicalProcessorIt->socketRankInMachine != remainingLogicalProcessorIt->socketRankInMachine) ||
132 (logicalProcessorIt->coreRankInSocket != remainingLogicalProcessorIt->coreRankInSocket) ||
133 (logicalProcessorIt->hwThreadRankInCore != remainingLogicalProcessorIt->hwThreadRankInCore)) <<
134 "This pair of logical processors have the same descriptive information, which is an error";
140 TEST(HardwareTopologyTest, NumaCacheSelfconsistency)
142 gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
144 if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Full)
146 // Check that numa node id corresponds to rank
147 for (std::size_t i = 0; i < hwTop.machine().numa.nodes.size(); i++)
149 EXPECT_EQ(hwTop.machine().numa.nodes[i].id, i);
152 // Check that the sum of numa domains is the total processor count
153 int processorsinNumaNudes = 0;
154 for (auto &n : hwTop.machine().numa.nodes)
156 processorsinNumaNudes += n.logicalProcessorId.size();
158 EXPECT_EQ(processorsinNumaNudes, hwTop.machine().logicalProcessorCount);
160 // Check that every processor is in a numa domain (i.e., that they are unique)
161 std::vector<int> v(hwTop.machine().logicalProcessorCount);
162 for (auto &elem : v)
164 elem = 0;
166 for (auto &n : hwTop.machine().numa.nodes)
168 for (auto &idx : n.logicalProcessorId)
170 v[idx] = 1;
173 int uniqueProcessorsinNumaNudes = std::count(v.begin(), v.end(), 1);
174 EXPECT_EQ(uniqueProcessorsinNumaNudes, hwTop.machine().logicalProcessorCount);
176 // We must have some memory in a numa node
177 for (auto &n : hwTop.machine().numa.nodes)
179 EXPECT_GT(n.memory, 0);
182 // Check latency matrix size and contents
183 EXPECT_GT(hwTop.machine().numa.baseLatency, 0);
184 EXPECT_GT(hwTop.machine().numa.maxRelativeLatency, 0);
185 // Check number of rows matches # numa nodes
186 EXPECT_EQ(hwTop.machine().numa.relativeLatency.size(), hwTop.machine().numa.nodes.size());
187 for (auto &v2 : hwTop.machine().numa.relativeLatency)
189 // Check that size of each row matches # numa nodes
190 EXPECT_EQ(v2.size(), hwTop.machine().numa.nodes.size());
191 for (auto &latency : v2)
193 // Latency values should be positive
194 EXPECT_GT(latency, 0);
198 // Check cache. The hwloc cache detection is fragile and can report
199 // 0 for line size or associativity (=unknown), so we just check the size.
200 for (auto &c : hwTop.machine().caches)
202 EXPECT_GT(c.size, 0);
208 } // namespace