GROMACS 2020 first beta release
[alexxy/gromacs.git] / src / gromacs / hardware / detecthardware.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 "detecthardware.h"
38
39 #include "config.h"
40
41 #include <algorithm>
42 #include <array>
43 #include <chrono>
44 #include <memory>
45 #include <string>
46 #include <thread>
47 #include <vector>
48
49 #include "gromacs/compat/pointers.h"
50 #include "gromacs/gpu_utils/gpu_utils.h"
51 #include "gromacs/hardware/cpuinfo.h"
52 #include "gromacs/hardware/hardwaretopology.h"
53 #include "gromacs/hardware/hw_info.h"
54 #include "gromacs/simd/support.h"
55 #include "gromacs/utility/basedefinitions.h"
56 #include "gromacs/utility/basenetwork.h"
57 #include "gromacs/utility/baseversion.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/logger.h"
63 #include "gromacs/utility/mutex.h"
64 #include "gromacs/utility/physicalnodecommunicator.h"
65
66 #include "architecture.h"
67
68 #ifdef HAVE_UNISTD_H
69 #    include <unistd.h>       // sysconf()
70 #endif
71
72 gmx_hw_info_t::gmx_hw_info_t(std::unique_ptr<gmx::CpuInfo>          cpuInfo,
73                              std::unique_ptr<gmx::HardwareTopology> hardwareTopology)
74     : cpuInfo(std::move(cpuInfo)),
75       hardwareTopology(std::move(hardwareTopology))
76 {
77 }
78
79 gmx_hw_info_t::~gmx_hw_info_t()
80 {
81     free_gpu_info(&gpu_info);
82 }
83
84 namespace gmx
85 {
86
87 //! Convenience macro to help us avoid ifdefs each time we use sysconf
88 #if !defined(_SC_NPROCESSORS_ONLN) && defined(_SC_NPROC_ONLN)
89 #    define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
90 #endif
91
92 //! Convenience macro to help us avoid ifdefs each time we use sysconf
93 #if !defined(_SC_NPROCESSORS_CONF) && defined(_SC_NPROC_CONF)
94 #    define _SC_NPROCESSORS_CONF _SC_NPROC_CONF
95 #endif
96
97 /*! \brief Information about the hardware of all nodes (common to all threads in this process).
98  *
99  * This information is constructed only when required, but thereafter
100  * its lifetime is that of the whole process, potentially across
101  * multiple successive simulation parts. It's wise to ensure that only
102  * one thread can create the information, but thereafter they can all
103  * read it without e.g. needing a std::shared_ptr to ensure its
104  * lifetime exceeds that of the thread. */
105 static std::unique_ptr<gmx_hw_info_t> g_hardwareInfo;
106 //! A mutex to protect the hwinfo structure
107 static Mutex                          g_hardwareInfoMutex;
108
109 //! Detect GPUs, if that makes sense to attempt.
110 static void gmx_detect_gpus(const gmx::MDLogger              &mdlog,
111                             const PhysicalNodeCommunicator   &physicalNodeComm,
112                             compat::not_null<gmx_hw_info_t *> hardwareInfo)
113 {
114     hardwareInfo->gpu_info.bDetectGPUs = canPerformGpuDetection();
115
116     if (!hardwareInfo->gpu_info.bDetectGPUs)
117     {
118         return;
119     }
120
121     bool isMasterRankOfPhysicalNode = true;
122 #if GMX_LIB_MPI
123     isMasterRankOfPhysicalNode = (physicalNodeComm.rank_ == 0);
124 #else
125     // We choose to run the detection only once with thread-MPI and
126     // use a mutex to enforce it.
127     GMX_UNUSED_VALUE(physicalNodeComm);
128     isMasterRankOfPhysicalNode = true;
129 #endif
130
131     /* The OpenCL support requires us to run detection on all ranks.
132      * With CUDA we don't need to, and prefer to detect on one rank
133      * and send the information to the other ranks over MPI. */
134     bool allRanksMustDetectGpus = (GMX_GPU == GMX_GPU_OPENCL);
135     bool gpusCanBeDetected      = false;
136     if (isMasterRankOfPhysicalNode || allRanksMustDetectGpus)
137     {
138         std::string errorMessage;
139         gpusCanBeDetected = isGpuDetectionFunctional(&errorMessage);
140         if (!gpusCanBeDetected)
141         {
142             GMX_LOG(mdlog.info).asParagraph().appendTextFormatted(
143                     "NOTE: Detection of GPUs failed. The API reported:\n"
144                     "      %s\n"
145                     "      GROMACS cannot run tasks on a GPU.",
146                     errorMessage.c_str());
147         }
148     }
149
150     if (gpusCanBeDetected)
151     {
152         findGpus(&hardwareInfo->gpu_info);
153         // No need to tell the user anything at this point, they get a
154         // hardware report later.
155     }
156
157 #if GMX_LIB_MPI
158     if (!allRanksMustDetectGpus)
159     {
160         /* Broadcast the GPU info to the other ranks within this node */
161         MPI_Bcast(&hardwareInfo->gpu_info.n_dev, 1, MPI_INT, 0, physicalNodeComm.comm_);
162
163         if (hardwareInfo->gpu_info.n_dev > 0)
164         {
165             int dev_size;
166
167             dev_size = hardwareInfo->gpu_info.n_dev*sizeof_gpu_dev_info();
168
169             if (!isMasterRankOfPhysicalNode)
170             {
171                 hardwareInfo->gpu_info.gpu_dev =
172                     (struct gmx_device_info_t *)malloc(dev_size);
173             }
174             MPI_Bcast(hardwareInfo->gpu_info.gpu_dev, dev_size, MPI_BYTE,
175                       0, physicalNodeComm.comm_);
176             MPI_Bcast(&hardwareInfo->gpu_info.n_dev_compatible, 1, MPI_INT,
177                       0, physicalNodeComm.comm_);
178         }
179     }
180 #endif
181 }
182
183 //! Reduce the locally collected \p hardwareInfo over MPI ranks
184 static void gmx_collect_hardware_mpi(const gmx::CpuInfo               &cpuInfo,
185                                      const PhysicalNodeCommunicator   &physicalNodeComm,
186                                      compat::not_null<gmx_hw_info_t *> hardwareInfo)
187 {
188     const int  ncore        = hardwareInfo->hardwareTopology->numberOfCores();
189     /* Zen has family=23, for now we treat future AMD CPUs like Zen
190      * and Hygon Dhyana like Zen */
191     const bool cpuIsAmdZen  = ((cpuInfo.vendor() == CpuInfo::Vendor::Amd &&
192                                 cpuInfo.family() >= 23) ||
193                                cpuInfo.vendor() == CpuInfo::Vendor::Hygon);
194
195 #if GMX_LIB_MPI
196     int       nhwthread, ngpu, i;
197     int       gpu_hash;
198
199     nhwthread = hardwareInfo->nthreads_hw_avail;
200     ngpu      = hardwareInfo->gpu_info.n_dev_compatible;
201     /* Create a unique hash of the GPU type(s) in this node */
202     gpu_hash  = 0;
203     /* Here it might be better to only loop over the compatible GPU, but we
204      * don't have that information available and it would also require
205      * removing the device ID from the device info string.
206      */
207     for (i = 0; i < hardwareInfo->gpu_info.n_dev; i++)
208     {
209         char stmp[STRLEN];
210
211         /* Since the device ID is incorporated in the hash, the order of
212          * the GPUs affects the hash. Also two identical GPUs won't give
213          * a gpu_hash of zero after XORing.
214          */
215         get_gpu_device_info_string(stmp, hardwareInfo->gpu_info, i);
216         gpu_hash ^= gmx_string_fullhash_func(stmp, gmx_string_hash_init);
217     }
218
219     constexpr int                          numElementsCounts =  4;
220     std::array<int, numElementsCounts>     countsReduced;
221     {
222         std::array<int, numElementsCounts> countsLocal = {{0}};
223         // Organize to sum values from only one rank within each node,
224         // so we get the sum over all nodes.
225         bool isMasterRankOfPhysicalNode = (physicalNodeComm.rank_ == 0);
226         if (isMasterRankOfPhysicalNode)
227         {
228             countsLocal[0] = 1;
229             countsLocal[1] = ncore;
230             countsLocal[2] = nhwthread;
231             countsLocal[3] = ngpu;
232         }
233
234         MPI_Allreduce(countsLocal.data(), countsReduced.data(), countsLocal.size(),
235                       MPI_INT, MPI_SUM, MPI_COMM_WORLD);
236     }
237
238     constexpr int                       numElementsMax = 11;
239     std::array<int, numElementsMax>     maxMinReduced;
240     {
241         std::array<int, numElementsMax> maxMinLocal;
242         /* Store + and - values for all ranks,
243          * so we can get max+min with one MPI call.
244          */
245         maxMinLocal[0]  = ncore;
246         maxMinLocal[1]  = nhwthread;
247         maxMinLocal[2]  = ngpu;
248         maxMinLocal[3]  = static_cast<int>(gmx::simdSuggested(cpuInfo));
249         maxMinLocal[4]  = gpu_hash;
250         maxMinLocal[5]  = -maxMinLocal[0];
251         maxMinLocal[6]  = -maxMinLocal[1];
252         maxMinLocal[7]  = -maxMinLocal[2];
253         maxMinLocal[8]  = -maxMinLocal[3];
254         maxMinLocal[9]  = -maxMinLocal[4];
255         maxMinLocal[10] = (cpuIsAmdZen ? 1 : 0);
256
257         MPI_Allreduce(maxMinLocal.data(), maxMinReduced.data(), maxMinLocal.size(),
258                       MPI_INT, MPI_MAX, MPI_COMM_WORLD);
259     }
260
261     hardwareInfo->nphysicalnode       = countsReduced[0];
262     hardwareInfo->ncore_tot           = countsReduced[1];
263     hardwareInfo->ncore_min           = -maxMinReduced[5];
264     hardwareInfo->ncore_max           = maxMinReduced[0];
265     hardwareInfo->nhwthread_tot       = countsReduced[2];
266     hardwareInfo->nhwthread_min       = -maxMinReduced[6];
267     hardwareInfo->nhwthread_max       = maxMinReduced[1];
268     hardwareInfo->ngpu_compatible_tot = countsReduced[3];
269     hardwareInfo->ngpu_compatible_min = -maxMinReduced[7];
270     hardwareInfo->ngpu_compatible_max = maxMinReduced[2];
271     hardwareInfo->simd_suggest_min    = -maxMinReduced[8];
272     hardwareInfo->simd_suggest_max    = maxMinReduced[3];
273     hardwareInfo->bIdenticalGPUs      = (maxMinReduced[4] == -maxMinReduced[9]);
274     hardwareInfo->haveAmdZenCpu       = (maxMinReduced[10] > 0);
275 #else
276     /* All ranks use the same pointer, protected by a mutex in the caller */
277     hardwareInfo->nphysicalnode       = 1;
278     hardwareInfo->ncore_tot           = ncore;
279     hardwareInfo->ncore_min           = ncore;
280     hardwareInfo->ncore_max           = ncore;
281     hardwareInfo->nhwthread_tot       = hardwareInfo->nthreads_hw_avail;
282     hardwareInfo->nhwthread_min       = hardwareInfo->nthreads_hw_avail;
283     hardwareInfo->nhwthread_max       = hardwareInfo->nthreads_hw_avail;
284     hardwareInfo->ngpu_compatible_tot = hardwareInfo->gpu_info.n_dev_compatible;
285     hardwareInfo->ngpu_compatible_min = hardwareInfo->gpu_info.n_dev_compatible;
286     hardwareInfo->ngpu_compatible_max = hardwareInfo->gpu_info.n_dev_compatible;
287     hardwareInfo->simd_suggest_min    = static_cast<int>(simdSuggested(cpuInfo));
288     hardwareInfo->simd_suggest_max    = static_cast<int>(simdSuggested(cpuInfo));
289     hardwareInfo->bIdenticalGPUs      = TRUE;
290     hardwareInfo->haveAmdZenCpu       = cpuIsAmdZen;
291     GMX_UNUSED_VALUE(physicalNodeComm);
292 #endif
293 }
294
295 /*! \brief Utility that does dummy computing for max 2 seconds to spin up cores
296  *
297  *  This routine will check the number of cores configured and online
298  *  (using sysconf), and the spins doing dummy compute operations for up to
299  *  2 seconds, or until all cores have come online. This can be used prior to
300  *  hardware detection for platforms that take unused processors offline.
301  *
302  *  This routine will not throw exceptions.
303  */
304 static void
305 spinUpCore() noexcept
306 {
307 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF) && defined(_SC_NPROCESSORS_ONLN)
308     float dummy           = 0.1;
309     int   countConfigured = sysconf(_SC_NPROCESSORS_CONF);    // noexcept
310     auto  start           = std::chrono::steady_clock::now(); // noexcept
311
312     while (sysconf(_SC_NPROCESSORS_ONLN) < countConfigured &&
313            std::chrono::steady_clock::now() - start < std::chrono::seconds(2))
314     {
315         for (int i = 1; i < 10000; i++)
316         {
317             dummy /= i;
318         }
319     }
320
321     if (dummy < 0)
322     {
323         printf("This cannot happen, but prevents loop from being optimized away.");
324     }
325 #endif
326 }
327
328 /*! \brief Prepare the system before hardware topology detection
329  *
330  * This routine should perform any actions we want to put the system in a state
331  * where we want it to be before detecting the hardware topology. For most
332  * processors there is nothing to do, but some architectures (in particular ARM)
333  * have support for taking configured cores offline, which will make them disappear
334  * from the online processor count.
335  *
336  * This routine checks if there is a mismatch between the number of cores
337  * configured and online, and in that case we issue a small workload that
338  * attempts to wake sleeping cores before doing the actual detection.
339  *
340  * This type of mismatch can also occur for x86 or PowerPC on Linux, if SMT has only
341  * been disabled in the kernel (rather than bios). Since those cores will never
342  * come online automatically, we currently skip this test for x86 & PowerPC to
343  * avoid wasting 2 seconds. We also skip the test if there is no thread support.
344  *
345  * \note Cores will sleep relatively quickly again, so it's important to issue
346  *       the real detection code directly after this routine.
347  */
348 static void
349 hardwareTopologyPrepareDetection()
350 {
351 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF) && \
352     (defined(THREAD_PTHREADS) || defined(THREAD_WINDOWS))
353
354     // Modify this conditional when/if x86 or PowerPC starts to sleep some cores
355     if (c_architecture != Architecture::X86 &&
356         c_architecture != Architecture::PowerPC)
357     {
358         int                      countConfigured  = sysconf(_SC_NPROCESSORS_CONF);
359         std::vector<std::thread> workThreads(countConfigured);
360
361         for (auto &t : workThreads)
362         {
363             t = std::thread(spinUpCore);
364         }
365
366         for (auto &t : workThreads)
367         {
368             t.join();
369         }
370     }
371 #endif
372 }
373
374 /*! \brief Sanity check hardware topology and print some notes to log
375  *
376  *  \param mdlog            Logger.
377  *  \param hardwareTopology Reference to hardwareTopology object.
378  */
379 static void
380 hardwareTopologyDoubleCheckDetection(const gmx::MDLogger gmx_unused         &mdlog,
381                                      const gmx::HardwareTopology gmx_unused &hardwareTopology)
382 {
383 #if defined HAVE_SYSCONF && defined(_SC_NPROCESSORS_CONF)
384     if (hardwareTopology.supportLevel() < gmx::HardwareTopology::SupportLevel::LogicalProcessorCount)
385     {
386         return;
387     }
388
389     int countFromDetection = hardwareTopology.machine().logicalProcessorCount;
390     int countConfigured    = sysconf(_SC_NPROCESSORS_CONF);
391
392     /* BIOS, kernel or user actions can take physical processors
393      * offline. We already cater for the some of the cases inside the hardwareToplogy
394      * by trying to spin up cores just before we detect, but there could be other
395      * cases where it is worthwhile to hint that there might be more resources available.
396      */
397     if (countConfigured >= 0 && countConfigured != countFromDetection)
398     {
399         GMX_LOG(mdlog.info).
400             appendTextFormatted("Note: %d CPUs configured, but only %d were detected to be online.\n", countConfigured, countFromDetection);
401
402         if (c_architecture == Architecture::X86 &&
403             countConfigured == 2*countFromDetection)
404         {
405             GMX_LOG(mdlog.info).
406                 appendText("      X86 Hyperthreading is likely disabled; enable it for better performance.");
407         }
408         // For PowerPC (likely Power8) it is possible to set SMT to either 2,4, or 8-way hardware threads.
409         // We only warn if it is completely disabled since default performance drops with SMT8.
410         if (c_architecture == Architecture::PowerPC &&
411             countConfigured == 8*countFromDetection)
412         {
413             GMX_LOG(mdlog.info).
414                 appendText("      PowerPC SMT is likely disabled; enable SMT2/SMT4 for better performance.");
415         }
416     }
417 #endif
418 }
419
420 gmx_hw_info_t *gmx_detect_hardware(const gmx::MDLogger            &mdlog,
421                                    const PhysicalNodeCommunicator &physicalNodeComm)
422 {
423     // By construction, only one thread ever runs hardware detection,
424     // but we may as well prevent issues arising if that would change.
425     // Taking the lock early ensures that exactly one thread can
426     // attempt to construct g_hardwareInfo.
427     lock_guard<Mutex> lock(g_hardwareInfoMutex);
428
429     // If we already have the information, just return a handle to it.
430     if (g_hardwareInfo != nullptr)
431     {
432         return g_hardwareInfo.get();
433     }
434
435     // Make the new hardwareInfo in a temporary.
436     hardwareTopologyPrepareDetection();
437
438     // TODO: We should also do CPU hardware detection only once on each
439     // physical node and broadcast it, instead of doing it on every MPI rank.
440     auto hardwareInfo = std::make_unique<gmx_hw_info_t>(std::make_unique<CpuInfo>(CpuInfo::detect()),
441                                                         std::make_unique<HardwareTopology>(HardwareTopology::detect()));
442
443     // If we detected the topology on this system, double-check that it makes sense
444     if (hardwareInfo->hardwareTopology->isThisSystem())
445     {
446         hardwareTopologyDoubleCheckDetection(mdlog, *hardwareInfo->hardwareTopology);
447     }
448
449     // TODO: Get rid of this altogether.
450     hardwareInfo->nthreads_hw_avail = hardwareInfo->hardwareTopology->machine().logicalProcessorCount;
451
452     // Detect GPUs
453     hardwareInfo->gpu_info.n_dev            = 0;
454     hardwareInfo->gpu_info.n_dev_compatible = 0;
455     hardwareInfo->gpu_info.gpu_dev          = nullptr;
456
457     gmx_detect_gpus(mdlog, physicalNodeComm, compat::make_not_null(hardwareInfo));
458     gmx_collect_hardware_mpi(*hardwareInfo->cpuInfo, physicalNodeComm, compat::make_not_null(hardwareInfo));
459
460     // Now that the temporary is fully constructed, swap it to become
461     // the real thing.
462     g_hardwareInfo.swap(hardwareInfo);
463
464     return g_hardwareInfo.get();
465 }
466
467 bool compatibleGpusFound(const gmx_gpu_info_t &gpu_info)
468 {
469     return gpu_info.n_dev_compatible > 0;
470 }
471
472 }  // namespace gmx