2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
39 #include "gromacs/legacyheaders/main.h"
49 #ifdef GMX_NATIVE_WINDOWS
52 #ifdef HAVE_SYS_TIME_H
59 #include "gromacs/fileio/filenm.h"
60 #include "gromacs/fileio/gmxfio.h"
61 #include "gromacs/legacyheaders/copyrite.h"
62 #include "gromacs/legacyheaders/macros.h"
63 #include "gromacs/legacyheaders/network.h"
64 #include "gromacs/legacyheaders/types/commrec.h"
65 #include "gromacs/utility/basenetwork.h"
66 #include "gromacs/utility/cstringutil.h"
67 #include "gromacs/utility/exceptions.h"
68 #include "gromacs/utility/fatalerror.h"
69 #include "gromacs/utility/futil.h"
70 #include "gromacs/utility/gmxmpi.h"
71 #include "gromacs/utility/programcontext.h"
72 #include "gromacs/utility/smalloc.h"
74 /* The source code in this file should be thread-safe.
75 Please keep it that way. */
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)
83 if ((size_t)bufsize < (strlen(base)+10))
85 gmx_mem("Character buffer too small!");
88 /* Copy to buf, and strip extension */
90 buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
94 sprintf(buf+strlen(buf), "%d", cr->ms->sim);
99 sprintf(buf+strlen(buf), "%d", cr->nodeid);
103 /* Add extension again */
104 strcat(buf, (ftp == efTPR) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
107 fprintf(debug, "rank %d par_fn '%s'\n", cr->nodeid, buf);
108 if (fn2ftp(buf) == efLOG)
110 fprintf(debug, "log\n");
115 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
120 gmx_bool bCompatible;
122 if (NULL != log && !bQuiet)
124 fprintf(log, "Multi-checking %s ... ", name);
130 "check_multi_int called with a NULL communication pointer");
133 snew(ibuf, ms->nsim);
135 gmx_sumi_sim(ms->nsim, ibuf, ms);
138 for (p = 1; p < ms->nsim; p++)
140 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
145 if (NULL != log && !bQuiet)
147 fprintf(log, "OK\n");
154 fprintf(log, "\n%s is not equal for all subsystems\n", name);
155 for (p = 0; p < ms->nsim; p++)
157 fprintf(log, " subsystem %d: %d\n", p, ibuf[p]);
160 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
166 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
167 gmx_int64_t val, const char *name,
172 gmx_bool bCompatible;
174 if (NULL != log && !bQuiet)
176 fprintf(log, "Multi-checking %s ... ", name);
182 "check_multi_int called with a NULL communication pointer");
185 snew(ibuf, ms->nsim);
187 gmx_sumli_sim(ms->nsim, ibuf, ms);
190 for (p = 1; p < ms->nsim; p++)
192 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
197 if (NULL != log && !bQuiet)
199 fprintf(log, "OK\n");
206 fprintf(log, "\n%s is not equal for all subsystems\n", name);
207 for (p = 0; p < ms->nsim; p++)
210 /* first make the format string */
211 snprintf(strbuf, 255, " subsystem %%d: %s\n",
213 fprintf(log, strbuf, p, ibuf[p]);
216 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
223 void gmx_log_open(const char *lognm, const t_commrec *cr,
224 gmx_bool bAppendFiles, FILE** fplog)
229 char timebuf[STRLEN];
236 fp = gmx_fio_fopen(lognm, bAppendFiles ? "a+" : "w+" );
239 gmx_fatal_set_log_file(fp);
241 /* Get some machine parameters */
242 gmx_gethostname(host, 256);
247 # ifdef GMX_NATIVE_WINDOWS
261 "-----------------------------------------------------------\n"
262 "Restarting from checkpoint, appending to previous log file.\n"
267 gmx_ctime_r(&t, timebuf, STRLEN);
270 "Log file opened on %s"
271 "Host: %s pid: %d rank ID: %d number of ranks: %d\n",
272 timebuf, host, pid, cr->nodeid, cr->nnodes);
275 gmx::BinaryInformationSettings settings;
276 settings.extendedInfo(true);
277 settings.copyright(!bAppendFiles);
278 gmx::printBinaryInformation(fp, gmx::getProgramContext(), settings);
280 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
289 void gmx_log_close(FILE *fp)
293 gmx_fatal_set_log_file(NULL);
298 void init_multisystem(t_commrec *cr, int nsim, char **multidirs,
299 int nfile, const t_filenm fnm[], gmx_bool bParFn)
302 int nnodes, nnodpersim, sim, i, ftp;
305 MPI_Group mpi_group_world;
312 gmx_fatal(FARGS, "This binary is compiled without MPI support, can not do multiple simulations.");
317 if (nnodes % nsim != 0)
319 gmx_fatal(FARGS, "The number of ranks (%d) is not a multiple of the number of simulations (%d)", nnodes, nsim);
322 nnodpersim = nnodes/nsim;
323 sim = cr->nodeid/nnodpersim;
327 fprintf(debug, "We have %d simulations, %d ranks per simulation, local simulation is %d\n", nsim, nnodpersim, sim);
335 /* Create a communicator for the master nodes */
336 snew(rank, ms->nsim);
337 for (i = 0; i < ms->nsim; i++)
339 rank[i] = i*nnodpersim;
341 MPI_Comm_group(MPI_COMM_WORLD, &mpi_group_world);
342 MPI_Group_incl(mpi_group_world, nsim, rank, &ms->mpi_group_masters);
344 MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
345 &ms->mpi_comm_masters);
347 #if !defined(MPI_IN_PLACE_EXISTS)
348 /* initialize the MPI_IN_PLACE replacement buffers */
350 ms->mpb->ibuf = NULL;
351 ms->mpb->libuf = NULL;
352 ms->mpb->fbuf = NULL;
353 ms->mpb->dbuf = NULL;
354 ms->mpb->ibuf_alloc = 0;
355 ms->mpb->libuf_alloc = 0;
356 ms->mpb->fbuf_alloc = 0;
357 ms->mpb->dbuf_alloc = 0;
362 /* Reduce the intra-simulation communication */
363 cr->sim_nodeid = cr->nodeid % nnodpersim;
364 cr->nnodes = nnodpersim;
366 MPI_Comm_split(MPI_COMM_WORLD, sim, cr->sim_nodeid, &cr->mpi_comm_mysim);
367 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
368 cr->nodeid = cr->sim_nodeid;
373 fprintf(debug, "This is simulation %d", cr->ms->sim);
376 fprintf(debug, ", local number of ranks %d, local rank ID %d",
377 cr->nnodes, cr->sim_nodeid);
379 fprintf(debug, "\n\n");
386 fprintf(debug, "Changing to directory %s\n", multidirs[cr->ms->sim]);
388 gmx_chdir(multidirs[cr->ms->sim]);
392 /* Patch output and tpx, cpt and rerun input file names */
393 for (i = 0; (i < nfile); i++)
395 /* Because of possible multiple extensions per type we must look
396 * at the actual file name
398 if (is_output(&fnm[i]) ||
399 fnm[i].ftp == efTPR || fnm[i].ftp == efCPT ||
400 strcmp(fnm[i].opt, "-rerun") == 0)
402 ftp = fn2ftp(fnm[i].fns[0]);
403 par_fn(fnm[i].fns[0], ftp, cr, TRUE, FALSE, buf, 255);
404 sfree(fnm[i].fns[0]);
405 fnm[i].fns[0] = gmx_strdup(buf);