9b026403c9f5aab52ca98062a07c6443822bd077
[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 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 #include "gmxpre.h"
38
39 #include "gromacs/gmxlib/nrnb.h"
40 #include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
41 #include "gromacs/gmxlib/nonbonded/nonbonded.h"
42 #include "gromacs/math/vectypes.h"
43 #include "gromacs/mdlib/enerdata_utils.h"
44 #include "gromacs/mdlib/force.h"
45 #include "gromacs/mdlib/gmx_omp_nthreads.h"
46 #include "gromacs/mdtypes/enerdata.h"
47 #include "gromacs/mdtypes/forceoutput.h"
48 #include "gromacs/mdtypes/forcerec.h"
49 #include "gromacs/mdtypes/inputrec.h"
50 #include "gromacs/mdtypes/interaction_const.h"
51 #include "gromacs/mdtypes/md_enums.h"
52 #include "gromacs/mdtypes/mdatom.h"
53 #include "gromacs/mdtypes/nblist.h"
54 #include "gromacs/mdtypes/simulation_workload.h"
55 #include "gromacs/nbnxm/gpu_data_mgmt.h"
56 #include "gromacs/nbnxm/nbnxm.h"
57 #include "gromacs/simd/simd.h"
58 #include "gromacs/timing/wallcycle.h"
59 #include "gromacs/utility/enumerationhelpers.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/real.h"
63
64 #include "kernel_common.h"
65 #include "nbnxm_gpu.h"
66 #include "nbnxm_gpu_data_mgmt.h"
67 #include "nbnxm_simd.h"
68 #include "pairlistset.h"
69 #include "pairlistsets.h"
70 #include "kernels_reference/kernel_gpu_ref.h"
71 #define INCLUDE_KERNELFUNCTION_TABLES
72 #include "kernels_reference/kernel_ref.h"
73 #ifdef GMX_NBNXN_SIMD_2XNN
74 #    include "kernels_simd_2xmm/kernels.h"
75 #endif
76 #ifdef GMX_NBNXN_SIMD_4XN
77 #    include "kernels_simd_4xm/kernels.h"
78 #endif
79 #undef INCLUDE_FUNCTION_TABLES
80
81 /*! \brief Clears the energy group output buffers
82  *
83  * \param[in,out] out  nbnxn kernel output struct
84  */
85 static void clearGroupEnergies(nbnxn_atomdata_output_t* out)
86 {
87     std::fill(out->Vvdw.begin(), out->Vvdw.end(), 0.0_real);
88     std::fill(out->Vc.begin(), out->Vc.end(), 0.0_real);
89     std::fill(out->VSvdw.begin(), out->VSvdw.end(), 0.0_real);
90     std::fill(out->VSc.begin(), out->VSc.end(), 0.0_real);
91 }
92
93 /*! \brief Reduce the group-pair energy buffers produced by a SIMD kernel
94  * to single terms in the output buffers.
95  *
96  * The SIMD kernels produce a large number of energy buffer in SIMD registers
97  * to avoid scattered reads and writes.
98  *
99  * \tparam        unrollj         The unroll size for j-particles in the SIMD kernel
100  * \param[in]     numGroups       The number of energy groups
101  * \param[in]     numGroups_2log  Log2 of numGroups, rounded up
102  * \param[in,out] out             Struct with energy buffers
103  */
104 template<int unrollj>
105 static void reduceGroupEnergySimdBuffers(int numGroups, int numGroups_2log, nbnxn_atomdata_output_t* out)
106 {
107     const int unrollj_half = unrollj / 2;
108     /* Energies are stored in SIMD registers with size 2^numGroups_2log */
109     const int numGroupsStorage = (1 << numGroups_2log);
110
111     const real* gmx_restrict vVdwSimd     = out->VSvdw.data();
112     const real* gmx_restrict vCoulombSimd = out->VSc.data();
113     real* gmx_restrict vVdw               = out->Vvdw.data();
114     real* gmx_restrict vCoulomb           = out->Vc.data();
115
116     /* The size of the SIMD energy group buffer array is:
117      * numGroups*numGroups*numGroupsStorage*unrollj_half*simd_width
118      */
119     for (int i = 0; i < numGroups; i++)
120     {
121         for (int j1 = 0; j1 < numGroups; j1++)
122         {
123             for (int j0 = 0; j0 < numGroups; j0++)
124             {
125                 int c = ((i * numGroups + j1) * numGroupsStorage + j0) * unrollj_half * unrollj;
126                 for (int s = 0; s < unrollj_half; s++)
127                 {
128                     vVdw[i * numGroups + j0] += vVdwSimd[c + 0];
129                     vVdw[i * numGroups + j1] += vVdwSimd[c + 1];
130                     vCoulomb[i * numGroups + j0] += vCoulombSimd[c + 0];
131                     vCoulomb[i * numGroups + j1] += vCoulombSimd[c + 1];
132                     c += unrollj + 2;
133                 }
134             }
135         }
136     }
137 }
138
139 static int getCoulombKernelType(const Nbnxm::KernelSetup& kernelSetup, const interaction_const_t& ic)
140 {
141
142     if (EEL_RF(ic.eeltype) || ic.eeltype == CoulombInteractionType::Cut)
143     {
144         return coulktRF;
145     }
146     else
147     {
148         if (kernelSetup.ewaldExclusionType == Nbnxm::EwaldExclusionType::Table)
149         {
150             if (ic.rcoulomb == ic.rvdw)
151             {
152                 return coulktTAB;
153             }
154             else
155             {
156                 return coulktTAB_TWIN;
157             }
158         }
159         else
160         {
161             if (ic.rcoulomb == ic.rvdw)
162             {
163                 return coulktEWALD;
164             }
165             else
166             {
167                 return coulktEWALD_TWIN;
168             }
169         }
170     }
171 }
172
173 static int getVdwKernelType(const Nbnxm::KernelSetup&       kernelSetup,
174                             const nbnxn_atomdata_t::Params& nbatParams,
175                             const interaction_const_t&      ic)
176 {
177     if (ic.vdwtype == VanDerWaalsType::Cut)
178     {
179         switch (ic.vdw_modifier)
180         {
181             case InteractionModifiers::None:
182             case InteractionModifiers::PotShift:
183                 switch (nbatParams.ljCombinationRule)
184                 {
185                     case LJCombinationRule::Geometric: return vdwktLJCUT_COMBGEOM;
186                     case LJCombinationRule::LorentzBerthelot: return vdwktLJCUT_COMBLB;
187                     case LJCombinationRule::None: return vdwktLJCUT_COMBNONE;
188                     default: gmx_incons("Unknown combination rule");
189                 }
190             case InteractionModifiers::ForceSwitch: return vdwktLJFORCESWITCH;
191             case InteractionModifiers::PotSwitch: return vdwktLJPOTSWITCH;
192             default:
193                 std::string errorMsg =
194                         gmx::formatString("Unsupported VdW interaction modifier %s (%d)",
195                                           enumValueToString(ic.vdw_modifier),
196                                           static_cast<int>(ic.vdw_modifier));
197                 gmx_incons(errorMsg);
198         }
199     }
200     else if (ic.vdwtype == VanDerWaalsType::Pme)
201     {
202         if (ic.ljpme_comb_rule == LongRangeVdW::Geom)
203         {
204             return vdwktLJEWALDCOMBGEOM;
205         }
206         else
207         {
208             /* At setup we (should have) selected the C reference kernel */
209             GMX_RELEASE_ASSERT(kernelSetup.kernelType == Nbnxm::KernelType::Cpu4x4_PlainC,
210                                "Only the C reference nbnxn SIMD kernel supports LJ-PME with LB "
211                                "combination rules");
212             return vdwktLJEWALDCOMBLB;
213         }
214     }
215     else
216     {
217         std::string errorMsg = gmx::formatString("Unsupported VdW interaction type %s (%d)",
218                                                  enumValueToString(ic.vdwtype),
219                                                  static_cast<int>(ic.vdwtype));
220         gmx_incons(errorMsg);
221     }
222 }
223
224 /*! \brief Dispatches the non-bonded N versus M atom cluster CPU kernels.
225  *
226  * OpenMP parallelization is performed within this function.
227  * Energy reduction, but not force and shift force reduction, is performed
228  * within this function.
229  *
230  * \param[in]     pairlistSet   Pairlists with local or non-local interactions to compute
231  * \param[in]     kernelSetup   The non-bonded kernel setup
232  * \param[in,out] nbat          The atomdata for the interactions
233  * \param[in]     ic            Non-bonded interaction constants
234  * \param[in]     shiftVectors  The PBC shift vectors
235  * \param[in]     stepWork      Flags that tell what to compute
236  * \param[in]     clearF        Enum that tells if to clear the force output buffer
237  * \param[out]    vCoulomb      Output buffer for Coulomb energies
238  * \param[out]    vVdw          Output buffer for Van der Waals energies
239  * \param[in]     wcycle        Pointer to cycle counting data structure.
240  */
241 static void nbnxn_kernel_cpu(const PairlistSet&             pairlistSet,
242                              const Nbnxm::KernelSetup&      kernelSetup,
243                              nbnxn_atomdata_t*              nbat,
244                              const interaction_const_t&     ic,
245                              gmx::ArrayRef<const gmx::RVec> shiftVectors,
246                              const gmx::StepWorkload&       stepWork,
247                              int                            clearF,
248                              real*                          vCoulomb,
249                              real*                          vVdw,
250                              gmx_wallcycle*                 wcycle)
251 {
252
253     const nbnxn_atomdata_t::Params& nbatParams = nbat->params();
254
255     const int coulkt = getCoulombKernelType(kernelSetup, ic);
256     const int vdwkt  = getVdwKernelType(kernelSetup, nbatParams, ic);
257
258     gmx::ArrayRef<const NbnxnPairlistCpu> pairlists = pairlistSet.cpuLists();
259
260     auto shiftVecPointer = as_rvec_array(shiftVectors.data());
261
262     int gmx_unused nthreads = gmx_omp_nthreads_get(emntNonbonded);
263     wallcycle_sub_start(wcycle, ewcsNONBONDED_CLEAR);
264 #pragma omp parallel for schedule(static) num_threads(nthreads)
265     for (gmx::index nb = 0; nb < pairlists.ssize(); nb++)
266     {
267         // Presently, the kernels do not call C++ code that can throw,
268         // so no need for a try/catch pair in this OpenMP region.
269         nbnxn_atomdata_output_t* out = &nbat->out[nb];
270
271         if (clearF == enbvClearFYes)
272         {
273             clearForceBuffer(nbat, nb);
274
275             clear_fshift(out->fshift.data());
276         }
277
278         if (nb == 0)
279         {
280             wallcycle_sub_stop(wcycle, ewcsNONBONDED_CLEAR);
281             wallcycle_sub_start(wcycle, ewcsNONBONDED_KERNEL);
282         }
283
284         // TODO: Change to reference
285         const NbnxnPairlistCpu* pairlist = &pairlists[nb];
286
287         if (!stepWork.computeEnergy)
288         {
289             /* Don't calculate energies */
290             switch (kernelSetup.kernelType)
291             {
292                 case Nbnxm::KernelType::Cpu4x4_PlainC:
293                     nbnxn_kernel_noener_ref[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
294                     break;
295 #ifdef GMX_NBNXN_SIMD_2XNN
296                 case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
297                     nbnxm_kernel_noener_simd_2xmm[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
298                     break;
299 #endif
300 #ifdef GMX_NBNXN_SIMD_4XN
301                 case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
302                     nbnxm_kernel_noener_simd_4xm[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
303                     break;
304 #endif
305                 default: GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
306             }
307         }
308         else if (out->Vvdw.size() == 1)
309         {
310             /* A single energy group (pair) */
311             out->Vvdw[0] = 0;
312             out->Vc[0]   = 0;
313
314             switch (kernelSetup.kernelType)
315             {
316                 case Nbnxm::KernelType::Cpu4x4_PlainC:
317                     nbnxn_kernel_ener_ref[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
318                     break;
319 #ifdef GMX_NBNXN_SIMD_2XNN
320                 case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
321                     nbnxm_kernel_ener_simd_2xmm[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
322                     break;
323 #endif
324 #ifdef GMX_NBNXN_SIMD_4XN
325                 case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
326                     nbnxm_kernel_ener_simd_4xm[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
327                     break;
328 #endif
329                 default: GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
330             }
331         }
332         else
333         {
334             /* Calculate energy group contributions */
335             clearGroupEnergies(out);
336
337             int unrollj = 0;
338
339             switch (kernelSetup.kernelType)
340             {
341                 case Nbnxm::KernelType::Cpu4x4_PlainC:
342                     unrollj = c_nbnxnCpuIClusterSize;
343                     nbnxn_kernel_energrp_ref[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
344                     break;
345 #ifdef GMX_NBNXN_SIMD_2XNN
346                 case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
347                     unrollj = GMX_SIMD_REAL_WIDTH / 2;
348                     nbnxm_kernel_energrp_simd_2xmm[coulkt][vdwkt](
349                             pairlist, nbat, &ic, shiftVecPointer, out);
350                     break;
351 #endif
352 #ifdef GMX_NBNXN_SIMD_4XN
353                 case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
354                     unrollj = GMX_SIMD_REAL_WIDTH;
355                     nbnxm_kernel_energrp_simd_4xm[coulkt][vdwkt](pairlist, nbat, &ic, shiftVecPointer, out);
356                     break;
357 #endif
358                 default: GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
359             }
360
361             if (kernelSetup.kernelType != Nbnxm::KernelType::Cpu4x4_PlainC)
362             {
363                 switch (unrollj)
364                 {
365                     case 2:
366                         reduceGroupEnergySimdBuffers<2>(nbatParams.nenergrp, nbatParams.neg_2log, out);
367                         break;
368                     case 4:
369                         reduceGroupEnergySimdBuffers<4>(nbatParams.nenergrp, nbatParams.neg_2log, out);
370                         break;
371                     case 8:
372                         reduceGroupEnergySimdBuffers<8>(nbatParams.nenergrp, nbatParams.neg_2log, out);
373                         break;
374                     default: GMX_RELEASE_ASSERT(false, "Unsupported j-unroll size");
375                 }
376             }
377         }
378     }
379     wallcycle_sub_stop(wcycle, ewcsNONBONDED_KERNEL);
380
381     if (stepWork.computeEnergy)
382     {
383         reduce_energies_over_lists(nbat, pairlists.ssize(), vVdw, vCoulomb);
384     }
385 }
386
387 static void accountFlops(t_nrnb*                    nrnb,
388                          const PairlistSet&         pairlistSet,
389                          const nonbonded_verlet_t&  nbv,
390                          const interaction_const_t& ic,
391                          const gmx::StepWorkload&   stepWork)
392 {
393     const bool usingGpuKernels = nbv.useGpu();
394
395     int enr_nbnxn_kernel_ljc = eNRNB;
396     if (EEL_RF(ic.eeltype) || ic.eeltype == CoulombInteractionType::Cut)
397     {
398         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
399     }
400     else if ((!usingGpuKernels && nbv.kernelSetup().ewaldExclusionType == Nbnxm::EwaldExclusionType::Analytical)
401              || (usingGpuKernels && Nbnxm::gpu_is_kernel_ewald_analytical(nbv.gpu_nbv)))
402     {
403         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
404     }
405     else
406     {
407         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
408     }
409     int enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
410     if (stepWork.computeEnergy)
411     {
412         /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
413         enr_nbnxn_kernel_ljc += 1;
414         enr_nbnxn_kernel_lj += 1;
415     }
416
417     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc, pairlistSet.natpair_ljq_);
418     inc_nrnb(nrnb, enr_nbnxn_kernel_lj, pairlistSet.natpair_lj_);
419     /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
420     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc - eNR_NBNXN_LJ_RF + eNR_NBNXN_RF, pairlistSet.natpair_q_);
421
422     if (ic.vdw_modifier == InteractionModifiers::ForceSwitch)
423     {
424         /* We add up the switch cost separately */
425         inc_nrnb(nrnb,
426                  eNR_NBNXN_ADD_LJ_FSW + (stepWork.computeEnergy ? 1 : 0),
427                  pairlistSet.natpair_ljq_ + pairlistSet.natpair_lj_);
428     }
429     if (ic.vdw_modifier == InteractionModifiers::PotSwitch)
430     {
431         /* We add up the switch cost separately */
432         inc_nrnb(nrnb,
433                  eNR_NBNXN_ADD_LJ_PSW + (stepWork.computeEnergy ? 1 : 0),
434                  pairlistSet.natpair_ljq_ + pairlistSet.natpair_lj_);
435     }
436     if (ic.vdwtype == VanDerWaalsType::Pme)
437     {
438         /* We add up the LJ Ewald cost separately */
439         inc_nrnb(nrnb,
440                  eNR_NBNXN_ADD_LJ_EWALD + (stepWork.computeEnergy ? 1 : 0),
441                  pairlistSet.natpair_ljq_ + pairlistSet.natpair_lj_);
442     }
443 }
444
445 void nonbonded_verlet_t::dispatchNonbondedKernel(gmx::InteractionLocality   iLocality,
446                                                  const interaction_const_t& ic,
447                                                  const gmx::StepWorkload&   stepWork,
448                                                  int                        clearF,
449                                                  const t_forcerec&          fr,
450                                                  gmx_enerdata_t*            enerd,
451                                                  t_nrnb*                    nrnb)
452 {
453     const PairlistSet& pairlistSet = pairlistSets().pairlistSet(iLocality);
454
455     switch (kernelSetup().kernelType)
456     {
457         case Nbnxm::KernelType::Cpu4x4_PlainC:
458         case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
459         case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
460             nbnxn_kernel_cpu(
461                     pairlistSet,
462                     kernelSetup(),
463                     nbat.get(),
464                     ic,
465                     fr.shift_vec,
466                     stepWork,
467                     clearF,
468                     enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::CoulombSR].data(),
469                     fr.haveBuckingham
470                             ? enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::BuckinghamSR].data()
471                             : enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::LJSR].data(),
472                     wcycle_);
473             break;
474
475         case Nbnxm::KernelType::Gpu8x8x8:
476             Nbnxm::gpu_launch_kernel(gpu_nbv, stepWork, iLocality);
477             break;
478
479         case Nbnxm::KernelType::Cpu8x8x8_PlainC:
480             nbnxn_kernel_gpu_ref(
481                     pairlistSet.gpuList(),
482                     nbat.get(),
483                     &ic,
484                     fr.shift_vec,
485                     stepWork,
486                     clearF,
487                     nbat->out[0].f,
488                     nbat->out[0].fshift.data(),
489                     enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::CoulombSR].data(),
490                     fr.haveBuckingham
491                             ? enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::BuckinghamSR].data()
492                             : enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::LJSR].data());
493             break;
494
495         default: GMX_RELEASE_ASSERT(false, "Invalid nonbonded kernel type passed!");
496     }
497
498     accountFlops(nrnb, pairlistSet, *this, ic, stepWork);
499 }
500
501 void nonbonded_verlet_t::dispatchFreeEnergyKernel(gmx::InteractionLocality       iLocality,
502                                                   const t_forcerec&              fr,
503                                                   gmx::ArrayRef<const gmx::RVec> coords,
504                                                   gmx::ForceWithShiftForces* forceWithShiftForces,
505                                                   gmx::ArrayRef<const real>  chargeA,
506                                                   gmx::ArrayRef<const real>  chargeB,
507                                                   gmx::ArrayRef<const int>   typeA,
508                                                   gmx::ArrayRef<const int>   typeB,
509                                                   t_lambda*                  fepvals,
510                                                   gmx::ArrayRef<const real>  lambda,
511                                                   gmx_enerdata_t*            enerd,
512                                                   const gmx::StepWorkload&   stepWork,
513                                                   t_nrnb*                    nrnb)
514 {
515     const auto nbl_fep = pairlistSets().pairlistSet(iLocality).fepLists();
516
517     /* When the first list is empty, all are empty and there is nothing to do */
518     if (!pairlistSets().params().haveFep || nbl_fep[0]->nrj == 0)
519     {
520         return;
521     }
522
523     int donb_flags = 0;
524     /* Add short-range interactions */
525     donb_flags |= GMX_NONBONDED_DO_SR;
526
527     if (stepWork.computeForces)
528     {
529         donb_flags |= GMX_NONBONDED_DO_FORCE;
530     }
531     if (stepWork.computeVirial)
532     {
533         donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
534     }
535     if (stepWork.computeEnergy)
536     {
537         donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
538     }
539
540     gmx::EnumerationArray<FreeEnergyPerturbationCouplingType, real> dvdl_nb      = { 0 };
541     int                                                             kernelFlags  = donb_flags;
542     gmx::ArrayRef<const real>                                       kernelLambda = lambda;
543     gmx::ArrayRef<real>                                             kernelDvdl   = dvdl_nb;
544
545     gmx::ArrayRef<real> energygrp_elec = enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::CoulombSR];
546     gmx::ArrayRef<real> energygrp_vdw = enerd->grpp.energyGroupPairTerms[NonBondedEnergyTerms::LJSR];
547
548     GMX_ASSERT(gmx_omp_nthreads_get(emntNonbonded) == nbl_fep.ssize(),
549                "Number of lists should be same as number of NB threads");
550
551     wallcycle_sub_start(wcycle_, ewcsNONBONDED_FEP);
552 #pragma omp parallel for schedule(static) num_threads(nbl_fep.ssize())
553     for (gmx::index th = 0; th < nbl_fep.ssize(); th++)
554     {
555         try
556         {
557             gmx_nb_free_energy_kernel(*nbl_fep[th],
558                                       coords,
559                                       forceWithShiftForces,
560                                       fr,
561                                       chargeA,
562                                       chargeB,
563                                       typeA,
564                                       typeB,
565                                       kernelFlags,
566                                       kernelLambda,
567                                       kernelDvdl,
568                                       energygrp_elec,
569                                       energygrp_vdw,
570                                       nrnb);
571         }
572         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
573     }
574
575     if (fepvals->sc_alpha != 0)
576     {
577         enerd->dvdl_nonlin[FreeEnergyPerturbationCouplingType::Vdw] +=
578                 dvdl_nb[FreeEnergyPerturbationCouplingType::Vdw];
579         enerd->dvdl_nonlin[FreeEnergyPerturbationCouplingType::Coul] +=
580                 dvdl_nb[FreeEnergyPerturbationCouplingType::Coul];
581     }
582     else
583     {
584         enerd->dvdl_lin[FreeEnergyPerturbationCouplingType::Vdw] +=
585                 dvdl_nb[FreeEnergyPerturbationCouplingType::Vdw];
586         enerd->dvdl_lin[FreeEnergyPerturbationCouplingType::Coul] +=
587                 dvdl_nb[FreeEnergyPerturbationCouplingType::Coul];
588     }
589
590     /* If we do foreign lambda and we have soft-core interactions
591      * we have to recalculate the (non-linear) energies contributions.
592      */
593     if (fepvals->n_lambda > 0 && stepWork.computeDhdl && fepvals->sc_alpha != 0)
594     {
595         gmx::EnumerationArray<FreeEnergyPerturbationCouplingType, real> lam_i;
596         kernelFlags = (donb_flags & ~(GMX_NONBONDED_DO_FORCE | GMX_NONBONDED_DO_SHIFTFORCE))
597                       | GMX_NONBONDED_DO_FOREIGNLAMBDA;
598         kernelLambda = lam_i;
599         kernelDvdl   = dvdl_nb;
600         gmx::ArrayRef<real> energygrp_elec =
601                 enerd->foreign_grpp.energyGroupPairTerms[NonBondedEnergyTerms::CoulombSR];
602         gmx::ArrayRef<real> energygrp_vdw =
603                 enerd->foreign_grpp.energyGroupPairTerms[NonBondedEnergyTerms::LJSR];
604
605         for (gmx::index i = 0; i < 1 + enerd->foreignLambdaTerms.numLambdas(); i++)
606         {
607             std::fill(std::begin(dvdl_nb), std::end(dvdl_nb), 0);
608             for (int j = 0; j < static_cast<int>(FreeEnergyPerturbationCouplingType::Count); j++)
609             {
610                 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i - 1]);
611             }
612             reset_foreign_enerdata(enerd);
613 #pragma omp parallel for schedule(static) num_threads(nbl_fep.ssize())
614             for (gmx::index th = 0; th < nbl_fep.ssize(); th++)
615             {
616                 try
617                 {
618                     gmx_nb_free_energy_kernel(*nbl_fep[th],
619                                               coords,
620                                               forceWithShiftForces,
621                                               fr,
622                                               chargeA,
623                                               chargeB,
624                                               typeA,
625                                               typeB,
626                                               kernelFlags,
627                                               kernelLambda,
628                                               kernelDvdl,
629                                               energygrp_elec,
630                                               energygrp_vdw,
631                                               nrnb);
632                 }
633                 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
634             }
635
636             sum_epot(enerd->foreign_grpp, enerd->foreign_term);
637             enerd->foreignLambdaTerms.accumulate(
638                     i,
639                     enerd->foreign_term[F_EPOT],
640                     dvdl_nb[FreeEnergyPerturbationCouplingType::Vdw]
641                             + dvdl_nb[FreeEnergyPerturbationCouplingType::Coul]);
642         }
643     }
644     wallcycle_sub_stop(wcycle_, ewcsNONBONDED_FEP);
645 }