537807bd65180a139c4040f00d95754cad318349
[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, 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(); ++logicalProcessorIt)
112         {
113             // Check that logical processor information contains
114             // reasonable values.
115             SCOPED_TRACE(gmx::formatString("Socket rank in machine: %d",
116                                            logicalProcessorIt->socketRankInMachine));
117             SCOPED_TRACE(gmx::formatString("Core rank in socket:    %d",
118                                            logicalProcessorIt->coreRankInSocket));
119             SCOPED_TRACE(gmx::formatString("Hw thread rank in core: %d",
120                                            logicalProcessorIt->hwThreadRankInCore));
121             EXPECT_TRUE(logicalProcessorIt->socketRankInMachine >= 0
122                         && logicalProcessorIt->socketRankInMachine < socketsInMachine);
123             EXPECT_TRUE(logicalProcessorIt->coreRankInSocket >= 0
124                         && logicalProcessorIt->coreRankInSocket < coresPerSocket);
125             EXPECT_TRUE(logicalProcessorIt->hwThreadRankInCore >= 0
126                         && logicalProcessorIt->hwThreadRankInCore < hwThreadsPerCore);
127             // Check that logical processor information is distinct
128             // for each logical processor.
129
130             for (auto remainingLogicalProcessorIt = logicalProcessorIt + 1;
131                  remainingLogicalProcessorIt != logicalProcessors.end(); ++remainingLogicalProcessorIt)
132             {
133                 SCOPED_TRACE(gmx::formatString("Other socket rank in machine: %d",
134                                                remainingLogicalProcessorIt->socketRankInMachine));
135                 SCOPED_TRACE(gmx::formatString("Other core rank in socket:    %d",
136                                                remainingLogicalProcessorIt->coreRankInSocket));
137                 SCOPED_TRACE(gmx::formatString("Other hw thread rank in core: %d",
138                                                remainingLogicalProcessorIt->hwThreadRankInCore));
139                 EXPECT_TRUE((logicalProcessorIt->socketRankInMachine != remainingLogicalProcessorIt->socketRankInMachine)
140                             || (logicalProcessorIt->coreRankInSocket != remainingLogicalProcessorIt->coreRankInSocket)
141                             || (logicalProcessorIt->hwThreadRankInCore
142                                 != remainingLogicalProcessorIt->hwThreadRankInCore))
143                         << "This pair of logical processors have the same descriptive information, "
144                            "which is an error";
145             }
146         }
147     }
148 }
149
150 TEST(HardwareTopologyTest, NumaCacheSelfconsistency)
151 {
152     gmx::HardwareTopology hwTop(gmx::HardwareTopology::detect());
153
154     if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Full)
155     {
156         // Check that numa node id corresponds to rank
157         for (std::size_t i = 0; i < hwTop.machine().numa.nodes.size(); i++)
158         {
159             EXPECT_EQ(hwTop.machine().numa.nodes[i].id, i);
160         }
161
162         // Check that the sum of numa domains is the total processor count
163         int processorsinNumaNudes = 0;
164         for (auto& n : hwTop.machine().numa.nodes)
165         {
166             processorsinNumaNudes += n.logicalProcessorId.size();
167         }
168         EXPECT_EQ(processorsinNumaNudes, hwTop.machine().logicalProcessorCount);
169
170         // Check that every processor is in a numa domain (i.e., that they are unique)
171         std::vector<int> v(hwTop.machine().logicalProcessorCount);
172         for (auto& elem : v)
173         {
174             elem = 0;
175         }
176         for (auto& n : hwTop.machine().numa.nodes)
177         {
178             for (auto& idx : n.logicalProcessorId)
179             {
180                 v[idx] = 1;
181             }
182         }
183         int uniqueProcessorsinNumaNudes = std::count(v.begin(), v.end(), 1);
184         EXPECT_EQ(uniqueProcessorsinNumaNudes, hwTop.machine().logicalProcessorCount);
185
186         // We must have some memory in a numa node
187         for (auto& n : hwTop.machine().numa.nodes)
188         {
189             EXPECT_GT(n.memory, 0);
190         }
191
192         // Check latency matrix size and contents
193         EXPECT_GT(hwTop.machine().numa.baseLatency, 0);
194         EXPECT_GT(hwTop.machine().numa.maxRelativeLatency, 0);
195         // Check number of rows matches # numa nodes
196         EXPECT_EQ(hwTop.machine().numa.relativeLatency.size(), hwTop.machine().numa.nodes.size());
197         for (auto& v2 : hwTop.machine().numa.relativeLatency)
198         {
199             // Check that size of each row matches # numa nodes
200             EXPECT_EQ(v2.size(), hwTop.machine().numa.nodes.size());
201             for (auto& latency : v2)
202             {
203                 // Latency values should be positive
204                 EXPECT_GT(latency, 0);
205             }
206         }
207
208         // We don't check cache fields because these tests depend both
209         // on whether hwloc can detect things correctly, and then
210         // whether GROMACS code packages the results correctly. The
211         // hwloc cache detection is fragile and can report 0 for cache
212         // size, line size or associativity (=unknown), so GROMACS
213         // doesn't test anything related to it.
214         //
215         // TODO Use proper unit tests on mock hardware to test that
216         // HardwareTopology construction is doing its job, rather than
217         // brittle tests that require that hwloc works correctly on
218         // the user's hardware even when GROMACS is barely using the
219         // values returned by hwloc.
220     }
221 }
222
223
224 } // namespace