Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / hardware / printhardware.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,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 #include "gmxpre.h"
36
37 #include "printhardware.h"
38
39 #include "config.h"
40
41 #include <cstdlib>
42
43 #include <string>
44 #include <vector>
45
46 #include "gromacs/gpu_utils/gpu_utils.h"
47 #include "gromacs/hardware/cpuinfo.h"
48 #include "gromacs/hardware/hardwaretopology.h"
49 #include "gromacs/hardware/hw_info.h"
50 #include "gromacs/hardware/identifyavx512fmaunits.h"
51 #include "gromacs/simd/support.h"
52 #include "gromacs/utility/basedefinitions.h"
53 #include "gromacs/utility/basenetwork.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/gmxmpi.h"
57 #include "gromacs/utility/logger.h"
58 #include "gromacs/utility/programcontext.h"
59 #include "gromacs/utility/stringutil.h"
60 #include "gromacs/utility/sysinfo.h"
61
62 //! Constant used to help minimize preprocessed code
63 static const bool bGPUBinary = GMX_GPU != GMX_GPU_NONE;
64
65 /*! \internal \brief
66  * Returns the GPU information text, one GPU per line.
67  */
68 static std::string sprint_gpus(const gmx_gpu_info_t& gpu_info)
69 {
70     char                     stmp[STRLEN];
71     std::vector<std::string> gpuStrings;
72     for (int i = 0; i < gpu_info.n_dev; i++)
73     {
74         get_gpu_device_info_string(stmp, gpu_info, i);
75         gpuStrings.push_back(gmx::formatString("    %s", stmp));
76     }
77     return gmx::joinStrings(gpuStrings, "\n");
78 }
79
80 /* Give a suitable fatal error or warning if the build configuration
81    and runtime CPU do not match. */
82 static void check_use_of_rdtscp_on_this_cpu(const gmx::MDLogger& mdlog, const gmx::CpuInfo& cpuInfo)
83 {
84     bool binaryUsesRdtscp = HAVE_RDTSCP;
85
86     const char* programName = gmx::getProgramContext().displayName();
87
88     if (cpuInfo.supportLevel() < gmx::CpuInfo::SupportLevel::Features)
89     {
90         if (binaryUsesRdtscp)
91         {
92             GMX_LOG(mdlog.warning)
93                     .asParagraph()
94                     .appendTextFormatted(
95                             "The %s executable was compiled to use the rdtscp CPU instruction. "
96                             "We cannot detect the features of your current CPU, but will proceed "
97                             "anyway. "
98                             "If you get a crash, rebuild GROMACS with the GMX_USE_RDTSCP=OFF CMake "
99                             "option.",
100                             programName);
101         }
102     }
103     else
104     {
105         bool cpuHasRdtscp = cpuInfo.feature(gmx::CpuInfo::Feature::X86_Rdtscp);
106
107         if (!cpuHasRdtscp && binaryUsesRdtscp)
108         {
109             gmx_fatal(FARGS,
110                       "The %s executable was compiled to use the rdtscp CPU instruction. "
111                       "However, this is not supported by the current hardware and continuing would "
112                       "lead to a crash. "
113                       "Please rebuild GROMACS with the GMX_USE_RDTSCP=OFF CMake option.",
114                       programName);
115         }
116
117         if (cpuHasRdtscp && !binaryUsesRdtscp)
118         {
119             GMX_LOG(mdlog.warning)
120                     .asParagraph()
121                     .appendTextFormatted(
122                             "The current CPU can measure timings more accurately than the code in\n"
123                             "%s was configured to use. This might affect your simulation\n"
124                             "speed as accurate timings are needed for load-balancing.\n"
125                             "Please consider rebuilding %s with the GMX_USE_RDTSCP=ON CMake "
126                             "option.",
127                             programName, programName);
128         }
129     }
130 }
131
132 static std::string detected_hardware_string(const gmx_hw_info_t* hwinfo, bool bFullCpuInfo)
133 {
134     std::string s;
135
136     const gmx::CpuInfo&          cpuInfo = *hwinfo->cpuInfo;
137     const gmx::HardwareTopology& hwTop   = *hwinfo->hardwareTopology;
138
139     s = gmx::formatString("\n");
140     s += gmx::formatString("Running on %d node%s with total", hwinfo->nphysicalnode,
141                            hwinfo->nphysicalnode == 1 ? "" : "s");
142     if (hwinfo->ncore_tot > 0)
143     {
144         s += gmx::formatString(" %d cores,", hwinfo->ncore_tot);
145     }
146     s += gmx::formatString(" %d logical cores", hwinfo->nhwthread_tot);
147     if (hwinfo->gpu_info.bDetectGPUs)
148     {
149         s += gmx::formatString(", %d compatible GPU%s", hwinfo->ngpu_compatible_tot,
150                                hwinfo->ngpu_compatible_tot == 1 ? "" : "s");
151     }
152     else if (bGPUBinary)
153     {
154         s += gmx::formatString(" (GPU detection deactivated)");
155     }
156     s += gmx::formatString("\n");
157
158     if (hwinfo->nphysicalnode > 1)
159     {
160         /* Print per node hardware feature counts */
161         if (hwinfo->ncore_max > 0)
162         {
163             s += gmx::formatString("  Cores per node:           %2d", hwinfo->ncore_min);
164             if (hwinfo->ncore_max > hwinfo->ncore_min)
165             {
166                 s += gmx::formatString(" - %2d", hwinfo->ncore_max);
167             }
168             s += gmx::formatString("\n");
169         }
170         s += gmx::formatString("  Logical cores per node:   %2d", hwinfo->nhwthread_min);
171         if (hwinfo->nhwthread_max > hwinfo->nhwthread_min)
172         {
173             s += gmx::formatString(" - %2d", hwinfo->nhwthread_max);
174         }
175         s += gmx::formatString("\n");
176         if (bGPUBinary)
177         {
178             s += gmx::formatString("  Compatible GPUs per node: %2d", hwinfo->ngpu_compatible_min);
179             if (hwinfo->ngpu_compatible_max > hwinfo->ngpu_compatible_min)
180             {
181                 s += gmx::formatString(" - %2d", hwinfo->ngpu_compatible_max);
182             }
183             s += gmx::formatString("\n");
184             if (hwinfo->ngpu_compatible_tot > 0)
185             {
186                 if (hwinfo->bIdenticalGPUs)
187                 {
188                     s += gmx::formatString("  All nodes have identical type(s) of GPUs\n");
189                 }
190                 else
191                 {
192                     /* This message will also appear with identical GPU types
193                      * when at least one node has no GPU.
194                      */
195                     s += gmx::formatString(
196                             "  Different nodes have different type(s) and/or order of GPUs\n");
197                 }
198             }
199         }
200     }
201
202 #if GMX_LIB_MPI
203     int  rank;
204     char host[STRLEN];
205
206     gmx_gethostname(host, STRLEN);
207
208     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
209
210     // TODO Use a wrapper around MPI_Get_processor_name instead.
211     s += gmx::formatString("Hardware detected on host %s (the node of MPI rank %d):\n", host, rank);
212 #else
213     s += gmx::formatString("Hardware detected:\n");
214 #endif
215     s += gmx::formatString("  CPU info:\n");
216
217     s += gmx::formatString("    Vendor: %s\n", cpuInfo.vendorString().c_str());
218
219     s += gmx::formatString("    Brand:  %s\n", cpuInfo.brandString().c_str());
220
221     if (bFullCpuInfo)
222     {
223         s += gmx::formatString("    Family: %d   Model: %d   Stepping: %d\n", cpuInfo.family(),
224                                cpuInfo.model(), cpuInfo.stepping());
225
226         s += gmx::formatString("    Features:");
227         for (auto& f : cpuInfo.featureSet())
228         {
229             s += gmx::formatString(" %s", gmx::CpuInfo::featureString(f).c_str());
230         }
231         s += gmx::formatString("\n");
232     }
233
234     if (cpuInfo.feature(gmx::CpuInfo::Feature::X86_Avx512F))
235     {
236         int avx512fmaunits = gmx::identifyAvx512FmaUnits();
237         s += gmx::formatString("    Number of AVX-512 FMA units:");
238         if (avx512fmaunits > 0)
239         {
240             s += gmx::formatString(" %d", avx512fmaunits);
241             if (avx512fmaunits == 1)
242             {
243                 s += gmx::formatString(" (AVX2 is faster w/o 2 AVX-512 FMA units)");
244             }
245         }
246         else
247         {
248             s += gmx::formatString(" Cannot run AVX-512 detection - assuming 2");
249         }
250         s += gmx::formatString("\n");
251     }
252
253     s += gmx::formatString("  Hardware topology: ");
254     switch (hwTop.supportLevel())
255     {
256         case gmx::HardwareTopology::SupportLevel::None: s += gmx::formatString("None\n"); break;
257         case gmx::HardwareTopology::SupportLevel::LogicalProcessorCount:
258             s += gmx::formatString("Only logical processor count\n");
259             break;
260         case gmx::HardwareTopology::SupportLevel::Basic: s += gmx::formatString("Basic\n"); break;
261         case gmx::HardwareTopology::SupportLevel::Full: s += gmx::formatString("Full\n"); break;
262         case gmx::HardwareTopology::SupportLevel::FullWithDevices:
263             s += gmx::formatString("Full, with devices\n");
264             break;
265     }
266
267     if (!hwTop.isThisSystem())
268     {
269         s += gmx::formatString("  NOTE: Hardware topology cached or synthetic, not detected.\n");
270         if (char* p = std::getenv("HWLOC_XMLFILE"))
271         {
272             s += gmx::formatString("        HWLOC_XMLFILE=%s\n", p);
273         }
274     }
275
276     if (bFullCpuInfo)
277     {
278         if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Basic)
279         {
280             s += gmx::formatString("    Sockets, cores, and logical processors:\n");
281
282             for (auto& socket : hwTop.machine().sockets)
283             {
284                 s += gmx::formatString("      Socket %2d:", socket.id);
285                 for (auto& c : socket.cores)
286                 {
287                     s += gmx::formatString(" [");
288                     for (auto& t : c.hwThreads)
289                     {
290                         s += gmx::formatString(" %3d", t.logicalProcessorId);
291                     }
292                     s += gmx::formatString("]");
293                 }
294                 s += gmx::formatString("\n");
295             }
296         }
297         if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Full)
298         {
299             s += gmx::formatString("    Numa nodes:\n");
300             for (auto& n : hwTop.machine().numa.nodes)
301             {
302                 s += gmx::formatString("      Node %2d (%zu bytes mem):", n.id, n.memory);
303                 for (auto& l : n.logicalProcessorId)
304                 {
305                     s += gmx::formatString(" %3d", l);
306                 }
307                 s += gmx::formatString("\n");
308             }
309             s += gmx::formatString("      Latency:\n          ");
310             for (std::size_t j = 0; j < hwTop.machine().numa.nodes.size(); j++)
311             {
312                 s += gmx::formatString(" %5zu", j);
313             }
314             s += gmx::formatString("\n");
315             for (std::size_t i = 0; i < hwTop.machine().numa.nodes.size(); i++)
316             {
317                 s += gmx::formatString("     %5zu", i);
318                 for (std::size_t j = 0; j < hwTop.machine().numa.nodes.size(); j++)
319                 {
320                     s += gmx::formatString(" %5.2f", hwTop.machine().numa.relativeLatency[i][j]);
321                 }
322                 s += gmx::formatString("\n");
323             }
324
325
326             s += gmx::formatString("    Caches:\n");
327             for (auto& c : hwTop.machine().caches)
328             {
329                 s += gmx::formatString(
330                         "      L%d: %zu bytes, linesize %d bytes, assoc. %d, shared %d ways\n",
331                         c.level, c.size, c.linesize, c.associativity, c.shared);
332             }
333         }
334         if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::FullWithDevices)
335         {
336             s += gmx::formatString("    PCI devices:\n");
337             for (auto& d : hwTop.machine().devices)
338             {
339                 s += gmx::formatString(
340                         "      %04x:%02x:%02x.%1x  Id: %04x:%04x  Class: 0x%04x  Numa: %d\n", d.domain,
341                         d.bus, d.dev, d.func, d.vendorId, d.deviceId, d.classId, d.numaNodeId);
342             }
343         }
344     }
345
346     if (bGPUBinary && hwinfo->gpu_info.n_dev > 0)
347     {
348         s += gmx::formatString("  GPU info:\n");
349         s += gmx::formatString("    Number of GPUs detected: %d\n", hwinfo->gpu_info.n_dev);
350         s += sprint_gpus(hwinfo->gpu_info) + "\n";
351     }
352     return s;
353 }
354
355 void gmx_print_detected_hardware(FILE*                fplog,
356                                  const bool           warnToStdErr,
357                                  const gmx::MDLogger& mdlog,
358                                  const gmx_hw_info_t* hwinfo)
359 {
360     const gmx::CpuInfo& cpuInfo = *hwinfo->cpuInfo;
361
362     if (fplog != nullptr)
363     {
364         std::string detected;
365
366         detected = detected_hardware_string(hwinfo, TRUE);
367
368         fprintf(fplog, "%s\n", detected.c_str());
369     }
370
371     // Do not spam stderr with all our internal information unless
372     // there was something that actually went wrong; general information
373     // belongs in the logfile.
374
375     /* Check the compiled SIMD instruction set against that of the node
376      * with the lowest SIMD level support (skip if SIMD detection did not work)
377      */
378     if (cpuInfo.supportLevel() >= gmx::CpuInfo::SupportLevel::Features)
379     {
380         gmx::simdCheck(static_cast<gmx::SimdType>(hwinfo->simd_suggest_min), fplog, warnToStdErr);
381     }
382
383     /* For RDTSCP we only check on our local node and skip the MPI reduction */
384     check_use_of_rdtscp_on_this_cpu(mdlog, cpuInfo);
385 }