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