Merge branch release-2018
[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/gmxassert.h"
59 #include "gromacs/utility/gmxmpi.h"
60 #include "gromacs/utility/path.h"
61 #include "gromacs/utility/programcontext.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/snprintf.h"
64 #include "gromacs/utility/stringutil.h"
65 #include "gromacs/utility/sysinfo.h"
66
67 /* The source code in this file should be thread-safe.
68          Please keep it that way. */
69
70 // TODO move this to multi-sim module
71 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
72                      const char *name,
73                      gmx_bool bQuiet)
74 {
75     int     *ibuf, p;
76     gmx_bool bCompatible;
77
78     if (nullptr != log && !bQuiet)
79     {
80         fprintf(log, "Multi-checking %s ... ", name);
81     }
82
83     if (ms == nullptr)
84     {
85         gmx_fatal(FARGS,
86                   "check_multi_int called with a NULL communication pointer");
87     }
88
89     snew(ibuf, ms->nsim);
90     ibuf[ms->sim] = val;
91     gmx_sumi_sim(ms->nsim, ibuf, ms);
92
93     bCompatible = TRUE;
94     for (p = 1; p < ms->nsim; p++)
95     {
96         bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
97     }
98
99     if (bCompatible)
100     {
101         if (nullptr != log && !bQuiet)
102         {
103             fprintf(log, "OK\n");
104         }
105     }
106     else
107     {
108         if (nullptr != log)
109         {
110             fprintf(log, "\n%s is not equal for all subsystems\n", name);
111             for (p = 0; p < ms->nsim; p++)
112             {
113                 fprintf(log, "  subsystem %d: %d\n", p, ibuf[p]);
114             }
115         }
116         gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
117     }
118
119     sfree(ibuf);
120 }
121
122 // TODO move this to multi-sim module
123 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
124                        gmx_int64_t val, const char *name,
125                        gmx_bool bQuiet)
126 {
127     gmx_int64_t      *ibuf;
128     int               p;
129     gmx_bool          bCompatible;
130
131     if (nullptr != log && !bQuiet)
132     {
133         fprintf(log, "Multi-checking %s ... ", name);
134     }
135
136     if (ms == nullptr)
137     {
138         gmx_fatal(FARGS,
139                   "check_multi_int called with a NULL communication pointer");
140     }
141
142     snew(ibuf, ms->nsim);
143     ibuf[ms->sim] = val;
144     gmx_sumli_sim(ms->nsim, ibuf, ms);
145
146     bCompatible = TRUE;
147     for (p = 1; p < ms->nsim; p++)
148     {
149         bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
150     }
151
152     if (bCompatible)
153     {
154         if (nullptr != log && !bQuiet)
155         {
156             fprintf(log, "OK\n");
157         }
158     }
159     else
160     {
161         // TODO Part of this error message would also be good to go to
162         // stderr (from one rank of one sim only)
163         if (nullptr != log)
164         {
165             fprintf(log, "\n%s is not equal for all subsystems\n", name);
166             for (p = 0; p < ms->nsim; p++)
167             {
168                 char strbuf[255];
169                 /* first make the format string */
170                 snprintf(strbuf, 255, "  subsystem %%d: %s\n",
171                          "%" GMX_PRId64);
172                 fprintf(log, strbuf, p, ibuf[p]);
173             }
174         }
175         gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
176     }
177
178     sfree(ibuf);
179 }
180
181
182 void gmx_log_open(const char *lognm, const t_commrec *cr,
183                   gmx_bool bAppendFiles, FILE** fplog)
184 {
185     int    pid;
186     char   host[256];
187     char   timebuf[STRLEN];
188     FILE  *fp = *fplog;
189
190     if (!bAppendFiles)
191     {
192         fp = gmx_fio_fopen(lognm, bAppendFiles ? "a+" : "w+" );
193     }
194
195     gmx_fatal_set_log_file(fp);
196
197     /* Get some machine parameters */
198     gmx_gethostname(host, 256);
199     pid = gmx_getpid();
200     gmx_format_current_time(timebuf, STRLEN);
201
202     if (bAppendFiles)
203     {
204         fprintf(fp,
205                 "\n"
206                 "\n"
207                 "-----------------------------------------------------------\n"
208                 "Restarting from checkpoint, appending to previous log file.\n"
209                 "\n"
210                 );
211     }
212
213     fprintf(fp,
214             "Log file opened on %s"
215             "Host: %s  pid: %d  rank ID: %d  number of ranks:  %d\n",
216             timebuf, host, pid, cr->nodeid, cr->nnodes);
217     try
218     {
219         gmx::BinaryInformationSettings settings;
220         settings.extendedInfo(true);
221         settings.copyright(!bAppendFiles);
222         gmx::printBinaryInformation(fp, gmx::getProgramContext(), settings);
223     }
224     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
225     fprintf(fp, "\n");
226
227     fflush(fp);
228
229     *fplog = fp;
230 }
231
232 void gmx_log_close(FILE *fp)
233 {
234     if (fp)
235     {
236         gmx_fatal_set_log_file(nullptr);
237         gmx_fio_fclose(fp);
238     }
239 }
240
241 // TODO move this to multi-sim module
242 gmx_multisim_t *init_multisystem(MPI_Comm                         comm,
243                                  gmx::ArrayRef<const std::string> multidirs)
244 {
245     gmx_multisim_t *ms;
246 #if GMX_MPI
247     MPI_Group       mpi_group_world;
248     int            *rank;
249 #endif
250
251     if (multidirs.empty())
252     {
253         return nullptr;
254     }
255
256     if (!GMX_LIB_MPI && multidirs.size() >= 1)
257     {
258         gmx_fatal(FARGS, "mdrun -multidir is only supported when GROMACS has been "
259                   "configured with a proper external MPI library.");
260     }
261
262     if (multidirs.size() == 1)
263     {
264         /* NOTE: It would be nice if this special case worked, but this requires checks/tests. */
265         gmx_fatal(FARGS, "To run mdrun in multiple simulation mode, more then one "
266                   "actual simulation is required. The single simulation case is not supported.");
267     }
268
269 #if GMX_MPI
270     int numRanks;
271     MPI_Comm_size(comm, &numRanks);
272     if (numRanks % multidirs.size() != 0)
273     {
274         gmx_fatal(FARGS, "The number of ranks (%d) is not a multiple of the number of simulations (%zu)", numRanks, multidirs.size());
275     }
276
277     int numRanksPerSim = numRanks/multidirs.size();
278     int rankWithinComm;
279     MPI_Comm_rank(comm, &rankWithinComm);
280
281     if (debug)
282     {
283         fprintf(debug, "We have %zu simulations, %d ranks per simulation, local simulation is %d\n", multidirs.size(), numRanksPerSim, rankWithinComm/numRanksPerSim);
284     }
285
286     ms       = new gmx_multisim_t;
287     ms->nsim = multidirs.size();
288     ms->sim  = rankWithinComm/numRanksPerSim;
289     /* Create a communicator for the master nodes */
290     snew(rank, ms->nsim);
291     for (int i = 0; i < ms->nsim; i++)
292     {
293         rank[i] = i*numRanksPerSim;
294     }
295     MPI_Comm_group(comm, &mpi_group_world);
296     MPI_Group_incl(mpi_group_world, ms->nsim, rank, &ms->mpi_group_masters);
297     sfree(rank);
298     MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
299                     &ms->mpi_comm_masters);
300
301 #if !MPI_IN_PLACE_EXISTS
302     /* initialize the MPI_IN_PLACE replacement buffers */
303     snew(ms->mpb, 1);
304     ms->mpb->ibuf        = NULL;
305     ms->mpb->libuf       = NULL;
306     ms->mpb->fbuf        = NULL;
307     ms->mpb->dbuf        = NULL;
308     ms->mpb->ibuf_alloc  = 0;
309     ms->mpb->libuf_alloc = 0;
310     ms->mpb->fbuf_alloc  = 0;
311     ms->mpb->dbuf_alloc  = 0;
312 #endif
313
314     // TODO This should throw upon error
315     gmx_chdir(multidirs[ms->sim].c_str());
316 #else
317     GMX_UNUSED_VALUE(comm);
318     ms = nullptr;
319 #endif
320
321     return ms;
322 }
323
324 void done_multisim(gmx_multisim_t *ms)
325 {
326     if (nullptr != ms)
327     {
328         done_mpi_in_place_buf(ms->mpb);
329         delete ms;
330     }
331 }