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
46 #ifdef HAVE_SYS_TIME_H
51 /* windows-specific include for _chdir() */
57 #include "gmx_fatal.h"
68 #include "thread_mpi.h"
71 /* The source code in this file should be thread-safe.
72 Please keep it that way. */
79 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
84 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
86 gmx_ctime_r(const time_t *clock,char *buf, int n);
91 /* this is not strictly thread-safe, but it's only written to at the beginning
92 of the simulation, once by each thread with the same value. We assume
93 that writing to an int is atomic.*/
94 static gmx_bool parallel_env_val;
96 tMPI_Thread_mutex_t parallel_env_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
100 /* returns 1 when running in a parallel environment, so could also be 1 if
101 mdrun was started with: mpirun -np 1.
103 Use this function only to check whether a parallel environment has
104 been initialized, for example when checking whether gmx_finalize()
105 needs to be called. Use PAR(cr) to check whether the simulation actually
106 has more than one node/thread. */
107 gmx_bool gmx_parallel_env_initialized(void)
110 #ifdef GMX_THREAD_MPI
111 tMPI_Thread_mutex_lock(¶llel_env_mutex);
113 ret=parallel_env_val;
114 #ifdef GMX_THREAD_MPI
115 tMPI_Thread_mutex_unlock(¶llel_env_mutex);
120 static void set_parallel_env(gmx_bool val)
122 #ifdef GMX_THREAD_MPI
123 tMPI_Thread_mutex_lock(¶llel_env_mutex);
125 if (!parallel_env_val)
127 /* we only allow it to be set, not unset */
128 parallel_env_val=val;
130 #ifdef GMX_THREAD_MPI
131 tMPI_Thread_mutex_unlock(¶llel_env_mutex);
136 static void par_fn(char *base,int ftp,const t_commrec *cr,
137 gmx_bool bAppendSimId,gmx_bool bAppendNodeId,
138 char buf[],int bufsize)
142 if((size_t)bufsize<(strlen(base)+10))
143 gmx_mem("Character buffer too small!");
145 /* Copy to buf, and strip extension */
147 buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
150 sprintf(buf+strlen(buf),"%d",cr->ms->sim);
154 sprintf(buf+strlen(buf),"%d",cr->nodeid);
158 /* Add extension again */
159 strcat(buf,(ftp == efTPX) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
160 if (cr->nodeid == 0) {
161 printf("node %d par_fn '%s'\n",cr->nodeid,buf);
162 if (fn2ftp(buf) == efLOG) {
168 void check_multi_int(FILE *log,const gmx_multisim_t *ms,int val,
172 gmx_bool bCompatible;
175 fprintf(log,"Multi-checking %s ... ",name);
179 "check_multi_int called with a NULL communication pointer");
183 gmx_sumi_sim(ms->nsim,ibuf,ms);
186 for(p=1; p<ms->nsim; p++)
187 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
198 fprintf(log,"\n%s is not equal for all subsystems\n",name);
199 for(p=0; p<ms->nsim; p++)
200 fprintf(log," subsystem %d: %d\n",p,ibuf[p]);
202 gmx_fatal(FARGS,"The %d subsystems are not compatible\n",ms->nsim);
208 void check_multi_large_int(FILE *log,const gmx_multisim_t *ms,
209 gmx_large_int_t val, const char *name)
211 gmx_large_int_t *ibuf;
213 gmx_bool bCompatible;
216 fprintf(log,"Multi-checking %s ... ",name);
220 "check_multi_int called with a NULL communication pointer");
224 gmx_sumli_sim(ms->nsim,ibuf,ms);
227 for(p=1; p<ms->nsim; p++)
228 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
239 fprintf(log,"\n%s is not equal for all subsystems\n",name);
240 for(p=0; p<ms->nsim; p++)
243 /* first make the format string */
244 snprintf(strbuf, 255, " subsystem %%d: %s\n",
246 fprintf(log,strbuf,p,ibuf[p]);
249 gmx_fatal(FARGS,"The %d subsystems are not compatible\n",ms->nsim);
256 void gmx_log_open(const char *lognm,const t_commrec *cr,gmx_bool bMasterOnly,
257 unsigned long Flags, FILE** fplog)
260 char buf[256],host[256];
262 char timebuf[STRLEN];
266 gmx_bool bAppend = Flags & MD_APPENDFILES;
270 /* Communicate the filename for logfile */
271 if (cr->nnodes > 1 && !bMasterOnly
272 #ifdef GMX_THREAD_MPI
273 /* With thread MPI the non-master log files are opened later
274 * when the files names are already known on all nodes.
282 len = strlen(lognm) + 1;
284 gmx_bcast(sizeof(len),&len,cr);
291 tmpnm=gmx_strdup(lognm);
293 gmx_bcast(len*sizeof(*tmpnm),tmpnm,cr);
297 tmpnm=gmx_strdup(lognm);
302 if (!bMasterOnly && !MASTER(cr))
304 /* Since log always ends with '.log' let's use this info */
305 par_fn(tmpnm,efLOG,cr,FALSE,!bMasterOnly,buf,255);
306 fp = gmx_fio_fopen(buf, bAppend ? "a+" : "w+" );
310 fp = gmx_fio_fopen(tmpnm, bAppend ? "a+" : "w+" );
315 gmx_fatal_set_log_file(fp);
317 /* Get some machine parameters */
319 if (gethostname(host,255) != 0)
321 sprintf(host,"unknown");
324 sprintf(host,"unknown");
330 # if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
344 "-----------------------------------------------------------\n"
345 "Restarting from checkpoint, appending to previous log file.\n"
350 gmx_ctime_r(&t,timebuf,STRLEN);
353 "Log file opened on %s"
354 "Host: %s pid: %d nodeid: %d nnodes: %d\n",
355 timebuf,host,pid,cr->nodeid,cr->nnodes);
357 #if (defined BUILD_MACHINE && defined BUILD_TIME && defined BUILD_USER)
359 "The Gromacs distribution was built %s by\n"
360 "%s (%s)\n\n\n",BUILD_TIME,BUILD_USER,BUILD_MACHINE);
369 void gmx_log_close(FILE *fp)
372 gmx_fatal_set_log_file(NULL);
377 static void comm_args(const t_commrec *cr,int *argc,char ***argv)
382 gmx_bcast(sizeof(*argc),argc,cr);
386 fprintf(stderr,"NODEID=%d argc=%d\n",cr->nodeid,*argc);
387 for(i=0; (i<*argc); i++) {
389 len = strlen((*argv)[i])+1;
390 gmx_bcast(sizeof(len),&len,cr);
392 snew((*argv)[i],len);
393 /*gmx_bcast(len*sizeof((*argv)[i][0]),(*argv)[i],cr);*/
394 gmx_bcast(len*sizeof(char),(*argv)[i],cr);
399 void init_multisystem(t_commrec *cr,int nsim, char **multidirs,
400 int nfile, const t_filenm fnm[],gmx_bool bParFn)
403 int nnodes,nnodpersim,sim,i,ftp;
406 MPI_Group mpi_group_world;
413 gmx_fatal(FARGS,"This binary is compiled without MPI support, can not do multiple simulations.");
418 if (nnodes % nsim != 0)
420 gmx_fatal(FARGS,"The number of nodes (%d) is not a multiple of the number of simulations (%d)",nnodes,nsim);
423 nnodpersim = nnodes/nsim;
424 sim = cr->nodeid/nnodpersim;
428 fprintf(debug,"We have %d simulations, %d nodes per simulation, local simulation is %d\n",nsim,nnodpersim,sim);
436 /* Create a communicator for the master nodes */
438 for(i=0; i<ms->nsim; i++)
440 rank[i] = i*nnodpersim;
442 MPI_Comm_group(MPI_COMM_WORLD,&mpi_group_world);
443 MPI_Group_incl(mpi_group_world,nsim,rank,&ms->mpi_group_masters);
445 MPI_Comm_create(MPI_COMM_WORLD,ms->mpi_group_masters,
446 &ms->mpi_comm_masters);
448 #if !defined(GMX_THREAD_MPI) && !defined(MPI_IN_PLACE_EXISTS)
449 /* initialize the MPI_IN_PLACE replacement buffers */
455 ms->mpb->ibuf_alloc=0;
456 ms->mpb->libuf_alloc=0;
457 ms->mpb->fbuf_alloc=0;
458 ms->mpb->dbuf_alloc=0;
463 /* Reduce the intra-simulation communication */
464 cr->sim_nodeid = cr->nodeid % nnodpersim;
465 cr->nnodes = nnodpersim;
467 MPI_Comm_split(MPI_COMM_WORLD,sim,cr->sim_nodeid,&cr->mpi_comm_mysim);
468 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
469 cr->nodeid = cr->sim_nodeid;
474 fprintf(debug,"This is simulation %d",cr->ms->sim);
477 fprintf(debug,", local number of nodes %d, local nodeid %d",
478 cr->nnodes,cr->sim_nodeid);
480 fprintf(debug,"\n\n");
488 fprintf(debug,"Changing to directory %s\n",multidirs[cr->ms->sim]);
490 if (chdir(multidirs[cr->ms->sim]) != 0)
492 gmx_fatal(FARGS, "Couldn't change directory to %s: %s",
493 multidirs[cr->ms->sim],
499 /* Patch output and tpx, cpt and rerun input file names */
500 for(i=0; (i<nfile); i++)
502 /* Because of possible multiple extensions per type we must look
503 * at the actual file name
505 if (is_output(&fnm[i]) ||
506 fnm[i].ftp == efTPX || fnm[i].ftp == efCPT ||
507 strcmp(fnm[i].opt,"-rerun") == 0)
509 ftp = fn2ftp(fnm[i].fns[0]);
510 par_fn(fnm[i].fns[0],ftp,cr,TRUE,FALSE,buf,255);
511 sfree(fnm[i].fns[0]);
512 fnm[i].fns[0] = gmx_strdup(buf);
518 t_commrec *init_par(int *argc,char ***argv_ptr)
532 #ifdef GMX_CHECK_MPI_ENV
533 /* Do not use MPI calls when env.var. GMX_CHECK_MPI_ENV is not set */
534 if (getenv(GMX_CHECK_MPI_ENV) == NULL)
536 #endif /* GMX_CHECK_MPI_ENV */
537 #endif /* GMX_LIB_MPI */
538 set_parallel_env(pe);
540 cr->sim_nodeid = gmx_setup(argc,argv,&cr->nnodes);
547 set_parallel_env(pe);
552 if (!PAR(cr) && (cr->sim_nodeid != 0))
553 gmx_comm("(!PAR(cr) && (cr->sim_nodeid != 0))");
558 cr->mpi_comm_mysim = MPI_COMM_WORLD;
559 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
562 cr->nodeid = cr->sim_nodeid;
564 cr->duty = (DUTY_PP | DUTY_PME);
566 /* Communicate arguments if parallel */
567 #ifndef GMX_THREAD_MPI
569 comm_args(cr,argc,argv_ptr);
570 #endif /* GMX_THREAD_MPI */
573 #if !defined(GMX_THREAD_MPI) && !defined(MPI_IN_PLACE_EXISTS)
574 /* initialize the MPI_IN_PLACE replacement buffers */
580 cr->mpb->ibuf_alloc=0;
581 cr->mpb->libuf_alloc=0;
582 cr->mpb->fbuf_alloc=0;
583 cr->mpb->dbuf_alloc=0;
590 t_commrec *init_par_threads(const t_commrec *cro)
592 #ifdef GMX_THREAD_MPI
596 /* make a thread-specific commrec */
598 /* now copy the whole thing, so settings like the number of PME nodes
602 /* and we start setting our own thread-specific values for things */
603 MPI_Initialized(&initialized);
605 gmx_comm("Initializing threads without comm");
606 set_parallel_env(TRUE);
607 /* once threads will be used together with MPI, we'll
608 fill the cr structure with distinct data here. This might even work: */
609 cr->sim_nodeid = gmx_setup(0,NULL, &cr->nnodes);
611 cr->mpi_comm_mysim = MPI_COMM_WORLD;
612 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
613 cr->nodeid = cr->sim_nodeid;
614 cr->duty = (DUTY_PP | DUTY_PME);
623 t_commrec *init_cr_nopar(void)
630 /* cr->nthreads = 1; */
633 /* cr->threadid = 0; */
634 cr->duty = (DUTY_PP | DUTY_PME);