98c31b55b1777642edf9becbe258067cbc487017
[alexxy/gromacs.git] / src / gromacs / timing / wallcycle.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2008, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gromacs/timing/wallcycle.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include <string.h>
44
45 #include "gromacs/utility/smalloc.h"
46 #include "gmx_fatal.h"
47 #include "md_logging.h"
48 #include "gromacs/utility/cstringutil.h"
49
50 #include "gromacs/timing/cyclecounter.h"
51 #include "gromacs/utility/gmxmpi.h"
52
53 /* DEBUG_WCYCLE adds consistency checking for the counters.
54  * It checks if you stop a counter different from the last
55  * one that was opened and if you do nest too deep.
56  */
57 /* #define DEBUG_WCYCLE */
58
59 typedef struct
60 {
61     int          n;
62     gmx_cycles_t c;
63     gmx_cycles_t start;
64     gmx_cycles_t last;
65 } wallcc_t;
66
67 typedef struct gmx_wallcycle
68 {
69     wallcc_t        *wcc;
70     /* variables for testing/debugging */
71     gmx_bool         wc_barrier;
72     wallcc_t        *wcc_all;
73     int              wc_depth;
74 #ifdef DEBUG_WCYCLE
75 #define DEPTH_MAX 6
76     int               counterlist[DEPTH_MAX];
77     int               count_depth;
78 #endif
79     int               ewc_prev;
80     gmx_cycles_t      cycle_prev;
81     gmx_int64_t       reset_counters;
82 #ifdef GMX_MPI
83     MPI_Comm          mpi_comm_mygroup;
84 #endif
85     int               nthreads_pp;
86     int               nthreads_pme;
87 #ifdef GMX_CYCLE_SUBCOUNTERS
88     wallcc_t         *wcsc;
89 #endif
90     double           *cycles_sum;
91 } gmx_wallcycle_t_t;
92
93 /* Each name should not exceed 19 printing characters
94    (ie. terminating null can be twentieth) */
95 static const char *wcn[ewcNR] =
96 {
97     "Run", "Step", "PP during PME", "Domain decomp.", "DD comm. load",
98     "DD comm. bounds", "Vsite constr.", "Send X to PME", "Neighbor search", "Launch GPU ops.",
99     "Comm. coord.", "Born radii", "Force", "Wait + Comm. F", "PME mesh",
100     "PME redist. X/F", "PME spread/gather", "PME 3D-FFT", "PME 3D-FFT Comm.", "PME solve LJ", "PME solve Elec",
101     "PME wait for PP", "Wait + Recv. PME F", "Wait GPU nonlocal", "Wait GPU local", "NB X/F buffer ops.",
102     "Vsite spread", "COM pull force",
103     "Write traj.", "Update", "Constraints", "Comm. energies",
104     "Enforced rotation", "Add rot. forces", "Coordinate swapping", "IMD", "Test"
105 };
106
107 static const char *wcsn[ewcsNR] =
108 {
109     "DD redist.", "DD NS grid + sort", "DD setup comm.",
110     "DD make top.", "DD make constr.", "DD top. other",
111     "NS grid local", "NS grid non-loc.", "NS search local", "NS search non-loc.",
112     "Bonded F", "Nonbonded F", "Ewald F correction",
113     "NB X buffer ops.", "NB F buffer ops."
114 };
115
116 gmx_bool wallcycle_have_counter(void)
117 {
118     return gmx_cycles_have_counter();
119 }
120
121 gmx_wallcycle_t wallcycle_init(FILE *fplog, int resetstep, t_commrec gmx_unused *cr,
122                                int nthreads_pp, int nthreads_pme)
123 {
124     gmx_wallcycle_t wc;
125
126
127     if (!wallcycle_have_counter())
128     {
129         return NULL;
130     }
131
132     snew(wc, 1);
133
134     wc->wc_barrier          = FALSE;
135     wc->wcc_all             = NULL;
136     wc->wc_depth            = 0;
137     wc->ewc_prev            = -1;
138     wc->reset_counters      = resetstep;
139     wc->nthreads_pp         = nthreads_pp;
140     wc->nthreads_pme        = nthreads_pme;
141     wc->cycles_sum          = NULL;
142
143 #ifdef GMX_MPI
144     if (PAR(cr) && getenv("GMX_CYCLE_BARRIER") != NULL)
145     {
146         if (fplog)
147         {
148             fprintf(fplog, "\nWill call MPI_Barrier before each cycle start/stop call\n\n");
149         }
150         wc->wc_barrier       = TRUE;
151         wc->mpi_comm_mygroup = cr->mpi_comm_mygroup;
152     }
153 #endif
154
155     snew(wc->wcc, ewcNR);
156     if (getenv("GMX_CYCLE_ALL") != NULL)
157     {
158         if (fplog)
159         {
160             fprintf(fplog, "\nWill time all the code during the run\n\n");
161         }
162         snew(wc->wcc_all, ewcNR*ewcNR);
163     }
164
165 #ifdef GMX_CYCLE_SUBCOUNTERS
166     snew(wc->wcsc, ewcsNR);
167 #endif
168
169 #ifdef DEBUG_WCYCLE
170     wc->count_depth = 0;
171 #endif
172
173     return wc;
174 }
175
176 void wallcycle_destroy(gmx_wallcycle_t wc)
177 {
178     if (wc == NULL)
179     {
180         return;
181     }
182
183     if (wc->wcc != NULL)
184     {
185         sfree(wc->wcc);
186     }
187     if (wc->wcc_all != NULL)
188     {
189         sfree(wc->wcc_all);
190     }
191 #ifdef GMX_CYCLE_SUBCOUNTERS
192     if (wc->wcsc != NULL)
193     {
194         sfree(wc->wcsc);
195     }
196 #endif
197     sfree(wc);
198 }
199
200 static void wallcycle_all_start(gmx_wallcycle_t wc, int ewc, gmx_cycles_t cycle)
201 {
202     wc->ewc_prev   = ewc;
203     wc->cycle_prev = cycle;
204 }
205
206 static void wallcycle_all_stop(gmx_wallcycle_t wc, int ewc, gmx_cycles_t cycle)
207 {
208     wc->wcc_all[wc->ewc_prev*ewcNR+ewc].n += 1;
209     wc->wcc_all[wc->ewc_prev*ewcNR+ewc].c += cycle - wc->cycle_prev;
210 }
211
212
213 #ifdef DEBUG_WCYCLE
214 static void debug_start_check(gmx_wallcycle_t wc, int ewc)
215 {
216     /* fprintf(stderr,"wcycle_start depth %d, %s\n",wc->count_depth,wcn[ewc]); */
217
218     if (wc->count_depth < 0 || wc->count_depth >= DEPTH_MAX)
219     {
220         gmx_fatal(FARGS, "wallcycle counter depth out of range: %d",
221                   wc->count_depth);
222     }
223     wc->counterlist[wc->count_depth] = ewc;
224     wc->count_depth++;
225 }
226
227 static void debug_stop_check(gmx_wallcycle_t wc, int ewc)
228 {
229     wc->count_depth--;
230
231     /* fprintf(stderr,"wcycle_stop depth %d, %s\n",wc->count_depth,wcn[ewc]); */
232
233     if (wc->count_depth < 0)
234     {
235         gmx_fatal(FARGS, "wallcycle counter depth out of range when stopping %s: %d", wcn[ewc], wc->count_depth);
236     }
237     if (wc->counterlist[wc->count_depth] != ewc)
238     {
239         gmx_fatal(FARGS, "wallcycle mismatch at stop, start %s, stop %s",
240                   wcn[wc->counterlist[wc->count_depth]], wcn[ewc]);
241     }
242 }
243 #endif
244
245 void wallcycle_start(gmx_wallcycle_t wc, int ewc)
246 {
247     gmx_cycles_t cycle;
248
249     if (wc == NULL)
250     {
251         return;
252     }
253
254 #ifdef GMX_MPI
255     if (wc->wc_barrier)
256     {
257         MPI_Barrier(wc->mpi_comm_mygroup);
258     }
259 #endif
260
261 #ifdef DEBUG_WCYCLE
262     debug_start_check(wc, ewc);
263 #endif
264
265     cycle              = gmx_cycles_read();
266     wc->wcc[ewc].start = cycle;
267     if (wc->wcc_all != NULL)
268     {
269         wc->wc_depth++;
270         if (ewc == ewcRUN)
271         {
272             wallcycle_all_start(wc, ewc, cycle);
273         }
274         else if (wc->wc_depth == 3)
275         {
276             wallcycle_all_stop(wc, ewc, cycle);
277         }
278     }
279 }
280
281 void wallcycle_start_nocount(gmx_wallcycle_t wc, int ewc)
282 {
283     if (wc == NULL)
284     {
285         return;
286     }
287
288     wallcycle_start(wc, ewc);
289     wc->wcc[ewc].n--;
290 }
291
292 double wallcycle_stop(gmx_wallcycle_t wc, int ewc)
293 {
294     gmx_cycles_t cycle, last;
295
296     if (wc == NULL)
297     {
298         return 0;
299     }
300
301 #ifdef GMX_MPI
302     if (wc->wc_barrier)
303     {
304         MPI_Barrier(wc->mpi_comm_mygroup);
305     }
306 #endif
307
308 #ifdef DEBUG_WCYCLE
309     debug_stop_check(wc, ewc);
310 #endif
311
312     cycle           = gmx_cycles_read();
313     last            = cycle - wc->wcc[ewc].start;
314     wc->wcc[ewc].c += last;
315     wc->wcc[ewc].n++;
316     if (wc->wcc_all)
317     {
318         wc->wc_depth--;
319         if (ewc == ewcRUN)
320         {
321             wallcycle_all_stop(wc, ewc, cycle);
322         }
323         else if (wc->wc_depth == 2)
324         {
325             wallcycle_all_start(wc, ewc, cycle);
326         }
327     }
328
329     return last;
330 }
331
332 void wallcycle_reset_all(gmx_wallcycle_t wc)
333 {
334     int i;
335
336     if (wc == NULL)
337     {
338         return;
339     }
340
341     for (i = 0; i < ewcNR; i++)
342     {
343         wc->wcc[i].n = 0;
344         wc->wcc[i].c = 0;
345     }
346     if (wc->wcc_all)
347     {
348         for (i = 0; i < ewcNR*ewcNR; i++)
349         {
350             wc->wcc_all[i].n = 0;
351             wc->wcc_all[i].c = 0;
352         }
353     }
354 #ifdef GMX_CYCLE_SUBCOUNTERS
355     for (i = 0; i < ewcsNR; i++)
356     {
357         wc->wcsc[i].n = 0;
358         wc->wcsc[i].c = 0;
359     }
360 #endif
361 }
362
363 static gmx_bool is_pme_counter(int ewc)
364 {
365     return (ewc >= ewcPMEMESH && ewc <= ewcPMEWAITCOMM);
366 }
367
368 static gmx_bool is_pme_subcounter(int ewc)
369 {
370     return (ewc >= ewcPME_REDISTXF && ewc < ewcPMEWAITCOMM);
371 }
372
373 void wallcycle_sum(t_commrec *cr, gmx_wallcycle_t wc)
374 {
375     wallcc_t *wcc;
376     double    cycles[ewcNR+ewcsNR];
377     double    cycles_n[ewcNR+ewcsNR], buf[ewcNR+ewcsNR], *cyc_all, *buf_all;
378     int       i, j;
379     int       nsum;
380
381     if (wc == NULL)
382     {
383         return;
384     }
385
386     snew(wc->cycles_sum, ewcNR+ewcsNR);
387
388     wcc = wc->wcc;
389
390     for (i = 0; i < ewcNR; i++)
391     {
392         if (is_pme_counter(i) || (i == ewcRUN && cr->duty == DUTY_PME))
393         {
394             wcc[i].c *= wc->nthreads_pme;
395
396             if (wc->wcc_all)
397             {
398                 for (j = 0; j < ewcNR; j++)
399                 {
400                     wc->wcc_all[i*ewcNR+j].c *= wc->nthreads_pme;
401                 }
402             }
403         }
404         else
405         {
406             wcc[i].c *= wc->nthreads_pp;
407
408             if (wc->wcc_all)
409             {
410                 for (j = 0; j < ewcNR; j++)
411                 {
412                     wc->wcc_all[i*ewcNR+j].c *= wc->nthreads_pp;
413                 }
414             }
415         }
416     }
417
418     if (wcc[ewcDDCOMMLOAD].n > 0)
419     {
420         wcc[ewcDOMDEC].c -= wcc[ewcDDCOMMLOAD].c;
421     }
422     if (wcc[ewcDDCOMMBOUND].n > 0)
423     {
424         wcc[ewcDOMDEC].c -= wcc[ewcDDCOMMBOUND].c;
425     }
426     if (wcc[ewcPME_FFTCOMM].n > 0)
427     {
428         wcc[ewcPME_FFT].c -= wcc[ewcPME_FFTCOMM].c;
429     }
430
431     if (cr->npmenodes == 0)
432     {
433         /* All nodes do PME (or no PME at all) */
434         if (wcc[ewcPMEMESH].n > 0)
435         {
436             wcc[ewcFORCE].c -= wcc[ewcPMEMESH].c;
437         }
438     }
439     else
440     {
441         /* The are PME-only nodes */
442         if (wcc[ewcPMEMESH].n > 0)
443         {
444             /* This must be a PME only node, calculate the Wait + Comm. time */
445             wcc[ewcPMEWAITCOMM].c = wcc[ewcRUN].c - wcc[ewcPMEMESH].c;
446         }
447     }
448
449     /* Store the cycles in a double buffer for summing */
450     for (i = 0; i < ewcNR; i++)
451     {
452         cycles_n[i] = (double)wcc[i].n;
453         cycles[i]   = (double)wcc[i].c;
454     }
455     nsum = ewcNR;
456 #ifdef GMX_CYCLE_SUBCOUNTERS
457     for (i = 0; i < ewcsNR; i++)
458     {
459         wc->wcsc[i].c    *= wc->nthreads_pp;
460         cycles_n[ewcNR+i] = (double)wc->wcsc[i].n;
461         cycles[ewcNR+i]   = (double)wc->wcsc[i].c;
462     }
463     nsum += ewcsNR;
464 #endif
465
466 #ifdef GMX_MPI
467     if (cr->nnodes > 1)
468     {
469         MPI_Allreduce(cycles_n, buf, nsum, MPI_DOUBLE, MPI_MAX,
470                       cr->mpi_comm_mysim);
471         for (i = 0; i < ewcNR; i++)
472         {
473             wcc[i].n = (int)(buf[i] + 0.5);
474         }
475 #ifdef GMX_CYCLE_SUBCOUNTERS
476         for (i = 0; i < ewcsNR; i++)
477         {
478             wc->wcsc[i].n = (int)(buf[ewcNR+i] + 0.5);
479         }
480 #endif
481
482         MPI_Allreduce(cycles, wc->cycles_sum, nsum, MPI_DOUBLE, MPI_SUM,
483                       cr->mpi_comm_mysim);
484
485         if (wc->wcc_all != NULL)
486         {
487             snew(cyc_all, ewcNR*ewcNR);
488             snew(buf_all, ewcNR*ewcNR);
489             for (i = 0; i < ewcNR*ewcNR; i++)
490             {
491                 cyc_all[i] = wc->wcc_all[i].c;
492             }
493             MPI_Allreduce(cyc_all, buf_all, ewcNR*ewcNR, MPI_DOUBLE, MPI_SUM,
494                           cr->mpi_comm_mysim);
495             for (i = 0; i < ewcNR*ewcNR; i++)
496             {
497                 wc->wcc_all[i].c = buf_all[i];
498             }
499             sfree(buf_all);
500             sfree(cyc_all);
501         }
502     }
503     else
504 #endif
505     {
506         for (i = 0; i < nsum; i++)
507         {
508             wc->cycles_sum[i] = cycles[i];
509         }
510     }
511 }
512
513 static void print_cycles(FILE *fplog, double c2t, const char *name,
514                          int nnodes, int nthreads,
515                          int ncalls, double c_sum, double tot)
516 {
517     char   nnodes_str[6];
518     char   nthreads_str[6];
519     char   ncalls_str[11];
520     double wallt;
521
522     if (c_sum > 0)
523     {
524         if (ncalls > 0)
525         {
526             snprintf(ncalls_str, sizeof(ncalls_str), "%10d", ncalls);
527             if (nnodes < 0)
528             {
529                 snprintf(nnodes_str, sizeof(nnodes_str), "N/A");
530             }
531             else
532             {
533                 snprintf(nnodes_str, sizeof(nnodes_str), "%4d", nnodes);
534             }
535             if (nthreads < 0)
536             {
537                 snprintf(nthreads_str, sizeof(nthreads_str), "N/A");
538             }
539             else
540             {
541                 snprintf(nthreads_str, sizeof(nthreads_str), "%4d", nthreads);
542             }
543         }
544         else
545         {
546             nnodes_str[0]   = 0;
547             nthreads_str[0] = 0;
548             ncalls_str[0]   = 0;
549         }
550         /* Convert the cycle count to wallclock time for this task */
551         wallt = c_sum*c2t;
552
553         fprintf(fplog, " %-19.19s %4s %4s %10s  %10.3f %14.3f %5.1f\n",
554                 name, nnodes_str, nthreads_str, ncalls_str, wallt,
555                 c_sum*1e-9, 100*c_sum/tot);
556     }
557 }
558
559 static void print_gputimes(FILE *fplog, const char *name,
560                            int n, double t, double tot_t)
561 {
562     char num[11];
563     char avg_perf[11];
564
565     if (n > 0)
566     {
567         snprintf(num, sizeof(num), "%10d", n);
568         snprintf(avg_perf, sizeof(avg_perf), "%10.3f", t/n);
569     }
570     else
571     {
572         sprintf(num, "          ");
573         sprintf(avg_perf, "          ");
574     }
575     if (t != tot_t)
576     {
577         fprintf(fplog, " %-29s %10s%12.3f   %s   %5.1f\n",
578                 name, num, t/1000, avg_perf, 100 * t/tot_t);
579     }
580     else
581     {
582         fprintf(fplog, " %-29s %10s%12.3f   %s   %5.1f\n",
583                 name, "", t/1000, avg_perf, 100.0);
584     }
585 }
586
587 static void print_header(FILE *fplog, int nrank_pp, int nth_pp, int nrank_pme, int nth_pme)
588 {
589     int nrank_tot = nrank_pp + nrank_pme;
590     if (0 == nrank_pme)
591     {
592         fprintf(fplog, "On %d MPI rank%s", nrank_tot, nrank_tot == 1 ? "" : "s");
593         if (nth_pp > 1)
594         {
595             fprintf(fplog, ", each using %d OpenMP threads", nth_pp);
596         }
597         /* Don't report doing PP+PME, because we can't tell here if
598          * this is RF, etc. */
599     }
600     else
601     {
602         fprintf(fplog, "On %d MPI rank%s doing PP", nrank_pp, nrank_pp == 1 ? "" : "s");
603         if (nth_pp > 1)
604         {
605             fprintf(fplog, ",%s using %d OpenMP threads", nrank_pp > 1 ? " each" : "", nth_pp);
606         }
607         fprintf(fplog, ", and\non %d MPI rank%s doing PME", nrank_pme, nrank_pme == 1 ? "" : "s");
608         if (nth_pme > 1)
609         {
610             fprintf(fplog, ",%s using %d OpenMP threads", nrank_pme > 1 ? " each" : "", nth_pme);
611         }
612     }
613
614     fprintf(fplog, "\n\n");
615     fprintf(fplog, " Computing:          Num   Num      Call    Wall time         Giga-Cycles\n");
616     fprintf(fplog, "                     Ranks Threads  Count      (s)         total sum    %%\n");
617 }
618
619 void wallcycle_print(FILE *fplog, int nnodes, int npme, double realtime,
620                      gmx_wallcycle_t wc, wallclock_gpu_t *gpu_t)
621 {
622     double     *cyc_sum;
623     double      tot, tot_for_pp, tot_for_rest, tot_gpu, tot_cpu_overlap, gpu_cpu_ratio, tot_k;
624     double      c2t, c2t_pp, c2t_pme;
625     int         i, j, npp, nth_pp, nth_pme, nth_tot;
626     char        buf[STRLEN];
627     const char *hline = "-----------------------------------------------------------------------------";
628
629     if (wc == NULL)
630     {
631         return;
632     }
633
634     nth_pp  = wc->nthreads_pp;
635     nth_pme = wc->nthreads_pme;
636
637     cyc_sum = wc->cycles_sum;
638
639     npp     = nnodes - npme;
640     nth_tot = npp*nth_pp + npme*nth_pme;
641
642     /* When using PME-only nodes, the next line is valid for both
643        PP-only and PME-only nodes because they started ewcRUN at the
644        same time. */
645     tot        = cyc_sum[ewcRUN];
646     tot_for_pp = 0;
647
648     /* Conversion factor from cycles to seconds */
649     if (tot > 0)
650     {
651         c2t     = realtime/tot;
652         c2t_pp  = c2t * nth_tot / (double) (npp*nth_pp);
653         c2t_pme = c2t * nth_tot / (double) (npme*nth_pme);
654     }
655     else
656     {
657         c2t     = 0;
658         c2t_pp  = 0;
659         c2t_pme = 0;
660     }
661
662     fprintf(fplog, "\n     R E A L   C Y C L E   A N D   T I M E   A C C O U N T I N G\n\n");
663
664     print_header(fplog, npp, nth_pp, npme, nth_pme);
665
666     fprintf(fplog, "%s\n", hline);
667     for (i = ewcPPDURINGPME+1; i < ewcNR; i++)
668     {
669         if (is_pme_subcounter(i))
670         {
671             /* Do not count these at all */
672         }
673         else if (npme > 0 && is_pme_counter(i))
674         {
675             /* Print timing information for PME-only nodes, but add an
676              * asterisk so the reader of the table can know that the
677              * walltimes are not meant to add up. The asterisk still
678              * fits in the required maximum of 19 characters. */
679             char buffer[STRLEN];
680             snprintf(buffer, STRLEN, "%s *", wcn[i]);
681             print_cycles(fplog, c2t_pme, buffer,
682                          npme, nth_pme,
683                          wc->wcc[i].n, cyc_sum[i], tot);
684         }
685         else
686         {
687             /* Print timing information when it is for a PP or PP+PME
688                node */
689             print_cycles(fplog, c2t_pp, wcn[i],
690                          npp, nth_pp,
691                          wc->wcc[i].n, cyc_sum[i], tot);
692             tot_for_pp += cyc_sum[i];
693         }
694     }
695     if (wc->wcc_all != NULL)
696     {
697         for (i = 0; i < ewcNR; i++)
698         {
699             for (j = 0; j < ewcNR; j++)
700             {
701                 snprintf(buf, 20, "%-9.9s %-9.9s", wcn[i], wcn[j]);
702                 print_cycles(fplog, c2t_pp, buf,
703                              npp, nth_pp,
704                              wc->wcc_all[i*ewcNR+j].n,
705                              wc->wcc_all[i*ewcNR+j].c,
706                              tot);
707             }
708         }
709     }
710     tot_for_rest = tot * (npp * nth_pp) / (double) nth_tot;
711     print_cycles(fplog, c2t_pp, "Rest",
712                  npp, nth_pp,
713                  -1, tot_for_rest - tot_for_pp, tot);
714     fprintf(fplog, "%s\n", hline);
715     print_cycles(fplog, c2t, "Total",
716                  npp, nth_pp,
717                  -1, tot, tot);
718     fprintf(fplog, "%s\n", hline);
719
720     if (npme > 0)
721     {
722         fprintf(fplog,
723                 "(*) Note that with separate PME ranks, the walltime column actually sums to\n"
724                 "    twice the total reported, but the cycle count total and %% are correct.\n"
725                 "%s\n", hline);
726     }
727
728     if (wc->wcc[ewcPMEMESH].n > 0)
729     {
730         fprintf(fplog, " Breakdown of PME mesh computation\n");
731         fprintf(fplog, "%s\n", hline);
732         for (i = ewcPPDURINGPME+1; i < ewcNR; i++)
733         {
734             if (is_pme_subcounter(i))
735             {
736                 print_cycles(fplog, npme > 0 ? c2t_pme : c2t_pp, wcn[i],
737                              npme > 0 ? npme : npp, nth_pme,
738                              wc->wcc[i].n, cyc_sum[i], tot);
739             }
740         }
741         fprintf(fplog, "%s\n", hline);
742     }
743
744 #ifdef GMX_CYCLE_SUBCOUNTERS
745     fprintf(fplog, " Breakdown of PP computation\n");
746     fprintf(fplog, "%s\n", hline);
747     for (i = 0; i < ewcsNR; i++)
748     {
749         print_cycles(fplog, c2t_pp, wcsn[i],
750                      npp, nth_pp,
751                      wc->wcsc[i].n, cyc_sum[ewcNR+i], tot);
752     }
753     fprintf(fplog, "%s\n", hline);
754 #endif
755
756     /* print GPU timing summary */
757     if (gpu_t)
758     {
759         const char *k_log_str[2][2] = {
760             {"Nonbonded F kernel", "Nonbonded F+ene k."},
761             {"Nonbonded F+prune k.", "Nonbonded F+ene+prune k."}
762         };
763
764         tot_gpu = gpu_t->pl_h2d_t + gpu_t->nb_h2d_t + gpu_t->nb_d2h_t;
765
766         /* add up the kernel timings */
767         tot_k = 0.0;
768         for (i = 0; i < 2; i++)
769         {
770             for (j = 0; j < 2; j++)
771             {
772                 tot_k += gpu_t->ktime[i][j].t;
773             }
774         }
775         tot_gpu += tot_k;
776
777         tot_cpu_overlap = wc->wcc[ewcFORCE].c;
778         if (wc->wcc[ewcPMEMESH].n > 0)
779         {
780             tot_cpu_overlap += wc->wcc[ewcPMEMESH].c;
781         }
782         tot_cpu_overlap *= realtime*1000/tot; /* convert s to ms */
783
784         fprintf(fplog, "\n GPU timings\n%s\n", hline);
785         fprintf(fplog, " Computing:                         Count  Wall t (s)      ms/step       %c\n", '%');
786         fprintf(fplog, "%s\n", hline);
787         print_gputimes(fplog, "Pair list H2D",
788                        gpu_t->pl_h2d_c, gpu_t->pl_h2d_t, tot_gpu);
789         print_gputimes(fplog, "X / q H2D",
790                        gpu_t->nb_c, gpu_t->nb_h2d_t, tot_gpu);
791
792         for (i = 0; i < 2; i++)
793         {
794             for (j = 0; j < 2; j++)
795             {
796                 if (gpu_t->ktime[i][j].c)
797                 {
798                     print_gputimes(fplog, k_log_str[i][j],
799                                    gpu_t->ktime[i][j].c, gpu_t->ktime[i][j].t, tot_gpu);
800                 }
801             }
802         }
803
804         print_gputimes(fplog, "F D2H",  gpu_t->nb_c, gpu_t->nb_d2h_t, tot_gpu);
805         fprintf(fplog, "%s\n", hline);
806         print_gputimes(fplog, "Total ", gpu_t->nb_c, tot_gpu, tot_gpu);
807         fprintf(fplog, "%s\n", hline);
808
809         gpu_cpu_ratio = tot_gpu/tot_cpu_overlap;
810         fprintf(fplog, "\nForce evaluation time GPU/CPU: %.3f ms/%.3f ms = %.3f\n",
811                 tot_gpu/gpu_t->nb_c, tot_cpu_overlap/wc->wcc[ewcFORCE].n,
812                 gpu_cpu_ratio);
813
814         /* only print notes related to CPU-GPU load balance with PME */
815         if (wc->wcc[ewcPMEMESH].n > 0)
816         {
817             fprintf(fplog, "For optimal performance this ratio should be close to 1!\n");
818
819             /* print note if the imbalance is high with PME case in which
820              * CPU-GPU load balancing is possible */
821             if (gpu_cpu_ratio < 0.75 || gpu_cpu_ratio > 1.2)
822             {
823                 /* Only the sim master calls this function, so always print to stderr */
824                 if (gpu_cpu_ratio < 0.75)
825                 {
826                     if (npp > 1)
827                     {
828                         /* The user could have used -notunepme,
829                          * but we currently can't check that here.
830                          */
831                         md_print_warn(NULL, fplog,
832                                       "\nNOTE: The GPU has >25%% less load than the CPU. This imbalance causes\n"
833                                       "      performance loss. Maybe the domain decomposition limits the PME tuning.\n"
834                                       "      In that case, try setting the DD grid manually (-dd) or lowering -dds.");
835                     }
836                     else
837                     {
838                         /* We should not end up here, unless the box is
839                          * too small for increasing the cut-off for PME tuning.
840                          */
841                         md_print_warn(NULL, fplog,
842                                       "\nNOTE: The GPU has >25%% less load than the CPU. This imbalance causes\n"
843                                       "      performance loss.");
844                     }
845                 }
846                 if (gpu_cpu_ratio > 1.2)
847                 {
848                     md_print_warn(NULL, fplog,
849                                   "\nNOTE: The GPU has >20%% more load than the CPU. This imbalance causes\n"
850                                   "      performance loss, consider using a shorter cut-off and a finer PME grid.");
851                 }
852             }
853         }
854     }
855
856     if (wc->wcc[ewcNB_XF_BUF_OPS].n > 0 &&
857         (cyc_sum[ewcDOMDEC] > tot*0.1 ||
858          cyc_sum[ewcNS] > tot*0.1))
859     {
860         /* Only the sim master calls this function, so always print to stderr */
861         if (wc->wcc[ewcDOMDEC].n == 0)
862         {
863             md_print_warn(NULL, fplog,
864                           "NOTE: %d %% of the run time was spent in pair search,\n"
865                           "      you might want to increase nstlist (this has no effect on accuracy)\n",
866                           (int)(100*cyc_sum[ewcNS]/tot+0.5));
867         }
868         else
869         {
870             md_print_warn(NULL, fplog,
871                           "NOTE: %d %% of the run time was spent in domain decomposition,\n"
872                           "      %d %% of the run time was spent in pair search,\n"
873                           "      you might want to increase nstlist (this has no effect on accuracy)\n",
874                           (int)(100*cyc_sum[ewcDOMDEC]/tot+0.5),
875                           (int)(100*cyc_sum[ewcNS]/tot+0.5));
876         }
877     }
878
879     if (cyc_sum[ewcMoveE] > tot*0.05)
880     {
881         /* Only the sim master calls this function, so always print to stderr */
882         md_print_warn(NULL, fplog,
883                       "NOTE: %d %% of the run time was spent communicating energies,\n"
884                       "      you might want to use the -gcom option of mdrun\n",
885                       (int)(100*cyc_sum[ewcMoveE]/tot+0.5));
886     }
887 }
888
889 extern gmx_int64_t wcycle_get_reset_counters(gmx_wallcycle_t wc)
890 {
891     if (wc == NULL)
892     {
893         return -1;
894     }
895
896     return wc->reset_counters;
897 }
898
899 extern void wcycle_set_reset_counters(gmx_wallcycle_t wc, gmx_int64_t reset_counters)
900 {
901     if (wc == NULL)
902     {
903         return;
904     }
905
906     wc->reset_counters = reset_counters;
907 }
908
909 #ifdef GMX_CYCLE_SUBCOUNTERS
910
911 void wallcycle_sub_start(gmx_wallcycle_t wc, int ewcs)
912 {
913     if (wc != NULL)
914     {
915         wc->wcsc[ewcs].start = gmx_cycles_read();
916     }
917 }
918
919 void wallcycle_sub_stop(gmx_wallcycle_t wc, int ewcs)
920 {
921     if (wc != NULL)
922     {
923         wc->wcsc[ewcs].c += gmx_cycles_read() - wc->wcsc[ewcs].start;
924         wc->wcsc[ewcs].n++;
925     }
926 }
927
928 #else
929
930 void wallcycle_sub_start(gmx_wallcycle_t gmx_unused wc, int gmx_unused ewcs)
931 {
932 }
933 void wallcycle_sub_stop(gmx_wallcycle_t gmx_unused wc, int gmx_unused ewcs)
934 {
935 }
936
937 #endif /* GMX_CYCLE_SUBCOUNTERS */