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
52 #include "gmx_fatal.h"
63 #include "thread_mpi.h"
66 /* The source code in this file should be thread-safe.
67 Please keep it that way. */
74 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
79 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
81 gmx_ctime_r(const time_t *clock,char *buf, int n);
86 /* this is not strictly thread-safe, but it's only written to at the beginning
87 of the simulation, once by each thread with the same value. We assume
88 that writing to an int is atomic.*/
89 static gmx_bool parallel_env_val;
91 tMPI_Thread_mutex_t parallel_env_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
95 /* returns 1 when running in a parallel environment, so could also be 1 if
96 mdrun was started with: mpirun -np 1.
98 Use this function only to check whether a parallel environment has
99 been initialized, for example when checking whether gmx_finalize()
100 needs to be called. Use PAR(cr) to check whether the simulation actually
101 has more than one node/thread. */
102 gmx_bool gmx_parallel_env_initialized(void)
106 tMPI_Thread_mutex_lock(¶llel_env_mutex);
108 ret=parallel_env_val;
110 tMPI_Thread_mutex_unlock(¶llel_env_mutex);
115 static void set_parallel_env(gmx_bool val)
118 tMPI_Thread_mutex_lock(¶llel_env_mutex);
120 if (!parallel_env_val)
122 /* we only allow it to be set, not unset */
123 parallel_env_val=val;
126 tMPI_Thread_mutex_unlock(¶llel_env_mutex);
131 static void par_fn(char *base,int ftp,const t_commrec *cr,
132 gmx_bool bAppendSimId,gmx_bool bAppendNodeId,
133 char buf[],int bufsize)
137 if((size_t)bufsize<(strlen(base)+10))
138 gmx_mem("Character buffer too small!");
140 /* Copy to buf, and strip extension */
142 buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
145 sprintf(buf+strlen(buf),"%d",cr->ms->sim);
149 sprintf(buf+strlen(buf),"%d",cr->nodeid);
153 /* Add extension again */
154 strcat(buf,(ftp == efTPX) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
155 if (cr->nodeid == 0) {
156 printf("node %d par_fn '%s'\n",cr->nodeid,buf);
157 if (fn2ftp(buf) == efLOG) {
163 void check_multi_int(FILE *log,const gmx_multisim_t *ms,int val,
167 gmx_bool bCompatible;
169 fprintf(log,"Multi-checking %s ... ",name);
173 "check_multi_int called with a NULL communication pointer");
177 gmx_sumi_sim(ms->nsim,ibuf,ms);
180 for(p=1; p<ms->nsim; p++)
181 bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
186 fprintf(log,"\n%s is not equal for all subsystems\n",name);
187 for(p=0; p<ms->nsim; p++)
188 fprintf(log," subsystem %d: %d\n",p,ibuf[p]);
189 gmx_fatal(FARGS,"The %d subsystems are not compatible\n",ms->nsim);
195 void gmx_log_open(const char *lognm,const t_commrec *cr,gmx_bool bMasterOnly,
196 unsigned long Flags, FILE** fplog)
199 char buf[256],host[256];
201 char timebuf[STRLEN];
205 gmx_bool bAppend = Flags & MD_APPENDFILES;
209 /* Communicate the filename for logfile */
210 if (cr->nnodes > 1 && !bMasterOnly
212 /* With thread MPI the non-master log files are opened later
213 * when the files names are already known on all nodes.
221 len = strlen(lognm) + 1;
223 gmx_bcast(sizeof(len),&len,cr);
232 gmx_bcast(len*sizeof(*tmpnm),tmpnm,cr);
243 /* Since log always ends with '.log' let's use this info */
244 par_fn(tmpnm,efLOG,cr,FALSE,!bMasterOnly,buf,255);
245 fp = gmx_fio_fopen(buf, bAppend ? "a+" : "w+" );
249 fp = gmx_fio_fopen(tmpnm, bAppend ? "a+" : "w+" );
254 gmx_fatal_set_log_file(fp);
256 /* Get some machine parameters */
258 if (gethostname(host,255) != 0)
260 sprintf(host,"unknown");
263 sprintf(host,"unknown");
269 # if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
283 "-----------------------------------------------------------\n"
284 "Restarting from checkpoint, appending to previous log file.\n"
289 gmx_ctime_r(&t,timebuf,STRLEN);
292 "Log file opened on %s"
293 "Host: %s pid: %d nodeid: %d nnodes: %d\n",
294 timebuf,host,pid,cr->nodeid,cr->nnodes);
296 #if (defined BUILD_MACHINE && defined BUILD_TIME && defined BUILD_USER)
298 "The Gromacs distribution was built %s by\n"
299 "%s (%s)\n\n\n",BUILD_TIME,BUILD_USER,BUILD_MACHINE);
308 void gmx_log_close(FILE *fp)
311 gmx_fatal_set_log_file(NULL);
316 static void comm_args(const t_commrec *cr,int *argc,char ***argv)
321 gmx_bcast(sizeof(*argc),argc,cr);
325 fprintf(stderr,"NODEID=%d argc=%d\n",cr->nodeid,*argc);
326 for(i=0; (i<*argc); i++) {
328 len = strlen((*argv)[i])+1;
329 gmx_bcast(sizeof(len),&len,cr);
331 snew((*argv)[i],len);
332 /*gmx_bcast(len*sizeof((*argv)[i][0]),(*argv)[i],cr);*/
333 gmx_bcast(len*sizeof(char),(*argv)[i],cr);
338 void init_multisystem(t_commrec *cr,int nsim, int nfile,
339 const t_filenm fnm[],gmx_bool bParFn)
342 int nnodes,nnodpersim,sim,i,ftp;
345 MPI_Group mpi_group_world;
352 gmx_fatal(FARGS,"This binary is compiled without MPI support, can not do multiple simulations.");
357 if (nnodes % nsim != 0)
359 gmx_fatal(FARGS,"The number of nodes (%d) is not a multiple of the number of simulations (%d)",nnodes,nsim);
362 nnodpersim = nnodes/nsim;
363 sim = cr->nodeid/nnodpersim;
367 fprintf(debug,"We have %d simulations, %d nodes per simulation, local simulation is %d\n",nsim,nnodpersim,sim);
375 /* Create a communicator for the master nodes */
377 for(i=0; i<ms->nsim; i++)
379 rank[i] = i*nnodpersim;
381 MPI_Comm_group(MPI_COMM_WORLD,&mpi_group_world);
382 MPI_Group_incl(mpi_group_world,nsim,rank,&ms->mpi_group_masters);
384 MPI_Comm_create(MPI_COMM_WORLD,ms->mpi_group_masters,
385 &ms->mpi_comm_masters);
387 #if !defined(GMX_THREADS) && !defined(MPI_IN_PLACE_EXISTS)
388 /* initialize the MPI_IN_PLACE replacement buffers */
393 ms->mpb->ibuf_alloc=0;
394 ms->mpb->fbuf_alloc=0;
395 ms->mpb->dbuf_alloc=0;
400 /* Reduce the intra-simulation communication */
401 cr->sim_nodeid = cr->nodeid % nnodpersim;
402 cr->nnodes = nnodpersim;
404 MPI_Comm_split(MPI_COMM_WORLD,sim,cr->sim_nodeid,&cr->mpi_comm_mysim);
405 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
406 cr->nodeid = cr->sim_nodeid;
411 fprintf(debug,"This is simulation %d",cr->ms->sim);
414 fprintf(debug,", local number of nodes %d, local nodeid %d",
415 cr->nnodes,cr->sim_nodeid);
417 fprintf(debug,"\n\n");
422 /* Patch output and tpx, cpt and rerun input file names */
423 for(i=0; (i<nfile); i++)
425 /* Because of possible multiple extensions per type we must look
426 * at the actual file name
428 if (is_output(&fnm[i]) ||
429 fnm[i].ftp == efTPX || fnm[i].ftp == efCPT ||
430 strcmp(fnm[i].opt,"-rerun") == 0)
432 ftp = fn2ftp(fnm[i].fns[0]);
433 par_fn(fnm[i].fns[0],ftp,cr,TRUE,FALSE,buf,255);
434 sfree(fnm[i].fns[0]);
435 fnm[i].fns[0] = strdup(buf);
441 t_commrec *init_par(int *argc,char ***argv_ptr)
455 #ifdef GMX_CHECK_MPI_ENV
456 /* Do not use MPI calls when env.var. GMX_CHECK_MPI_ENV is not set */
457 if (getenv(GMX_CHECK_MPI_ENV) == NULL)
459 #endif /* GMX_CHECK_MPI_ENV */
460 #endif /* GMX_LIB_MPI */
461 set_parallel_env(pe);
463 cr->sim_nodeid = gmx_setup(argc,argv,&cr->nnodes);
470 set_parallel_env(pe);
475 if (!PAR(cr) && (cr->sim_nodeid != 0))
476 gmx_comm("(!PAR(cr) && (cr->sim_nodeid != 0))");
481 cr->mpi_comm_mysim = MPI_COMM_WORLD;
482 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
485 cr->nodeid = cr->sim_nodeid;
487 cr->duty = (DUTY_PP | DUTY_PME);
489 /* Communicate arguments if parallel */
492 comm_args(cr,argc,argv_ptr);
493 #endif /* GMX_THREADS */
496 #if !defined(GMX_THREADS) && !defined(MPI_IN_PLACE_EXISTS)
497 /* initialize the MPI_IN_PLACE replacement buffers */
502 cr->mpb->ibuf_alloc=0;
503 cr->mpb->fbuf_alloc=0;
504 cr->mpb->dbuf_alloc=0;
511 t_commrec *init_par_threads(const t_commrec *cro)
517 /* make a thread-specific commrec */
519 /* now copy the whole thing, so settings like the number of PME nodes
523 /* and we start setting our own thread-specific values for things */
524 MPI_Initialized(&initialized);
526 gmx_comm("Initializing threads without comm");
527 set_parallel_env(TRUE);
528 /* once threads will be used together with MPI, we'll
529 fill the cr structure with distinct data here. This might even work: */
530 cr->sim_nodeid = gmx_setup(0,NULL, &cr->nnodes);
532 cr->mpi_comm_mysim = MPI_COMM_WORLD;
533 cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
534 cr->nodeid = cr->sim_nodeid;
535 cr->duty = (DUTY_PP | DUTY_PME);
544 t_commrec *init_cr_nopar(void)
551 /* cr->nthreads = 1; */
554 /* cr->threadid = 0; */
555 cr->duty = (DUTY_PP | DUTY_PME);