Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / 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, 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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <limits.h>
45 #include <time.h>
46
47 #ifdef HAVE_SYS_TIME_H
48 #include <sys/time.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53 #ifdef GMX_NATIVE_WINDOWS
54 #include <process.h>
55 #endif
56
57 #include "types/commrec.h"
58 #include "network.h"
59 #include "main.h"
60 #include "macros.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/fileio/filenm.h"
63 #include "gromacs/fileio/gmxfio.h"
64 #include "copyrite.h"
65
66 #include "gromacs/utility/basenetwork.h"
67 #include "gromacs/utility/cstringutil.h"
68 #include "gromacs/utility/exceptions.h"
69 #include "gromacs/utility/fatalerror.h"
70 #include "gromacs/utility/gmxmpi.h"
71 #include "gromacs/utility/programcontext.h"
72 #include "gromacs/utility/smalloc.h"
73
74 /* The source code in this file should be thread-safe.
75          Please keep it that way. */
76
77 #define BUFSIZE 1024
78
79 static void par_fn(char *base, int ftp, const t_commrec *cr,
80                    gmx_bool bAppendSimId, gmx_bool bAppendNodeId,
81                    char buf[], int bufsize)
82 {
83     if ((size_t)bufsize < (strlen(base)+10))
84     {
85         gmx_mem("Character buffer too small!");
86     }
87
88     /* Copy to buf, and strip extension */
89     strcpy(buf, base);
90     buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
91
92     if (bAppendSimId)
93     {
94         sprintf(buf+strlen(buf), "%d", cr->ms->sim);
95     }
96     if (bAppendNodeId)
97     {
98         strcat(buf, "_rank");
99         sprintf(buf+strlen(buf), "%d", cr->nodeid);
100     }
101     strcat(buf, ".");
102
103     /* Add extension again */
104     strcat(buf, (ftp == efTPX) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
105     if (debug)
106     {
107         fprintf(debug, "rank %d par_fn '%s'\n", cr->nodeid, buf);
108         if (fn2ftp(buf) == efLOG)
109         {
110             fprintf(debug, "log\n");
111         }
112     }
113 }
114
115 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
116                      const char *name,
117                      gmx_bool bQuiet)
118 {
119     int     *ibuf, p;
120     gmx_bool bCompatible;
121
122     if (NULL != log && !bQuiet)
123     {
124         fprintf(log, "Multi-checking %s ... ", name);
125     }
126
127     if (ms == NULL)
128     {
129         gmx_fatal(FARGS,
130                   "check_multi_int called with a NULL communication pointer");
131     }
132
133     snew(ibuf, ms->nsim);
134     ibuf[ms->sim] = val;
135     gmx_sumi_sim(ms->nsim, ibuf, ms);
136
137     bCompatible = TRUE;
138     for (p = 1; p < ms->nsim; p++)
139     {
140         bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
141     }
142
143     if (bCompatible)
144     {
145         if (NULL != log && !bQuiet)
146         {
147             fprintf(log, "OK\n");
148         }
149     }
150     else
151     {
152         if (NULL != log)
153         {
154             fprintf(log, "\n%s is not equal for all subsystems\n", name);
155             for (p = 0; p < ms->nsim; p++)
156             {
157                 fprintf(log, "  subsystem %d: %d\n", p, ibuf[p]);
158             }
159         }
160         gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
161     }
162
163     sfree(ibuf);
164 }
165
166 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
167                        gmx_int64_t val, const char *name,
168                        gmx_bool bQuiet)
169 {
170     gmx_int64_t      *ibuf;
171     int               p;
172     gmx_bool          bCompatible;
173
174     if (NULL != log && !bQuiet)
175     {
176         fprintf(log, "Multi-checking %s ... ", name);
177     }
178
179     if (ms == NULL)
180     {
181         gmx_fatal(FARGS,
182                   "check_multi_int called with a NULL communication pointer");
183     }
184
185     snew(ibuf, ms->nsim);
186     ibuf[ms->sim] = val;
187     gmx_sumli_sim(ms->nsim, ibuf, ms);
188
189     bCompatible = TRUE;
190     for (p = 1; p < ms->nsim; p++)
191     {
192         bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
193     }
194
195     if (bCompatible)
196     {
197         if (NULL != log && !bQuiet)
198         {
199             fprintf(log, "OK\n");
200         }
201     }
202     else
203     {
204         if (NULL != log)
205         {
206             fprintf(log, "\n%s is not equal for all subsystems\n", name);
207             for (p = 0; p < ms->nsim; p++)
208             {
209                 char strbuf[255];
210                 /* first make the format string */
211                 snprintf(strbuf, 255, "  subsystem %%d: %s\n",
212                          "%" GMX_PRId64);
213                 fprintf(log, strbuf, p, ibuf[p]);
214             }
215         }
216         gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
217     }
218
219     sfree(ibuf);
220 }
221
222
223 void gmx_log_open(const char *lognm, const t_commrec *cr, gmx_bool bMasterOnly,
224                   gmx_bool bAppendFiles, FILE** fplog)
225 {
226     int    len, pid;
227     char   buf[256], host[256];
228     time_t t;
229     char   timebuf[STRLEN];
230     FILE  *fp = *fplog;
231     char  *tmpnm;
232
233     debug_gmx();
234
235     /* Communicate the filename for logfile */
236     if (cr->nnodes > 1 && !bMasterOnly
237 #ifdef GMX_THREAD_MPI
238         /* With thread MPI the non-master log files are opened later
239          * when the files names are already known on all nodes.
240          */
241         && FALSE
242 #endif
243         )
244     {
245         if (MASTER(cr))
246         {
247             len = strlen(lognm) + 1;
248         }
249         gmx_bcast(sizeof(len), &len, cr);
250         if (!MASTER(cr))
251         {
252             snew(tmpnm, len+8);
253         }
254         else
255         {
256             tmpnm = gmx_strdup(lognm);
257         }
258         gmx_bcast(len*sizeof(*tmpnm), tmpnm, cr);
259     }
260     else
261     {
262         tmpnm = gmx_strdup(lognm);
263     }
264
265     debug_gmx();
266
267     if (!bMasterOnly && !MASTER(cr))
268     {
269         /* Since log always ends with '.log' let's use this info */
270         par_fn(tmpnm, efLOG, cr, FALSE, !bMasterOnly, buf, 255);
271         fp = gmx_fio_fopen(buf, bAppendFiles ? "a+" : "w+" );
272     }
273     else if (!bAppendFiles)
274     {
275         fp = gmx_fio_fopen(tmpnm, bAppendFiles ? "a+" : "w+" );
276     }
277
278     sfree(tmpnm);
279
280     gmx_fatal_set_log_file(fp);
281
282     /* Get some machine parameters */
283     gmx_gethostname(host, 256);
284
285     time(&t);
286
287 #ifndef NO_GETPID
288 #   ifdef GMX_NATIVE_WINDOWS
289     pid = _getpid();
290 #   else
291     pid = getpid();
292 #   endif
293 #else
294     pid = 0;
295 #endif
296
297     if (bAppendFiles)
298     {
299         fprintf(fp,
300                 "\n"
301                 "\n"
302                 "-----------------------------------------------------------\n"
303                 "Restarting from checkpoint, appending to previous log file.\n"
304                 "\n"
305                 );
306     }
307
308     gmx_ctime_r(&t, timebuf, STRLEN);
309
310     fprintf(fp,
311             "Log file opened on %s"
312             "Host: %s  pid: %d  rank ID: %d  number of ranks:  %d\n",
313             timebuf, host, pid, cr->nodeid, cr->nnodes);
314     try
315     {
316         gmx::BinaryInformationSettings settings;
317         settings.extendedInfo(true);
318         settings.copyright(!bAppendFiles);
319         gmx::printBinaryInformation(fp, gmx::getProgramContext(), settings);
320     }
321     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
322     fprintf(fp, "\n\n");
323
324     fflush(fp);
325     debug_gmx();
326
327     *fplog = fp;
328 }
329
330 void gmx_log_close(FILE *fp)
331 {
332     if (fp)
333     {
334         gmx_fatal_set_log_file(NULL);
335         gmx_fio_fclose(fp);
336     }
337 }
338
339 void init_multisystem(t_commrec *cr, int nsim, char **multidirs,
340                       int nfile, const t_filenm fnm[], gmx_bool bParFn)
341 {
342     gmx_multisim_t *ms;
343     int             nnodes, nnodpersim, sim, i, ftp;
344     char            buf[256];
345 #ifdef GMX_MPI
346     MPI_Group       mpi_group_world;
347     int            *rank;
348 #endif
349
350 #ifndef GMX_MPI
351     if (nsim > 1)
352     {
353         gmx_fatal(FARGS, "This binary is compiled without MPI support, can not do multiple simulations.");
354     }
355 #endif
356
357     nnodes  = cr->nnodes;
358     if (nnodes % nsim != 0)
359     {
360         gmx_fatal(FARGS, "The number of ranks (%d) is not a multiple of the number of simulations (%d)", nnodes, nsim);
361     }
362
363     nnodpersim = nnodes/nsim;
364     sim        = cr->nodeid/nnodpersim;
365
366     if (debug)
367     {
368         fprintf(debug, "We have %d simulations, %d ranks per simulation, local simulation is %d\n", nsim, nnodpersim, sim);
369     }
370
371     snew(ms, 1);
372     cr->ms   = ms;
373     ms->nsim = nsim;
374     ms->sim  = sim;
375 #ifdef GMX_MPI
376     /* Create a communicator for the master nodes */
377     snew(rank, ms->nsim);
378     for (i = 0; i < ms->nsim; i++)
379     {
380         rank[i] = i*nnodpersim;
381     }
382     MPI_Comm_group(MPI_COMM_WORLD, &mpi_group_world);
383     MPI_Group_incl(mpi_group_world, nsim, rank, &ms->mpi_group_masters);
384     sfree(rank);
385     MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
386                     &ms->mpi_comm_masters);
387
388 #if !defined(MPI_IN_PLACE_EXISTS)
389     /* initialize the MPI_IN_PLACE replacement buffers */
390     snew(ms->mpb, 1);
391     ms->mpb->ibuf        = NULL;
392     ms->mpb->libuf       = NULL;
393     ms->mpb->fbuf        = NULL;
394     ms->mpb->dbuf        = NULL;
395     ms->mpb->ibuf_alloc  = 0;
396     ms->mpb->libuf_alloc = 0;
397     ms->mpb->fbuf_alloc  = 0;
398     ms->mpb->dbuf_alloc  = 0;
399 #endif
400
401 #endif
402
403     /* Reduce the intra-simulation communication */
404     cr->sim_nodeid = cr->nodeid % nnodpersim;
405     cr->nnodes     = nnodpersim;
406 #ifdef GMX_MPI
407     MPI_Comm_split(MPI_COMM_WORLD, sim, cr->sim_nodeid, &cr->mpi_comm_mysim);
408     cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
409     cr->nodeid           = cr->sim_nodeid;
410 #endif
411
412     if (debug)
413     {
414         fprintf(debug, "This is simulation %d", cr->ms->sim);
415         if (PAR(cr))
416         {
417             fprintf(debug, ", local number of ranks %d, local rank ID %d",
418                     cr->nnodes, cr->sim_nodeid);
419         }
420         fprintf(debug, "\n\n");
421     }
422
423     if (multidirs)
424     {
425         if (debug)
426         {
427             fprintf(debug, "Changing to directory %s\n", multidirs[cr->ms->sim]);
428         }
429         gmx_chdir(multidirs[cr->ms->sim]);
430     }
431     else if (bParFn)
432     {
433         /* Patch output and tpx, cpt and rerun input file names */
434         for (i = 0; (i < nfile); i++)
435         {
436             /* Because of possible multiple extensions per type we must look
437              * at the actual file name
438              */
439             if (is_output(&fnm[i]) ||
440                 fnm[i].ftp == efTPX || fnm[i].ftp == efCPT ||
441                 strcmp(fnm[i].opt, "-rerun") == 0)
442             {
443                 ftp = fn2ftp(fnm[i].fns[0]);
444                 par_fn(fnm[i].fns[0], ftp, cr, TRUE, FALSE, buf, 255);
445                 sfree(fnm[i].fns[0]);
446                 fnm[i].fns[0] = gmx_strdup(buf);
447             }
448         }
449     }
450 }