Remove unused canComputeOnGpu(...) function from device management
[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 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 #include "gmxpre.h"
37
38 #include "printhardware.h"
39
40 #include "config.h"
41
42 #include <cstdlib>
43
44 #include <string>
45 #include <vector>
46
47 #include "gromacs/hardware/cpuinfo.h"
48 #include "gromacs/hardware/device_management.h"
49 #include "gromacs/hardware/hardwaretopology.h"
50 #include "gromacs/hardware/hw_info.h"
51 #include "gromacs/hardware/identifyavx512fmaunits.h"
52 #include "gromacs/simd/support.h"
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/basenetwork.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/gmxmpi.h"
58 #include "gromacs/utility/logger.h"
59 #include "gromacs/utility/programcontext.h"
60 #include "gromacs/utility/stringutil.h"
61 #include "gromacs/utility/sysinfo.h"
62
63 //! Constant used to help minimize preprocessed code
64 static constexpr bool bGPUBinary = (GMX_GPU != 0);
65
66 /*! \internal \brief
67  * Returns the GPU information text, one GPU per line.
68  */
69 static std::string sprint_gpus(const std::vector<std::unique_ptr<DeviceInformation>>& deviceInfoList)
70 {
71     std::vector<std::string> gpuStrings(0);
72     for (const auto& deviceInfo : deviceInfoList)
73     {
74         gpuStrings.emplace_back("    " + getDeviceInformationString(*deviceInfo));
75     }
76     return gmx::joinStrings(gpuStrings, "\n");
77 }
78
79 /* Give a suitable fatal error or warning if the build configuration
80    and runtime CPU do not match. */
81 static void check_use_of_rdtscp_on_this_cpu(const gmx::MDLogger& mdlog, const gmx::CpuInfo& cpuInfo)
82 {
83     bool binaryUsesRdtscp = GMX_USE_RDTSCP;
84
85     const char* programName = gmx::getProgramContext().displayName();
86
87     if (cpuInfo.supportLevel() < gmx::CpuInfo::SupportLevel::Features)
88     {
89         if (binaryUsesRdtscp)
90         {
91             GMX_LOG(mdlog.warning)
92                     .asParagraph()
93                     .appendTextFormatted(
94                             "The %s executable was compiled to use the rdtscp CPU instruction. "
95                             "We cannot detect the features of your current CPU, but will proceed "
96                             "anyway. "
97                             "If you get a crash, rebuild GROMACS with the GMX_USE_RDTSCP=OFF CMake "
98                             "option.",
99                             programName);
100         }
101     }
102     else
103     {
104         bool cpuHasRdtscp = cpuInfo.feature(gmx::CpuInfo::Feature::X86_Rdtscp);
105
106         if (!cpuHasRdtscp && binaryUsesRdtscp)
107         {
108             gmx_fatal(FARGS,
109                       "The %s executable was compiled to use the rdtscp CPU instruction. "
110                       "However, this is not supported by the current hardware and continuing would "
111                       "lead to a crash. "
112                       "Please rebuild GROMACS with the GMX_USE_RDTSCP=OFF CMake option.",
113                       programName);
114         }
115
116         if (cpuHasRdtscp && !binaryUsesRdtscp)
117         {
118             GMX_LOG(mdlog.warning)
119                     .asParagraph()
120                     .appendTextFormatted(
121                             "The current CPU can measure timings more accurately than the code in\n"
122                             "%s was configured to use. This might affect your simulation\n"
123                             "speed as accurate timings are needed for load-balancing.\n"
124                             "Please consider rebuilding %s with the GMX_USE_RDTSCP=ON CMake "
125                             "option.",
126                             programName, programName);
127         }
128     }
129 }
130
131 static std::string detected_hardware_string(const gmx_hw_info_t* hwinfo, bool bFullCpuInfo)
132 {
133     std::string s;
134
135     const gmx::CpuInfo&          cpuInfo = *hwinfo->cpuInfo;
136     const gmx::HardwareTopology& hwTop   = *hwinfo->hardwareTopology;
137
138     s = gmx::formatString("\n");
139     s += gmx::formatString("Running on %d node%s with total", hwinfo->nphysicalnode,
140                            hwinfo->nphysicalnode == 1 ? "" : "s");
141     if (hwinfo->ncore_tot > 0)
142     {
143         s += gmx::formatString(" %d cores,", hwinfo->ncore_tot);
144     }
145     s += gmx::formatString(" %d logical cores", hwinfo->nhwthread_tot);
146     if (canPerformDeviceDetection(nullptr))
147     {
148         s += gmx::formatString(", %d compatible GPU%s", hwinfo->ngpu_compatible_tot,
149                                hwinfo->ngpu_compatible_tot == 1 ? "" : "s");
150     }
151     else if (bGPUBinary)
152     {
153         if (isDeviceDetectionEnabled())
154         {
155             s += gmx::formatString(" (GPU detection failed)");
156         }
157         else
158         {
159             s += gmx::formatString(" (GPU detection deactivated)");
160         }
161     }
162     s += gmx::formatString("\n");
163
164     if (hwinfo->nphysicalnode > 1)
165     {
166         /* Print per node hardware feature counts */
167         if (hwinfo->ncore_max > 0)
168         {
169             s += gmx::formatString("  Cores per node:           %2d", hwinfo->ncore_min);
170             if (hwinfo->ncore_max > hwinfo->ncore_min)
171             {
172                 s += gmx::formatString(" - %2d", hwinfo->ncore_max);
173             }
174             s += gmx::formatString("\n");
175         }
176         s += gmx::formatString("  Logical cores per node:   %2d", hwinfo->nhwthread_min);
177         if (hwinfo->nhwthread_max > hwinfo->nhwthread_min)
178         {
179             s += gmx::formatString(" - %2d", hwinfo->nhwthread_max);
180         }
181         s += gmx::formatString("\n");
182         if (bGPUBinary)
183         {
184             s += gmx::formatString("  Compatible GPUs per node: %2d", hwinfo->ngpu_compatible_min);
185             if (hwinfo->ngpu_compatible_max > hwinfo->ngpu_compatible_min)
186             {
187                 s += gmx::formatString(" - %2d", hwinfo->ngpu_compatible_max);
188             }
189             s += gmx::formatString("\n");
190             if (hwinfo->ngpu_compatible_tot > 0)
191             {
192                 if (hwinfo->bIdenticalGPUs)
193                 {
194                     s += gmx::formatString("  All nodes have identical type(s) of GPUs\n");
195                 }
196                 else
197                 {
198                     /* This message will also appear with identical GPU types
199                      * when at least one node has no GPU.
200                      */
201                     s += gmx::formatString(
202                             "  Different nodes have different type(s) and/or order of GPUs\n");
203                 }
204             }
205         }
206     }
207
208 #if GMX_LIB_MPI
209     int  rank;
210     char host[STRLEN];
211
212     gmx_gethostname(host, STRLEN);
213
214     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
215
216     // TODO Use a wrapper around MPI_Get_processor_name instead.
217     s += gmx::formatString("Hardware detected on host %s (the node of MPI rank %d):\n", host, rank);
218 #else
219     s += gmx::formatString("Hardware detected:\n");
220 #endif
221     s += gmx::formatString("  CPU info:\n");
222
223     s += gmx::formatString("    Vendor: %s\n", cpuInfo.vendorString().c_str());
224
225     s += gmx::formatString("    Brand:  %s\n", cpuInfo.brandString().c_str());
226
227     if (bFullCpuInfo)
228     {
229         s += gmx::formatString("    Family: %d   Model: %d   Stepping: %d\n", cpuInfo.family(),
230                                cpuInfo.model(), cpuInfo.stepping());
231
232         s += gmx::formatString("    Features:");
233         for (auto& f : cpuInfo.featureSet())
234         {
235             s += gmx::formatString(" %s", gmx::CpuInfo::featureString(f).c_str());
236         }
237         s += gmx::formatString("\n");
238     }
239
240     if (cpuInfo.feature(gmx::CpuInfo::Feature::X86_Avx512F))
241     {
242         int avx512fmaunits = gmx::identifyAvx512FmaUnits();
243         s += gmx::formatString("    Number of AVX-512 FMA units:");
244         if (avx512fmaunits > 0)
245         {
246             s += gmx::formatString(" %d", avx512fmaunits);
247             if (avx512fmaunits == 1)
248             {
249                 s += gmx::formatString(" (AVX2 is faster w/o 2 AVX-512 FMA units)");
250             }
251         }
252         else
253         {
254             s += gmx::formatString(" Cannot run AVX-512 detection - assuming 2");
255         }
256         s += gmx::formatString("\n");
257     }
258
259     s += gmx::formatString("  Hardware topology: ");
260     switch (hwTop.supportLevel())
261     {
262         case gmx::HardwareTopology::SupportLevel::None: s += gmx::formatString("None\n"); break;
263         case gmx::HardwareTopology::SupportLevel::LogicalProcessorCount:
264             s += gmx::formatString("Only logical processor count\n");
265             break;
266         case gmx::HardwareTopology::SupportLevel::Basic: s += gmx::formatString("Basic\n"); break;
267         case gmx::HardwareTopology::SupportLevel::Full: s += gmx::formatString("Full\n"); break;
268         case gmx::HardwareTopology::SupportLevel::FullWithDevices:
269             s += gmx::formatString("Full, with devices\n");
270             break;
271     }
272
273     if (!hwTop.isThisSystem())
274     {
275         s += gmx::formatString("  NOTE: Hardware topology cached or synthetic, not detected.\n");
276         if (char* p = std::getenv("HWLOC_XMLFILE"))
277         {
278             s += gmx::formatString("        HWLOC_XMLFILE=%s\n", p);
279         }
280     }
281
282     if (bFullCpuInfo)
283     {
284         if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Basic)
285         {
286             s += gmx::formatString("    Sockets, cores, and logical processors:\n");
287
288             for (auto& socket : hwTop.machine().sockets)
289             {
290                 s += gmx::formatString("      Socket %2d:", socket.id);
291                 for (auto& c : socket.cores)
292                 {
293                     s += gmx::formatString(" [");
294                     for (auto& t : c.hwThreads)
295                     {
296                         s += gmx::formatString(" %3d", t.logicalProcessorId);
297                     }
298                     s += gmx::formatString("]");
299                 }
300                 s += gmx::formatString("\n");
301             }
302         }
303         if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::Full)
304         {
305             s += gmx::formatString("    Numa nodes:\n");
306             for (auto& n : hwTop.machine().numa.nodes)
307             {
308                 s += gmx::formatString("      Node %2d (%zu bytes mem):", n.id, n.memory);
309                 for (auto& l : n.logicalProcessorId)
310                 {
311                     s += gmx::formatString(" %3d", l);
312                 }
313                 s += gmx::formatString("\n");
314             }
315             s += gmx::formatString("      Latency:\n          ");
316             for (std::size_t j = 0; j < hwTop.machine().numa.nodes.size(); j++)
317             {
318                 s += gmx::formatString(" %5zu", j);
319             }
320             s += gmx::formatString("\n");
321             for (std::size_t i = 0; i < hwTop.machine().numa.nodes.size(); i++)
322             {
323                 s += gmx::formatString("     %5zu", i);
324                 for (std::size_t j = 0; j < hwTop.machine().numa.nodes.size(); j++)
325                 {
326                     s += gmx::formatString(" %5.2f", hwTop.machine().numa.relativeLatency[i][j]);
327                 }
328                 s += gmx::formatString("\n");
329             }
330
331
332             s += gmx::formatString("    Caches:\n");
333             for (auto& c : hwTop.machine().caches)
334             {
335                 s += gmx::formatString(
336                         "      L%d: %zu bytes, linesize %d bytes, assoc. %d, shared %d ways\n",
337                         c.level, c.size, c.linesize, c.associativity, c.shared);
338             }
339         }
340         if (hwTop.supportLevel() >= gmx::HardwareTopology::SupportLevel::FullWithDevices)
341         {
342             s += gmx::formatString("    PCI devices:\n");
343             for (auto& d : hwTop.machine().devices)
344             {
345                 s += gmx::formatString(
346                         "      %04x:%02x:%02x.%1x  Id: %04x:%04x  Class: 0x%04x  Numa: %d\n", d.domain,
347                         d.bus, d.dev, d.func, d.vendorId, d.deviceId, d.classId, d.numaNodeId);
348             }
349         }
350     }
351
352     if (bGPUBinary && !hwinfo->deviceInfoList.empty())
353     {
354         s += gmx::formatString("  GPU info:\n");
355         s += gmx::formatString("    Number of GPUs detected: %d\n",
356                                static_cast<int>(hwinfo->deviceInfoList.size()));
357         s += sprint_gpus(hwinfo->deviceInfoList) + "\n";
358     }
359     return s;
360 }
361
362 void gmx_print_detected_hardware(FILE*                fplog,
363                                  const bool           warnToStdErr,
364                                  const gmx::MDLogger& mdlog,
365                                  const gmx_hw_info_t* hwinfo)
366 {
367     const gmx::CpuInfo& cpuInfo = *hwinfo->cpuInfo;
368
369     if (fplog != nullptr)
370     {
371         std::string detected;
372
373         detected = detected_hardware_string(hwinfo, TRUE);
374
375         fprintf(fplog, "%s\n", detected.c_str());
376     }
377
378     // Do not spam stderr with all our internal information unless
379     // there was something that actually went wrong; general information
380     // belongs in the logfile.
381
382     /* Check the compiled SIMD instruction set against that of the node
383      * with the lowest SIMD level support (skip if SIMD detection did not work)
384      */
385     if (cpuInfo.supportLevel() >= gmx::CpuInfo::SupportLevel::Features)
386     {
387         gmx::simdCheck(static_cast<gmx::SimdType>(hwinfo->simd_suggest_min), fplog, warnToStdErr);
388     }
389
390     /* For RDTSCP we only check on our local node and skip the MPI reduction */
391     check_use_of_rdtscp_on_this_cpu(mdlog, cpuInfo);
392 }