Merge branch release-2016 into release-2018
[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, 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
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_HWLOC
63 #include <hwloc.h>
64 #endif
65
66 #include <cstdio>
67 #include <cstdlib>
68 #include <cstring>
69
70 #include <algorithm>
71 #include <string>
72
73 /* This file is completely threadsafe - keep it that way! */
74
75 #include "buildinfo.h"
76 #include "gromacs/utility/arraysize.h"
77 #include "gromacs/utility/baseversion.h"
78 #include "gromacs/utility/exceptions.h"
79 #include "gromacs/utility/gmxassert.h"
80 #include "gromacs/utility/path.h"
81 #include "gromacs/utility/programcontext.h"
82 #include "gromacs/utility/stringutil.h"
83 #include "gromacs/utility/textwriter.h"
84
85 #include "cuda_version_information.h"
86
87 namespace
88 {
89
90 using gmx::formatString;
91
92 //! \cond Doxygen does not need to care about most of this stuff, and the macro usage is painful to document
93
94 int centeringOffset(int width, int length)
95 {
96     return std::max(width - length, 0) / 2;
97 }
98
99 std::string formatCentered(int width, const char *text)
100 {
101     const int offset = centeringOffset(width, std::strlen(text));
102     return formatString("%*s%s", offset, "", text);
103 }
104
105 void printCopyright(gmx::TextWriter *writer)
106 {
107     static const char * const Contributors[] = {
108         "Emile Apol",
109         "Rossen Apostolov",
110         "Herman J.C. Berendsen",
111         "Par Bjelkmar",
112         "Aldert van Buuren",
113         "Rudi van Drunen",
114         "Anton Feenstra",
115         "Gerrit Groenhof",
116         "Christoph Junghans",
117         "Anca Hamuraru",
118         "Vincent Hindriksen",
119         "Dimitrios Karkoulis",
120         "Peter Kasson",
121         "Jiri Kraus",
122         "Carsten Kutzner",
123         "Per Larsson",
124         "Justin A. Lemkul",
125         "Viveca Lindahl",
126         "Magnus Lundborg",
127         "Pieter Meulenhoff",
128         "Erik Marklund",
129         "Teemu Murtola",
130         "Szilard Pall",
131         "Sander Pronk",
132         "Roland Schulz",
133         "Alexey Shvetsov",
134         "Michael Shirts",
135         "Alfons Sijbers",
136         "Peter Tieleman",
137         "Teemu Virolainen",
138         "Christian Wennberg",
139         "Maarten Wolf"
140     };
141     static const char * const CopyrightText[] = {
142         "Copyright (c) 1991-2000, University of Groningen, The Netherlands.",
143         "Copyright (c) 2001-2017, The GROMACS development team at",
144         "Uppsala University, Stockholm University and",
145         "the Royal Institute of Technology, Sweden.",
146         "check out http://www.gromacs.org for more information."
147     };
148     static const char * const LicenseText[] = {
149         "GROMACS is free software; you can redistribute it and/or modify it",
150         "under the terms of the GNU Lesser General Public License",
151         "as published by the Free Software Foundation; either version 2.1",
152         "of the License, or (at your option) any later version."
153     };
154
155 #define NCONTRIBUTORS (int)asize(Contributors)
156 #define NCR (int)asize(CopyrightText)
157
158 // FAH has an exception permission from LGPL to allow digital signatures in Gromacs.
159 #ifdef GMX_FAHCORE
160 #define NLICENSE 0
161 #else
162 #define NLICENSE (int)asize(LicenseText)
163 #endif
164
165     // TODO a centering behaviour of TextWriter could be useful here
166     writer->writeLine(formatCentered(78, "GROMACS is written by:"));
167     for (int i = 0; i < NCONTRIBUTORS; )
168     {
169         for (int j = 0; j < 4 && i < NCONTRIBUTORS; ++j, ++i)
170         {
171             const int width = 18;
172             char      buf[30];
173             const int offset = centeringOffset(width, strlen(Contributors[i]));
174             GMX_RELEASE_ASSERT(strlen(Contributors[i]) + offset < asize(buf),
175                                "Formatting buffer is not long enough");
176             std::fill(buf, buf+width, ' ');
177             std::strcpy(buf+offset, Contributors[i]);
178             writer->writeString(formatString(" %-*s", width, buf));
179         }
180         writer->ensureLineBreak();
181     }
182     writer->writeLine(formatCentered(78, "and the project leaders:"));
183     writer->writeLine(formatCentered(78, "Mark Abraham, Berk Hess, Erik Lindahl, and David van der Spoel"));
184     writer->ensureEmptyLine();
185     for (int i = 0; i < NCR; ++i)
186     {
187         writer->writeLine(CopyrightText[i]);
188     }
189     writer->ensureEmptyLine();
190     for (int i = 0; i < NLICENSE; ++i)
191     {
192         writer->writeLine(LicenseText[i]);
193     }
194 }
195
196 // Construct a string that describes the library that provides FFT support to this build
197 const char *getFftDescriptionString()
198 {
199 // Define the FFT description string
200 #if GMX_FFT_FFTW3
201 #  if GMX_NATIVE_WINDOWS
202     // Don't buy trouble
203     return "fftw3";
204 #  else
205     // Use the version string provided by libfftw3
206 #    if GMX_DOUBLE
207     return fftw_version;
208 #    else
209     return fftwf_version;
210 #    endif
211 #  endif
212 #endif
213 #if GMX_FFT_MKL
214     return "Intel MKL";
215 #endif
216 #if GMX_FFT_FFTPACK
217     return "fftpack (built-in)";
218 #endif
219 };
220
221 void gmx_print_version_info(gmx::TextWriter *writer)
222 {
223     writer->writeLine(formatString("GROMACS version:    %s", gmx_version()));
224     const char *const git_hash = gmx_version_git_full_hash();
225     if (git_hash[0] != '\0')
226     {
227         writer->writeLine(formatString("GIT SHA1 hash:      %s", git_hash));
228     }
229     const char *const base_hash = gmx_version_git_central_base_hash();
230     if (base_hash[0] != '\0')
231     {
232         writer->writeLine(formatString("Branched from:      %s", base_hash));
233     }
234
235 #if GMX_DOUBLE
236     writer->writeLine("Precision:          double");
237 #else
238     writer->writeLine("Precision:          single");
239 #endif
240     writer->writeLine(formatString("Memory model:       %u bit", (unsigned)(8*sizeof(void *))));
241
242 #if GMX_THREAD_MPI
243     writer->writeLine("MPI library:        thread_mpi");
244 #elif GMX_MPI
245     writer->writeLine("MPI library:        MPI");
246 #else
247     writer->writeLine("MPI library:        none");
248 #endif
249 #if GMX_OPENMP
250     writer->writeLine(formatString("OpenMP support:     enabled (GMX_OPENMP_MAX_THREADS = %d)", GMX_OPENMP_MAX_THREADS));
251 #else
252     writer->writeLine("OpenMP support:     disabled");
253 #endif
254     writer->writeLine(formatString("GPU support:        %s", getGpuImplementationString()));
255     writer->writeLine(formatString("SIMD instructions:  %s", GMX_SIMD_STRING));
256     writer->writeLine(formatString("FFT library:        %s", getFftDescriptionString()));
257     writer->writeLine(formatString("RDTSCP usage:       %s", HAVE_RDTSCP ? "enabled" : "disabled"));
258 #ifdef GMX_USE_TNG
259     writer->writeLine("TNG support:        enabled");
260 #else
261     writer->writeLine("TNG support:        disabled");
262 #endif
263 #if GMX_HWLOC
264     writer->writeLine(formatString("Hwloc support:      hwloc-%d.%d.%d",
265                                    HWLOC_API_VERSION>>16,
266                                    (HWLOC_API_VERSION>>8) & 0xFF,
267                                    HWLOC_API_VERSION & 0xFF));
268 #else
269     writer->writeLine("Hwloc support:      disabled");
270 #endif
271 #if HAVE_EXTRAE
272     unsigned major, minor, revision;
273     Extrae_get_version(&major, &minor, &revision);
274     writer->writeLine(formatString("Tracing support:    enabled. Using Extrae-%d.%d.%d", major, minor, revision));
275 #else
276     writer->writeLine("Tracing support:    disabled");
277 #endif
278
279
280     writer->writeLine(formatString("Built on:           %s", BUILD_TIME));
281     writer->writeLine(formatString("Built by:           %s", BUILD_USER));
282     writer->writeLine(formatString("Build OS/arch:      %s", BUILD_HOST));
283     writer->writeLine(formatString("Build CPU vendor:   %s", BUILD_CPU_VENDOR));
284     writer->writeLine(formatString("Build CPU brand:    %s", BUILD_CPU_BRAND));
285     writer->writeLine(formatString("Build CPU family:   %d   Model: %d   Stepping: %d",
286                                    BUILD_CPU_FAMILY, BUILD_CPU_MODEL, BUILD_CPU_STEPPING));
287     /* TODO: The below strings can be quite long, so it would be nice to wrap
288      * them. Can wait for later, as the master branch has ready code to do all
289      * that. */
290     writer->writeLine(formatString("Build CPU features: %s", BUILD_CPU_FEATURES));
291     writer->writeLine(formatString("C compiler:         %s", BUILD_C_COMPILER));
292     writer->writeLine(formatString("C compiler flags:   %s", BUILD_CFLAGS));
293     writer->writeLine(formatString("C++ compiler:       %s", BUILD_CXX_COMPILER));
294     writer->writeLine(formatString("C++ compiler flags: %s", BUILD_CXXFLAGS));
295 #ifdef HAVE_LIBMKL
296     /* MKL might be used for LAPACK/BLAS even if FFTs use FFTW, so keep it separate */
297     writer->writeLine(formatString("Linked with Intel MKL version %d.%d.%d.",
298                                    __INTEL_MKL__, __INTEL_MKL_MINOR__, __INTEL_MKL_UPDATE__));
299 #endif
300 #if GMX_GPU == GMX_GPU_OPENCL
301     writer->writeLine(formatString("OpenCL include dir: %s", OPENCL_INCLUDE_DIR));
302     writer->writeLine(formatString("OpenCL library:     %s", OPENCL_LIBRARY));
303     writer->writeLine(formatString("OpenCL version:     %s", OPENCL_VERSION_STRING));
304 #endif
305 #if GMX_GPU == GMX_GPU_CUDA
306     writer->writeLine(formatString("CUDA compiler:      %s\n", CUDA_COMPILER_INFO));
307     writer->writeLine(formatString("CUDA compiler flags:%s\n", CUDA_COMPILER_FLAGS));
308     auto driverVersion = gmx::getCudaDriverVersion();
309     writer->writeLine(formatString("CUDA driver:        %d.%d\n", driverVersion.first, driverVersion.second));
310     auto runtimeVersion = gmx::getCudaRuntimeVersion();
311     writer->writeLine(formatString("CUDA runtime:       %d.%d\n", runtimeVersion.first, runtimeVersion.second));
312 #endif
313 }
314
315 //! \endcond
316
317 } // namespace
318
319 namespace gmx
320 {
321
322 BinaryInformationSettings::BinaryInformationSettings()
323     : bExtendedInfo_(false), bCopyright_(false),
324       bGeneratedByHeader_(false), prefix_(""), suffix_("")
325 {
326 }
327
328 void printBinaryInformation(FILE                  *fp,
329                             const IProgramContext &programContext)
330 {
331     TextWriter writer(fp);
332     printBinaryInformation(&writer, programContext, BinaryInformationSettings());
333 }
334
335 void printBinaryInformation(FILE                            *fp,
336                             const IProgramContext           &programContext,
337                             const BinaryInformationSettings &settings)
338 {
339     try
340     {
341         TextWriter writer(fp);
342         printBinaryInformation(&writer, programContext, settings);
343     }
344     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
345 }
346
347 void printBinaryInformation(TextWriter                      *writer,
348                             const IProgramContext           &programContext,
349                             const BinaryInformationSettings &settings)
350 {
351     // TODO Perhaps the writer could be configured with the prefix and
352     // suffix strings from the settings?
353     const char *prefix          = settings.prefix_;
354     const char *suffix          = settings.suffix_;
355     const char *precisionString = "";
356 #if GMX_DOUBLE
357     precisionString = " (double precision)";
358 #endif
359     const char *const name = programContext.displayName();
360     if (settings.bGeneratedByHeader_)
361     {
362         writer->writeLine(formatString("%sCreated by:%s", prefix, suffix));
363     }
364     // TODO: It would be nice to know here whether we are really running a
365     // Gromacs binary or some other binary that is calling Gromacs; we
366     // could then print "%s is part of GROMACS" or some alternative text.
367     std::string title
368         = formatString(":-) GROMACS - %s, %s%s (-:", name, gmx_version(), precisionString);
369     const int   indent
370         = centeringOffset(78 - std::strlen(prefix) - std::strlen(suffix), title.length()) + 1;
371     writer->writeLine(formatString("%s%*c%s%s", prefix, indent, ' ', title.c_str(), suffix));
372     writer->writeLine(formatString("%s%s", prefix, suffix));
373     if (settings.bCopyright_)
374     {
375         GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
376                            "Prefix/suffix not supported with copyright");
377         printCopyright(writer);
378         writer->ensureEmptyLine();
379         // This line is printed again after the copyright notice to make it
380         // appear together with all the other information, so that it is not
381         // necessary to read stuff above the copyright notice.
382         // The line above the copyright notice puts the copyright notice is
383         // context, though.
384         writer->writeLine(formatString("%sGROMACS:      %s, version %s%s%s", prefix, name,
385                                        gmx_version(), precisionString, suffix));
386     }
387     const char *const binaryPath = programContext.fullBinaryPath();
388     if (!gmx::isNullOrEmpty(binaryPath))
389     {
390         writer->writeLine(formatString("%sExecutable:   %s%s", prefix, binaryPath, suffix));
391     }
392     const gmx::InstallationPrefixInfo installPrefix = programContext.installationPrefix();
393     if (!gmx::isNullOrEmpty(installPrefix.path))
394     {
395         writer->writeLine(formatString("%sData prefix:  %s%s%s", prefix, installPrefix.path,
396                                        installPrefix.bSourceLayout ? " (source tree)" : "", suffix));
397     }
398     const std::string workingDir = Path::getWorkingDirectory();
399     if (!workingDir.empty())
400     {
401         writer->writeLine(formatString("%sWorking dir:  %s%s", prefix, workingDir.c_str(), suffix));
402     }
403     const char *const commandLine = programContext.commandLine();
404     if (!gmx::isNullOrEmpty(commandLine))
405     {
406         writer->writeLine(formatString("%sCommand line:%s\n%s  %s%s",
407                                        prefix, suffix, prefix, commandLine, suffix));
408     }
409     if (settings.bExtendedInfo_)
410     {
411         GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
412                            "Prefix/suffix not supported with extended info");
413         writer->ensureEmptyLine();
414         gmx_print_version_info(writer);
415     }
416 }
417
418 } // namespace gmx