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