Merge branch release-2020 into merge-2020-into-2021
[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", "the Royal Institute of Technology, Sweden.",
160         "check out http://www.gromacs.org for more information."
161     };
162
163 #define NCONTRIBUTORS static_cast<int>(asize(Contributors))
164 #define NCR static_cast<int>(asize(CopyrightText))
165
166     // TODO a centering behaviour of TextWriter could be useful here
167     writer->writeLine(formatCentered(78, "GROMACS is written by:"));
168     for (int i = 0; i < NCONTRIBUTORS;)
169     {
170         for (int j = 0; j < 3 && i < NCONTRIBUTORS; ++j, ++i)
171         {
172             const int            width = 26;
173             std::array<char, 30> buf;
174             const int            offset = centeringOffset(width, strlen(Contributors[i]));
175             GMX_RELEASE_ASSERT(static_cast<int>(strlen(Contributors[i])) + offset < gmx::ssize(buf),
176                                "Formatting buffer is not long enough");
177             std::fill(buf.begin(), buf.begin() + offset, ' ');
178             std::strncpy(buf.data() + offset, Contributors[i], gmx::ssize(buf) - offset);
179             writer->writeString(formatString(" %-*s", width, buf.data()));
180         }
181         writer->ensureLineBreak();
182     }
183     writer->writeLine(formatCentered(78, "and the project leaders:"));
184     writer->writeLine(
185             formatCentered(78, "Mark Abraham, Berk Hess, Erik Lindahl, and David van der Spoel"));
186     writer->ensureEmptyLine();
187     for (int i = 0; i < NCR; ++i)
188     {
189         writer->writeLine(CopyrightText[i]);
190     }
191     writer->ensureEmptyLine();
192
193     // Folding At Home has different licence to allow digital
194     // signatures in GROMACS, so does not need to show the normal
195     // license statement.
196     if (!GMX_FAHCORE)
197     {
198         writer->writeLine("GROMACS is free software; you can redistribute it and/or modify it");
199         writer->writeLine("under the terms of the GNU Lesser General Public License");
200         writer->writeLine("as published by the Free Software Foundation; either version 2.1");
201         writer->writeLine("of the License, or (at your option) any later version.");
202     }
203 }
204
205 // Construct a string that describes the library that provides FFT support to this build
206 const char* getFftDescriptionString()
207 {
208 // Define the FFT description string
209 #if GMX_FFT_FFTW3 || GMX_FFT_ARMPL_FFTW3
210 #    if GMX_NATIVE_WINDOWS
211     // Don't buy trouble
212     return "fftw3";
213 #    else
214     // Use the version string provided by libfftw3
215 #        if GMX_DOUBLE
216     return fftw_version;
217 #        else
218     return fftwf_version;
219 #        endif
220 #    endif
221 #endif
222 #if GMX_FFT_MKL
223     return "Intel MKL";
224 #endif
225 #if GMX_FFT_FFTPACK
226     return "fftpack (built-in)";
227 #endif
228 };
229
230 void gmx_print_version_info(gmx::TextWriter* writer)
231 {
232     writer->writeLine(formatString("GROMACS version:    %s", gmx_version()));
233     const char* const git_hash = gmx_version_git_full_hash();
234     if (git_hash[0] != '\0')
235     {
236         writer->writeLine(formatString("GIT SHA1 hash:      %s", git_hash));
237     }
238     const char* const base_hash = gmx_version_git_central_base_hash();
239     if (base_hash[0] != '\0')
240     {
241         writer->writeLine(formatString("Branched from:      %s", base_hash));
242     }
243     const char* const releaseSourceChecksum = gmxReleaseSourceChecksum();
244     const char* const currentSourceChecksum = gmxCurrentSourceChecksum();
245     if (releaseSourceChecksum[0] != '\0')
246     {
247         if (std::strcmp(releaseSourceChecksum, "NoChecksumFile") == 0)
248         {
249             writer->writeLine(formatString(
250                     "The source code this program was compiled from has not been verified because "
251                     "the reference checksum was missing during compilation. This means you have an "
252                     "incomplete GROMACS distribution, please make sure to download an intact "
253                     "source distribution and compile that before proceeding."));
254             writer->writeLine(formatString("Computed checksum: %s", currentSourceChecksum));
255         }
256         else if (std::strcmp(releaseSourceChecksum, "NoPythonAvailable") == 0)
257         {
258             writer->writeLine(
259                     formatString("Build source could not be verified, because the checksum could "
260                                  "not be computed."));
261         }
262         else if (std::strcmp(releaseSourceChecksum, currentSourceChecksum) != 0)
263         {
264             writer->writeLine(formatString(
265                     "This program has been built from source code that has been altered and does "
266                     "not match the code released as part of the official GROMACS version %s. If "
267                     "you did not intend to use an altered GROMACS version, make sure to download "
268                     "an intact source distribution and compile that before proceeding.",
269                     gmx_version()));
270             writer->writeLine(formatString(
271                     "If you have modified the source code, you are strongly encouraged to set your "
272                     "custom version suffix (using -DGMX_VERSION_STRING_OF_FORK) which will can "
273                     "help later with scientific reproducibility but also when reporting bugs."));
274             writer->writeLine(formatString("Release checksum: %s", releaseSourceChecksum));
275             writer->writeLine(formatString("Computed checksum: %s", currentSourceChecksum));
276         }
277         else
278         {
279             writer->writeLine(formatString("Verified release checksum is %s", releaseSourceChecksum));
280         }
281     }
282
283
284 #if GMX_DOUBLE
285     writer->writeLine("Precision:          double");
286 #else
287     writer->writeLine("Precision:          mixed");
288 #endif
289     writer->writeLine(formatString("Memory model:       %u bit", static_cast<unsigned>(8 * sizeof(void*))));
290
291 #if GMX_THREAD_MPI
292     writer->writeLine("MPI library:        thread_mpi");
293 #elif GMX_MPI
294     writer->writeLine("MPI library:        MPI");
295 #else
296     writer->writeLine("MPI library:        none");
297 #endif
298 #if GMX_OPENMP
299     writer->writeLine(formatString("OpenMP support:     enabled (GMX_OPENMP_MAX_THREADS = %d)",
300                                    GMX_OPENMP_MAX_THREADS));
301 #else
302     writer->writeLine("OpenMP support:     disabled");
303 #endif
304     writer->writeLine(formatString("GPU support:        %s", getGpuImplementationString()));
305     writer->writeLine(formatString("SIMD instructions:  %s", GMX_SIMD_STRING));
306     writer->writeLine(formatString("FFT library:        %s", getFftDescriptionString()));
307 #if GMX_TARGET_X86
308     writer->writeLine(formatString("RDTSCP usage:       %s", GMX_USE_RDTSCP ? "enabled" : "disabled"));
309 #endif
310 #if GMX_USE_TNG
311     writer->writeLine("TNG support:        enabled");
312 #else
313     writer->writeLine("TNG support:        disabled");
314 #endif
315 #if GMX_USE_HWLOC
316     writer->writeLine(formatString("Hwloc support:      hwloc-%s", HWLOC_VERSION));
317 #else
318     writer->writeLine("Hwloc support:      disabled");
319 #endif
320 #if HAVE_EXTRAE
321     unsigned major, minor, revision;
322     Extrae_get_version(&major, &minor, &revision);
323     writer->writeLine(formatString("Tracing support:    enabled. Using Extrae-%d.%d.%d", major,
324                                    minor, revision));
325 #else
326     writer->writeLine("Tracing support:    disabled");
327 #endif
328
329
330     /* TODO: The below strings can be quite long, so it would be nice to wrap
331      * them. Can wait for later, as the master branch has ready code to do all
332      * that. */
333     writer->writeLine(formatString("C compiler:         %s", BUILD_C_COMPILER));
334     writer->writeLine(formatString("C compiler flags:   %s %s", BUILD_CFLAGS,
335                                    CMAKE_BUILD_CONFIGURATION_C_FLAGS));
336     writer->writeLine(formatString("C++ compiler:       %s", BUILD_CXX_COMPILER));
337     writer->writeLine(formatString("C++ compiler flags: %s %s", BUILD_CXXFLAGS,
338                                    CMAKE_BUILD_CONFIGURATION_CXX_FLAGS));
339 #ifdef HAVE_LIBMKL
340     /* MKL might be used for LAPACK/BLAS even if FFTs use FFTW, so keep it separate */
341     writer->writeLine(formatString("Linked with Intel MKL version %d.%d.%d.", __INTEL_MKL__,
342                                    __INTEL_MKL_MINOR__, __INTEL_MKL_UPDATE__));
343 #endif
344 #if GMX_GPU_OPENCL
345     writer->writeLine(formatString("OpenCL include dir: %s", OPENCL_INCLUDE_DIR));
346     writer->writeLine(formatString("OpenCL library:     %s", OPENCL_LIBRARY));
347     writer->writeLine(formatString("OpenCL version:     %s", OPENCL_VERSION_STRING));
348 #endif
349 #if GMX_GPU_CUDA
350     writer->writeLine(formatString("CUDA compiler:      %s", CUDA_COMPILER_INFO));
351     writer->writeLine(formatString("CUDA compiler flags:%s %s", CUDA_COMPILER_FLAGS,
352                                    CMAKE_BUILD_CONFIGURATION_CXX_FLAGS));
353     writer->writeLine("CUDA driver:        " + gmx::getCudaDriverVersionString());
354     writer->writeLine("CUDA runtime:       " + gmx::getCudaRuntimeVersionString());
355 #endif
356 }
357
358 //! \endcond
359
360 } // namespace
361
362 namespace gmx
363 {
364
365 BinaryInformationSettings::BinaryInformationSettings() :
366     bExtendedInfo_(false),
367     bCopyright_(false),
368     bProcessId_(false),
369     bGeneratedByHeader_(false),
370     prefix_(""),
371     suffix_("")
372 {
373 }
374
375 void printBinaryInformation(FILE* fp, const IProgramContext& programContext)
376 {
377     TextWriter writer(fp);
378     printBinaryInformation(&writer, programContext, BinaryInformationSettings());
379 }
380
381 void printBinaryInformation(FILE*                            fp,
382                             const IProgramContext&           programContext,
383                             const BinaryInformationSettings& settings)
384 {
385     try
386     {
387         TextWriter writer(fp);
388         printBinaryInformation(&writer, programContext, settings);
389     }
390     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
391 }
392
393 void printBinaryInformation(TextWriter*                      writer,
394                             const IProgramContext&           programContext,
395                             const BinaryInformationSettings& settings)
396 {
397     // TODO Perhaps the writer could be configured with the prefix and
398     // suffix strings from the settings?
399     const char* prefix          = settings.prefix_;
400     const char* suffix          = settings.suffix_;
401     const char* precisionString = "";
402 #if GMX_DOUBLE
403     precisionString = " (double precision)";
404 #endif
405     const char* const name = programContext.displayName();
406     if (settings.bGeneratedByHeader_)
407     {
408         writer->writeLine(formatString("%sCreated by:%s", prefix, suffix));
409     }
410     // TODO: It would be nice to know here whether we are really running a
411     // Gromacs binary or some other binary that is calling Gromacs; we
412     // could then print "%s is part of GROMACS" or some alternative text.
413     std::string title = formatString(":-) GROMACS - %s, %s%s (-:", name, gmx_version(), precisionString);
414     const int indent =
415             centeringOffset(78 - std::strlen(prefix) - std::strlen(suffix), title.length()) + 1;
416     writer->writeLine(formatString("%s%*c%s%s", prefix, indent, ' ', title.c_str(), suffix));
417     writer->writeLine(formatString("%s%s", prefix, suffix));
418     if (settings.bCopyright_)
419     {
420         GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
421                            "Prefix/suffix not supported with copyright");
422         printCopyright(writer);
423         writer->ensureEmptyLine();
424         // This line is printed again after the copyright notice to make it
425         // appear together with all the other information, so that it is not
426         // necessary to read stuff above the copyright notice.
427         // The line above the copyright notice puts the copyright notice is
428         // context, though.
429         writer->writeLine(formatString("%sGROMACS:      %s, version %s%s%s", prefix, name,
430                                        gmx_version(), precisionString, suffix));
431     }
432     const char* const binaryPath = programContext.fullBinaryPath();
433     if (!gmx::isNullOrEmpty(binaryPath))
434     {
435         writer->writeLine(formatString("%sExecutable:   %s%s", prefix, binaryPath, suffix));
436     }
437     const gmx::InstallationPrefixInfo installPrefix = programContext.installationPrefix();
438     if (!gmx::isNullOrEmpty(installPrefix.path))
439     {
440         writer->writeLine(formatString("%sData prefix:  %s%s%s", prefix, installPrefix.path,
441                                        installPrefix.bSourceLayout ? " (source tree)" : "", suffix));
442     }
443     const std::string workingDir = Path::getWorkingDirectory();
444     if (!workingDir.empty())
445     {
446         writer->writeLine(formatString("%sWorking dir:  %s%s", prefix, workingDir.c_str(), suffix));
447     }
448     if (settings.bProcessId_)
449     {
450         writer->writeLine(formatString("%sProcess ID:   %d%s", prefix, gmx_getpid(), suffix));
451     }
452     const char* const commandLine = programContext.commandLine();
453     if (!gmx::isNullOrEmpty(commandLine))
454     {
455         writer->writeLine(formatString("%sCommand line:%s\n%s  %s%s", prefix, suffix, prefix,
456                                        commandLine, suffix));
457     }
458     if (settings.bExtendedInfo_)
459     {
460         GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
461                            "Prefix/suffix not supported with extended info");
462         writer->ensureEmptyLine();
463         gmx_print_version_info(writer);
464     }
465 }
466
467 } // namespace gmx