Merge branch 'release-4-6'
[alexxy/gromacs.git] / src / programs / mdrun / runner.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  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #ifdef __linux
40 #define _GNU_SOURCE
41 #include <sched.h>
42 #include <sys/syscall.h>
43 #endif
44 #include <signal.h>
45 #include <stdlib.h>
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include <string.h>
50 #include <assert.h>
51
52 #include "typedefs.h"
53 #include "smalloc.h"
54 #include "sysstuff.h"
55 #include "statutil.h"
56 #include "mdrun.h"
57 #include "md_logging.h"
58 #include "md_support.h"
59 #include "network.h"
60 #include "pull.h"
61 #include "pull_rotation.h"
62 #include "names.h"
63 #include "disre.h"
64 #include "orires.h"
65 #include "pme.h"
66 #include "mdatoms.h"
67 #include "repl_ex.h"
68 #include "qmmm.h"
69 #include "domdec.h"
70 #include "partdec.h"
71 #include "coulomb.h"
72 #include "constr.h"
73 #include "mvdata.h"
74 #include "checkpoint.h"
75 #include "mtop_util.h"
76 #include "sighandler.h"
77 #include "tpxio.h"
78 #include "txtdump.h"
79 #include "gmx_detect_hardware.h"
80 #include "gmx_omp_nthreads.h"
81 #include "pull_rotation.h"
82 #include "calc_verletbuf.h"
83 #include "nbnxn_search.h"
84 #include "../mdlib/nbnxn_consts.h"
85 #include "gmx_fatal_collective.h"
86 #include "membed.h"
87 #include "macros.h"
88 #include "gmx_omp.h"
89
90 #ifdef GMX_LIB_MPI
91 #include <mpi.h>
92 #endif
93 #ifdef GMX_THREAD_MPI
94 #include "tmpi.h"
95 #endif
96
97 #ifdef GMX_FAHCORE
98 #include "corewrap.h"
99 #endif
100
101 #ifdef GMX_OPENMM
102 #include "md_openmm.h"
103 #endif
104
105 #include "gpu_utils.h"
106 #include "nbnxn_cuda_data_mgmt.h"
107
108 typedef struct { 
109     gmx_integrator_t *func;
110 } gmx_intp_t;
111
112 /* The array should match the eI array in include/types/enums.h */
113 #ifdef GMX_OPENMM  /* FIXME do_md_openmm needs fixing */
114 const gmx_intp_t integrator[eiNR] = { {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm}, {do_md_openmm},{do_md_openmm}};
115 #else
116 const gmx_intp_t integrator[eiNR] = { {do_md}, {do_steep}, {do_cg}, {do_md}, {do_md}, {do_nm}, {do_lbfgs}, {do_tpi}, {do_tpi}, {do_md}, {do_md},{do_md}};
117 #endif
118
119 gmx_large_int_t     deform_init_init_step_tpx;
120 matrix              deform_init_box_tpx;
121 #ifdef GMX_THREAD_MPI
122 tMPI_Thread_mutex_t deform_init_box_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
123 #endif
124
125
126 #ifdef GMX_THREAD_MPI
127 struct mdrunner_arglist
128 {
129     gmx_hw_opt_t *hw_opt;
130     FILE *fplog;
131     t_commrec *cr;
132     int nfile;
133     const t_filenm *fnm;
134     output_env_t oenv;
135     gmx_bool bVerbose;
136     gmx_bool bCompact;
137     int nstglobalcomm;
138     ivec ddxyz;
139     int dd_node_order;
140     real rdd;
141     real rconstr;
142     const char *dddlb_opt;
143     real dlb_scale;
144     const char *ddcsx;
145     const char *ddcsy;
146     const char *ddcsz;
147     const char *nbpu_opt;
148     int nsteps_cmdline;
149     int nstepout;
150     int resetstep;
151     int nmultisim;
152     int repl_ex_nst;
153     int repl_ex_nex;
154     int repl_ex_seed;
155     real pforce;
156     real cpt_period;
157     real max_hours;
158     const char *deviceOptions;
159     unsigned long Flags;
160     int ret; /* return value */
161 };
162
163
164 /* The function used for spawning threads. Extracts the mdrunner() 
165    arguments from its one argument and calls mdrunner(), after making
166    a commrec. */
167 static void mdrunner_start_fn(void *arg)
168 {
169     struct mdrunner_arglist *mda=(struct mdrunner_arglist*)arg;
170     struct mdrunner_arglist mc=*mda; /* copy the arg list to make sure 
171                                         that it's thread-local. This doesn't
172                                         copy pointed-to items, of course,
173                                         but those are all const. */
174     t_commrec *cr;  /* we need a local version of this */
175     FILE *fplog=NULL;
176     t_filenm *fnm;
177
178     fnm = dup_tfn(mc.nfile, mc.fnm);
179
180     cr = init_par_threads(mc.cr);
181
182     if (MASTER(cr))
183     {
184         fplog=mc.fplog;
185     }
186
187     mda->ret=mdrunner(mc.hw_opt, fplog, cr, mc.nfile, fnm, mc.oenv, 
188                       mc.bVerbose, mc.bCompact, mc.nstglobalcomm, 
189                       mc.ddxyz, mc.dd_node_order, mc.rdd,
190                       mc.rconstr, mc.dddlb_opt, mc.dlb_scale, 
191                       mc.ddcsx, mc.ddcsy, mc.ddcsz,
192                       mc.nbpu_opt,
193                       mc.nsteps_cmdline, mc.nstepout, mc.resetstep,
194                       mc.nmultisim, mc.repl_ex_nst, mc.repl_ex_nex, mc.repl_ex_seed, mc.pforce, 
195                       mc.cpt_period, mc.max_hours, mc.deviceOptions, mc.Flags);
196 }
197
198 /* called by mdrunner() to start a specific number of threads (including 
199    the main thread) for thread-parallel runs. This in turn calls mdrunner()
200    for each thread. 
201    All options besides nthreads are the same as for mdrunner(). */
202 static t_commrec *mdrunner_start_threads(gmx_hw_opt_t *hw_opt, 
203               FILE *fplog,t_commrec *cr,int nfile, 
204               const t_filenm fnm[], const output_env_t oenv, gmx_bool bVerbose,
205               gmx_bool bCompact, int nstglobalcomm,
206               ivec ddxyz,int dd_node_order,real rdd,real rconstr,
207               const char *dddlb_opt,real dlb_scale,
208               const char *ddcsx,const char *ddcsy,const char *ddcsz,
209               const char *nbpu_opt,
210               int nsteps_cmdline, int nstepout,int resetstep,
211               int nmultisim,int repl_ex_nst,int repl_ex_nex, int repl_ex_seed,
212               real pforce,real cpt_period, real max_hours, 
213               const char *deviceOptions, unsigned long Flags)
214 {
215     int ret;
216     struct mdrunner_arglist *mda;
217     t_commrec *crn; /* the new commrec */
218     t_filenm *fnmn;
219
220     /* first check whether we even need to start tMPI */
221     if (hw_opt->nthreads_tmpi < 2)
222     {
223         return cr;
224     }
225
226     /* a few small, one-time, almost unavoidable memory leaks: */
227     snew(mda,1);
228     fnmn=dup_tfn(nfile, fnm);
229
230     /* fill the data structure to pass as void pointer to thread start fn */
231     mda->hw_opt=hw_opt;
232     mda->fplog=fplog;
233     mda->cr=cr;
234     mda->nfile=nfile;
235     mda->fnm=fnmn;
236     mda->oenv=oenv;
237     mda->bVerbose=bVerbose;
238     mda->bCompact=bCompact;
239     mda->nstglobalcomm=nstglobalcomm;
240     mda->ddxyz[XX]=ddxyz[XX];
241     mda->ddxyz[YY]=ddxyz[YY];
242     mda->ddxyz[ZZ]=ddxyz[ZZ];
243     mda->dd_node_order=dd_node_order;
244     mda->rdd=rdd;
245     mda->rconstr=rconstr;
246     mda->dddlb_opt=dddlb_opt;
247     mda->dlb_scale=dlb_scale;
248     mda->ddcsx=ddcsx;
249     mda->ddcsy=ddcsy;
250     mda->ddcsz=ddcsz;
251     mda->nbpu_opt=nbpu_opt;
252     mda->nsteps_cmdline=nsteps_cmdline;
253     mda->nstepout=nstepout;
254     mda->resetstep=resetstep;
255     mda->nmultisim=nmultisim;
256     mda->repl_ex_nst=repl_ex_nst;
257     mda->repl_ex_nex=repl_ex_nex;
258     mda->repl_ex_seed=repl_ex_seed;
259     mda->pforce=pforce;
260     mda->cpt_period=cpt_period;
261     mda->max_hours=max_hours;
262     mda->deviceOptions=deviceOptions;
263     mda->Flags=Flags;
264
265     fprintf(stderr, "Starting %d tMPI threads\n",hw_opt->nthreads_tmpi);
266     fflush(stderr);
267     /* now spawn new threads that start mdrunner_start_fn(), while 
268        the main thread returns */
269     ret=tMPI_Init_fn(TRUE, hw_opt->nthreads_tmpi,
270                      mdrunner_start_fn, (void*)(mda) );
271     if (ret!=TMPI_SUCCESS)
272         return NULL;
273
274     /* make a new comm_rec to reflect the new situation */
275     crn=init_par_threads(cr);
276     return crn;
277 }
278
279
280 static int get_tmpi_omp_thread_distribution(const gmx_hw_opt_t *hw_opt,
281                                             int nthreads_tot,
282                                             int ngpu)
283 {
284     int nthreads_tmpi;
285
286     /* There are no separate PME nodes here, as we ensured in
287      * check_and_update_hw_opt that nthreads_tmpi>0 with PME nodes
288      * and a conditional ensures we would not have ended up here.
289      * Note that separate PME nodes might be switched on later.
290      */
291     if (ngpu > 0)
292     {
293         nthreads_tmpi = ngpu;
294         if (nthreads_tot > 0 && nthreads_tot < nthreads_tmpi)
295         {
296             nthreads_tmpi = nthreads_tot;
297         }
298     }
299     else if (hw_opt->nthreads_omp > 0)
300     {
301         if (hw_opt->nthreads_omp > nthreads_tot)
302         {
303             gmx_fatal(FARGS,"More OpenMP threads requested (%d) than the total number of threads requested (%d)",hw_opt->nthreads_omp,nthreads_tot);
304         }
305         nthreads_tmpi = nthreads_tot/hw_opt->nthreads_omp;
306     }
307     else
308     {
309         /* TODO choose nthreads_omp based on hardware topology
310            when we have a hardware topology detection library */
311         /* Don't use OpenMP parallelization */
312         nthreads_tmpi = nthreads_tot;
313     }
314
315     return nthreads_tmpi;
316 }
317
318
319 /* Get the number of threads to use for thread-MPI based on how many
320  * were requested, which algorithms we're using,
321  * and how many particles there are.
322  * At the point we have already called check_and_update_hw_opt.
323  * Thus all options should be internally consistent and consistent
324  * with the hardware, except that ntmpi could be larger than #GPU.
325  */
326 static int get_nthreads_mpi(gmx_hw_info_t *hwinfo,
327                             gmx_hw_opt_t *hw_opt,
328                             t_inputrec *inputrec, gmx_mtop_t *mtop,
329                             const t_commrec *cr,
330                             FILE *fplog)
331 {
332     int nthreads_tot_max,nthreads_tmpi,nthreads_new,ngpu;
333     int min_atoms_per_mpi_thread;
334     char *env;
335     char sbuf[STRLEN];
336     gmx_bool bCanUseGPU;
337
338     if (hw_opt->nthreads_tmpi > 0)
339     {
340         /* Trivial, return right away */
341         return hw_opt->nthreads_tmpi;
342     }
343
344     /* How many total (#tMPI*#OpenMP) threads can we start? */ 
345     if (hw_opt->nthreads_tot > 0)
346     {
347         nthreads_tot_max = hw_opt->nthreads_tot;
348     }
349     else
350     {
351         nthreads_tot_max = tMPI_Thread_get_hw_number();
352     }
353
354     bCanUseGPU = (inputrec->cutoff_scheme == ecutsVERLET && hwinfo->bCanUseGPU);
355     if (bCanUseGPU)
356     {
357         ngpu = hwinfo->gpu_info.ncuda_dev_use;
358     }
359     else
360     {
361         ngpu = 0;
362     }
363
364     nthreads_tmpi =
365         get_tmpi_omp_thread_distribution(hw_opt,nthreads_tot_max,ngpu);
366
367     if (inputrec->eI == eiNM || EI_TPI(inputrec->eI))
368     {
369         /* Steps are divided over the nodes iso splitting the atoms */
370         min_atoms_per_mpi_thread = 0;
371     }
372     else
373     {
374         if (bCanUseGPU)
375         {
376             min_atoms_per_mpi_thread = MIN_ATOMS_PER_GPU;
377         }
378         else
379         {
380             min_atoms_per_mpi_thread = MIN_ATOMS_PER_MPI_THREAD;
381         }
382     }
383
384     /* Check if an algorithm does not support parallel simulation.  */
385     if (nthreads_tmpi != 1 &&
386         ( inputrec->eI == eiLBFGS ||
387           inputrec->coulombtype == eelEWALD ) )
388     {
389         nthreads_tmpi = 1;
390
391         md_print_warn(cr,fplog,"The integration or electrostatics algorithm doesn't support parallel runs. Using a single thread-MPI thread.\n");
392         if (hw_opt->nthreads_tmpi > nthreads_tmpi)
393         {
394             gmx_fatal(FARGS,"You asked for more than 1 thread-MPI thread, but an algorithm doesn't support that");
395         }
396     }
397     else if (mtop->natoms/nthreads_tmpi < min_atoms_per_mpi_thread)
398     {
399         /* the thread number was chosen automatically, but there are too many
400            threads (too few atoms per thread) */
401         nthreads_new = max(1,mtop->natoms/min_atoms_per_mpi_thread);
402
403         if (nthreads_new > 8 || (nthreads_tmpi == 8 && nthreads_new > 4))
404         {
405             /* TODO replace this once we have proper HT detection
406              * Use only multiples of 4 above 8 threads
407              * or with an 8-core processor
408              * (to avoid 6 threads on 8 core processors with 4 real cores).
409              */
410             nthreads_new = (nthreads_new/4)*4;
411         }
412         else if (nthreads_new > 4)
413         {
414             /* Avoid 5 or 7 threads */
415             nthreads_new = (nthreads_new/2)*2;
416         }
417
418         nthreads_tmpi = nthreads_new;
419
420         fprintf(stderr,"\n");
421         fprintf(stderr,"NOTE: Parallelization is limited by the small number of atoms,\n");
422         fprintf(stderr,"      only starting %d thread-MPI threads.\n",nthreads_tmpi);
423         fprintf(stderr,"      You can use the -nt and/or -ntmpi option to optimize the number of threads.\n\n");
424     }
425
426     return nthreads_tmpi;
427 }
428 #endif /* GMX_THREAD_MPI */
429
430
431 /* Environment variable for setting nstlist */
432 static const char*  NSTLIST_ENVVAR          =  "GMX_NSTLIST";
433 /* Try to increase nstlist when using a GPU with nstlist less than this */
434 static const int    NSTLIST_GPU_ENOUGH      = 20;
435 /* Increase nstlist until the non-bonded cost increases more than this factor */
436 static const float  NBNXN_GPU_LIST_OK_FAC   = 1.25;
437 /* Don't increase nstlist beyond a non-bonded cost increases of this factor */
438 static const float  NBNXN_GPU_LIST_MAX_FAC  = 1.40;
439
440 /* Try to increase nstlist when running on a GPU */
441 static void increase_nstlist(FILE *fp,t_commrec *cr,
442                              t_inputrec *ir,const gmx_mtop_t *mtop,matrix box)
443 {
444     char *env;
445     int  nstlist_orig,nstlist_prev;
446     verletbuf_list_setup_t ls;
447     real rlist_inc,rlist_ok,rlist_max,rlist_new,rlist_prev;
448     int  i;
449     t_state state_tmp;
450     gmx_bool bBox,bDD,bCont;
451     const char *nstl_fmt="\nFor optimal performance with a GPU nstlist (now %d) should be larger.\nThe optimum depends on your CPU and GPU resources.\nYou might want to try several nstlist values.\n";
452     const char *vbd_err="Can not increase nstlist for GPU run because verlet-buffer-drift is not set or used";
453     const char *box_err="Can not increase nstlist for GPU run because the box is too small";
454     const char *dd_err ="Can not increase nstlist for GPU run because of domain decomposition limitations";
455     char buf[STRLEN];
456
457     /* Number of + nstlist alternative values to try when switching  */
458     const int nstl[]={ 20, 25, 40, 50 };
459 #define NNSTL  sizeof(nstl)/sizeof(nstl[0])
460
461     env = getenv(NSTLIST_ENVVAR);
462     if (env == NULL)
463     {
464         if (fp != NULL)
465         {
466             fprintf(fp,nstl_fmt,ir->nstlist);
467         }
468     }
469
470     if (ir->verletbuf_drift == 0)
471     {
472         gmx_fatal(FARGS,"You are using an old tpr file with a GPU, please generate a new tpr file with an up to date version of grompp");
473     }
474
475     if (ir->verletbuf_drift < 0)
476     {
477         if (MASTER(cr))
478         {
479             fprintf(stderr,"%s\n",vbd_err);
480         }
481         if (fp != NULL)
482         {
483             fprintf(fp,"%s\n",vbd_err);
484         }
485
486         return;
487     }
488
489     nstlist_orig = ir->nstlist;
490     if (env != NULL)
491     {
492         sprintf(buf,"Getting nstlist from environment variable GMX_NSTLIST=%s",env);
493         if (MASTER(cr))
494         {
495             fprintf(stderr,"%s\n",buf);
496         }
497         if (fp != NULL)
498         {
499             fprintf(fp,"%s\n",buf);
500         }
501         sscanf(env,"%d",&ir->nstlist);
502     }
503
504     verletbuf_get_list_setup(TRUE,&ls);
505
506     /* Allow rlist to make the list double the size of the cut-off sphere */
507     rlist_inc = nbnxn_get_rlist_effective_inc(NBNXN_GPU_CLUSTER_SIZE,mtop->natoms/det(box));
508     rlist_ok  = (max(ir->rvdw,ir->rcoulomb) + rlist_inc)*pow(NBNXN_GPU_LIST_OK_FAC,1.0/3.0) - rlist_inc;
509     rlist_max = (max(ir->rvdw,ir->rcoulomb) + rlist_inc)*pow(NBNXN_GPU_LIST_MAX_FAC,1.0/3.0) - rlist_inc;
510     if (debug)
511     {
512         fprintf(debug,"GPU nstlist tuning: rlist_inc %.3f rlist_max %.3f\n",
513                 rlist_inc,rlist_max);
514     }
515
516     i = 0;
517     nstlist_prev = nstlist_orig;
518     rlist_prev   = ir->rlist;
519     do
520     {
521         if (env == NULL)
522         {
523             ir->nstlist = nstl[i];
524         }
525
526         /* Set the pair-list buffer size in ir */
527         calc_verlet_buffer_size(mtop,det(box),ir,ir->verletbuf_drift,&ls,
528                                 NULL,&rlist_new);
529
530         /* Does rlist fit in the box? */
531         bBox = (sqr(rlist_new) < max_cutoff2(ir->ePBC,box));
532         bDD  = TRUE;
533         if (bBox && DOMAINDECOMP(cr))
534         {
535             /* Check if rlist fits in the domain decomposition */
536             if (inputrec2nboundeddim(ir) < DIM)
537             {
538                 gmx_incons("Changing nstlist with domain decomposition and unbounded dimensions is not implemented yet");
539             }
540             copy_mat(box,state_tmp.box);
541             bDD = change_dd_cutoff(cr,&state_tmp,ir,rlist_new);
542         }
543
544         bCont = FALSE;
545
546         if (env == NULL)
547         {
548             if (bBox && bDD && rlist_new <= rlist_max)
549             {
550                 /* Increase nstlist */
551                 nstlist_prev = ir->nstlist;
552                 rlist_prev   = rlist_new;
553                 bCont = (i+1 < NNSTL && rlist_new < rlist_ok);
554             }
555             else
556             {
557                 /* Stick with the previous nstlist */
558                 ir->nstlist = nstlist_prev;
559                 rlist_new   = rlist_prev;
560                 bBox = TRUE;
561                 bDD  = TRUE;
562             }
563         }
564
565         i++;
566     }
567     while (bCont);
568
569     if (!bBox || !bDD)
570     {
571         gmx_warning(!bBox ? box_err : dd_err);
572         if (fp != NULL)
573         {
574             fprintf(fp,"\n%s\n",bBox ? box_err : dd_err);
575         }
576         ir->nstlist = nstlist_orig;
577     }
578     else if (ir->nstlist != nstlist_orig || rlist_new != ir->rlist)
579     {
580         sprintf(buf,"Changing nstlist from %d to %d, rlist from %g to %g",
581                 nstlist_orig,ir->nstlist,
582                 ir->rlist,rlist_new);
583         if (MASTER(cr))
584         {
585             fprintf(stderr,"%s\n\n",buf);
586         }
587         if (fp != NULL)
588         {
589             fprintf(fp,"%s\n\n",buf);
590         }
591         ir->rlist     = rlist_new;
592         ir->rlistlong = rlist_new;
593     }
594 }
595
596 static void prepare_verlet_scheme(FILE *fplog,
597                                   gmx_hw_info_t *hwinfo,
598                                   t_commrec *cr,
599                                   gmx_hw_opt_t *hw_opt,
600                                   const char *nbpu_opt,
601                                   t_inputrec *ir,
602                                   const gmx_mtop_t *mtop,
603                                   matrix box,
604                                   gmx_bool *bUseGPU)
605 {
606     /* Here we only check for GPU usage on the MPI master process,
607      * as here we don't know how many GPUs we will use yet.
608      * We check for a GPU on all processes later.
609      */
610     *bUseGPU = hwinfo->bCanUseGPU || (getenv("GMX_EMULATE_GPU") != NULL);
611
612     if (ir->verletbuf_drift > 0)
613     {
614         /* Update the Verlet buffer size for the current run setup */
615         verletbuf_list_setup_t ls;
616         real rlist_new;
617
618         /* Here we assume CPU acceleration is on. But as currently
619          * calc_verlet_buffer_size gives the same results for 4x8 and 4x4
620          * and 4x2 gives a larger buffer than 4x4, this is ok.
621          */
622         verletbuf_get_list_setup(*bUseGPU,&ls);
623
624         calc_verlet_buffer_size(mtop,det(box),ir,
625                                 ir->verletbuf_drift,&ls,
626                                 NULL,&rlist_new);
627         if (rlist_new != ir->rlist)
628         {
629             if (fplog != NULL)
630             {
631                 fprintf(fplog,"\nChanging rlist from %g to %g for non-bonded %dx%d atom kernels\n\n",
632                         ir->rlist,rlist_new,
633                         ls.cluster_size_i,ls.cluster_size_j);
634             }
635             ir->rlist     = rlist_new;
636             ir->rlistlong = rlist_new;
637         }
638     }
639
640     /* With GPU or emulation we should check nstlist for performance */
641     if ((EI_DYNAMICS(ir->eI) &&
642          *bUseGPU &&
643          ir->nstlist < NSTLIST_GPU_ENOUGH) ||
644         getenv(NSTLIST_ENVVAR) != NULL)
645     {
646         /* Choose a better nstlist */
647         increase_nstlist(fplog,cr,ir,mtop,box);
648     }
649 }
650
651 static void convert_to_verlet_scheme(FILE *fplog,
652                                      t_inputrec *ir,
653                                      gmx_mtop_t *mtop,real box_vol)
654 {
655     char *conv_mesg="Converting input file with group cut-off scheme to the Verlet cut-off scheme";
656
657     md_print_warn(NULL,fplog,"%s\n",conv_mesg);
658
659     ir->cutoff_scheme   = ecutsVERLET;
660     ir->verletbuf_drift = 0.005;
661
662     if (ir->rcoulomb != ir->rvdw)
663     {
664         gmx_fatal(FARGS,"The VdW and Coulomb cut-offs are different, whereas the Verlet scheme only supports equal cut-offs");
665     }
666
667     if (ir->vdwtype == evdwUSER || EEL_USER(ir->coulombtype))
668     {
669         gmx_fatal(FARGS,"User non-bonded potentials are not (yet) supported with the Verlet scheme");
670     }
671     else if (EVDW_SWITCHED(ir->vdwtype) || EEL_SWITCHED(ir->coulombtype))
672     {
673         md_print_warn(NULL,fplog,"Converting switched or shifted interactions to a shifted potential (without force shift), this will lead to slightly different interaction potentials");
674
675         if (EVDW_SWITCHED(ir->vdwtype))
676         {
677             ir->vdwtype = evdwCUT;
678         }
679         if (EEL_SWITCHED(ir->coulombtype))
680         {
681             if (EEL_FULL(ir->coulombtype))
682             {
683                 /* With full electrostatic only PME can be switched */
684                 ir->coulombtype = eelPME;
685             }
686             else
687             {
688                 md_print_warn(NULL,fplog,"NOTE: Replacing %s electrostatics with reaction-field with epsilon-rf=inf\n",eel_names[ir->coulombtype]);
689                 ir->coulombtype = eelRF;
690                 ir->epsilon_rf  = 0.0;
691             }
692         }
693
694         /* We set the target energy drift to a small number.
695          * Note that this is only for testing. For production the user
696          * should think about this and set the mdp options.
697          */
698         ir->verletbuf_drift = 1e-4;
699     }
700
701     if (inputrec2nboundeddim(ir) != 3)
702     {
703         gmx_fatal(FARGS,"Can only convert old tpr files to the Verlet cut-off scheme with 3D pbc");
704     }
705
706     if (ir->efep != efepNO || ir->implicit_solvent != eisNO)
707     {
708         gmx_fatal(FARGS,"Will not convert old tpr files to the Verlet cut-off scheme with free-energy calculations or implicit solvent");
709     }
710
711     if (EI_DYNAMICS(ir->eI) && !(EI_MD(ir->eI) && ir->etc == etcNO))
712     {
713         verletbuf_list_setup_t ls;
714
715         verletbuf_get_list_setup(FALSE,&ls);
716         calc_verlet_buffer_size(mtop,box_vol,ir,ir->verletbuf_drift,&ls,
717                                 NULL,&ir->rlist);
718     }
719     else
720     {
721         ir->verletbuf_drift = -1;
722         ir->rlist           = 1.05*max(ir->rvdw,ir->rcoulomb);
723     }
724
725     gmx_mtop_remove_chargegroups(mtop);
726 }
727
728
729 /* Set CPU affinity. Can be important for performance.
730    On some systems (e.g. Cray) CPU Affinity is set by default.
731    But default assigning doesn't work (well) with only some ranks
732    having threads. This causes very low performance.
733    External tools have cumbersome syntax for setting affinity
734    in the case that only some ranks have threads.
735    Thus it is important that GROMACS sets the affinity internally
736    if only PME is using threads.
737 */
738 static void set_cpu_affinity(FILE *fplog,
739                              const t_commrec *cr,
740                              const gmx_hw_opt_t *hw_opt,
741                              int nthreads_pme,
742                              const gmx_hw_info_t *hwinfo,
743                              const t_inputrec *inputrec)
744 {
745 #ifdef GMX_OPENMP /* TODO: actually we could do this even without OpenMP?! */
746 #ifdef __linux /* TODO: only linux? why not everywhere if sched_setaffinity is available */
747     if (hw_opt->bThreadPinning)
748     {
749         int thread, nthread_local, nthread_node, nthread_hw_max, nphyscore;
750         int offset;
751         char *env;
752
753         /* threads on this MPI process or TMPI thread */
754         if (cr->duty & DUTY_PP)
755         {
756             nthread_local = gmx_omp_nthreads_get(emntNonbonded);
757         }
758         else
759         {
760             nthread_local = gmx_omp_nthreads_get(emntPME);
761         }
762
763         /* map the current process to cores */
764         thread = 0;
765         nthread_node = nthread_local;
766 #ifdef GMX_MPI
767         if (PAR(cr) || MULTISIM(cr))
768         {
769             /* We need to determine a scan of the thread counts in this
770              * compute node.
771              */
772             MPI_Comm comm_intra;
773
774             MPI_Comm_split(MPI_COMM_WORLD,gmx_hostname_num(),cr->nodeid_intra,
775                            &comm_intra);
776             MPI_Scan(&nthread_local,&thread,1,MPI_INT,MPI_SUM,comm_intra);
777             /* MPI_Scan is inclusive, but here we need exclusive */
778             thread -= nthread_local;
779             /* Get the total number of threads on this physical node */
780             MPI_Allreduce(&nthread_local,&nthread_node,1,MPI_INT,MPI_SUM,comm_intra);
781             MPI_Comm_free(&comm_intra);
782         }
783 #endif
784
785         offset = 0;
786         if (hw_opt->core_pinning_offset > 0)
787         {
788             offset = hw_opt->core_pinning_offset;
789             if (SIMMASTER(cr))
790             {
791                 fprintf(stderr, "Applying core pinning offset %d\n", offset);
792             }
793             if (fplog)
794             {
795                 fprintf(fplog, "Applying core pinning offset %d\n", offset);
796             }
797         }
798
799         /* With Intel Hyper-Threading enabled, we want to pin consecutive
800          * threads to physical cores when using more threads than physical
801          * cores or when the user requests so.
802          */
803         nthread_hw_max = hwinfo->nthreads_hw_avail;
804         nphyscore = -1;
805         if (hw_opt->bPinHyperthreading ||
806             (gmx_cpuid_x86_smt(hwinfo->cpuid_info) == GMX_CPUID_X86_SMT_ENABLED &&
807              nthread_node > nthread_hw_max/2 && getenv("GMX_DISABLE_PINHT") == NULL))
808         {
809             if (gmx_cpuid_x86_smt(hwinfo->cpuid_info) != GMX_CPUID_X86_SMT_ENABLED)
810             {
811                 /* We print to stderr on all processes, as we might have
812                  * different settings on different physical nodes.
813                  */
814                 if (gmx_cpuid_vendor(hwinfo->cpuid_info) != GMX_CPUID_VENDOR_INTEL)
815                 {
816                     md_print_warn(NULL, fplog, "Pinning for Hyper-Threading layout requested, "
817                                   "but non-Intel CPU detected (vendor: %s)\n",
818                                   gmx_cpuid_vendor_string[gmx_cpuid_vendor(hwinfo->cpuid_info)]);
819                 }
820                 else
821                 {
822                     md_print_warn(NULL, fplog, "Pinning for Hyper-Threading layout requested, "
823                                   "but the CPU detected does not have Intel Hyper-Threading support "
824                                   "(or it is turned off)\n");
825                 }
826             }
827             nphyscore = nthread_hw_max/2;
828
829             if (SIMMASTER(cr))
830             {
831                 fprintf(stderr, "Pinning to Hyper-Threading cores with %d physical cores in a compute node\n",
832                         nphyscore);
833             }
834             if (fplog)
835             {
836                 fprintf(fplog, "Pinning to Hyper-Threading cores with %d physical cores in a compute node\n",
837                         nphyscore);
838             }
839         }
840
841         /* set the per-thread affinity */
842 #pragma omp parallel firstprivate(thread) num_threads(nthread_local)
843         {
844             cpu_set_t mask;
845             int core;
846
847             CPU_ZERO(&mask);
848             thread += gmx_omp_get_thread_num();
849             if (nphyscore <= 0)
850             {
851                 core = offset + thread;
852             }
853             else
854             {
855                 /* Lock pairs of threads to the same hyperthreaded core */
856                 core = offset + thread/2 + (thread % 2)*nphyscore;
857             }
858             CPU_SET(core, &mask);
859             sched_setaffinity((pid_t) syscall (SYS_gettid), sizeof(cpu_set_t), &mask);
860         }
861     }
862 #endif /* __linux    */
863 #endif /* GMX_OPENMP */
864 }
865
866
867 static void check_and_update_hw_opt(gmx_hw_opt_t *hw_opt,
868                                     int cutoff_scheme)
869 {
870     gmx_omp_nthreads_read_env(&hw_opt->nthreads_omp);
871
872 #ifndef GMX_THREAD_MPI
873     if (hw_opt->nthreads_tot > 0)
874     {
875         gmx_fatal(FARGS,"Setting the total number of threads is only supported with thread-MPI and Gromacs was compiled without thread-MPI");
876     }
877     if (hw_opt->nthreads_tmpi > 0)
878     {
879         gmx_fatal(FARGS,"Setting the number of thread-MPI threads is only supported with thread-MPI and Gromacs was compiled without thread-MPI");
880     }
881 #endif
882
883     if (hw_opt->nthreads_tot > 0 && hw_opt->nthreads_omp_pme <= 0)
884     {
885         /* We have the same number of OpenMP threads for PP and PME processes,
886          * thus we can perform several consistency checks.
887          */
888         if (hw_opt->nthreads_tmpi > 0 &&
889             hw_opt->nthreads_omp > 0 &&
890             hw_opt->nthreads_tot != hw_opt->nthreads_tmpi*hw_opt->nthreads_omp)
891         {
892             gmx_fatal(FARGS,"The total number of threads requested (%d) does not match the thread-MPI threads (%d) times the OpenMP threads (%d) requested",
893                       hw_opt->nthreads_tot,hw_opt->nthreads_tmpi,hw_opt->nthreads_omp);
894         }
895
896         if (hw_opt->nthreads_tmpi > 0 &&
897             hw_opt->nthreads_tot % hw_opt->nthreads_tmpi != 0)
898         {
899             gmx_fatal(FARGS,"The total number of threads requested (%d) is not divisible by the number of thread-MPI threads requested (%d)",
900                       hw_opt->nthreads_tot,hw_opt->nthreads_tmpi);
901         }
902
903         if (hw_opt->nthreads_omp > 0 &&
904             hw_opt->nthreads_tot % hw_opt->nthreads_omp != 0)
905         {
906             gmx_fatal(FARGS,"The total number of threads requested (%d) is not divisible by the number of OpenMP threads requested (%d)",
907                       hw_opt->nthreads_tot,hw_opt->nthreads_omp);
908         }
909
910         if (hw_opt->nthreads_tmpi > 0 &&
911             hw_opt->nthreads_omp <= 0)
912         {
913             hw_opt->nthreads_omp = hw_opt->nthreads_tot/hw_opt->nthreads_tmpi;
914         }
915     }
916
917 #ifndef GMX_OPENMP
918     if (hw_opt->nthreads_omp > 1)
919     {
920         gmx_fatal(FARGS,"OpenMP threads are requested, but Gromacs was compiled without OpenMP support");
921     }
922 #endif
923
924     if (cutoff_scheme == ecutsGROUP)
925     {
926         /* We only have OpenMP support for PME only nodes */
927         if (hw_opt->nthreads_omp > 1)
928         {
929             gmx_fatal(FARGS,"OpenMP threads have been requested with cut-off scheme %s, but these are only supported with cut-off scheme %s",
930                       ecutscheme_names[cutoff_scheme],
931                       ecutscheme_names[ecutsVERLET]);
932         }
933         hw_opt->nthreads_omp = 1;
934     }
935
936     if (hw_opt->nthreads_omp_pme > 0 && hw_opt->nthreads_omp <= 0)
937     {
938         gmx_fatal(FARGS,"You need to specify -ntomp in addition to -ntomp_pme");
939     }
940
941     if (hw_opt->nthreads_tot == 1)
942     {
943         hw_opt->nthreads_tmpi = 1;
944
945         if (hw_opt->nthreads_omp > 1)
946         {
947             gmx_fatal(FARGS,"You requested %d OpenMP threads with %d total threads",
948                       hw_opt->nthreads_tmpi,hw_opt->nthreads_tot);
949         }
950         hw_opt->nthreads_omp = 1;
951     }
952
953     if (hw_opt->nthreads_omp_pme <= 0 && hw_opt->nthreads_omp > 0)
954     {
955         hw_opt->nthreads_omp_pme = hw_opt->nthreads_omp;
956     }
957
958     if (debug)
959     {
960         fprintf(debug,"hw_opt: nt %d ntmpi %d ntomp %d ntomp_pme %d gpu_id '%s'\n",
961                 hw_opt->nthreads_tot,
962                 hw_opt->nthreads_tmpi,
963                 hw_opt->nthreads_omp,
964                 hw_opt->nthreads_omp_pme,
965                 hw_opt->gpu_id!=NULL ? hw_opt->gpu_id : "");
966                 
967     }
968 }
969
970
971 /* Override the value in inputrec with value passed on the command line (if any) */
972 static void override_nsteps_cmdline(FILE *fplog,
973                                     int nsteps_cmdline,
974                                     t_inputrec *ir,
975                                     const t_commrec *cr)
976 {
977     assert(ir);
978     assert(cr);
979
980     /* override with anything else than the default -2 */
981     if (nsteps_cmdline > -2)
982     {
983         char stmp[STRLEN];
984
985         ir->nsteps = nsteps_cmdline;
986         if (EI_DYNAMICS(ir->eI))
987         {
988             sprintf(stmp, "Overriding nsteps with value passed on the command line: %d steps, %.3f ps",
989                     nsteps_cmdline, nsteps_cmdline*ir->delta_t);
990         }
991         else
992         {
993             sprintf(stmp, "Overriding nsteps with value passed on the command line: %d steps",
994                     nsteps_cmdline);
995         }
996
997         md_print_warn(cr, fplog, "%s\n", stmp);
998     }
999 }
1000
1001 /* Data structure set by SIMMASTER which needs to be passed to all nodes
1002  * before the other nodes have read the tpx file and called gmx_detect_hardware.
1003  */
1004 typedef struct {
1005     int      cutoff_scheme; /* The cutoff-scheme from inputrec_t */
1006     gmx_bool bUseGPU;       /* Use GPU or GPU emulation          */
1007 } master_inf_t;
1008
1009 int mdrunner(gmx_hw_opt_t *hw_opt,
1010              FILE *fplog,t_commrec *cr,int nfile,
1011              const t_filenm fnm[], const output_env_t oenv, gmx_bool bVerbose,
1012              gmx_bool bCompact, int nstglobalcomm,
1013              ivec ddxyz,int dd_node_order,real rdd,real rconstr,
1014              const char *dddlb_opt,real dlb_scale,
1015              const char *ddcsx,const char *ddcsy,const char *ddcsz,
1016              const char *nbpu_opt,
1017              int nsteps_cmdline, int nstepout,int resetstep,
1018              int nmultisim,int repl_ex_nst,int repl_ex_nex,
1019              int repl_ex_seed, real pforce,real cpt_period,real max_hours,
1020              const char *deviceOptions, unsigned long Flags)
1021 {
1022     gmx_bool   bForceUseGPU,bTryUseGPU;
1023     double     nodetime=0,realtime;
1024     t_inputrec *inputrec;
1025     t_state    *state=NULL;
1026     matrix     box;
1027     gmx_ddbox_t ddbox={0};
1028     int        npme_major,npme_minor;
1029     real       tmpr1,tmpr2;
1030     t_nrnb     *nrnb;
1031     gmx_mtop_t *mtop=NULL;
1032     t_mdatoms  *mdatoms=NULL;
1033     t_forcerec *fr=NULL;
1034     t_fcdata   *fcd=NULL;
1035     real       ewaldcoeff=0;
1036     gmx_pme_t  *pmedata=NULL;
1037     gmx_vsite_t *vsite=NULL;
1038     gmx_constr_t constr;
1039     int        i,m,nChargePerturbed=-1,status,nalloc;
1040     char       *gro;
1041     gmx_wallcycle_t wcycle;
1042     gmx_bool       bReadRNG,bReadEkin;
1043     int        list;
1044     gmx_runtime_t runtime;
1045     int        rc;
1046     gmx_large_int_t reset_counters;
1047     gmx_edsam_t ed=NULL;
1048     t_commrec   *cr_old=cr; 
1049     int         nthreads_pme=1;
1050     int         nthreads_pp=1;
1051     gmx_membed_t membed=NULL;
1052     gmx_hw_info_t *hwinfo=NULL;
1053     master_inf_t minf={-1,FALSE};
1054
1055     /* CAUTION: threads may be started later on in this function, so
1056        cr doesn't reflect the final parallel state right now */
1057     snew(inputrec,1);
1058     snew(mtop,1);
1059     
1060     if (Flags & MD_APPENDFILES) 
1061     {
1062         fplog = NULL;
1063     }
1064
1065     bForceUseGPU = (strncmp(nbpu_opt, "gpu", 3) == 0);
1066     bTryUseGPU   = (strncmp(nbpu_opt, "auto", 4) == 0) || bForceUseGPU;
1067
1068     snew(state,1);
1069     if (SIMMASTER(cr)) 
1070     {
1071         /* Read (nearly) all data required for the simulation */
1072         read_tpx_state(ftp2fn(efTPX,nfile,fnm),inputrec,state,NULL,mtop);
1073
1074         if (inputrec->cutoff_scheme != ecutsVERLET &&
1075             ((Flags & MD_TESTVERLET) || getenv("GMX_VERLET_SCHEME") != NULL))
1076         {
1077             convert_to_verlet_scheme(fplog,inputrec,mtop,det(state->box));
1078         }
1079
1080         /* Detect hardware, gather information. With tMPI only thread 0 does it
1081          * and after threads are started broadcasts hwinfo around. */
1082         snew(hwinfo, 1);
1083         gmx_detect_hardware(fplog, hwinfo, cr,
1084                             bForceUseGPU, bTryUseGPU, hw_opt->gpu_id);
1085
1086         minf.cutoff_scheme = inputrec->cutoff_scheme;
1087         minf.bUseGPU       = FALSE;
1088
1089         if (inputrec->cutoff_scheme == ecutsVERLET)
1090         {
1091             prepare_verlet_scheme(fplog,hwinfo,cr,hw_opt,nbpu_opt,
1092                                   inputrec,mtop,state->box,
1093                                   &minf.bUseGPU);
1094         }
1095         else if (hwinfo->bCanUseGPU)
1096         {
1097             md_print_warn(cr,fplog,
1098                           "NOTE: GPU(s) found, but the current simulation can not use GPUs\n"
1099                           "      To use a GPU, set the mdp option: cutoff-scheme = Verlet\n"
1100                           "      (for quick performance testing you can use the -testverlet option)\n");
1101
1102             if (bForceUseGPU)
1103             {
1104                 gmx_fatal(FARGS,"GPU requested, but can't be used without cutoff-scheme=Verlet");
1105             }
1106         }
1107     }
1108 #ifndef GMX_THREAD_MPI
1109     if (PAR(cr))
1110     {
1111         gmx_bcast_sim(sizeof(minf),&minf,cr);
1112     }
1113 #endif
1114     if (minf.bUseGPU && cr->npmenodes == -1)
1115     {
1116         /* Don't automatically use PME-only nodes with GPUs */
1117         cr->npmenodes = 0;
1118     }
1119
1120 #ifdef GMX_THREAD_MPI
1121     /* With thread-MPI inputrec is only set here on the master thread */
1122     if (SIMMASTER(cr))
1123 #endif
1124     {
1125         check_and_update_hw_opt(hw_opt,minf.cutoff_scheme);
1126
1127 #ifdef GMX_THREAD_MPI
1128         if (cr->npmenodes > 0 && hw_opt->nthreads_tmpi <= 0)
1129         {
1130             gmx_fatal(FARGS,"You need to explicitly specify the number of MPI threads (-ntmpi) when using separate PME nodes");
1131         }
1132 #endif
1133
1134         if (hw_opt->nthreads_omp_pme != hw_opt->nthreads_omp &&
1135             cr->npmenodes <= 0)
1136         {
1137             gmx_fatal(FARGS,"You need to explicitly specify the number of PME nodes (-npme) when using different number of OpenMP threads for PP and PME nodes");
1138         }
1139     }
1140
1141 #ifdef GMX_THREAD_MPI
1142     if (SIMMASTER(cr))
1143     {
1144         /* NOW the threads will be started: */
1145         hw_opt->nthreads_tmpi = get_nthreads_mpi(hwinfo,
1146                                                  hw_opt,
1147                                                  inputrec, mtop,
1148                                                  cr, fplog);
1149         if (hw_opt->nthreads_tot > 0 && hw_opt->nthreads_omp <= 0)
1150         {
1151             hw_opt->nthreads_omp = hw_opt->nthreads_tot/hw_opt->nthreads_tmpi;
1152         }
1153
1154         if (hw_opt->nthreads_tmpi > 1)
1155         {
1156             /* now start the threads. */
1157             cr=mdrunner_start_threads(hw_opt, fplog, cr_old, nfile, fnm, 
1158                                       oenv, bVerbose, bCompact, nstglobalcomm, 
1159                                       ddxyz, dd_node_order, rdd, rconstr, 
1160                                       dddlb_opt, dlb_scale, ddcsx, ddcsy, ddcsz,
1161                                       nbpu_opt,
1162                                       nsteps_cmdline, nstepout, resetstep, nmultisim, 
1163                                       repl_ex_nst, repl_ex_nex, repl_ex_seed, pforce,
1164                                       cpt_period, max_hours, deviceOptions, 
1165                                       Flags);
1166             /* the main thread continues here with a new cr. We don't deallocate
1167                the old cr because other threads may still be reading it. */
1168             if (cr == NULL)
1169             {
1170                 gmx_comm("Failed to spawn threads");
1171             }
1172         }
1173     }
1174 #endif
1175     /* END OF CAUTION: cr is now reliable */
1176
1177     /* g_membed initialisation *
1178      * Because we change the mtop, init_membed is called before the init_parallel *
1179      * (in case we ever want to make it run in parallel) */
1180     if (opt2bSet("-membed",nfile,fnm))
1181     {
1182         if (MASTER(cr))
1183         {
1184             fprintf(stderr,"Initializing membed");
1185         }
1186         membed = init_membed(fplog,nfile,fnm,mtop,inputrec,state,cr,&cpt_period);
1187     }
1188
1189     if (PAR(cr))
1190     {
1191         /* now broadcast everything to the non-master nodes/threads: */
1192         init_parallel(fplog, cr, inputrec, mtop);
1193
1194         /* This check needs to happen after get_nthreads_mpi() */
1195         if (inputrec->cutoff_scheme == ecutsVERLET && (Flags & MD_PARTDEC))
1196         {
1197             gmx_fatal_collective(FARGS,cr,NULL,
1198                                  "The Verlet cut-off scheme is not supported with particle decomposition.\n"
1199                                  "You can achieve the same effect as particle decomposition by running in parallel using only OpenMP threads.");
1200         }
1201     }
1202     if (fplog != NULL)
1203     {
1204         pr_inputrec(fplog,0,"Input Parameters",inputrec,FALSE);
1205     }
1206
1207 #if defined GMX_THREAD_MPI
1208     /* With tMPI we detected on thread 0 and we'll just pass the hwinfo pointer
1209      * to the other threads  -- slightly uncool, but works fine, just need to
1210      * make sure that the data doesn't get freed twice. */
1211     if (cr->nnodes > 1)
1212     {
1213         if (!SIMMASTER(cr))
1214         {
1215             snew(hwinfo, 1);
1216         }
1217         gmx_bcast(sizeof(&hwinfo), &hwinfo, cr);
1218     }
1219 #else
1220     if (PAR(cr) && !SIMMASTER(cr))
1221     {
1222         /* now we have inputrec on all nodes, can run the detection */
1223         /* TODO: perhaps it's better to propagate within a node instead? */
1224         snew(hwinfo, 1);
1225         gmx_detect_hardware(fplog, hwinfo, cr,
1226                                  bForceUseGPU, bTryUseGPU, hw_opt->gpu_id);
1227     }
1228 #endif
1229
1230     /* now make sure the state is initialized and propagated */
1231     set_state_entries(state,inputrec,cr->nnodes);
1232
1233     /* remove when vv and rerun works correctly! */
1234     if (PAR(cr) && EI_VV(inputrec->eI) && ((Flags & MD_RERUN) || (Flags & MD_RERUN_VSITE)))
1235     {
1236         gmx_fatal(FARGS,
1237                   "Currently can't do velocity verlet with rerun in parallel.");
1238     }
1239
1240     /* A parallel command line option consistency check that we can
1241        only do after any threads have started. */
1242     if (!PAR(cr) &&
1243         (ddxyz[XX] > 1 || ddxyz[YY] > 1 || ddxyz[ZZ] > 1 || cr->npmenodes > 0))
1244     {
1245         gmx_fatal(FARGS,
1246                   "The -dd or -npme option request a parallel simulation, "
1247 #ifndef GMX_MPI
1248                   "but %s was compiled without threads or MPI enabled"
1249 #else
1250 #ifdef GMX_THREAD_MPI
1251                   "but the number of threads (option -nt) is 1"
1252 #else
1253                   "but %s was not started through mpirun/mpiexec or only one process was requested through mpirun/mpiexec"
1254 #endif
1255 #endif
1256                   , ShortProgram()
1257             );
1258     }
1259
1260     if ((Flags & MD_RERUN) &&
1261         (EI_ENERGY_MINIMIZATION(inputrec->eI) || eiNM == inputrec->eI))
1262     {
1263         gmx_fatal(FARGS, "The .mdp file specified an energy mininization or normal mode algorithm, and these are not compatible with mdrun -rerun");
1264     }
1265
1266     if (can_use_allvsall(inputrec,mtop,TRUE,cr,fplog) && PAR(cr))
1267     {
1268         /* All-vs-all loops do not work with domain decomposition */
1269         Flags |= MD_PARTDEC;
1270     }
1271
1272     if (!EEL_PME(inputrec->coulombtype) || (Flags & MD_PARTDEC))
1273     {
1274         if (cr->npmenodes > 0)
1275         {
1276             if (!EEL_PME(inputrec->coulombtype))
1277             {
1278                 gmx_fatal_collective(FARGS,cr,NULL,
1279                                      "PME nodes are requested, but the system does not use PME electrostatics");
1280             }
1281             if (Flags & MD_PARTDEC)
1282             {
1283                 gmx_fatal_collective(FARGS,cr,NULL,
1284                                      "PME nodes are requested, but particle decomposition does not support separate PME nodes");
1285             }
1286         }
1287
1288         cr->npmenodes = 0;
1289     }
1290
1291 #ifdef GMX_FAHCORE
1292     fcRegisterSteps(inputrec->nsteps,inputrec->init_step);
1293 #endif
1294
1295     /* NMR restraints must be initialized before load_checkpoint,
1296      * since with time averaging the history is added to t_state.
1297      * For proper consistency check we therefore need to extend
1298      * t_state here.
1299      * So the PME-only nodes (if present) will also initialize
1300      * the distance restraints.
1301      */
1302     snew(fcd,1);
1303
1304     /* This needs to be called before read_checkpoint to extend the state */
1305     init_disres(fplog,mtop,inputrec,cr,Flags & MD_PARTDEC,fcd,state);
1306
1307     if (gmx_mtop_ftype_count(mtop,F_ORIRES) > 0)
1308     {
1309         if (PAR(cr) && !(Flags & MD_PARTDEC))
1310         {
1311             gmx_fatal(FARGS,"Orientation restraints do not work (yet) with domain decomposition, use particle decomposition (mdrun option -pd)");
1312         }
1313         /* Orientation restraints */
1314         if (MASTER(cr))
1315         {
1316             init_orires(fplog,mtop,state->x,inputrec,cr->ms,&(fcd->orires),
1317                         state);
1318         }
1319     }
1320
1321     if (DEFORM(*inputrec))
1322     {
1323         /* Store the deform reference box before reading the checkpoint */
1324         if (SIMMASTER(cr))
1325         {
1326             copy_mat(state->box,box);
1327         }
1328         if (PAR(cr))
1329         {
1330             gmx_bcast(sizeof(box),box,cr);
1331         }
1332         /* Because we do not have the update struct available yet
1333          * in which the reference values should be stored,
1334          * we store them temporarily in static variables.
1335          * This should be thread safe, since they are only written once
1336          * and with identical values.
1337          */
1338 #ifdef GMX_THREAD_MPI
1339         tMPI_Thread_mutex_lock(&deform_init_box_mutex);
1340 #endif
1341         deform_init_init_step_tpx = inputrec->init_step;
1342         copy_mat(box,deform_init_box_tpx);
1343 #ifdef GMX_THREAD_MPI
1344         tMPI_Thread_mutex_unlock(&deform_init_box_mutex);
1345 #endif
1346     }
1347
1348     if (opt2bSet("-cpi",nfile,fnm)) 
1349     {
1350         /* Check if checkpoint file exists before doing continuation.
1351          * This way we can use identical input options for the first and subsequent runs...
1352          */
1353         if( gmx_fexist_master(opt2fn_master("-cpi",nfile,fnm,cr),cr) )
1354         {
1355             load_checkpoint(opt2fn_master("-cpi",nfile,fnm,cr),&fplog,
1356                             cr,Flags & MD_PARTDEC,ddxyz,
1357                             inputrec,state,&bReadRNG,&bReadEkin,
1358                             (Flags & MD_APPENDFILES),
1359                             (Flags & MD_APPENDFILESSET));
1360             
1361             if (bReadRNG)
1362             {
1363                 Flags |= MD_READ_RNG;
1364             }
1365             if (bReadEkin)
1366             {
1367                 Flags |= MD_READ_EKIN;
1368             }
1369         }
1370     }
1371
1372     if (((MASTER(cr) || (Flags & MD_SEPPOT)) && (Flags & MD_APPENDFILES))
1373 #ifdef GMX_THREAD_MPI
1374         /* With thread MPI only the master node/thread exists in mdrun.c,
1375          * therefore non-master nodes need to open the "seppot" log file here.
1376          */
1377         || (!MASTER(cr) && (Flags & MD_SEPPOT))
1378 #endif
1379         )
1380     {
1381         gmx_log_open(ftp2fn(efLOG,nfile,fnm),cr,!(Flags & MD_SEPPOT),
1382                              Flags,&fplog);
1383     }
1384
1385     /* override nsteps with value from cmdline */
1386     override_nsteps_cmdline(fplog, nsteps_cmdline, inputrec, cr);
1387
1388     if (SIMMASTER(cr)) 
1389     {
1390         copy_mat(state->box,box);
1391     }
1392
1393     if (PAR(cr)) 
1394     {
1395         gmx_bcast(sizeof(box),box,cr);
1396     }
1397
1398     /* Essential dynamics */
1399     if (opt2bSet("-ei",nfile,fnm))
1400     {
1401         /* Open input and output files, allocate space for ED data structure */
1402         ed = ed_open(nfile,fnm,Flags,cr);
1403     }
1404
1405     if (PAR(cr) && !((Flags & MD_PARTDEC) ||
1406                      EI_TPI(inputrec->eI) ||
1407                      inputrec->eI == eiNM))
1408     {
1409         cr->dd = init_domain_decomposition(fplog,cr,Flags,ddxyz,rdd,rconstr,
1410                                            dddlb_opt,dlb_scale,
1411                                            ddcsx,ddcsy,ddcsz,
1412                                            mtop,inputrec,
1413                                            box,state->x,
1414                                            &ddbox,&npme_major,&npme_minor);
1415
1416         make_dd_communicators(fplog,cr,dd_node_order);
1417
1418         /* Set overallocation to avoid frequent reallocation of arrays */
1419         set_over_alloc_dd(TRUE);
1420     }
1421     else
1422     {
1423         /* PME, if used, is done on all nodes with 1D decomposition */
1424         cr->npmenodes = 0;
1425         cr->duty = (DUTY_PP | DUTY_PME);
1426         npme_major = 1;
1427         npme_minor = 1;
1428         if (!EI_TPI(inputrec->eI))
1429         {
1430             npme_major = cr->nnodes;
1431         }
1432         
1433         if (inputrec->ePBC == epbcSCREW)
1434         {
1435             gmx_fatal(FARGS,
1436                       "pbc=%s is only implemented with domain decomposition",
1437                       epbc_names[inputrec->ePBC]);
1438         }
1439     }
1440
1441     if (PAR(cr))
1442     {
1443         /* After possible communicator splitting in make_dd_communicators.
1444          * we can set up the intra/inter node communication.
1445          */
1446         gmx_setup_nodecomm(fplog,cr);
1447     }
1448
1449     /* Initialize per-node process ID and counters. */
1450     gmx_init_intra_counters(cr);
1451
1452 #ifdef GMX_MPI
1453     md_print_info(cr,fplog,"Using %d MPI %s\n",
1454                   cr->nnodes,
1455 #ifdef GMX_THREAD_MPI
1456                   cr->nnodes==1 ? "thread" : "threads"
1457 #else
1458                   cr->nnodes==1 ? "process" : "processes"
1459 #endif
1460                   );
1461 #endif
1462
1463     gmx_omp_nthreads_init(fplog, cr,
1464                           hwinfo->nthreads_hw_avail,
1465                           hw_opt->nthreads_omp,
1466                           hw_opt->nthreads_omp_pme,
1467                           (cr->duty & DUTY_PP) == 0,
1468                           inputrec->cutoff_scheme == ecutsVERLET);
1469
1470     gmx_check_hw_runconf_consistency(fplog, hwinfo, cr, hw_opt->nthreads_tmpi, minf.bUseGPU);
1471
1472     /* getting number of PP/PME threads
1473        PME: env variable should be read only on one node to make sure it is 
1474        identical everywhere;
1475      */
1476     /* TODO nthreads_pp is only used for pinning threads.
1477      * This is a temporary solution until we have a hw topology library.
1478      */
1479     nthreads_pp  = gmx_omp_nthreads_get(emntNonbonded);
1480     nthreads_pme = gmx_omp_nthreads_get(emntPME);
1481
1482     wcycle = wallcycle_init(fplog,resetstep,cr,nthreads_pp,nthreads_pme);
1483
1484     if (PAR(cr))
1485     {
1486         /* Master synchronizes its value of reset_counters with all nodes 
1487          * including PME only nodes */
1488         reset_counters = wcycle_get_reset_counters(wcycle);
1489         gmx_bcast_sim(sizeof(reset_counters),&reset_counters,cr);
1490         wcycle_set_reset_counters(wcycle, reset_counters);
1491     }
1492
1493     snew(nrnb,1);
1494     if (cr->duty & DUTY_PP)
1495     {
1496         /* For domain decomposition we allocate dynamically
1497          * in dd_partition_system.
1498          */
1499         if (DOMAINDECOMP(cr))
1500         {
1501             bcast_state_setup(cr,state);
1502         }
1503         else
1504         {
1505             if (PAR(cr))
1506             {
1507                 bcast_state(cr,state,TRUE);
1508             }
1509         }
1510
1511         /* Initiate forcerecord */
1512         fr = mk_forcerec();
1513         fr->hwinfo = hwinfo;
1514         init_forcerec(fplog,oenv,fr,fcd,inputrec,mtop,cr,box,FALSE,
1515                       opt2fn("-table",nfile,fnm),
1516                       opt2fn("-tabletf",nfile,fnm),
1517                       opt2fn("-tablep",nfile,fnm),
1518                       opt2fn("-tableb",nfile,fnm),
1519                       nbpu_opt,
1520                       FALSE,pforce);
1521
1522         /* version for PCA_NOT_READ_NODE (see md.c) */
1523         /*init_forcerec(fplog,fr,fcd,inputrec,mtop,cr,box,FALSE,
1524           "nofile","nofile","nofile","nofile",FALSE,pforce);
1525           */        
1526         fr->bSepDVDL = ((Flags & MD_SEPPOT) == MD_SEPPOT);
1527
1528         /* Initialize QM-MM */
1529         if(fr->bQMMM)
1530         {
1531             init_QMMMrec(cr,box,mtop,inputrec,fr);
1532         }
1533
1534         /* Initialize the mdatoms structure.
1535          * mdatoms is not filled with atom data,
1536          * as this can not be done now with domain decomposition.
1537          */
1538         mdatoms = init_mdatoms(fplog,mtop,inputrec->efep!=efepNO);
1539
1540         /* Initialize the virtual site communication */
1541         vsite = init_vsite(mtop,cr);
1542
1543         calc_shifts(box,fr->shift_vec);
1544
1545         /* With periodic molecules the charge groups should be whole at start up
1546          * and the virtual sites should not be far from their proper positions.
1547          */
1548         if (!inputrec->bContinuation && MASTER(cr) &&
1549             !(inputrec->ePBC != epbcNONE && inputrec->bPeriodicMols))
1550         {
1551             /* Make molecules whole at start of run */
1552             if (fr->ePBC != epbcNONE)
1553             {
1554                 do_pbc_first_mtop(fplog,inputrec->ePBC,box,mtop,state->x);
1555             }
1556             if (vsite)
1557             {
1558                 /* Correct initial vsite positions are required
1559                  * for the initial distribution in the domain decomposition
1560                  * and for the initial shell prediction.
1561                  */
1562                 construct_vsites_mtop(fplog,vsite,mtop,state->x);
1563             }
1564         }
1565
1566         if (EEL_PME(fr->eeltype))
1567         {
1568             ewaldcoeff = fr->ewaldcoeff;
1569             pmedata = &fr->pmedata;
1570         }
1571         else
1572         {
1573             pmedata = NULL;
1574         }
1575     }
1576     else
1577     {
1578         /* This is a PME only node */
1579
1580         /* We don't need the state */
1581         done_state(state);
1582
1583         ewaldcoeff = calc_ewaldcoeff(inputrec->rcoulomb, inputrec->ewald_rtol);
1584         snew(pmedata,1);
1585     }
1586
1587 #if defined GMX_THREAD_MPI
1588     /* With the number of TMPI threads equal to the number of cores
1589      * we already pinned in thread-MPI, so don't pin again here.
1590      */
1591     if (hw_opt->nthreads_tmpi != tMPI_Thread_get_hw_number())
1592 #endif
1593     {
1594         /* Set the CPU affinity */
1595         set_cpu_affinity(fplog,cr,hw_opt,nthreads_pme,hwinfo,inputrec);
1596     }
1597
1598     /* Initiate PME if necessary,
1599      * either on all nodes or on dedicated PME nodes only. */
1600     if (EEL_PME(inputrec->coulombtype))
1601     {
1602         if (mdatoms)
1603         {
1604             nChargePerturbed = mdatoms->nChargePerturbed;
1605         }
1606         if (cr->npmenodes > 0)
1607         {
1608             /* The PME only nodes need to know nChargePerturbed */
1609             gmx_bcast_sim(sizeof(nChargePerturbed),&nChargePerturbed,cr);
1610         }
1611
1612         if (cr->duty & DUTY_PME)
1613         {
1614             status = gmx_pme_init(pmedata,cr,npme_major,npme_minor,inputrec,
1615                                   mtop ? mtop->natoms : 0,nChargePerturbed,
1616                                   (Flags & MD_REPRODUCIBLE),nthreads_pme);
1617             if (status != 0) 
1618             {
1619                 gmx_fatal(FARGS,"Error %d initializing PME",status);
1620             }
1621         }
1622     }
1623
1624
1625     if (integrator[inputrec->eI].func == do_md
1626 #ifdef GMX_OPENMM
1627         ||
1628         integrator[inputrec->eI].func == do_md_openmm
1629 #endif
1630         )
1631     {
1632         /* Turn on signal handling on all nodes */
1633         /*
1634          * (A user signal from the PME nodes (if any)
1635          * is communicated to the PP nodes.
1636          */
1637         signal_handler_install();
1638     }
1639
1640     if (cr->duty & DUTY_PP)
1641     {
1642         if (inputrec->ePull != epullNO)
1643         {
1644             /* Initialize pull code */
1645             init_pull(fplog,inputrec,nfile,fnm,mtop,cr,oenv, inputrec->fepvals->init_lambda,
1646                       EI_DYNAMICS(inputrec->eI) && MASTER(cr),Flags);
1647         }
1648         
1649         if (inputrec->bRot)
1650         {
1651            /* Initialize enforced rotation code */
1652            init_rot(fplog,inputrec,nfile,fnm,cr,state->x,box,mtop,oenv,
1653                     bVerbose,Flags);
1654         }
1655
1656         constr = init_constraints(fplog,mtop,inputrec,ed,state,cr);
1657
1658         if (DOMAINDECOMP(cr))
1659         {
1660             dd_init_bondeds(fplog,cr->dd,mtop,vsite,constr,inputrec,
1661                             Flags & MD_DDBONDCHECK,fr->cginfo_mb);
1662
1663             set_dd_parameters(fplog,cr->dd,dlb_scale,inputrec,fr,&ddbox);
1664
1665             setup_dd_grid(fplog,cr->dd);
1666         }
1667
1668         /* Now do whatever the user wants us to do (how flexible...) */
1669         integrator[inputrec->eI].func(fplog,cr,nfile,fnm,
1670                                       oenv,bVerbose,bCompact,
1671                                       nstglobalcomm,
1672                                       vsite,constr,
1673                                       nstepout,inputrec,mtop,
1674                                       fcd,state,
1675                                       mdatoms,nrnb,wcycle,ed,fr,
1676                                       repl_ex_nst,repl_ex_nex,repl_ex_seed,
1677                                       membed,
1678                                       cpt_period,max_hours,
1679                                       deviceOptions,
1680                                       Flags,
1681                                       &runtime);
1682
1683         if (inputrec->ePull != epullNO)
1684         {
1685             finish_pull(fplog,inputrec->pull);
1686         }
1687         
1688         if (inputrec->bRot)
1689         {
1690             finish_rot(fplog,inputrec->rot);
1691         }
1692
1693     } 
1694     else 
1695     {
1696         /* do PME only */
1697         gmx_pmeonly(*pmedata,cr,nrnb,wcycle,ewaldcoeff,FALSE,inputrec);
1698     }
1699
1700     if (EI_DYNAMICS(inputrec->eI) || EI_TPI(inputrec->eI))
1701     {
1702         /* Some timing stats */  
1703         if (SIMMASTER(cr))
1704         {
1705             if (runtime.proc == 0)
1706             {
1707                 runtime.proc = runtime.real;
1708             }
1709         }
1710         else
1711         {
1712             runtime.real = 0;
1713         }
1714     }
1715
1716     wallcycle_stop(wcycle,ewcRUN);
1717
1718     /* Finish up, write some stuff
1719      * if rerunMD, don't write last frame again 
1720      */
1721     finish_run(fplog,cr,ftp2fn(efSTO,nfile,fnm),
1722                inputrec,nrnb,wcycle,&runtime,
1723                fr != NULL && fr->nbv != NULL && fr->nbv->bUseGPU ?
1724                  nbnxn_cuda_get_timings(fr->nbv->cu_nbv) : NULL,
1725                nthreads_pp, 
1726                EI_DYNAMICS(inputrec->eI) && !MULTISIM(cr));
1727
1728     if ((cr->duty & DUTY_PP) && fr->nbv != NULL && fr->nbv->bUseGPU)
1729     {
1730         char gpu_err_str[STRLEN];
1731
1732         /* free GPU memory and uninitialize GPU (by destroying the context) */
1733         nbnxn_cuda_free(fplog, fr->nbv->cu_nbv);
1734
1735         if (!free_gpu(gpu_err_str))
1736         {
1737             gmx_warning("On node %d failed to free GPU #%d: %s",
1738                         cr->nodeid, get_current_gpu_device_id(), gpu_err_str);
1739         }
1740     }
1741
1742     if (opt2bSet("-membed",nfile,fnm))
1743     {
1744         sfree(membed);
1745     }
1746
1747 #ifdef GMX_THREAD_MPI
1748     if (PAR(cr) && SIMMASTER(cr))
1749 #endif
1750     {
1751         gmx_hardware_info_free(hwinfo);
1752     }
1753
1754     /* Does what it says */  
1755     print_date_and_time(fplog,cr->nodeid,"Finished mdrun",&runtime);
1756
1757     /* Close logfile already here if we were appending to it */
1758     if (MASTER(cr) && (Flags & MD_APPENDFILES))
1759     {
1760         gmx_log_close(fplog);
1761     }   
1762
1763     rc=(int)gmx_get_stop_condition();
1764
1765 #ifdef GMX_THREAD_MPI
1766     /* we need to join all threads. The sub-threads join when they
1767        exit this function, but the master thread needs to be told to 
1768        wait for that. */
1769     if (PAR(cr) && MASTER(cr))
1770     {
1771         tMPI_Finalize();
1772     }
1773 #endif
1774
1775     return rc;
1776 }