Detect CUDA-aware support in underlying MPI implementation
[alexxy/gromacs.git] / src / gromacs / utility / binaryinformation.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \internal \file
39  * \brief Implements functionality for printing information about the
40  * currently running binary
41  *
42  * \ingroup module_utility
43  */
44 #include "gmxpre.h"
45
46 #include "binaryinformation.h"
47
48 #include "config.h"
49
50 #if GMX_FFT_FFTW3 || GMX_FFT_ARMPL_FFTW3
51 // Needed for construction of the FFT library description string
52 #    include <fftw3.h>
53 #endif
54
55 #ifdef HAVE_LIBMKL
56 #    include <mkl.h>
57 #endif
58
59 #if HAVE_EXTRAE
60 #    include <extrae_user_events.h>
61 #endif
62
63 #if GMX_USE_HWLOC
64 #    include <hwloc.h>
65 #endif
66
67 #include <cstdio>
68 #include <cstdlib>
69 #include <cstring>
70
71 #include <algorithm>
72 #include <array>
73 #include <string>
74
75 /* This file is completely threadsafe - keep it that way! */
76
77 #include "buildinfo.h"
78 #include "gromacs/utility/arraysize.h"
79 #include "gromacs/utility/baseversion.h"
80 #include "gromacs/utility/exceptions.h"
81 #include "gromacs/utility/gmxassert.h"
82 #include "gromacs/utility/path.h"
83 #include "gromacs/utility/programcontext.h"
84 #include "gromacs/utility/stringutil.h"
85 #include "gromacs/utility/sysinfo.h"
86 #include "gromacs/utility/textwriter.h"
87
88 #include "cuda_version_information.h"
89
90 namespace
91 {
92
93 using gmx::formatString;
94
95 //! \cond Doxygen does not need to care about most of this stuff, and the macro usage is painful to document
96
97 int centeringOffset(int width, int length)
98 {
99     return std::max(width - length, 0) / 2;
100 }
101
102 std::string formatCentered(int width, const char* text)
103 {
104     const int offset = centeringOffset(width, std::strlen(text));
105     return formatString("%*s%s", offset, "", text);
106 }
107
108 void printCopyright(gmx::TextWriter* writer)
109 {
110     // Contributors sorted alphabetically by last name
111     static const char* const Contributors[]  = { "Andrey Alekseenko",
112                                                 "Emile Apol",
113                                                 "Rossen Apostolov",
114                                                 "Paul Bauer",
115                                                 "Herman J.C. Berendsen",
116                                                 "Par Bjelkmar",
117                                                 "Christian Blau",
118                                                 "Viacheslav Bolnykh",
119                                                 "Kevin Boyd",
120                                                 "Aldert van Buuren",
121                                                 "Rudi van Drunen",
122                                                 "Anton Feenstra",
123                                                 "Gilles Gouaillardet",
124                                                 "Alan Gray",
125                                                 "Gerrit Groenhof",
126                                                 "Anca Hamuraru",
127                                                 "Vincent Hindriksen",
128                                                 "M. Eric Irrgang",
129                                                 "Aleksei Iupinov",
130                                                 "Christoph Junghans",
131                                                 "Joe Jordan",
132                                                 "Dimitrios Karkoulis",
133                                                 "Peter Kasson",
134                                                 "Jiri Kraus",
135                                                 "Carsten Kutzner",
136                                                 "Per Larsson",
137                                                 "Justin A. Lemkul",
138                                                 "Viveca Lindahl",
139                                                 "Magnus Lundborg",
140                                                 "Erik Marklund",
141                                                 "Pascal Merz",
142                                                 "Pieter Meulenhoff",
143                                                 "Teemu Murtola",
144                                                 "Szilard Pall",
145                                                 "Sander Pronk",
146                                                 "Roland Schulz",
147                                                 "Michael Shirts",
148                                                 "Alexey Shvetsov",
149                                                 "Alfons Sijbers",
150                                                 "Peter Tieleman",
151                                                 "Jon Vincent",
152                                                 "Teemu Virolainen",
153                                                 "Christian Wennberg",
154                                                 "Maarten Wolf",
155                                                 "Artem Zhmurov" };
156     static const char* const CopyrightText[] = {
157         "Copyright (c) 1991-2000, University of Groningen, The Netherlands.",
158         "Copyright (c) 2001-2019, The GROMACS development team at",
159         "Uppsala University, Stockholm University and",
160         "the Royal Institute of Technology, Sweden.",
161         "check out http://www.gromacs.org for more information."
162     };
163
164 #define NCONTRIBUTORS static_cast<int>(asize(Contributors))
165 #define NCR static_cast<int>(asize(CopyrightText))
166
167     // TODO a centering behaviour of TextWriter could be useful here
168     writer->writeLine(formatCentered(78, "GROMACS is written by:"));
169     for (int i = 0; i < NCONTRIBUTORS;)
170     {
171         for (int j = 0; j < 3 && i < NCONTRIBUTORS; ++j, ++i)
172         {
173             const int            width = 26;
174             std::array<char, 30> buf;
175             const int            offset = centeringOffset(width, strlen(Contributors[i]));
176             GMX_RELEASE_ASSERT(static_cast<int>(strlen(Contributors[i])) + offset < gmx::ssize(buf),
177                                "Formatting buffer is not long enough");
178             std::fill(buf.begin(), buf.begin() + offset, ' ');
179             std::strncpy(buf.data() + offset, Contributors[i], gmx::ssize(buf) - offset);
180             writer->writeString(formatString(" %-*s", width, buf.data()));
181         }
182         writer->ensureLineBreak();
183     }
184     writer->writeLine(formatCentered(78, "and the project leaders:"));
185     writer->writeLine(
186             formatCentered(78, "Mark Abraham, Berk Hess, Erik Lindahl, and David van der Spoel"));
187     writer->ensureEmptyLine();
188     for (int i = 0; i < NCR; ++i)
189     {
190         writer->writeLine(CopyrightText[i]);
191     }
192     writer->ensureEmptyLine();
193
194     // Folding At Home has different licence to allow digital
195     // signatures in GROMACS, so does not need to show the normal
196     // license statement.
197     if (!GMX_FAHCORE)
198     {
199         writer->writeLine("GROMACS is free software; you can redistribute it and/or modify it");
200         writer->writeLine("under the terms of the GNU Lesser General Public License");
201         writer->writeLine("as published by the Free Software Foundation; either version 2.1");
202         writer->writeLine("of the License, or (at your option) any later version.");
203     }
204 }
205
206 // Construct a string that describes the library that provides FFT support to this build
207 const char* getFftDescriptionString()
208 {
209 // Define the FFT description string
210 #if GMX_FFT_FFTW3 || GMX_FFT_ARMPL_FFTW3
211 #    if GMX_NATIVE_WINDOWS
212     // Don't buy trouble
213     return "fftw3";
214 #    else
215     // Use the version string provided by libfftw3
216 #        if GMX_DOUBLE
217     return fftw_version;
218 #        else
219     return fftwf_version;
220 #        endif
221 #    endif
222 #endif
223 #if GMX_FFT_MKL
224     return "Intel MKL";
225 #endif
226 #if GMX_FFT_FFTPACK
227     return "fftpack (built-in)";
228 #endif
229 };
230
231 void gmx_print_version_info(gmx::TextWriter* writer)
232 {
233     writer->writeLine(formatString("GROMACS version:    %s", gmx_version()));
234     const char* const git_hash = gmx_version_git_full_hash();
235     if (git_hash[0] != '\0')
236     {
237         writer->writeLine(formatString("GIT SHA1 hash:      %s", git_hash));
238     }
239     const char* const base_hash = gmx_version_git_central_base_hash();
240     if (base_hash[0] != '\0')
241     {
242         writer->writeLine(formatString("Branched from:      %s", base_hash));
243     }
244     const char* const releaseSourceChecksum = gmxReleaseSourceChecksum();
245     const char* const currentSourceChecksum = gmxCurrentSourceChecksum();
246     if (releaseSourceChecksum[0] != '\0')
247     {
248         if (std::strcmp(releaseSourceChecksum, "NoChecksumFile") == 0)
249         {
250             writer->writeLine(formatString(
251                     "The source code this program was compiled from has not been verified because "
252                     "the reference checksum was missing during compilation. This means you have an "
253                     "incomplete GROMACS distribution, please make sure to download an intact "
254                     "source distribution and compile that before proceeding."));
255             writer->writeLine(formatString("Computed checksum: %s", currentSourceChecksum));
256         }
257         else if (std::strcmp(releaseSourceChecksum, "NoPythonAvailable") == 0)
258         {
259             writer->writeLine(
260                     formatString("Build source could not be verified, because the checksum could "
261                                  "not be computed."));
262         }
263         else if (std::strcmp(releaseSourceChecksum, currentSourceChecksum) != 0)
264         {
265             writer->writeLine(formatString(
266                     "This program has been built from source code that has been altered and does "
267                     "not match the code released as part of the official GROMACS version %s. If "
268                     "you did not intend to use an altered GROMACS version, make sure to download "
269                     "an intact source distribution and compile that before proceeding.",
270                     gmx_version()));
271             writer->writeLine(formatString(
272                     "If you have modified the source code, you are strongly encouraged to set your "
273                     "custom version suffix (using -DGMX_VERSION_STRING_OF_FORK) which will can "
274                     "help later with scientific reproducibility but also when reporting bugs."));
275             writer->writeLine(formatString("Release checksum: %s", releaseSourceChecksum));
276             writer->writeLine(formatString("Computed checksum: %s", currentSourceChecksum));
277         }
278         else
279         {
280             writer->writeLine(formatString("Verified release checksum is %s", releaseSourceChecksum));
281         }
282     }
283
284
285 #if GMX_DOUBLE
286     writer->writeLine("Precision:          double");
287 #else
288     writer->writeLine("Precision:          mixed");
289 #endif
290     writer->writeLine(formatString("Memory model:       %u bit", static_cast<unsigned>(8 * sizeof(void*))));
291
292 #if GMX_THREAD_MPI
293     writer->writeLine("MPI library:        thread_mpi");
294 #elif GMX_MPI
295 #    if HAVE_CUDA_AWARE_MPI
296     writer->writeLine("MPI library:        MPI (CUDA-aware)");
297 #    else
298     writer->writeLine("MPI library:        MPI");
299 #    endif
300 #else
301     writer->writeLine("MPI library:        none");
302 #endif
303 #if GMX_OPENMP
304     writer->writeLine(formatString("OpenMP support:     enabled (GMX_OPENMP_MAX_THREADS = %d)",
305                                    GMX_OPENMP_MAX_THREADS));
306 #else
307     writer->writeLine("OpenMP support:     disabled");
308 #endif
309     writer->writeLine(formatString("GPU support:        %s", getGpuImplementationString()));
310     writer->writeLine(formatString("SIMD instructions:  %s", GMX_SIMD_STRING));
311     writer->writeLine(formatString("FFT library:        %s", getFftDescriptionString()));
312 #if GMX_TARGET_X86
313     writer->writeLine(formatString("RDTSCP usage:       %s", GMX_USE_RDTSCP ? "enabled" : "disabled"));
314 #endif
315 #if GMX_USE_TNG
316     writer->writeLine("TNG support:        enabled");
317 #else
318     writer->writeLine("TNG support:        disabled");
319 #endif
320 #if GMX_USE_HWLOC
321     writer->writeLine(formatString("Hwloc support:      hwloc-%s", HWLOC_VERSION));
322 #else
323     writer->writeLine("Hwloc support:      disabled");
324 #endif
325 #if HAVE_EXTRAE
326     unsigned major, minor, revision;
327     Extrae_get_version(&major, &minor, &revision);
328     writer->writeLine(formatString(
329             "Tracing support:    enabled. Using Extrae-%d.%d.%d", major, minor, revision));
330 #else
331     writer->writeLine("Tracing support:    disabled");
332 #endif
333
334
335     /* TODO: The below strings can be quite long, so it would be nice to wrap
336      * them. Can wait for later, as the master branch has ready code to do all
337      * that. */
338     writer->writeLine(formatString("C compiler:         %s", BUILD_C_COMPILER));
339     writer->writeLine(formatString(
340             "C compiler flags:   %s %s", BUILD_CFLAGS, CMAKE_BUILD_CONFIGURATION_C_FLAGS));
341     writer->writeLine(formatString("C++ compiler:       %s", BUILD_CXX_COMPILER));
342     writer->writeLine(formatString(
343             "C++ compiler flags: %s %s", BUILD_CXXFLAGS, CMAKE_BUILD_CONFIGURATION_CXX_FLAGS));
344 #ifdef HAVE_LIBMKL
345     /* MKL might be used for LAPACK/BLAS even if FFTs use FFTW, so keep it separate */
346     writer->writeLine(formatString(
347             "Linked with Intel MKL version %d.%d.%d.", __INTEL_MKL__, __INTEL_MKL_MINOR__, __INTEL_MKL_UPDATE__));
348 #endif
349 #if GMX_GPU_OPENCL
350     writer->writeLine(formatString("OpenCL include dir: %s", OPENCL_INCLUDE_DIR));
351     writer->writeLine(formatString("OpenCL library:     %s", OPENCL_LIBRARY));
352     writer->writeLine(formatString("OpenCL version:     %s", OPENCL_VERSION_STRING));
353 #endif
354 #if GMX_GPU_CUDA
355     writer->writeLine(formatString("CUDA compiler:      %s", CUDA_COMPILER_INFO));
356     writer->writeLine(formatString(
357             "CUDA compiler flags:%s %s", CUDA_COMPILER_FLAGS, CMAKE_BUILD_CONFIGURATION_CXX_FLAGS));
358     writer->writeLine("CUDA driver:        " + gmx::getCudaDriverVersionString());
359     writer->writeLine("CUDA runtime:       " + gmx::getCudaRuntimeVersionString());
360 #endif
361 }
362
363 //! \endcond
364
365 } // namespace
366
367 namespace gmx
368 {
369
370 BinaryInformationSettings::BinaryInformationSettings() :
371     bExtendedInfo_(false),
372     bCopyright_(false),
373     bProcessId_(false),
374     bGeneratedByHeader_(false),
375     prefix_(""),
376     suffix_("")
377 {
378 }
379
380 void printBinaryInformation(FILE* fp, const IProgramContext& programContext)
381 {
382     TextWriter writer(fp);
383     printBinaryInformation(&writer, programContext, BinaryInformationSettings());
384 }
385
386 void printBinaryInformation(FILE*                            fp,
387                             const IProgramContext&           programContext,
388                             const BinaryInformationSettings& settings)
389 {
390     try
391     {
392         TextWriter writer(fp);
393         printBinaryInformation(&writer, programContext, settings);
394     }
395     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
396 }
397
398 void printBinaryInformation(TextWriter*                      writer,
399                             const IProgramContext&           programContext,
400                             const BinaryInformationSettings& settings)
401 {
402     // TODO Perhaps the writer could be configured with the prefix and
403     // suffix strings from the settings?
404     const char* prefix          = settings.prefix_;
405     const char* suffix          = settings.suffix_;
406     const char* precisionString = "";
407 #if GMX_DOUBLE
408     precisionString = " (double precision)";
409 #endif
410     const char* const name = programContext.displayName();
411     if (settings.bGeneratedByHeader_)
412     {
413         writer->writeLine(formatString("%sCreated by:%s", prefix, suffix));
414     }
415     // TODO: It would be nice to know here whether we are really running a
416     // Gromacs binary or some other binary that is calling Gromacs; we
417     // could then print "%s is part of GROMACS" or some alternative text.
418     std::string title = formatString(":-) GROMACS - %s, %s%s (-:", name, gmx_version(), precisionString);
419     const int indent =
420             centeringOffset(78 - std::strlen(prefix) - std::strlen(suffix), title.length()) + 1;
421     writer->writeLine(formatString("%s%*c%s%s", prefix, indent, ' ', title.c_str(), suffix));
422     writer->writeLine(formatString("%s%s", prefix, suffix));
423     if (settings.bCopyright_)
424     {
425         GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
426                            "Prefix/suffix not supported with copyright");
427         printCopyright(writer);
428         writer->ensureEmptyLine();
429         // This line is printed again after the copyright notice to make it
430         // appear together with all the other information, so that it is not
431         // necessary to read stuff above the copyright notice.
432         // The line above the copyright notice puts the copyright notice is
433         // context, though.
434         writer->writeLine(formatString(
435                 "%sGROMACS:      %s, version %s%s%s", prefix, name, gmx_version(), precisionString, suffix));
436     }
437     const char* const binaryPath = programContext.fullBinaryPath();
438     if (!gmx::isNullOrEmpty(binaryPath))
439     {
440         writer->writeLine(formatString("%sExecutable:   %s%s", prefix, binaryPath, suffix));
441     }
442     const gmx::InstallationPrefixInfo installPrefix = programContext.installationPrefix();
443     if (!gmx::isNullOrEmpty(installPrefix.path))
444     {
445         writer->writeLine(formatString("%sData prefix:  %s%s%s",
446                                        prefix,
447                                        installPrefix.path,
448                                        installPrefix.bSourceLayout ? " (source tree)" : "",
449                                        suffix));
450     }
451     const std::string workingDir = Path::getWorkingDirectory();
452     if (!workingDir.empty())
453     {
454         writer->writeLine(formatString("%sWorking dir:  %s%s", prefix, workingDir.c_str(), suffix));
455     }
456     if (settings.bProcessId_)
457     {
458         writer->writeLine(formatString("%sProcess ID:   %d%s", prefix, gmx_getpid(), suffix));
459     }
460     const char* const commandLine = programContext.commandLine();
461     if (!gmx::isNullOrEmpty(commandLine))
462     {
463         writer->writeLine(formatString(
464                 "%sCommand line:%s\n%s  %s%s", prefix, suffix, prefix, commandLine, suffix));
465     }
466     if (settings.bExtendedInfo_)
467     {
468         GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
469                            "Prefix/suffix not supported with extended info");
470         writer->ensureEmptyLine();
471         gmx_print_version_info(writer);
472     }
473 }
474
475 } // namespace gmx