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, 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.
47 #ifdef HAVE_SYS_TIME_H
52 #include "gmx_fatal.h"
56 #include "gromacs/fileio/futil.h"
57 #include "gromacs/fileio/filenm.h"
58 #include "gromacs/fileio/gmxfio.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxmpi.h"
64 #include "gromacs/utility/programinfo.h"
66 /* The source code in this file should be thread-safe.
67 Please keep it that way. */
74 #ifdef GMX_NATIVE_WINDOWS
81 static void par_fn(char *base, int ftp, const t_commrec *cr,
82 gmx_bool bAppendSimId, gmx_bool bAppendNodeId,
83 char buf[], int bufsize)
85 if ((size_t)bufsize < (strlen(base)+10))
87 gmx_mem("Character buffer too small!");
90 /* Copy to buf, and strip extension */
92 buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
96 sprintf(buf+strlen(buf), "%d", cr->ms->sim);
100 strcat(buf, "_node");
101 sprintf(buf+strlen(buf), "%d", cr->nodeid);
105 /* Add extension again */
106 strcat(buf, (ftp == efTPX) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
109 fprintf(debug, "node %d par_fn '%s'\n", cr->nodeid, buf);
110 if (fn2ftp(buf) == efLOG)
112 fprintf(debug, "log\n");
117 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
122 gmx_bool bCompatible;
124 if (NULL != log && !bQuiet)
126 fprintf(log, "Multi-checking %s ... ", name);
132 "check_multi_int called with a NULL communication pointer");
135 snew(ibuf, ms->nsim);
137 gmx_sumi_sim(ms->nsim, ibuf, ms);
140 for (p = 1; p < ms->nsim; p++)
142 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
147 if (NULL != log && !bQuiet)
149 fprintf(log, "OK\n");
156 fprintf(log, "\n%s is not equal for all subsystems\n", name);
157 for (p = 0; p < ms->nsim; p++)
159 fprintf(log, " subsystem %d: %d\n", p, ibuf[p]);
162 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
168 void check_multi_int64(FILE *log, const gmx_multisim_t *ms,
169 gmx_int64_t val, const char *name,
174 gmx_bool bCompatible;
176 if (NULL != log && !bQuiet)
178 fprintf(log, "Multi-checking %s ... ", name);
184 "check_multi_int called with a NULL communication pointer");
187 snew(ibuf, ms->nsim);
189 gmx_sumli_sim(ms->nsim, ibuf, ms);
192 for (p = 1; p < ms->nsim; p++)
194 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
199 if (NULL != log && !bQuiet)
201 fprintf(log, "OK\n");
208 fprintf(log, "\n%s is not equal for all subsystems\n", name);
209 for (p = 0; p < ms->nsim; p++)
212 /* first make the format string */
213 snprintf(strbuf, 255, " subsystem %%d: %s\n",
215 fprintf(log, strbuf, p, ibuf[p]);
218 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
225 char *gmx_gethostname(char *name, size_t len)
229 gmx_incons("gmx_gethostname called with len<8");
232 if (gethostname(name, len-1) != 0)
234 strncpy(name, "unknown", 8);
237 strncpy(name, "unknown", 8);
244 void gmx_log_open(const char *lognm, const t_commrec *cr, gmx_bool bMasterOnly,
245 gmx_bool bAppendFiles, FILE** fplog)
248 char buf[256], host[256];
250 char timebuf[STRLEN];
256 /* Communicate the filename for logfile */
257 if (cr->nnodes > 1 && !bMasterOnly
258 #ifdef GMX_THREAD_MPI
259 /* With thread MPI the non-master log files are opened later
260 * when the files names are already known on all nodes.
268 len = strlen(lognm) + 1;
270 gmx_bcast(sizeof(len), &len, cr);
277 tmpnm = gmx_strdup(lognm);
279 gmx_bcast(len*sizeof(*tmpnm), tmpnm, cr);
283 tmpnm = gmx_strdup(lognm);
288 if (!bMasterOnly && !MASTER(cr))
290 /* Since log always ends with '.log' let's use this info */
291 par_fn(tmpnm, efLOG, cr, FALSE, !bMasterOnly, buf, 255);
292 fp = gmx_fio_fopen(buf, bAppendFiles ? "a+" : "w+" );
294 else if (!bAppendFiles)
296 fp = gmx_fio_fopen(tmpnm, bAppendFiles ? "a+" : "w+" );
301 gmx_fatal_set_log_file(fp);
303 /* Get some machine parameters */
304 gmx_gethostname(host, 256);
309 # ifdef GMX_NATIVE_WINDOWS
323 "-----------------------------------------------------------\n"
324 "Restarting from checkpoint, appending to previous log file.\n"
329 gmx_ctime_r(&t, timebuf, STRLEN);
332 "Log file opened on %s"
333 "Host: %s pid: %d nodeid: %d nnodes: %d\n",
334 timebuf, host, pid, cr->nodeid, cr->nnodes);
337 gmx::BinaryInformationSettings settings;
338 settings.extendedInfo(true);
339 settings.copyright(!bAppendFiles);
340 gmx::printBinaryInformation(fp, gmx::ProgramInfo::getInstance(), settings);
342 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
351 void gmx_log_close(FILE *fp)
355 gmx_fatal_set_log_file(NULL);
360 void init_multisystem(t_commrec *cr, int nsim, char **multidirs,
361 int nfile, const t_filenm fnm[], gmx_bool bParFn)
364 int nnodes, nnodpersim, sim, i, ftp;
367 MPI_Group mpi_group_world;
374 gmx_fatal(FARGS, "This binary is compiled without MPI support, can not do multiple simulations.");
379 if (nnodes % nsim != 0)
381 gmx_fatal(FARGS, "The number of nodes (%d) is not a multiple of the number of simulations (%d)", nnodes, nsim);
384 nnodpersim = nnodes/nsim;
385 sim = cr->nodeid/nnodpersim;
389 fprintf(debug, "We have %d simulations, %d nodes per simulation, local simulation is %d\n", nsim, nnodpersim, sim);
397 /* Create a communicator for the master nodes */
398 snew(rank, ms->nsim);
399 for (i = 0; i < ms->nsim; i++)
401 rank[i] = i*nnodpersim;
403 MPI_Comm_group(MPI_COMM_WORLD, &mpi_group_world);
404 MPI_Group_incl(mpi_group_world, nsim, rank, &ms->mpi_group_masters);
406 MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
407 &ms->mpi_comm_masters);
409 #if !defined(MPI_IN_PLACE_EXISTS)
410 /* initialize the MPI_IN_PLACE replacement buffers */
412 ms->mpb->ibuf = NULL;
413 ms->mpb->libuf = NULL;
414 ms->mpb->fbuf = NULL;
415 ms->mpb->dbuf = NULL;
416 ms->mpb->ibuf_alloc = 0;
417 ms->mpb->libuf_alloc = 0;
418 ms->mpb->fbuf_alloc = 0;
419 ms->mpb->dbuf_alloc = 0;
424 /* Reduce the intra-simulation communication */
425 cr->sim_nodeid = cr->nodeid % nnodpersim;
426 cr->nnodes = nnodpersim;
428 MPI_Comm_split(MPI_COMM_WORLD, sim, cr->sim_nodeid, &cr->mpi_comm_mysim);
429 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
430 cr->nodeid = cr->sim_nodeid;
435 fprintf(debug, "This is simulation %d", cr->ms->sim);
438 fprintf(debug, ", local number of nodes %d, local nodeid %d",
439 cr->nnodes, cr->sim_nodeid);
441 fprintf(debug, "\n\n");
448 fprintf(debug, "Changing to directory %s\n", multidirs[cr->ms->sim]);
450 gmx_chdir(multidirs[cr->ms->sim]);
454 /* Patch output and tpx, cpt and rerun input file names */
455 for (i = 0; (i < nfile); i++)
457 /* Because of possible multiple extensions per type we must look
458 * at the actual file name
460 if (is_output(&fnm[i]) ||
461 fnm[i].ftp == efTPX || fnm[i].ftp == efCPT ||
462 strcmp(fnm[i].opt, "-rerun") == 0)
464 ftp = fn2ftp(fnm[i].fns[0]);
465 par_fn(fnm[i].fns[0], ftp, cr, TRUE, FALSE, buf, 255);
466 sfree(fnm[i].fns[0]);
467 fnm[i].fns[0] = gmx_strdup(buf);