Merge origin/release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / nrnb.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  * 
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <string.h>
40 #include "types/commrec.h"
41 #include "sysstuff.h"
42 #include "gmx_fatal.h"
43 #include "names.h"
44 #include "macros.h"
45 #include "nrnb.h"
46 #include "main.h"
47 #include "smalloc.h"
48 #include "copyrite.h"
49
50
51
52
53
54 typedef struct {
55   const char *name;
56   int  flop;
57 } t_nrnb_data;
58
59
60 static const t_nrnb_data nbdata[eNRNB] = {
61     /* These are re-used for different NB kernels, since there are so many.
62      * The actual number of flops is set dynamically.
63      */
64     { "NB VdW [V&F]",                    1 },
65     { "NB VdW [F]",                      1 },
66     { "NB Elec. [V&F]",                  1 },
67     { "NB Elec. [F]",                    1 },
68     { "NB Elec. [W3,V&F]",               1 },
69     { "NB Elec. [W3,F]",                 1 },
70     { "NB Elec. [W3-W3,V&F]",            1 },
71     { "NB Elec. [W3-W3,F]",              1 },
72     { "NB Elec. [W4,V&F]",               1 },
73     { "NB Elec. [W4,F]",                 1 },
74     { "NB Elec. [W4-W4,V&F]",            1 },
75     { "NB Elec. [W4-W4,F]",              1 },
76     { "NB VdW & Elec. [V&F]",            1 },
77     { "NB VdW & Elec. [F]",              1 },
78     { "NB VdW & Elec. [W3,V&F]",         1 },
79     { "NB VdW & Elec. [W3,F]",           1 },
80     { "NB VdW & Elec. [W3-W3,V&F]",      1 },
81     { "NB VdW & Elec. [W3-W3,F]",        1 },
82     { "NB VdW & Elec. [W4,V&F]",         1 },
83     { "NB VdW & Elec. [W4,F]",           1 },
84     { "NB VdW & Elec. [W4-W4,V&F]",      1 },
85     { "NB VdW & Elec. [W4-W4,F]",        1 },
86     
87     { "NB Generic kernel",               1 },
88     { "NB Free energy kernel",           1 },
89     { "NB All-vs-all",                   1 },
90     { "NB All-vs-all, GB",               1 },
91
92     { "Pair Search distance check",      9 }, /* nbnxn pair dist. check */
93     /* nbnxn kernel flops are based on inner-loops without exclusion checks.
94      * Plain Coulomb runs through the RF kernels, except with CUDA.
95      * invsqrt is counted as 6 flops: 1 for _mm_rsqt_ps + 5 for iteration.
96      * The flops are equal for plain-C, x86 SIMD and CUDA, except for:
97      * - plain-C kernel uses one flop more for Coulomb-only (F) than listed
98      * - x86 SIMD LJ geom-comb.rule kernels (fastest) use 2 more flops
99      * - x86 SIMD LJ LB-comb.rule kernels (fast) use 3 (8 for F+E) more flops
100      * - GPU always does exclusions, which requires 2-4 flops, but as invsqrt
101      *   is always counted as 6 flops, this roughly compensates.
102      */
103     { "NxN RF Elec. + VdW [F]",         38 }, /* nbnxn kernel LJ+RF, no ener */
104     { "NxN RF Elec. + VdW [V&F]",       54 },
105     { "NxN CSTab Elec. + VdW [F]",      41 }, /* nbnxn kernel LJ+tab, no en */
106     { "NxN CSTab Elec. + VdW [V&F]",    59 },
107     { "NxN VdW [F]",                    33 }, /* nbnxn kernel LJ, no ener */
108     { "NxN VdW [V&F]",                  43 },
109     { "NxN RF Electrostatics [F]",      31 }, /* nbnxn kernel RF, no ener */
110     { "NxN RF Electrostatics [V&F]",    36 },
111     { "NxN CSTab Elec. [F]",            34 }, /* nbnxn kernel tab, no ener */
112     { "NxN CSTab Elec. [V&F]",          41 },
113     { "1,4 nonbonded interactions",     90 },
114     { "Born radii (Still)",             47 },
115     { "Born radii (HCT/OBC)",          183 },
116     { "Born force chain rule",          15 },
117     { "All-vs-All Still radii",          1 },
118     { "All-vs-All HCT/OBC radii",        1 },
119     { "All-vs-All Born chain rule",      1 },
120     { "Calc Weights",                   36 },
121     { "Spread Q",                        6 },
122     { "Spread Q Bspline",                2 }, 
123     { "Gather F",                      23  },
124     { "Gather F Bspline",              6   }, 
125     { "3D-FFT",                        8   },
126     { "Convolution",                   4   },
127     { "Solve PME",                     64  },
128     { "NS-Pairs",                      21  },
129     { "Reset In Box",                  3   },
130     { "Shift-X",                       6   },
131     { "CG-CoM",                        3   },
132     { "Sum Forces",                    1   },
133     { "Bonds",                         59  },
134     { "G96Bonds",                      44  },
135     { "FENE Bonds",                    58  },
136     { "Tab. Bonds",                    62  },
137     { "Restraint Potential",           86  },
138     { "Linear Angles",                 57  },
139     { "Angles",                        168 },
140     { "G96Angles",                     150 },
141     { "Quartic Angles",                160 },
142     { "Tab. Angles",                   169 },
143     { "Propers",                       229 },
144     { "Impropers",                     208 },
145     { "RB-Dihedrals",                  247 },
146     { "Four. Dihedrals",               247 },
147     { "Tab. Dihedrals",                227 },
148     { "Dist. Restr.",                  200 },
149     { "Orient. Restr.",                200 },
150     { "Dihedral Restr.",               200 },
151     { "Pos. Restr.",                   50  },
152     { "Flat-bottom posres",            50  },
153     { "Angle Restr.",                  191 },
154     { "Angle Restr. Z",                164 },
155     { "Morse Potent.",                 83  },
156     { "Cubic Bonds",                   54  },
157     { "Walls",                         31  },
158     { "Polarization",                  59  },
159     { "Anharmonic Polarization",       72  },
160     { "Water Pol.",                    62  },
161     { "Thole Pol.",                    296 },
162     { "Virial",                        18  },
163     { "Update",                        31  },
164     { "Ext.ens. Update",               54  },
165     { "Stop-CM",                       10  },
166     { "P-Coupling",                    6   },
167     { "Calc-Ekin",                     27  },
168     { "Lincs",                         60  },
169     { "Lincs-Mat",                     4   },
170     { "Shake",                         30  },
171     { "Constraint-V",                   8  },
172     { "Shake-Init",                    10  },
173     { "Constraint-Vir",                24  },
174     { "Settle",                        323 },
175     { "Virtual Site 2",                23  },
176     { "Virtual Site 3",                37  },
177     { "Virtual Site 3fd",              95  },
178     { "Virtual Site 3fad",             176 },
179     { "Virtual Site 3out",             87  },
180     { "Virtual Site 4fd",              110 }, 
181     { "Virtual Site 4fdn",             254 }, 
182     { "Virtual Site N",                 15 },
183     { "Mixed Generalized Born stuff",   10 } 
184 };
185
186
187 void init_nrnb(t_nrnb *nrnb)
188 {
189   int i;
190
191   for(i=0; (i<eNRNB); i++)
192     nrnb->n[i]=0.0;
193 }
194
195 void cp_nrnb(t_nrnb *dest, t_nrnb *src)
196 {
197   int i;
198
199   for(i=0; (i<eNRNB); i++)
200     dest->n[i]=src->n[i];
201 }
202
203 void add_nrnb(t_nrnb *dest, t_nrnb *s1, t_nrnb *s2)
204 {
205   int i;
206
207   for(i=0; (i<eNRNB); i++)
208     dest->n[i]=s1->n[i]+s2->n[i];
209 }
210
211 void print_nrnb(FILE *out, t_nrnb *nrnb)
212 {
213   int i;
214
215   for(i=0; (i<eNRNB); i++)
216     if (nrnb->n[i] > 0)
217       fprintf(out," %-26s %10.0f.\n",nbdata[i].name,nrnb->n[i]);
218 }
219
220 void _inc_nrnb(t_nrnb *nrnb,int enr,int inc,char *file,int line)
221 {
222   nrnb->n[enr]+=inc;
223 #ifdef DEBUG_NRNB
224   printf("nrnb %15s(%2d) incremented with %8d from file %s line %d\n",
225           nbdata[enr].name,enr,inc,file,line);
226 #endif
227 }
228
229 void print_flop(FILE *out,t_nrnb *nrnb,double *nbfs,double *mflop)
230 {
231   int    i;
232   double mni,frac,tfrac,tflop;
233   const char   *myline = "-----------------------------------------------------------------------------";
234   
235   *nbfs = 0.0;
236   for(i=0; (i<eNR_NBKERNEL_ALLVSALLGB); i++) {
237     if (strstr(nbdata[i].name,"W3-W3") != NULL)
238       *nbfs += 9e-6*nrnb->n[i];
239     else if (strstr(nbdata[i].name,"W3") != NULL)
240       *nbfs += 3e-6*nrnb->n[i];
241     else if (strstr(nbdata[i].name,"W4-W4") != NULL)
242       *nbfs += 10e-6*nrnb->n[i];
243     else if (strstr(nbdata[i].name,"W4") != NULL)
244       *nbfs += 4e-6*nrnb->n[i];
245     else
246       *nbfs += 1e-6*nrnb->n[i];
247   }
248   tflop=0;
249   for(i=0; (i<eNRNB); i++) 
250     tflop+=1e-6*nrnb->n[i]*nbdata[i].flop;
251   
252   if (tflop == 0) {
253     fprintf(out,"No MEGA Flopsen this time\n");
254     return;
255   }
256   if (out) {
257     fprintf(out,"\n\tM E G A - F L O P S   A C C O U N T I N G\n\n");
258   }
259
260   if (out)
261   {
262       fprintf(out," NB=Group-cutoff nonbonded kernels    NxN=N-by-N tile Verlet kernels\n");
263       fprintf(out," RF=Reaction-Field  VdW=Van der Waals  CSTab=Cubic-spline table\n");
264       fprintf(out," W3=SPC/TIP3p  W4=TIP4p (single or pairs)\n");
265       fprintf(out," V&F=Potential and force  V=Potential only  F=Force only\n\n");
266
267       fprintf(out," %-32s %16s %15s  %7s\n",
268               "Computing:","M-Number","M-Flops","% Flops");
269       fprintf(out,"%s\n",myline);
270   }
271   *mflop=0.0;
272   tfrac=0.0;
273   for(i=0; (i<eNRNB); i++) {
274     mni     = 1e-6*nrnb->n[i];
275     *mflop += mni*nbdata[i].flop;
276     frac    = 100.0*mni*nbdata[i].flop/tflop;
277     tfrac  += frac;
278     if (out && mni != 0)
279       fprintf(out," %-32s %16.6f %15.3f  %6.1f\n",
280               nbdata[i].name,mni,mni*nbdata[i].flop,frac);
281   }
282   if (out) {
283     fprintf(out,"%s\n",myline);
284     fprintf(out," %-32s %16s %15.3f  %6.1f\n",
285             "Total","",*mflop,tfrac);
286     fprintf(out,"%s\n\n",myline);
287   }
288 }
289
290 void print_perf(FILE *out,double nodetime,double realtime,int nprocs,
291                 gmx_large_int_t nsteps,real delta_t,
292                 double nbfs,double mflop,
293                 int omp_nth_pp)
294 {
295   real runtime;
296
297   fprintf(out,"\n");
298
299   if (realtime > 0) 
300   {
301     fprintf(out,"%12s %12s %12s %10s\n","","Core t (s)","Wall t (s)","(%)");
302     fprintf(out,"%12s %12.3f %12.3f %10.1f\n","Time:",
303             nodetime, realtime, 100.0*nodetime/realtime);
304     /* only print day-hour-sec format if realtime is more than 30 min */
305     if (realtime > 30*60)
306     {
307       fprintf(out,"%12s %12s","","");
308       pr_difftime(out,realtime);
309     }
310     if (delta_t > 0) 
311     {
312       mflop = mflop/realtime;
313       runtime = nsteps*delta_t;
314
315       if (getenv("GMX_DETAILED_PERF_STATS") == NULL)
316       {
317           fprintf(out,"%12s %12s %12s\n",
318                   "","(ns/day)","(hour/ns)");
319           fprintf(out,"%12s %12.3f %12.3f\n","Performance:",
320                   runtime*24*3.6/realtime,1000*realtime/(3600*runtime));
321       }
322       else
323       {
324         fprintf(out,"%12s %12s %12s %12s %12s\n",
325                 "","(Mnbf/s)",(mflop > 1000) ? "(GFlops)" : "(MFlops)",
326                 "(ns/day)","(hour/ns)");
327         fprintf(out,"%12s %12.3f %12.3f %12.3f %12.3f\n","Performance:",
328                 nbfs/realtime,(mflop > 1000) ? (mflop/1000) : mflop,
329                 runtime*24*3.6/realtime,1000*realtime/(3600*runtime));
330       }
331     } 
332     else 
333     {
334       if (getenv("GMX_DETAILED_PERF_STATS") == NULL)
335       {
336           fprintf(out,"%12s %14s\n",
337                   "","(steps/hour)");
338           fprintf(out,"%12s %14.1f\n","Performance:",
339                   nsteps*3600.0/realtime);
340       }
341       else
342       {
343           fprintf(out,"%12s %12s %12s %14s\n",
344                   "","(Mnbf/s)",(mflop > 1000) ? "(GFlops)" : "(MFlops)",
345                   "(steps/hour)");
346           fprintf(out,"%12s %12.3f %12.3f %14.1f\n","Performance:",
347               nbfs/realtime,(mflop > 1000) ? (mflop/1000) : mflop,
348               nsteps*3600.0/realtime);
349       }
350     }
351   }
352 }
353
354 int cost_nrnb(int enr)
355 {
356   return nbdata[enr].flop;
357 }
358
359 const char *nrnb_str(int enr)
360 {
361   return nbdata[enr].name;
362 }
363
364 static const int    force_index[]={ 
365   eNR_BONDS,  eNR_ANGLES,  eNR_PROPER, eNR_IMPROPER, 
366   eNR_RB,     eNR_DISRES,  eNR_ORIRES, eNR_POSRES,
367   eNR_FBPOSRES, eNR_NS,
368 };
369 #define NFORCE_INDEX asize(force_index)
370
371 static const int    constr_index[]={ 
372   eNR_SHAKE,     eNR_SHAKE_RIJ, eNR_SETTLE,       eNR_UPDATE,       eNR_PCOUPL,
373   eNR_CONSTR_VIR,eNR_CONSTR_V
374 };
375 #define NCONSTR_INDEX asize(constr_index)
376
377 static double pr_av(FILE *log,t_commrec *cr,
378                     double fav,double ftot[],const char *title)
379 {
380   int    i,perc;
381   double dperc,unb;
382   
383   unb=0;
384   if (fav > 0) {
385     fav /= cr->nnodes - cr->npmenodes;
386     fprintf(log,"\n %-26s",title);
387     for(i=0; (i<cr->nnodes); i++) {
388         dperc=(100.0*ftot[i])/fav;
389         unb=max(unb,dperc);
390         perc=dperc;
391         fprintf(log,"%3d ",perc);
392     }
393     if (unb > 0) {
394       perc=10000.0/unb;
395       fprintf(log,"%6d%%\n\n",perc);
396     }
397     else
398       fprintf(log,"\n\n");
399   }
400   return unb;
401 }
402
403 void pr_load(FILE *log,t_commrec *cr,t_nrnb nrnb[])
404 {
405   int    i,j,perc;
406   double dperc,unb,uf,us;
407   double *ftot,fav;
408   double *stot,sav;
409   t_nrnb *av;
410
411   snew(av,1);
412   snew(ftot,cr->nnodes);
413   snew(stot,cr->nnodes);
414   init_nrnb(av);
415   for(i=0; (i<cr->nnodes); i++) {
416       add_nrnb(av,av,&(nrnb[i]));
417       /* Cost due to forces */
418       for(j=0; (j<eNR_NBKERNEL_ALLVSALLGB); j++)
419         ftot[i]+=nrnb[i].n[j]*cost_nrnb(j);
420       for(j=0; (j<NFORCE_INDEX); j++) 
421         ftot[i]+=nrnb[i].n[force_index[j]]*cost_nrnb(force_index[j]);
422       /* Due to shake */
423       for(j=0; (j<NCONSTR_INDEX); j++) {
424         stot[i]+=nrnb[i].n[constr_index[j]]*cost_nrnb(constr_index[j]);
425       }
426   }   
427   for(j=0; (j<eNRNB); j++)
428     av->n[j]=av->n[j]/(double)(cr->nnodes - cr->npmenodes);
429     
430     fprintf(log,"\nDetailed load balancing info in percentage of average\n");
431   
432   fprintf(log," Type                 NODE:");
433   for(i=0; (i<cr->nnodes); i++)
434       fprintf(log,"%3d ",i);
435   fprintf(log,"Scaling\n");
436   fprintf(log,"---------------------------");
437   for(i=0; (i<cr->nnodes); i++)
438       fprintf(log,"----");
439   fprintf(log,"-------\n");
440   
441   for(j=0; (j<eNRNB); j++) {
442     unb=100.0;
443     if (av->n[j] > 0) {
444       fprintf(log," %-26s",nrnb_str(j));
445       for(i=0; (i<cr->nnodes); i++) {
446           dperc=(100.0*nrnb[i].n[j])/av->n[j];
447           unb=max(unb,dperc);
448           perc=dperc;
449           fprintf(log,"%3d ",perc);
450       }
451       if (unb > 0) {
452         perc=10000.0/unb;
453         fprintf(log,"%6d%%\n",perc);
454       }
455       else
456         fprintf(log,"\n");
457     }   
458   }
459   fav=sav=0;
460   for(i=0; (i<cr->nnodes); i++) {
461     fav+=ftot[i];
462     sav+=stot[i];
463   }
464   uf=pr_av(log,cr,fav,ftot,"Total Force");
465   us=pr_av(log,cr,sav,stot,"Total Constr.");
466   
467   unb=(uf*fav+us*sav)/(fav+sav);
468   if (unb > 0) {
469     unb=10000.0/unb;
470     fprintf(log,"\nTotal Scaling: %.0f%% of max performance\n\n",unb);
471   }
472 }
473