More selection unit tests for variables and fixes.
[alexxy/gromacs.git] / src / gromacs / gmxlib / main.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
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.
15
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.
20  * 
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.
27  * 
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.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #include "gromacs/utility/gmx_header_config.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <limits.h>
45 #include <time.h>
46
47 #ifdef HAVE_SYS_TIME_H
48 #include <sys/time.h>
49 #endif
50
51 #include "smalloc.h"
52 #include "gmx_fatal.h"
53 #include "network.h"
54 #include "main.h"
55 #include "macros.h"
56 #include "futil.h"
57 #include "filenm.h"
58 #include "mdrun.h"
59 #include "gmxfio.h"
60 #include "string2.h"
61
62 #ifdef GMX_THREAD_MPI
63 #include "thread_mpi.h"
64 #endif
65
66 /* The source code in this file should be thread-safe. 
67          Please keep it that way. */
68
69
70 #ifdef HAVE_UNISTD_H
71 #include <unistd.h>
72 #endif
73
74 #ifdef GMX_NATIVE_WINDOWS
75 #include <process.h>
76 #endif
77
78
79 /* Portable version of ctime_r implemented in src/gmxlib/string2.c, but we do not want it declared in public installed headers */
80 char *
81 gmx_ctime_r(const time_t *clock,char *buf, int n);
82
83
84 #define BUFSIZE 1024
85
86
87 static void par_fn(char *base,int ftp,const t_commrec *cr,
88                    gmx_bool bAppendSimId,gmx_bool bAppendNodeId,
89                    char buf[],int bufsize)
90 {
91   int n;
92   
93   if((size_t)bufsize<(strlen(base)+10))
94      gmx_mem("Character buffer too small!");
95
96   /* Copy to buf, and strip extension */
97   strcpy(buf,base);
98   buf[strlen(base) - strlen(ftp2ext(fn2ftp(base))) - 1] = '\0';
99
100   if (bAppendSimId) {
101     sprintf(buf+strlen(buf),"%d",cr->ms->sim);
102   }
103   if (bAppendNodeId) {
104     strcat(buf,"_node");
105     sprintf(buf+strlen(buf),"%d",cr->nodeid);
106   }
107   strcat(buf,".");
108   
109   /* Add extension again */
110   strcat(buf,(ftp == efTPX) ? "tpr" : (ftp == efEDR) ? "edr" : ftp2ext(ftp));
111   if (cr->nodeid == 0) {
112     printf("node %d par_fn '%s'\n",cr->nodeid,buf);
113     if (fn2ftp(buf) == efLOG) {
114       printf("log\n");
115     }
116   }
117 }
118
119 void check_multi_int(FILE *log,const gmx_multisim_t *ms,int val,
120                      const char *name)
121 {
122   int  *ibuf,p;
123   gmx_bool bCompatible;
124
125   if (NULL != log)
126       fprintf(log,"Multi-checking %s ... ",name);
127   
128   if (ms == NULL)
129     gmx_fatal(FARGS,
130               "check_multi_int called with a NULL communication pointer");
131
132   snew(ibuf,ms->nsim);
133   ibuf[ms->sim] = val;
134   gmx_sumi_sim(ms->nsim,ibuf,ms);
135   
136   bCompatible = TRUE;
137   for(p=1; p<ms->nsim; p++)
138     bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
139   
140   if (bCompatible) 
141   {
142       if (NULL != log)
143           fprintf(log,"OK\n");
144   }
145   else 
146   {
147       if (NULL != log)
148       {
149           fprintf(log,"\n%s is not equal for all subsystems\n",name);
150           for(p=0; p<ms->nsim; p++)
151               fprintf(log,"  subsystem %d: %d\n",p,ibuf[p]);
152       }
153       gmx_fatal(FARGS,"The %d subsystems are not compatible\n",ms->nsim);
154   }
155   
156   sfree(ibuf);
157 }
158
159 void check_multi_large_int(FILE *log,const gmx_multisim_t *ms,
160                            gmx_large_int_t val, const char *name)
161 {
162   gmx_large_int_t  *ibuf;
163   int p;
164   gmx_bool bCompatible;
165
166   if (NULL != log)
167       fprintf(log,"Multi-checking %s ... ",name);
168   
169   if (ms == NULL)
170     gmx_fatal(FARGS,
171               "check_multi_int called with a NULL communication pointer");
172
173   snew(ibuf,ms->nsim);
174   ibuf[ms->sim] = val;
175   gmx_sumli_sim(ms->nsim,ibuf,ms);
176   
177   bCompatible = TRUE;
178   for(p=1; p<ms->nsim; p++)
179     bCompatible = bCompatible && (ibuf[p-1] == ibuf[p]);
180   
181   if (bCompatible) 
182   {
183       if (NULL != log)
184           fprintf(log,"OK\n");
185   }
186   else 
187   {
188       if (NULL != log)
189       {
190           fprintf(log,"\n%s is not equal for all subsystems\n",name);
191           for(p=0; p<ms->nsim; p++)
192           {
193               char strbuf[255];
194               /* first make the format string */
195               snprintf(strbuf, 255, "  subsystem %%d: %s\n", 
196                        gmx_large_int_pfmt);
197               fprintf(log,strbuf,p,ibuf[p]);
198           }
199       }
200       gmx_fatal(FARGS,"The %d subsystems are not compatible\n",ms->nsim);
201   }
202   
203   sfree(ibuf);
204 }
205
206
207 void gmx_log_open(const char *lognm,const t_commrec *cr,gmx_bool bMasterOnly, 
208                    unsigned long Flags, FILE** fplog)
209 {
210     int  len,testlen,pid;
211     char buf[256],host[256];
212     time_t t;
213     char timebuf[STRLEN];
214     FILE *fp=*fplog;
215     char *tmpnm;
216
217     gmx_bool bAppend = Flags & MD_APPENDFILES;  
218   
219     debug_gmx();
220   
221     /* Communicate the filename for logfile */
222     if (cr->nnodes > 1 && !bMasterOnly
223 #ifdef GMX_THREAD_MPI
224         /* With thread MPI the non-master log files are opened later
225          * when the files names are already known on all nodes.
226          */
227         && FALSE
228 #endif
229         )
230     {
231         if (MASTER(cr))
232         {
233             len = strlen(lognm) + 1;
234         }
235         gmx_bcast(sizeof(len),&len,cr);
236         if (!MASTER(cr))
237         {
238             snew(tmpnm,len+8);
239         }
240         else
241         {
242             tmpnm=gmx_strdup(lognm);
243         }
244         gmx_bcast(len*sizeof(*tmpnm),tmpnm,cr);
245     }
246     else
247     {
248         tmpnm=gmx_strdup(lognm);
249     }
250   
251     debug_gmx();
252
253     if (!bMasterOnly && !MASTER(cr))
254     {
255         /* Since log always ends with '.log' let's use this info */
256         par_fn(tmpnm,efLOG,cr,FALSE,!bMasterOnly,buf,255);
257         fp = gmx_fio_fopen(buf, bAppend ? "a+" : "w+" );
258     }
259     else if (!bAppend)
260     {
261         fp = gmx_fio_fopen(tmpnm, bAppend ? "a+" : "w+" );
262     }
263
264     sfree(tmpnm);
265
266     gmx_fatal_set_log_file(fp);
267   
268     /* Get some machine parameters */
269 #ifdef HAVE_UNISTD_H
270     if (gethostname(host,255) != 0)
271     {
272         sprintf(host,"unknown");
273     }
274 #else
275     sprintf(host,"unknown");
276 #endif  
277
278     time(&t);
279
280 #ifndef NO_GETPID
281 #   ifdef GMX_NATIVE_WINDOWS
282     pid = _getpid();
283 #   else
284     pid = getpid();
285 #   endif
286 #else
287         pid = 0;
288 #endif
289
290     if (bAppend)
291     {
292         fprintf(fp,
293                 "\n"
294                 "\n"
295                 "-----------------------------------------------------------\n"
296                 "Restarting from checkpoint, appending to previous log file.\n"
297                 "\n"
298             );
299     }
300         
301     gmx_ctime_r(&t,timebuf,STRLEN);
302
303     fprintf(fp,
304             "Log file opened on %s"
305             "Host: %s  pid: %d  nodeid: %d  nnodes:  %d\n",
306             timebuf,host,pid,cr->nodeid,cr->nnodes);
307     fprintf(fp,
308             "Built %s by %s\n"
309             "Build os/architecture: %s\n"
310             "Build CPU Vendor: %s  Brand: %s\n"
311             "Build CPU Family: %d  Model: %d  Stepping: %d\n"
312             "Build CPU Features: %s\n"
313             "Compiler: %s\n"
314             "CFLAGS: %s\n\n",
315             BUILD_TIME,BUILD_USER,BUILD_HOST,
316             BUILD_CPU_VENDOR,BUILD_CPU_BRAND,
317             BUILD_CPU_FAMILY,BUILD_CPU_MODEL,BUILD_CPU_STEPPING,
318             BUILD_CPU_FEATURES,BUILD_COMPILER,BUILD_CFLAGS);
319
320     fflush(fp);
321     debug_gmx();
322
323     *fplog = fp;
324 }
325
326 void gmx_log_close(FILE *fp)
327 {
328   if (fp) {
329     gmx_fatal_set_log_file(NULL);
330     gmx_fio_fclose(fp);
331   }
332 }
333
334 static void comm_args(const t_commrec *cr,int *argc,char ***argv)
335 {
336   int i,len;
337   
338   if (PAR(cr))
339     gmx_bcast(sizeof(*argc),argc,cr);
340   
341   if (!MASTER(cr))
342     snew(*argv,*argc+1);
343   fprintf(stderr,"NODEID=%d argc=%d\n",cr->nodeid,*argc);
344   for(i=0; (i<*argc); i++) {
345     if (MASTER(cr))
346       len = strlen((*argv)[i])+1;
347     gmx_bcast(sizeof(len),&len,cr);
348     if (!MASTER(cr))
349       snew((*argv)[i],len);
350     /*gmx_bcast(len*sizeof((*argv)[i][0]),(*argv)[i],cr);*/
351     gmx_bcast(len*sizeof(char),(*argv)[i],cr);
352   }
353   debug_gmx();
354 }
355
356 void init_multisystem(t_commrec *cr,int nsim, char **multidirs,
357                       int nfile, const t_filenm fnm[],gmx_bool bParFn)
358 {
359     gmx_multisim_t *ms;
360     int  nnodes,nnodpersim,sim,i,ftp;
361     char buf[256];
362 #ifdef GMX_MPI
363     MPI_Group mpi_group_world;
364 #endif  
365     int *rank;
366
367 #ifndef GMX_MPI
368     if (nsim > 1)
369     {
370         gmx_fatal(FARGS,"This binary is compiled without MPI support, can not do multiple simulations.");
371     }
372 #endif
373
374     nnodes  = cr->nnodes;
375     if (nnodes % nsim != 0)
376     {
377         gmx_fatal(FARGS,"The number of nodes (%d) is not a multiple of the number of simulations (%d)",nnodes,nsim);
378     }
379
380     nnodpersim = nnodes/nsim;
381     sim = cr->nodeid/nnodpersim;
382
383     if (debug)
384     {
385         fprintf(debug,"We have %d simulations, %d nodes per simulation, local simulation is %d\n",nsim,nnodpersim,sim);
386     }
387
388     snew(ms,1);
389     cr->ms = ms;
390     ms->nsim = nsim;
391     ms->sim  = sim;
392 #ifdef GMX_MPI
393     /* Create a communicator for the master nodes */
394     snew(rank,ms->nsim);
395     for(i=0; i<ms->nsim; i++)
396     {
397         rank[i] = i*nnodpersim;
398     }
399     MPI_Comm_group(MPI_COMM_WORLD,&mpi_group_world);
400     MPI_Group_incl(mpi_group_world,nsim,rank,&ms->mpi_group_masters);
401     sfree(rank);
402     MPI_Comm_create(MPI_COMM_WORLD,ms->mpi_group_masters,
403                     &ms->mpi_comm_masters);
404
405 #if !defined(GMX_THREAD_MPI) && !defined(MPI_IN_PLACE_EXISTS)
406     /* initialize the MPI_IN_PLACE replacement buffers */
407     snew(ms->mpb, 1);
408     ms->mpb->ibuf=NULL;
409     ms->mpb->libuf=NULL;
410     ms->mpb->fbuf=NULL;
411     ms->mpb->dbuf=NULL;
412     ms->mpb->ibuf_alloc=0;
413     ms->mpb->libuf_alloc=0;
414     ms->mpb->fbuf_alloc=0;
415     ms->mpb->dbuf_alloc=0;
416 #endif
417
418 #endif
419
420     /* Reduce the intra-simulation communication */
421     cr->sim_nodeid = cr->nodeid % nnodpersim;
422     cr->nnodes = nnodpersim;
423 #ifdef GMX_MPI
424     MPI_Comm_split(MPI_COMM_WORLD,sim,cr->sim_nodeid,&cr->mpi_comm_mysim);
425     cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
426     cr->nodeid = cr->sim_nodeid;
427 #endif
428
429     if (debug)
430     {
431         fprintf(debug,"This is simulation %d",cr->ms->sim);
432         if (PAR(cr))
433         {
434             fprintf(debug,", local number of nodes %d, local nodeid %d",
435                     cr->nnodes,cr->sim_nodeid);
436         }
437         fprintf(debug,"\n\n");
438     }
439
440     if (multidirs)
441     {
442         int ret;
443         if (debug)
444         {
445             fprintf(debug,"Changing to directory %s\n",multidirs[cr->ms->sim]);
446         }
447         gmx_chdir(multidirs[cr->ms->sim]);
448     }
449     else if (bParFn)
450     {
451         /* Patch output and tpx, cpt and rerun input file names */
452         for(i=0; (i<nfile); i++)
453         {
454             /* Because of possible multiple extensions per type we must look 
455              * at the actual file name 
456              */
457             if (is_output(&fnm[i]) ||
458                 fnm[i].ftp == efTPX || fnm[i].ftp == efCPT ||
459                 strcmp(fnm[i].opt,"-rerun") == 0)
460             {
461                 ftp = fn2ftp(fnm[i].fns[0]);
462                 par_fn(fnm[i].fns[0],ftp,cr,TRUE,FALSE,buf,255);
463                 sfree(fnm[i].fns[0]);
464                 fnm[i].fns[0] = gmx_strdup(buf);
465             }
466         }
467     }
468 }
469
470 t_commrec *init_par(int *argc,char ***argv_ptr)
471 {
472     t_commrec *cr;
473     char      **argv;
474     int       i;
475     gmx_bool      pe=FALSE;
476
477     snew(cr,1);
478
479     argv = argv_ptr ? *argv_ptr : NULL;
480
481 #if defined GMX_MPI && !defined GMX_THREAD_MPI
482     cr->sim_nodeid = gmx_setup(argc,argv,&cr->nnodes);
483
484     if (!PAR(cr) && (cr->sim_nodeid != 0))
485     {
486         gmx_comm("(!PAR(cr) && (cr->sim_nodeid != 0))");
487     }
488
489     cr->mpi_comm_mysim   = MPI_COMM_WORLD;
490     cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
491 #else
492     /* These should never be accessed */
493     cr->mpi_comm_mysim   = NULL;
494     cr->mpi_comm_mygroup = NULL;
495     cr->nnodes           = 1;
496     cr->sim_nodeid       = 0;
497 #endif
498
499     cr->nodeid = cr->sim_nodeid;
500
501     cr->duty = (DUTY_PP | DUTY_PME);
502
503     /* Communicate arguments if parallel */
504 #ifndef GMX_THREAD_MPI
505     if (PAR(cr))
506     {
507         comm_args(cr,argc,argv_ptr);
508     }
509 #endif /* GMX_THREAD_MPI */
510
511 #ifdef GMX_MPI
512 #if !defined(GMX_THREAD_MPI) && !defined(MPI_IN_PLACE_EXISTS)
513   /* initialize the MPI_IN_PLACE replacement buffers */
514   snew(cr->mpb, 1);
515   cr->mpb->ibuf=NULL;
516   cr->mpb->libuf=NULL;
517   cr->mpb->fbuf=NULL;
518   cr->mpb->dbuf=NULL;
519   cr->mpb->ibuf_alloc=0;
520   cr->mpb->libuf_alloc=0;
521   cr->mpb->fbuf_alloc=0;
522   cr->mpb->dbuf_alloc=0;
523 #endif
524 #endif
525
526     return cr;
527 }
528
529 t_commrec *init_par_threads(const t_commrec *cro)
530 {
531 #ifdef GMX_THREAD_MPI
532     int initialized;
533     t_commrec *cr;
534
535     /* make a thread-specific commrec */
536     snew(cr,1);
537     /* now copy the whole thing, so settings like the number of PME nodes
538        get propagated. */
539     *cr=*cro;
540
541     /* and we start setting our own thread-specific values for things */
542     MPI_Initialized(&initialized);
543     if (!initialized)
544     {
545         gmx_comm("Initializing threads without comm");
546     }
547     /* once threads will be used together with MPI, we'll
548        fill the cr structure with distinct data here. This might even work: */
549     cr->sim_nodeid = gmx_setup(0,NULL, &cr->nnodes);
550
551     cr->mpi_comm_mysim = MPI_COMM_WORLD;
552     cr->mpi_comm_mygroup = cr->mpi_comm_mysim;
553     cr->nodeid = cr->sim_nodeid;
554     cr->duty = (DUTY_PP | DUTY_PME);
555
556     return cr;
557 #else
558     return NULL;
559 #endif
560 }