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-2011, 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 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
46 #include "nbnxn_cuda_data_mgmt.h"
48 #include "pme_loadbal.h"
50 /* Parameters and setting for one PP-PME setup */
52 real rcut_coulomb; /* Coulomb cut-off */
53 real rlist; /* pair-list cut-off */
54 real rlistlong; /* LR pair-list cut-off */
55 int nstcalclr; /* frequency of evaluating long-range forces for group scheme */
56 real spacing; /* (largest) PME grid spacing */
57 ivec grid; /* the PME grid dimensions */
58 real grid_efficiency; /* ineffiency factor for non-uniform grids <= 1 */
59 real ewaldcoeff; /* the Ewald coefficient */
60 gmx_pme_t pmedata; /* the data structure used in the PME code */
62 int count; /* number of times this setup has been timed */
63 double cycles; /* the fastest time for this setup in cycles */
66 /* In the initial scan, step by grids that are at least a factor 0.8 coarser */
67 #define PME_LB_GRID_SCALE_FAC 0.8
68 /* In the initial scan, try to skip grids with uneven x/y/z spacing,
69 * checking if the "efficiency" is more than 5% worse than the previous grid.
71 #define PME_LB_GRID_EFFICIENCY_REL_FAC 1.05
72 /* Rerun up till 12% slower setups than the fastest up till now */
73 #define PME_LB_SLOW_FAC 1.12
74 /* If setups get more than 2% faster, do another round to avoid
75 * choosing a slower setup due to acceleration or fluctuations.
77 #define PME_LB_ACCEL_TOL 1.02
79 enum { epmelblimNO, epmelblimBOX, epmelblimDD, epmelblimNR };
81 const char *pmelblim_str[epmelblimNR] =
82 { "no", "box size", "domain decompostion" };
84 struct pme_load_balancing {
85 int nstage; /* the current maximum number of stages */
87 real cut_spacing; /* the minimum cutoff / PME grid spacing ratio */
88 real rcut_vdw; /* Vdw cutoff (does not change) */
89 real rcut_coulomb_start; /* Initial electrostatics cutoff */
90 int nstcalclr_start; /* Initial electrostatics cutoff */
91 real rbuf_coulomb; /* the pairlist buffer size */
92 real rbuf_vdw; /* the pairlist buffer size */
93 matrix box_start; /* the initial simulation box */
94 int n; /* the count of setup as well as the allocation size */
95 pme_setup_t *setup; /* the PME+cutoff setups */
96 int cur; /* the current setup */
97 int fastest; /* fastest setup up till now */
98 int start; /* start of setup range to consider in stage>0 */
99 int end; /* end of setup range to consider in stage>0 */
100 int elimited; /* was the balancing limited, uses enum above */
101 int cutoff_scheme; /* Verlet or group cut-offs */
103 int stage; /* the current stage */
106 void pme_loadbal_init(pme_load_balancing_t *pme_lb_p,
107 const t_inputrec *ir,matrix box,
108 const interaction_const_t *ic,
111 pme_load_balancing_t pme_lb;
117 /* Any number of stages >= 2 is supported */
120 pme_lb->cutoff_scheme = ir->cutoff_scheme;
122 if(pme_lb->cutoff_scheme == ecutsVERLET)
124 pme_lb->rbuf_coulomb = ic->rlist - ic->rcoulomb;
125 pme_lb->rbuf_vdw = pme_lb->rbuf_coulomb;
129 if(ic->rcoulomb > ic->rlist)
131 pme_lb->rbuf_coulomb = ic->rlistlong - ic->rcoulomb;
135 pme_lb->rbuf_coulomb = ic->rlist - ic->rcoulomb;
137 if(ic->rvdw > ic->rlist)
139 pme_lb->rbuf_vdw = ic->rlistlong - ic->rvdw;
143 pme_lb->rbuf_vdw = ic->rlist - ic->rvdw;
147 copy_mat(box,pme_lb->box_start);
148 if (ir->ePBC==epbcXY && ir->nwall==2)
150 svmul(ir->wall_ewald_zfac,pme_lb->box_start[ZZ],pme_lb->box_start[ZZ]);
154 snew(pme_lb->setup,pme_lb->n);
156 pme_lb->rcut_vdw = ic->rvdw;
157 pme_lb->rcut_coulomb_start = ir->rcoulomb;
158 pme_lb->nstcalclr_start = ir->nstcalclr;
161 pme_lb->setup[0].rcut_coulomb = ic->rcoulomb;
162 pme_lb->setup[0].rlist = ic->rlist;
163 pme_lb->setup[0].rlistlong = ic->rlistlong;
164 pme_lb->setup[0].nstcalclr = ir->nstcalclr;
165 pme_lb->setup[0].grid[XX] = ir->nkx;
166 pme_lb->setup[0].grid[YY] = ir->nky;
167 pme_lb->setup[0].grid[ZZ] = ir->nkz;
168 pme_lb->setup[0].ewaldcoeff = ic->ewaldcoeff;
170 pme_lb->setup[0].pmedata = pmedata;
175 sp = norm(pme_lb->box_start[d])/pme_lb->setup[0].grid[d];
181 pme_lb->setup[0].spacing = spm;
183 if (ir->fourier_spacing > 0)
185 pme_lb->cut_spacing = ir->rcoulomb/ir->fourier_spacing;
189 pme_lb->cut_spacing = ir->rcoulomb/pme_lb->setup[0].spacing;
197 pme_lb->elimited = epmelblimNO;
202 static gmx_bool pme_loadbal_increase_cutoff(pme_load_balancing_t pme_lb,
207 real tmpr_coulomb,tmpr_vdw;
210 /* Try to add a new setup with next larger cut-off to the list */
212 srenew(pme_lb->setup,pme_lb->n);
213 set = &pme_lb->setup[pme_lb->n-1];
220 clear_ivec(set->grid);
221 sp = calc_grid(NULL,pme_lb->box_start,
222 fac*pme_lb->setup[pme_lb->cur].spacing,
227 /* In parallel we can't have grids smaller than 2*pme_order,
228 * and we would anyhow not gain much speed at these grid sizes.
232 if (set->grid[d] <= 2*pme_order)
240 while (sp <= 1.001*pme_lb->setup[pme_lb->cur].spacing);
242 set->rcut_coulomb = pme_lb->cut_spacing*sp;
244 if(pme_lb->cutoff_scheme == ecutsVERLET)
246 set->rlist = set->rcut_coulomb + pme_lb->rbuf_coulomb;
247 /* We dont use LR lists with Verlet, but this avoids if-statements in further checks */
248 set->rlistlong = set->rlist;
252 tmpr_coulomb = set->rcut_coulomb + pme_lb->rbuf_coulomb;
253 tmpr_vdw = pme_lb->rcut_vdw + pme_lb->rbuf_vdw;
254 set->rlist = min(tmpr_coulomb,tmpr_vdw);
255 set->rlistlong = max(tmpr_coulomb,tmpr_vdw);
257 /* Set the long-range update frequency */
258 if(set->rlist == set->rlistlong)
260 /* No long-range interactions if the short-/long-range cutoffs are identical */
263 else if(pme_lb->nstcalclr_start==0 || pme_lb->nstcalclr_start==1)
265 /* We were not doing long-range before, but now we are since rlist!=rlistlong */
270 /* We were already doing long-range interactions from the start */
271 if(pme_lb->rcut_vdw > pme_lb->rcut_coulomb_start)
273 /* We were originally doing long-range VdW-only interactions.
274 * If rvdw is still longer than rcoulomb we keep the original nstcalclr,
275 * but if the coulomb cutoff has become longer we should update the long-range
278 set->nstcalclr = (tmpr_vdw > tmpr_coulomb) ? pme_lb->nstcalclr_start : 1;
282 /* We were not doing any long-range interaction from the start,
283 * since it is not possible to do twin-range coulomb for the PME interaction.
291 /* The grid efficiency is the size wrt a grid with uniform x/y/z spacing */
292 set->grid_efficiency = 1;
295 set->grid_efficiency *= (set->grid[d]*sp)/norm(pme_lb->box_start[d]);
297 /* The Ewald coefficient is inversly proportional to the cut-off */
299 pme_lb->setup[0].ewaldcoeff*pme_lb->setup[0].rcut_coulomb/set->rcut_coulomb;
306 fprintf(debug,"PME loadbal: grid %d %d %d, coulomb cutoff %f\n",
307 set->grid[XX],set->grid[YY],set->grid[ZZ],set->rcut_coulomb);
312 static void print_grid(FILE *fp_err,FILE *fp_log,
315 const pme_setup_t *set,
318 char buf[STRLEN],buft[STRLEN];
322 sprintf(buft,": %.1f M-cycles",cycles*1e-6);
328 sprintf(buf,"%-11s%10s pme grid %d %d %d, coulomb cutoff %.3f%s",
330 desc,set->grid[XX],set->grid[YY],set->grid[ZZ],set->rcut_coulomb,
334 fprintf(fp_err,"\r%s\n",buf);
338 fprintf(fp_log,"%s\n",buf);
342 static int pme_loadbal_end(pme_load_balancing_t pme_lb)
344 /* In the initial stage only n is set; end is not set yet */
355 static void print_loadbal_limited(FILE *fp_err,FILE *fp_log,
356 gmx_large_int_t step,
357 pme_load_balancing_t pme_lb)
359 char buf[STRLEN],sbuf[22];
361 sprintf(buf,"step %4s: the %s limited the PME load balancing to a coulomb cut-off of %.3f",
362 gmx_step_str(step,sbuf),
363 pmelblim_str[pme_lb->elimited],
364 pme_lb->setup[pme_loadbal_end(pme_lb)-1].rcut_coulomb);
367 fprintf(fp_err,"\r%s\n",buf);
371 fprintf(fp_log,"%s\n",buf);
375 static void switch_to_stage1(pme_load_balancing_t pme_lb)
378 while (pme_lb->start+1 < pme_lb->n &&
379 (pme_lb->setup[pme_lb->start].count == 0 ||
380 pme_lb->setup[pme_lb->start].cycles >
381 pme_lb->setup[pme_lb->fastest].cycles*PME_LB_SLOW_FAC))
385 while (pme_lb->start > 0 && pme_lb->setup[pme_lb->start-1].cycles == 0)
390 pme_lb->end = pme_lb->n;
391 if (pme_lb->setup[pme_lb->end-1].count > 0 &&
392 pme_lb->setup[pme_lb->end-1].cycles >
393 pme_lb->setup[pme_lb->fastest].cycles*PME_LB_SLOW_FAC)
400 /* Next we want to choose setup pme_lb->start, but as we will increase
401 * pme_ln->cur by one right after returning, we subtract 1 here.
403 pme_lb->cur = pme_lb->start - 1;
406 gmx_bool pme_load_balance(pme_load_balancing_t pme_lb,
413 interaction_const_t *ic,
414 nonbonded_verlet_t *nbv,
416 gmx_large_int_t step)
421 char buf[STRLEN],sbuf[22];
423 gmx_bool bUsesSimpleTables = TRUE;
425 if (pme_lb->stage == pme_lb->nstage)
432 gmx_sumd(1,&cycles,cr);
433 cycles /= cr->nnodes;
436 set = &pme_lb->setup[pme_lb->cur];
439 rtab = ir->rlistlong + ir->tabext;
441 if (set->count % 2 == 1)
443 /* Skip the first cycle, because the first step after a switch
444 * is much slower due to allocation and/or caching effects.
449 sprintf(buf, "step %4s: ", gmx_step_str(step,sbuf));
450 print_grid(fp_err,fp_log,buf,"timed with",set,cycles);
454 set->cycles = cycles;
458 if (cycles*PME_LB_ACCEL_TOL < set->cycles &&
459 pme_lb->stage == pme_lb->nstage - 1)
461 /* The performance went up a lot (due to e.g. DD load balancing).
462 * Add a stage, keep the minima, but rescan all setups.
468 fprintf(debug,"The performance for grid %d %d %d went from %.3f to %.1f M-cycles, this is more than %f\n"
469 "Increased the number stages to %d"
470 " and ignoring the previous performance\n",
471 set->grid[XX],set->grid[YY],set->grid[ZZ],
472 cycles*1e-6,set->cycles*1e-6,PME_LB_ACCEL_TOL,
476 set->cycles = min(set->cycles,cycles);
479 if (set->cycles < pme_lb->setup[pme_lb->fastest].cycles)
481 pme_lb->fastest = pme_lb->cur;
483 cycles_fast = pme_lb->setup[pme_lb->fastest].cycles;
485 /* Check in stage 0 if we should stop scanning grids.
486 * Stop when the time is more than SLOW_FAC longer than the fastest.
488 if (pme_lb->stage == 0 && pme_lb->cur > 0 &&
489 cycles > pme_lb->setup[pme_lb->fastest].cycles*PME_LB_SLOW_FAC)
491 pme_lb->n = pme_lb->cur + 1;
492 /* Done with scanning, go to stage 1 */
493 switch_to_stage1(pme_lb);
496 if (pme_lb->stage == 0)
500 gridsize_start = set->grid[XX]*set->grid[YY]*set->grid[ZZ];
504 if (pme_lb->cur+1 < pme_lb->n)
506 /* We had already generated the next setup */
511 /* Find the next setup */
512 OK = pme_loadbal_increase_cutoff(pme_lb,ir->pme_order);
515 if (OK && ir->ePBC != epbcNONE)
517 OK = (sqr(pme_lb->setup[pme_lb->cur+1].rlistlong)
518 <= max_cutoff2(ir->ePBC,state->box));
521 pme_lb->elimited = epmelblimBOX;
529 if (DOMAINDECOMP(cr))
531 OK = change_dd_cutoff(cr,state,ir,
532 pme_lb->setup[pme_lb->cur].rlistlong);
535 /* Failed: do not use this setup */
537 pme_lb->elimited = epmelblimDD;
543 /* We hit the upper limit for the cut-off,
544 * the setup should not go further than cur.
546 pme_lb->n = pme_lb->cur + 1;
547 print_loadbal_limited(fp_err,fp_log,step,pme_lb);
548 /* Switch to the next stage */
549 switch_to_stage1(pme_lb);
553 !(pme_lb->setup[pme_lb->cur].grid[XX]*
554 pme_lb->setup[pme_lb->cur].grid[YY]*
555 pme_lb->setup[pme_lb->cur].grid[ZZ] <
556 gridsize_start*PME_LB_GRID_SCALE_FAC
558 pme_lb->setup[pme_lb->cur].grid_efficiency <
559 pme_lb->setup[pme_lb->cur-1].grid_efficiency*PME_LB_GRID_EFFICIENCY_REL_FAC));
562 if (pme_lb->stage > 0 && pme_lb->end == 1)
565 pme_lb->stage = pme_lb->nstage;
567 else if (pme_lb->stage > 0 && pme_lb->end > 1)
569 /* If stage = nstage-1:
570 * scan over all setups, rerunning only those setups
571 * which are not much slower than the fastest
578 if (pme_lb->cur == pme_lb->end)
581 pme_lb->cur = pme_lb->start;
584 while (pme_lb->stage == pme_lb->nstage - 1 &&
585 pme_lb->setup[pme_lb->cur].count > 0 &&
586 pme_lb->setup[pme_lb->cur].cycles > cycles_fast*PME_LB_SLOW_FAC);
588 if (pme_lb->stage == pme_lb->nstage)
590 /* We are done optimizing, use the fastest setup we found */
591 pme_lb->cur = pme_lb->fastest;
595 if (DOMAINDECOMP(cr) && pme_lb->stage > 0)
597 OK = change_dd_cutoff(cr,state,ir,pme_lb->setup[pme_lb->cur].rlistlong);
600 /* Failsafe solution */
601 if (pme_lb->cur > 1 && pme_lb->stage == pme_lb->nstage)
607 pme_lb->end = pme_lb->cur;
608 pme_lb->cur = pme_lb->start;
609 pme_lb->elimited = epmelblimDD;
610 print_loadbal_limited(fp_err,fp_log,step,pme_lb);
614 /* Change the Coulomb cut-off and the PME grid */
616 set = &pme_lb->setup[pme_lb->cur];
618 ic->rcoulomb = set->rcut_coulomb;
619 ic->rlist = set->rlist;
620 ic->rlistlong = set->rlistlong;
621 ir->nstcalclr = set->nstcalclr;
622 ic->ewaldcoeff = set->ewaldcoeff;
624 bUsesSimpleTables = uses_simple_tables(ir->cutoff_scheme, nbv, 0);
625 if (pme_lb->cutoff_scheme == ecutsVERLET && nbv->grp[0].kernel_type == nbk8x8x8_CUDA)
627 nbnxn_cuda_pme_loadbal_update_param(nbv->cu_nbv,ic);
631 init_interaction_const_tables(NULL,ic,bUsesSimpleTables,
635 if (pme_lb->cutoff_scheme == ecutsVERLET && nbv->ngrp > 1)
637 init_interaction_const_tables(NULL,ic,bUsesSimpleTables,
641 if (cr->duty & DUTY_PME)
643 if (pme_lb->setup[pme_lb->cur].pmedata == NULL)
645 /* Generate a new PME data structure,
646 * copying part of the old pointers.
648 gmx_pme_reinit(&set->pmedata,
649 cr,pme_lb->setup[0].pmedata,ir,
652 *pmedata = set->pmedata;
656 /* Tell our PME-only node to switch grid */
657 gmx_pme_send_switch(cr, set->grid, set->ewaldcoeff);
662 print_grid(NULL,debug,"","switched to",set,-1);
665 if (pme_lb->stage == pme_lb->nstage)
667 print_grid(fp_err,fp_log,"","optimal",set,-1);
673 void restart_pme_loadbal(pme_load_balancing_t pme_lb, int n)
678 static int pme_grid_points(const pme_setup_t *setup)
680 return setup->grid[XX]*setup->grid[YY]*setup->grid[ZZ];
683 static void print_pme_loadbal_setting(FILE *fplog,
685 const pme_setup_t *setup)
688 " %-7s %6.3f nm %6.3f nm %3d %3d %3d %5.3f nm %5.3f nm\n",
690 setup->rcut_coulomb,setup->rlist,
691 setup->grid[XX],setup->grid[YY],setup->grid[ZZ],
692 setup->spacing,1/setup->ewaldcoeff);
695 static void print_pme_loadbal_settings(pme_load_balancing_t pme_lb,
698 double pp_ratio,grid_ratio;
700 pp_ratio = pow(pme_lb->setup[pme_lb->cur].rlist/pme_lb->setup[0].rlistlong,3.0);
701 grid_ratio = pme_grid_points(&pme_lb->setup[pme_lb->cur])/
702 (double)pme_grid_points(&pme_lb->setup[0]);
705 fprintf(fplog," P P - P M E L O A D B A L A N C I N G\n");
707 /* Here we only warn when the optimal setting is the last one */
708 if (pme_lb->elimited != epmelblimNO &&
709 pme_lb->cur == pme_loadbal_end(pme_lb)-1)
711 fprintf(fplog," NOTE: The PP/PME load balancing was limited by the %s,\n",
712 pmelblim_str[pme_lb->elimited]);
713 fprintf(fplog," you might not have reached a good load balance.\n");
714 if (pme_lb->elimited == epmelblimDD)
716 fprintf(fplog," Try different mdrun -dd settings or lower the -dds value.\n");
720 fprintf(fplog," PP/PME load balancing changed the cut-off and PME settings:\n");
721 fprintf(fplog," particle-particle PME\n");
722 fprintf(fplog," rcoulomb rlist grid spacing 1/beta\n");
723 print_pme_loadbal_setting(fplog,"initial",&pme_lb->setup[0]);
724 print_pme_loadbal_setting(fplog,"final" ,&pme_lb->setup[pme_lb->cur]);
725 fprintf(fplog," cost-ratio %4.2f %4.2f\n",
726 pp_ratio,grid_ratio);
727 fprintf(fplog," (note that these numbers concern only part of the total PP and PME load)\n");
731 void pme_loadbal_done(pme_load_balancing_t pme_lb, FILE *fplog)
733 if (fplog != NULL && (pme_lb->cur > 0 || pme_lb->elimited != epmelblimNO))
735 print_pme_loadbal_settings(pme_lb,fplog);
738 /* TODO: Here we should free all pointers in pme_lb,
739 * but as it contains pme data structures,
740 * we need to first make pme.c free all data.