a1ad25208ab981762c6ed29b9b2b79c8164c44fa
[alexxy/gromacs.git] / src / gromacs / nbnxm / kerneldispatch.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 #include "gmxpre.h"
37
38 #include "gromacs/gmxlib/nrnb.h"
39 #include "gromacs/math/vectypes.h"
40 #include "gromacs/mdlib/force_flags.h"
41 #include "gromacs/mdlib/gmx_omp_nthreads.h"
42 #include "gromacs/mdtypes/enerdata.h"
43 #include "gromacs/mdtypes/interaction_const.h"
44 #include "gromacs/mdtypes/md_enums.h"
45 #include "gromacs/nbnxm/gpu_data_mgmt.h"
46 #include "gromacs/nbnxm/nbnxm.h"
47 #include "gromacs/nbnxm/nbnxm_simd.h"
48 #include "gromacs/nbnxm/kernels_reference/kernel_gpu_ref.h"
49 #include "gromacs/simd/simd.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/real.h"
52
53 #include "kernel_common.h"
54 #define INCLUDE_KERNELFUNCTION_TABLES
55 #include "gromacs/nbnxm/kernels_reference/kernel_ref.h"
56 #ifdef GMX_NBNXN_SIMD_2XNN
57 #include "gromacs/nbnxm/kernels_simd_2xmm/kernels.h"
58 #endif
59 #ifdef GMX_NBNXN_SIMD_4XN
60 #include "gromacs/nbnxm/kernels_simd_4xm/kernels.h"
61 #endif
62 #undef INCLUDE_FUNCTION_TABLES
63
64 /*! \brief Clears the energy group output buffers
65  *
66  * \param[in,out] out  nbnxn kernel output struct
67  */
68 static void clearGroupEnergies(nbnxn_atomdata_output_t *out)
69 {
70     std::fill(out->Vvdw.begin(), out->Vvdw.end(), 0.0_real);
71     std::fill(out->Vc.begin(), out->Vc.end(), 0.0_real);
72     std::fill(out->VSvdw.begin(), out->VSvdw.end(), 0.0_real);
73     std::fill(out->VSc.begin(), out->VSc.end(), 0.0_real);
74 }
75
76 /*! \brief Reduce the group-pair energy buffers produced by a SIMD kernel
77  * to single terms in the output buffers.
78  *
79  * The SIMD kernels produce a large number of energy buffer in SIMD registers
80  * to avoid scattered reads and writes.
81  *
82  * \tparam        unrollj         The unroll size for j-particles in the SIMD kernel
83  * \param[in]     numGroups       The number of energy groups
84  * \param[in]     numGroups_2log  Log2 of numGroups, rounded up
85  * \param[in,out] out             Struct with energy buffers
86  */
87 template <int unrollj> static void
88 reduceGroupEnergySimdBuffers(int                       numGroups,
89                              int                       numGroups_2log,
90                              nbnxn_atomdata_output_t  *out)
91 {
92     const int                 unrollj_half     = unrollj/2;
93     /* Energies are stored in SIMD registers with size 2^numGroups_2log */
94     const int                 numGroupsStorage = (1 << numGroups_2log);
95
96     const real * gmx_restrict vVdwSimd     = out->VSvdw.data();
97     const real * gmx_restrict vCoulombSimd = out->VSc.data();
98     real * gmx_restrict       vVdw         = out->Vvdw.data();
99     real * gmx_restrict       vCoulomb     = out->Vc.data();
100
101     /* The size of the SIMD energy group buffer array is:
102      * numGroups*numGroups*numGroupsStorage*unrollj_half*simd_width
103      */
104     for (int i = 0; i < numGroups; i++)
105     {
106         for (int j1 = 0; j1 < numGroups; j1++)
107         {
108             for (int j0 = 0; j0 < numGroups; j0++)
109             {
110                 int c = ((i*numGroups + j1)*numGroupsStorage + j0)*unrollj_half*unrollj;
111                 for (int s = 0; s < unrollj_half; s++)
112                 {
113                     vVdw    [i*numGroups + j0] += vVdwSimd    [c + 0];
114                     vVdw    [i*numGroups + j1] += vVdwSimd    [c + 1];
115                     vCoulomb[i*numGroups + j0] += vCoulombSimd[c + 0];
116                     vCoulomb[i*numGroups + j1] += vCoulombSimd[c + 1];
117                     c                          += unrollj + 2;
118                 }
119             }
120         }
121     }
122 }
123
124 /*! \brief Dispatches the non-bonded N versus M atom cluster CPU kernels.
125  *
126  * OpenMP parallelization is performed within this function.
127  * Energy reduction, but not force and shift force reduction, is performed
128  * within this function.
129  *
130  * \param[in]     pairlistSet   Pairlists with local or non-local interactions to compute
131  * \param[in]     kernel_type   The non-bonded kernel type
132  * \param[in]     ewald_excl    The Ewald exclusion treatment
133  * \param[in,out] nbat          The atomdata for the interactions
134  * \param[in]     ic            Non-bonded interaction constants
135  * \param[in]     shiftVectors  The PBC shift vectors
136  * \param[in]     forceFlags    Flags that tell what to compute
137  * \param[in]     clearF        Enum that tells if to clear the force output buffer
138  * \param[out]    fshift        Shift force output buffer
139  * \param[out]    vCoulomb      Output buffer for Coulomb energies
140  * \param[out]    vVdw          Output buffer for Van der Waals energies
141  */
142 static void
143 nbnxn_kernel_cpu(const nbnxn_pairlist_set_t     &pairlistSet,
144                  const int                       kernel_type,
145                  const int                       ewald_excl,
146                  nbnxn_atomdata_t               *nbat,
147                  const interaction_const_t      &ic,
148                  rvec                           *shiftVectors,
149                  int                             forceFlags,
150                  int                             clearF,
151                  real                           *fshift,
152                  real                           *vCoulomb,
153                  real                           *vVdw)
154 {
155
156     int                      coulkt;
157     if (EEL_RF(ic.eeltype) || ic.eeltype == eelCUT)
158     {
159         coulkt = coulktRF;
160     }
161     else
162     {
163         if (ewald_excl == ewaldexclTable)
164         {
165             if (ic.rcoulomb == ic.rvdw)
166             {
167                 coulkt = coulktTAB;
168             }
169             else
170             {
171                 coulkt = coulktTAB_TWIN;
172             }
173         }
174         else
175         {
176             if (ic.rcoulomb == ic.rvdw)
177             {
178                 coulkt = coulktEWALD;
179             }
180             else
181             {
182                 coulkt = coulktEWALD_TWIN;
183             }
184         }
185     }
186
187     const nbnxn_atomdata_t::Params &nbatParams = nbat->params();
188
189     int vdwkt = 0;
190     if (ic.vdwtype == evdwCUT)
191     {
192         switch (ic.vdw_modifier)
193         {
194             case eintmodNONE:
195             case eintmodPOTSHIFT:
196                 switch (nbatParams.comb_rule)
197                 {
198                     case ljcrGEOM: vdwkt = vdwktLJCUT_COMBGEOM; break;
199                     case ljcrLB:   vdwkt = vdwktLJCUT_COMBLB;   break;
200                     case ljcrNONE: vdwkt = vdwktLJCUT_COMBNONE; break;
201                     default:
202                         GMX_RELEASE_ASSERT(false, "Unknown combination rule");
203                 }
204                 break;
205             case eintmodFORCESWITCH:
206                 vdwkt = vdwktLJFORCESWITCH;
207                 break;
208             case eintmodPOTSWITCH:
209                 vdwkt = vdwktLJPOTSWITCH;
210                 break;
211             default:
212                 GMX_RELEASE_ASSERT(false, "Unsupported VdW interaction modifier");
213         }
214     }
215     else if (ic.vdwtype == evdwPME)
216     {
217         if (ic.ljpme_comb_rule == eljpmeGEOM)
218         {
219             vdwkt = vdwktLJEWALDCOMBGEOM;
220         }
221         else
222         {
223             vdwkt = vdwktLJEWALDCOMBLB;
224             /* At setup we (should have) selected the C reference kernel */
225             GMX_RELEASE_ASSERT(kernel_type == nbnxnk4x4_PlainC, "Only the C reference nbnxn SIMD kernel supports LJ-PME with LB combination rules");
226         }
227     }
228     else
229     {
230         GMX_RELEASE_ASSERT(false, "Unsupported VdW interaction type");
231     }
232
233     int                        nnbl = pairlistSet.nnbl;
234     NbnxnPairlistCpu * const * nbl  = pairlistSet.nbl;
235
236     int gmx_unused             nthreads = gmx_omp_nthreads_get(emntNonbonded);
237 #pragma omp parallel for schedule(static) num_threads(nthreads)
238     for (int nb = 0; nb < nnbl; nb++)
239     {
240         // Presently, the kernels do not call C++ code that can throw,
241         // so no need for a try/catch pair in this OpenMP region.
242         nbnxn_atomdata_output_t *out = &nbat->out[nb];
243
244         if (clearF == enbvClearFYes)
245         {
246             clear_f(nbat, nb, out->f.data());
247         }
248
249         real *fshift_p;
250         if ((forceFlags & GMX_FORCE_VIRIAL) && nnbl == 1)
251         {
252             fshift_p = fshift;
253         }
254         else
255         {
256             fshift_p = out->fshift.data();
257
258             if (clearF == enbvClearFYes)
259             {
260                 clear_fshift(fshift_p);
261             }
262         }
263
264         if (!(forceFlags & GMX_FORCE_ENERGY))
265         {
266             /* Don't calculate energies */
267             switch (kernel_type)
268             {
269                 case nbnxnk4x4_PlainC:
270                     nbnxn_kernel_noener_ref[coulkt][vdwkt](nbl[nb], nbat,
271                                                            &ic,
272                                                            shiftVectors,
273                                                            out->f.data(),
274                                                            fshift_p);
275                     break;
276 #ifdef GMX_NBNXN_SIMD_2XNN
277                 case nbnxnk4xN_SIMD_2xNN:
278                     nbnxm_kernel_noener_simd_2xmm[coulkt][vdwkt](nbl[nb], nbat,
279                                                                  &ic,
280                                                                  shiftVectors,
281                                                                  out->f.data(),
282                                                                  fshift_p);
283                     break;
284 #endif
285 #ifdef GMX_NBNXN_SIMD_4XN
286                 case nbnxnk4xN_SIMD_4xN:
287                     nbnxm_kernel_noener_simd_4xm[coulkt][vdwkt](nbl[nb], nbat,
288                                                                 &ic,
289                                                                 shiftVectors,
290                                                                 out->f.data(),
291                                                                 fshift_p);
292                     break;
293 #endif
294                 default:
295                     GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
296             }
297         }
298         else if (out->Vvdw.size() == 1)
299         {
300             /* A single energy group (pair) */
301             out->Vvdw[0] = 0;
302             out->Vc[0]   = 0;
303
304             switch (kernel_type)
305             {
306                 case nbnxnk4x4_PlainC:
307                     nbnxn_kernel_ener_ref[coulkt][vdwkt](nbl[nb], nbat,
308                                                          &ic,
309                                                          shiftVectors,
310                                                          out->f.data(),
311                                                          fshift_p,
312                                                          out->Vvdw.data(),
313                                                          out->Vc.data());
314                     break;
315 #ifdef GMX_NBNXN_SIMD_2XNN
316                 case nbnxnk4xN_SIMD_2xNN:
317                     nbnxm_kernel_ener_simd_2xmm[coulkt][vdwkt](nbl[nb], nbat,
318                                                                &ic,
319                                                                shiftVectors,
320                                                                out->f.data(),
321                                                                fshift_p,
322                                                                out->Vvdw.data(),
323                                                                out->Vc.data());
324                     break;
325 #endif
326 #ifdef GMX_NBNXN_SIMD_4XN
327                 case nbnxnk4xN_SIMD_4xN:
328                     nbnxm_kernel_ener_simd_4xm[coulkt][vdwkt](nbl[nb], nbat,
329                                                               &ic,
330                                                               shiftVectors,
331                                                               out->f.data(),
332                                                               fshift_p,
333                                                               out->Vvdw.data(),
334                                                               out->Vc.data());
335                     break;
336 #endif
337                 default:
338                     GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
339             }
340         }
341         else
342         {
343             /* Calculate energy group contributions */
344             clearGroupEnergies(out);
345
346             int unrollj = 0;
347
348             switch (kernel_type)
349             {
350                 case nbnxnk4x4_PlainC:
351                     unrollj = c_nbnxnCpuIClusterSize;
352                     nbnxn_kernel_energrp_ref[coulkt][vdwkt](nbl[nb], nbat,
353                                                             &ic,
354                                                             shiftVectors,
355                                                             out->f.data(),
356                                                             fshift_p,
357                                                             out->Vvdw.data(),
358                                                             out->Vc.data());
359                     break;
360 #ifdef GMX_NBNXN_SIMD_2XNN
361                 case nbnxnk4xN_SIMD_2xNN:
362                     unrollj = GMX_SIMD_REAL_WIDTH/2;
363                     nbnxm_kernel_energrp_simd_2xmm[coulkt][vdwkt](nbl[nb], nbat,
364                                                                   &ic,
365                                                                   shiftVectors,
366                                                                   out->f.data(),
367                                                                   fshift_p,
368                                                                   out->VSvdw.data(),
369                                                                   out->VSc.data());
370                     break;
371 #endif
372 #ifdef GMX_NBNXN_SIMD_4XN
373                 case nbnxnk4xN_SIMD_4xN:
374                     unrollj = GMX_SIMD_REAL_WIDTH;
375                     nbnxm_kernel_energrp_simd_4xm[coulkt][vdwkt](nbl[nb], nbat,
376                                                                  &ic,
377                                                                  shiftVectors,
378                                                                  out->f.data(),
379                                                                  fshift_p,
380                                                                  out->VSvdw.data(),
381                                                                  out->VSc.data());
382                     break;
383 #endif
384                 default:
385                     GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
386             }
387
388             if (kernel_type != nbnxnk4x4_PlainC)
389             {
390                 switch (unrollj)
391                 {
392                     case 2:
393                         reduceGroupEnergySimdBuffers<2>(nbatParams.nenergrp,
394                                                         nbatParams.neg_2log,
395                                                         out);
396                         break;
397                     case 4:
398                         reduceGroupEnergySimdBuffers<4>(nbatParams.nenergrp,
399                                                         nbatParams.neg_2log,
400                                                         out);
401                         break;
402                     case 8:
403                         reduceGroupEnergySimdBuffers<8>(nbatParams.nenergrp,
404                                                         nbatParams.neg_2log,
405                                                         out);
406                         break;
407                     default:
408                         GMX_RELEASE_ASSERT(false, "Unsupported j-unroll size");
409                 }
410             }
411         }
412     }
413
414     if (forceFlags & GMX_FORCE_ENERGY)
415     {
416         reduce_energies_over_lists(nbat, nnbl, vVdw, vCoulomb);
417     }
418 }
419
420 static void accountFlops(t_nrnb                           *nrnb,
421                          const nonbonded_verlet_t         &nbv,
422                          const Nbnxm::InteractionLocality  iLocality,
423                          const interaction_const_t        &ic,
424                          const int                         forceFlags)
425 {
426     const nbnxn_pairlist_set_t &pairlistSet     = nbv.pairlistSets[iLocality];
427     const bool                  usingGpuKernels = nbv.useGpu();
428
429     int enr_nbnxn_kernel_ljc;
430     if (EEL_RF(ic.eeltype) || ic.eeltype == eelCUT)
431     {
432         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
433     }
434     else if ((!usingGpuKernels && nbv.ewaldExclusionType_ == ewaldexclAnalytical) ||
435              (usingGpuKernels && Nbnxm::gpu_is_kernel_ewald_analytical(nbv.gpu_nbv)))
436     {
437         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
438     }
439     else
440     {
441         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
442     }
443     int enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
444     if (forceFlags & GMX_FORCE_ENERGY)
445     {
446         /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
447         enr_nbnxn_kernel_ljc += 1;
448         enr_nbnxn_kernel_lj  += 1;
449     }
450
451     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
452              pairlistSet.natpair_ljq);
453     inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
454              pairlistSet.natpair_lj);
455     /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
456     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
457              pairlistSet.natpair_q);
458
459     const bool calcEnergy = ((forceFlags & GMX_FORCE_ENERGY) != 0);
460     if (ic.vdw_modifier == eintmodFORCESWITCH)
461     {
462         /* We add up the switch cost separately */
463         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_FSW + (calcEnergy ? 1 : 0),
464                  pairlistSet.natpair_ljq + pairlistSet.natpair_lj);
465     }
466     if (ic.vdw_modifier == eintmodPOTSWITCH)
467     {
468         /* We add up the switch cost separately */
469         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_PSW + (calcEnergy ? 1 : 0),
470                  pairlistSet.natpair_ljq + pairlistSet.natpair_lj);
471     }
472     if (ic.vdwtype == evdwPME)
473     {
474         /* We add up the LJ Ewald cost separately */
475         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_EWALD + (calcEnergy ? 1 : 0),
476                  pairlistSet.natpair_ljq + pairlistSet.natpair_lj);
477     }
478 }
479
480 void NbnxnDispatchKernel(nonbonded_verlet_t        *nbv,
481                          Nbnxm::InteractionLocality iLocality,
482                          const interaction_const_t &ic,
483                          int                        forceFlags,
484                          int                        clearF,
485                          t_forcerec                *fr,
486                          gmx_enerdata_t            *enerd,
487                          t_nrnb                    *nrnb)
488 {
489     const nbnxn_pairlist_set_t &pairlistSet = nbv->pairlistSets[iLocality];
490
491     switch (nbv->kernelType_)
492     {
493         case nbnxnk4x4_PlainC:
494         case nbnxnk4xN_SIMD_4xN:
495         case nbnxnk4xN_SIMD_2xNN:
496             nbnxn_kernel_cpu(pairlistSet,
497                              nbv->kernelType_,
498                              nbv->ewaldExclusionType_,
499                              nbv->nbat,
500                              ic,
501                              fr->shift_vec,
502                              forceFlags,
503                              clearF,
504                              fr->fshift[0],
505                              enerd->grpp.ener[egCOULSR],
506                              fr->bBHAM ?
507                              enerd->grpp.ener[egBHAMSR] :
508                              enerd->grpp.ener[egLJSR]);
509             break;
510
511         case nbnxnk8x8x8_GPU:
512             Nbnxm::gpu_launch_kernel(nbv->gpu_nbv, forceFlags, iLocality);
513             break;
514
515         case nbnxnk8x8x8_PlainC:
516             nbnxn_kernel_gpu_ref(pairlistSet.nblGpu[0],
517                                  nbv->nbat, &ic,
518                                  fr->shift_vec,
519                                  forceFlags,
520                                  clearF,
521                                  nbv->nbat->out[0].f,
522                                  fr->fshift[0],
523                                  enerd->grpp.ener[egCOULSR],
524                                  fr->bBHAM ?
525                                  enerd->grpp.ener[egBHAMSR] :
526                                  enerd->grpp.ener[egLJSR]);
527             break;
528
529         default:
530             GMX_RELEASE_ASSERT(false, "Invalid nonbonded kernel type passed!");
531
532     }
533
534     accountFlops(nrnb, *nbv, iLocality, ic, forceFlags);
535 }