2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2018,2019,2020,2021, 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.
37 * Tests for gmx::HardwareTopology
39 * \author Erik Lindahl <erik.lindahl@gmail.com>
40 * \ingroup module_hardware
44 #include "gromacs/hardware/hardwaretopology.h"
50 #include <gtest/gtest.h>
52 #include "gromacs/utility/stringutil.h"
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.";
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"
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.";
97 TEST(HardwareTopologyTest, ProcessorSelfconsistency)
99 gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
101 if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Basic)
103 SCOPED_TRACE(gmx::formatString("Logical Processor count %d", hwTop.machine().logicalProcessorCount));
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();
109 auto logicalProcessors = hwTop.machine().logicalProcessors;
110 for (auto logicalProcessorIt = logicalProcessors.begin();
111 logicalProcessorIt != logicalProcessors.end();
112 ++logicalProcessorIt)
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.
131 for (auto remainingLogicalProcessorIt = logicalProcessorIt + 1;
132 remainingLogicalProcessorIt != logicalProcessors.end();
133 ++remainingLogicalProcessorIt)
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, "
152 TEST(HardwareTopologyTest, NumaCacheSelfconsistency)
154 gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
156 if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Full)
158 // Check that numa node id corresponds to rank
159 for (std::size_t i = 0; i < hwTop.machine().numa.nodes.size(); i++)
161 EXPECT_EQ(hwTop.machine().numa.nodes[i].id, i);
164 // Check that the sum of numa domains is the total processor count
165 int processorsinNumaNudes = 0;
166 for (const auto& n : hwTop.machine().numa.nodes)
168 processorsinNumaNudes += n.logicalProcessorId.size();
170 EXPECT_EQ(processorsinNumaNudes, hwTop.machine().logicalProcessorCount);
172 // Check that every processor is in a numa domain (i.e., that they are unique)
173 std::vector<int> v(hwTop.machine().logicalProcessorCount);
178 for (const auto& n : hwTop.machine().numa.nodes)
180 for (const auto& idx : n.logicalProcessorId)
185 int uniqueProcessorsinNumaNudes = std::count(v.begin(), v.end(), 1);
186 EXPECT_EQ(uniqueProcessorsinNumaNudes, hwTop.machine().logicalProcessorCount);
188 // We must have some memory in a numa node
189 for (const auto& n : hwTop.machine().numa.nodes)
191 EXPECT_GT(n.memory, 0);
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 (const auto& v2 : hwTop.machine().numa.relativeLatency)
201 // Check that size of each row matches # numa nodes
202 EXPECT_EQ(v2.size(), hwTop.machine().numa.nodes.size());
203 for (const auto& latency : v2)
205 // Latency values should be positive
206 EXPECT_GT(latency, 0);
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.
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.