1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
39 #include "gromacs/utility/gmx_header_config.h"
47 #ifdef HAVE_SYS_TIME_H
52 #include "gmx_fatal.h"
63 #include "thread_mpi.h"
66 #include "gromacs/utility/exceptions.h"
67 #include "gromacs/utility/programinfo.h"
69 /* The source code in this file should be thread-safe.
70 Please keep it that way. */
77 #ifdef GMX_NATIVE_WINDOWS
84 static void par_fn(char *base, int ftp, const t_commrec *cr,
85 gmx_bool bAppendSimId, gmx_bool bAppendNodeId,
86 char buf[], int bufsize)
88 if ((size_t)bufsize < (strlen(base)+10))
90 gmx_mem("Character buffer too small!");
93 /* Copy to buf, and strip extension */
95 buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
99 sprintf(buf+strlen(buf), "%d", cr->ms->sim);
103 strcat(buf, "_node");
104 sprintf(buf+strlen(buf), "%d", cr->nodeid);
108 /* Add extension again */
109 strcat(buf, (ftp == efTPX) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
112 fprintf(debug, "node %d par_fn '%s'\n", cr->nodeid, buf);
113 if (fn2ftp(buf) == efLOG)
115 fprintf(debug, "log\n");
120 void check_multi_int(FILE *log, const gmx_multisim_t *ms, int val,
125 gmx_bool bCompatible;
127 if (NULL != log && !bQuiet)
129 fprintf(log, "Multi-checking %s ... ", name);
135 "check_multi_int called with a NULL communication pointer");
138 snew(ibuf, ms->nsim);
140 gmx_sumi_sim(ms->nsim, ibuf, ms);
143 for (p = 1; p < ms->nsim; p++)
145 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
150 if (NULL != log && !bQuiet)
152 fprintf(log, "OK\n");
159 fprintf(log, "\n%s is not equal for all subsystems\n", name);
160 for (p = 0; p < ms->nsim; p++)
162 fprintf(log, " subsystem %d: %d\n", p, ibuf[p]);
165 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
171 void check_multi_large_int(FILE *log, const gmx_multisim_t *ms,
172 gmx_large_int_t val, const char *name,
175 gmx_large_int_t *ibuf;
177 gmx_bool bCompatible;
179 if (NULL != log && !bQuiet)
181 fprintf(log, "Multi-checking %s ... ", name);
187 "check_multi_int called with a NULL communication pointer");
190 snew(ibuf, ms->nsim);
192 gmx_sumli_sim(ms->nsim, ibuf, ms);
195 for (p = 1; p < ms->nsim; p++)
197 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
202 if (NULL != log && !bQuiet)
204 fprintf(log, "OK\n");
211 fprintf(log, "\n%s is not equal for all subsystems\n", name);
212 for (p = 0; p < ms->nsim; p++)
215 /* first make the format string */
216 snprintf(strbuf, 255, " subsystem %%d: %s\n",
218 fprintf(log, strbuf, p, ibuf[p]);
221 gmx_fatal(FARGS, "The %d subsystems are not compatible\n", ms->nsim);
228 char *gmx_gethostname(char *name, size_t len)
232 gmx_incons("gmx_gethostname called with len<8");
235 if (gethostname(name, len-1) != 0)
237 strncpy(name, "unknown", 8);
240 strncpy(name, "unknown", 8);
247 void gmx_log_open(const char *lognm, const t_commrec *cr, gmx_bool bMasterOnly,
248 gmx_bool bAppendFiles, FILE** fplog)
251 char buf[256], host[256];
253 char timebuf[STRLEN];
259 /* Communicate the filename for logfile */
260 if (cr->nnodes > 1 && !bMasterOnly
261 #ifdef GMX_THREAD_MPI
262 /* With thread MPI the non-master log files are opened later
263 * when the files names are already known on all nodes.
271 len = strlen(lognm) + 1;
273 gmx_bcast(sizeof(len), &len, cr);
280 tmpnm = gmx_strdup(lognm);
282 gmx_bcast(len*sizeof(*tmpnm), tmpnm, cr);
286 tmpnm = gmx_strdup(lognm);
291 if (!bMasterOnly && !MASTER(cr))
293 /* Since log always ends with '.log' let's use this info */
294 par_fn(tmpnm, efLOG, cr, FALSE, !bMasterOnly, buf, 255);
295 fp = gmx_fio_fopen(buf, bAppendFiles ? "a+" : "w+" );
297 else if (!bAppendFiles)
299 fp = gmx_fio_fopen(tmpnm, bAppendFiles ? "a+" : "w+" );
304 gmx_fatal_set_log_file(fp);
306 /* Get some machine parameters */
307 gmx_gethostname(host, 256);
312 # ifdef GMX_NATIVE_WINDOWS
326 "-----------------------------------------------------------\n"
327 "Restarting from checkpoint, appending to previous log file.\n"
332 gmx_ctime_r(&t, timebuf, STRLEN);
335 "Log file opened on %s"
336 "Host: %s pid: %d nodeid: %d nnodes: %d\n",
337 timebuf, host, pid, cr->nodeid, cr->nnodes);
340 gmx::BinaryInformationSettings settings;
341 settings.extendedInfo(true);
342 settings.copyright(!bAppendFiles);
343 gmx::printBinaryInformation(fp, gmx::ProgramInfo::getInstance(), settings);
345 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
354 void gmx_log_close(FILE *fp)
358 gmx_fatal_set_log_file(NULL);
363 void init_multisystem(t_commrec *cr, int nsim, char **multidirs,
364 int nfile, const t_filenm fnm[], gmx_bool bParFn)
367 int nnodes, nnodpersim, sim, i, ftp;
370 MPI_Group mpi_group_world;
377 gmx_fatal(FARGS, "This binary is compiled without MPI support, can not do multiple simulations.");
382 if (nnodes % nsim != 0)
384 gmx_fatal(FARGS, "The number of nodes (%d) is not a multiple of the number of simulations (%d)", nnodes, nsim);
387 nnodpersim = nnodes/nsim;
388 sim = cr->nodeid/nnodpersim;
392 fprintf(debug, "We have %d simulations, %d nodes per simulation, local simulation is %d\n", nsim, nnodpersim, sim);
400 /* Create a communicator for the master nodes */
401 snew(rank, ms->nsim);
402 for (i = 0; i < ms->nsim; i++)
404 rank[i] = i*nnodpersim;
406 MPI_Comm_group(MPI_COMM_WORLD, &mpi_group_world);
407 MPI_Group_incl(mpi_group_world, nsim, rank, &ms->mpi_group_masters);
409 MPI_Comm_create(MPI_COMM_WORLD, ms->mpi_group_masters,
410 &ms->mpi_comm_masters);
412 #if !defined(GMX_THREAD_MPI) && !defined(MPI_IN_PLACE_EXISTS)
413 /* initialize the MPI_IN_PLACE replacement buffers */
415 ms->mpb->ibuf = NULL;
416 ms->mpb->libuf = NULL;
417 ms->mpb->fbuf = NULL;
418 ms->mpb->dbuf = NULL;
419 ms->mpb->ibuf_alloc = 0;
420 ms->mpb->libuf_alloc = 0;
421 ms->mpb->fbuf_alloc = 0;
422 ms->mpb->dbuf_alloc = 0;
427 /* Reduce the intra-simulation communication */
428 cr->sim_nodeid = cr->nodeid % nnodpersim;
429 cr->nnodes = nnodpersim;
431 MPI_Comm_split(MPI_COMM_WORLD, sim, cr->sim_nodeid, &cr->mpi_comm_mysim);
432 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
433 cr->nodeid = cr->sim_nodeid;
438 fprintf(debug, "This is simulation %d", cr->ms->sim);
441 fprintf(debug, ", local number of nodes %d, local nodeid %d",
442 cr->nnodes, cr->sim_nodeid);
444 fprintf(debug, "\n\n");
451 fprintf(debug, "Changing to directory %s\n", multidirs[cr->ms->sim]);
453 gmx_chdir(multidirs[cr->ms->sim]);
457 /* Patch output and tpx, cpt and rerun input file names */
458 for (i = 0; (i < nfile); i++)
460 /* Because of possible multiple extensions per type we must look
461 * at the actual file name
463 if (is_output(&fnm[i]) ||
464 fnm[i].ftp == efTPX || fnm[i].ftp == efCPT ||
465 strcmp(fnm[i].opt, "-rerun") == 0)
467 ftp = fn2ftp(fnm[i].fns[0]);
468 par_fn(fnm[i].fns[0], ftp, cr, TRUE, FALSE, buf, 255);
469 sfree(fnm[i].fns[0]);
470 fnm[i].fns[0] = gmx_strdup(buf);
476 t_commrec *init_par()
482 #if defined GMX_MPI && !defined GMX_THREAD_MPI
483 if (!gmx_mpi_initialized())
485 gmx_comm("MPI has not been initialized properly");
487 cr->nnodes = gmx_node_num();
488 cr->sim_nodeid = gmx_node_rank();
489 if (!PAR(cr) && (cr->sim_nodeid != 0))
491 gmx_comm("(!PAR(cr) && (cr->sim_nodeid != 0))");
494 cr->mpi_comm_mysim = MPI_COMM_WORLD;
495 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
497 /* These should never be accessed */
498 cr->mpi_comm_mysim = NULL;
499 cr->mpi_comm_mygroup = NULL;
504 cr->nodeid = cr->sim_nodeid;
506 cr->duty = (DUTY_PP | DUTY_PME);
509 #if !defined(GMX_THREAD_MPI) && !defined(MPI_IN_PLACE_EXISTS)
510 /* initialize the MPI_IN_PLACE replacement buffers */
512 cr->mpb->ibuf = NULL;
513 cr->mpb->libuf = NULL;
514 cr->mpb->fbuf = NULL;
515 cr->mpb->dbuf = NULL;
516 cr->mpb->ibuf_alloc = 0;
517 cr->mpb->libuf_alloc = 0;
518 cr->mpb->fbuf_alloc = 0;
519 cr->mpb->dbuf_alloc = 0;
526 t_commrec *init_par_threads(const t_commrec *cro)
528 #ifdef GMX_THREAD_MPI
532 /* make a thread-specific commrec */
534 /* now copy the whole thing, so settings like the number of PME nodes
538 /* and we start setting our own thread-specific values for things */
539 MPI_Initialized(&initialized);
542 gmx_comm("Initializing threads without comm");
544 /* once threads will be used together with MPI, we'll
545 fill the cr structure with distinct data here. This might even work: */
546 cr->sim_nodeid = gmx_setup(0, NULL, &cr->nnodes);
548 cr->mpi_comm_mysim = MPI_COMM_WORLD;
549 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
550 cr->nodeid = cr->sim_nodeid;
551 cr->duty = (DUTY_PP | DUTY_PME);