More gcc-8 fixes for POWER
[alexxy/gromacs.git] / src / gromacs / mdlib / main.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, 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 #include "gmxpre.h"
38
39 #include "main.h"
40
41 #include "config.h"
42
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46
47 #include <string>
48
49 #include "gromacs/commandline/filenm.h"
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/gmxlib/network.h"
52 #include "gromacs/mdtypes/commrec.h"
53 #include "gromacs/utility/binaryinformation.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/gmxmpi.h"
59 #include "gromacs/utility/path.h"
60 #include "gromacs/utility/programcontext.h"
61 #include "gromacs/utility/smalloc.h"
62 #include "gromacs/utility/snprintf.h"
63 #include "gromacs/utility/stringutil.h"
64 #include "gromacs/utility/sysinfo.h"
65
66 /* The source code in this file should be thread-safe.
67          Please keep it that way. */
68
69 // TODO move this to multi-sim module
70 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
71                      const char *name,
72                      gmx_bool bQuiet)
73 {
74     int     *ibuf, p;
75     gmx_bool bCompatible;
76
77     if (nullptr != log && !bQuiet)
78     {
79         fprintf(log, "Multi-checking %s ... ", name);
80     }
81
82     if (ms == nullptr)
83     {
84         gmx_fatal(FARGS,
85                   "check_multi_int called with a NULL communication pointer");
86     }
87
88     snew(ibuf, ms->nsim);
89     ibuf[ms->sim] = val;
90     gmx_sumi_sim(ms->nsim, ibuf, ms);
91
92     bCompatible = TRUE;
93     for (p = 1; p < ms->nsim; p++)
94     {
95         bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
96     }
97
98     if (bCompatible)
99     {
100         if (nullptr != log && !bQuiet)
101         {
102             fprintf(log, "OK\n");
103         }
104     }
105     else
106     {
107         if (nullptr != log)
108         {
109             fprintf(log, "\n%s is not equal for all subsystems\n", name);
110             for (p = 0; p < ms->nsim; p++)
111             {
112                 fprintf(log, "  subsystem %d: %d\n", p, ibuf[p]);
113             }
114         }
115         gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
116     }
117
118     sfree(ibuf);
119 }
120
121 // TODO move this to multi-sim module
122 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
123                        gmx_int64_t val, const char *name,
124                        gmx_bool bQuiet)
125 {
126     gmx_int64_t      *ibuf;
127     int               p;
128     gmx_bool          bCompatible;
129
130     if (nullptr != log && !bQuiet)
131     {
132         fprintf(log, "Multi-checking %s ... ", name);
133     }
134
135     if (ms == nullptr)
136     {
137         gmx_fatal(FARGS,
138                   "check_multi_int called with a NULL communication pointer");
139     }
140
141     snew(ibuf, ms->nsim);
142     ibuf[ms->sim] = val;
143     gmx_sumli_sim(ms->nsim, ibuf, ms);
144
145     bCompatible = TRUE;
146     for (p = 1; p < ms->nsim; p++)
147     {
148         bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
149     }
150
151     if (bCompatible)
152     {
153         if (nullptr != log && !bQuiet)
154         {
155             fprintf(log, "OK\n");
156         }
157     }
158     else
159     {
160         // TODO Part of this error message would also be good to go to
161         // stderr (from one rank of one sim only)
162         if (nullptr != log)
163         {
164             fprintf(log, "\n%s is not equal for all subsystems\n", name);
165             for (p = 0; p < ms->nsim; p++)
166             {
167                 char strbuf[255];
168                 /* first make the format string */
169                 snprintf(strbuf, 255, "  subsystem %%d: %s\n",
170                          "%" GMX_PRId64);
171                 fprintf(log, strbuf, p, ibuf[p]);
172             }
173         }
174         gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
175     }
176
177     sfree(ibuf);
178 }
179
180
181 void gmx_log_open(const char *lognm, const t_commrec *cr,
182                   gmx_bool bAppendFiles, FILE** fplog)
183 {
184     int    pid;
185     char   host[256];
186     char   timebuf[STRLEN];
187     FILE  *fp = *fplog;
188
189     if (!bAppendFiles)
190     {
191         fp = gmx_fio_fopen(lognm, bAppendFiles ? "a+" : "w+" );
192     }
193
194     gmx_fatal_set_log_file(fp);
195
196     /* Get some machine parameters */
197     gmx_gethostname(host, 256);
198     pid = gmx_getpid();
199     gmx_format_current_time(timebuf, STRLEN);
200
201     if (bAppendFiles)
202     {
203         fprintf(fp,
204                 "\n"
205                 "\n"
206                 "-----------------------------------------------------------\n"
207                 "Restarting from checkpoint, appending to previous log file.\n"
208                 "\n"
209                 );
210     }
211
212     fprintf(fp,
213             "Log file opened on %s"
214             "Host: %s  pid: %d  rank ID: %d  number of ranks:  %d\n",
215             timebuf, host, pid, cr->nodeid, cr->nnodes);
216     try
217     {
218         gmx::BinaryInformationSettings settings;
219         settings.extendedInfo(true);
220         settings.copyright(!bAppendFiles);
221         gmx::printBinaryInformation(fp, gmx::getProgramContext(), settings);
222     }
223     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
224     fprintf(fp, "\n");
225
226     fflush(fp);
227
228     *fplog = fp;
229 }
230
231 void gmx_log_close(FILE *fp)
232 {
233     if (fp)
234     {
235         gmx_fatal_set_log_file(nullptr);
236         gmx_fio_fclose(fp);
237     }
238 }
239
240 // TODO move this to multi-sim module
241 void init_multisystem(t_commrec *cr, int nsim, char **multidirs,
242                       int nfile, const t_filenm fnm[])
243 {
244     gmx_multisim_t *ms;
245     int             nnodes, nnodpersim, sim, i;
246 #if GMX_MPI
247     MPI_Group       mpi_group_world;
248     int            *rank;
249 #endif
250
251 #if !GMX_MPI
252     if (nsim > 1)
253     {
254         gmx_fatal(FARGS, "This binary is compiled without MPI support, can not do multiple simulations.");
255     }
256 #endif
257
258     if (nsim == 1)
259     {
260         /* NOTE: It would be nice if this special case worked, but this requires checks/tests. */
261         gmx_fatal(FARGS, "To run mdrun in multiple simulation mode, more then one actual simulation is required. The single simulation case is not supported.");
262     }
263
264     nnodes  = cr->nnodes;
265     if (nnodes % nsim != 0)
266     {
267         gmx_fatal(FARGS, "The number of ranks (%d) is not a multiple of the number of simulations (%d)", nnodes, nsim);
268     }
269
270     nnodpersim = nnodes/nsim;
271     sim        = cr->nodeid/nnodpersim;
272
273     if (debug)
274     {
275         fprintf(debug, "We have %d simulations, %d ranks per simulation, local simulation is %d\n", nsim, nnodpersim, sim);
276     }
277
278     snew(ms, 1);
279     cr->ms   = ms;
280     ms->nsim = nsim;
281     ms->sim  = sim;
282 #if GMX_MPI
283     /* Create a communicator for the master nodes */
284     snew(rank, ms->nsim);
285     for (i = 0; i < ms->nsim; i++)
286     {
287         rank[i] = i*nnodpersim;
288     }
289     MPI_Comm_group(MPI_COMM_WORLD, &mpi_group_world);
290     MPI_Group_incl(mpi_group_world, nsim, rank, &ms->mpi_group_masters);
291     sfree(rank);
292     MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
293                     &ms->mpi_comm_masters);
294
295 #if !MPI_IN_PLACE_EXISTS
296     /* initialize the MPI_IN_PLACE replacement buffers */
297     snew(ms->mpb, 1);
298     ms->mpb->ibuf        = NULL;
299     ms->mpb->libuf       = NULL;
300     ms->mpb->fbuf        = NULL;
301     ms->mpb->dbuf        = NULL;
302     ms->mpb->ibuf_alloc  = 0;
303     ms->mpb->libuf_alloc = 0;
304     ms->mpb->fbuf_alloc  = 0;
305     ms->mpb->dbuf_alloc  = 0;
306 #endif
307
308 #endif
309
310     /* Reduce the intra-simulation communication */
311     cr->sim_nodeid = cr->nodeid % nnodpersim;
312     cr->nnodes     = nnodpersim;
313 #if GMX_MPI
314     MPI_Comm_split(MPI_COMM_WORLD, sim, cr->sim_nodeid, &cr->mpi_comm_mysim);
315     cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
316     cr->nodeid           = cr->sim_nodeid;
317 #endif
318
319     if (debug)
320     {
321         fprintf(debug, "This is simulation %d", cr->ms->sim);
322         if (PAR(cr))
323         {
324             fprintf(debug, ", local number of ranks %d, local rank ID %d",
325                     cr->nnodes, cr->sim_nodeid);
326         }
327         fprintf(debug, "\n\n");
328     }
329
330     if (multidirs)
331     {
332         if (debug)
333         {
334             fprintf(debug, "Changing to directory %s\n", multidirs[cr->ms->sim]);
335         }
336         gmx_chdir(multidirs[cr->ms->sim]);
337     }
338     else
339     {
340         try
341         {
342             std::string rankString = gmx::formatString("%d", cr->ms->sim);
343             /* Patch output and tpx, cpt and rerun input file names */
344             for (i = 0; (i < nfile); i++)
345             {
346                 /* Because of possible multiple extensions per type we must look
347                  * at the actual file name for rerun. */
348                 if (is_output(&fnm[i]) ||
349                     fnm[i].ftp == efTPR || fnm[i].ftp == efCPT ||
350                     strcmp(fnm[i].opt, "-rerun") == 0)
351                 {
352                     std::string newFileName = gmx::Path::concatenateBeforeExtension(fnm[i].fns[0], rankString);
353                     sfree(fnm[i].fns[0]);
354                     fnm[i].fns[0] = gmx_strdup(newFileName.c_str());
355                 }
356             }
357         }
358         catch (gmx::GromacsException &e)
359         {
360             e.prependContext(gmx::formatString("Failed to modify mdrun -multi filename to add per-simulation suffix. You could perhaps reorganize your files and try mdrun -multidir.\n"));
361             throw;
362         }
363     }
364 }