b4b79b0b11fabed001234dbe33fc8d10fc19c9d0
[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/gmxlib/nonbonded/nb_free_energy.h"
40 #include "gromacs/gmxlib/nonbonded/nb_kernel.h"
41 #include "gromacs/gmxlib/nonbonded/nonbonded.h"
42 #include "gromacs/math/vectypes.h"
43 #include "gromacs/mdlib/force.h"
44 #include "gromacs/mdlib/force_flags.h"
45 #include "gromacs/mdlib/gmx_omp_nthreads.h"
46 #include "gromacs/mdtypes/enerdata.h"
47 #include "gromacs/mdtypes/inputrec.h"
48 #include "gromacs/mdtypes/interaction_const.h"
49 #include "gromacs/mdtypes/md_enums.h"
50 #include "gromacs/mdtypes/mdatom.h"
51 #include "gromacs/nbnxm/gpu_data_mgmt.h"
52 #include "gromacs/nbnxm/nbnxm.h"
53 #include "gromacs/nbnxm/nbnxm_simd.h"
54 #include "gromacs/nbnxm/kernels_reference/kernel_gpu_ref.h"
55 #include "gromacs/simd/simd.h"
56 #include "gromacs/utility/gmxassert.h"
57 #include "gromacs/utility/real.h"
58
59 #include "kernel_common.h"
60 #define INCLUDE_KERNELFUNCTION_TABLES
61 #include "gromacs/nbnxm/kernels_reference/kernel_ref.h"
62 #ifdef GMX_NBNXN_SIMD_2XNN
63 #include "gromacs/nbnxm/kernels_simd_2xmm/kernels.h"
64 #endif
65 #ifdef GMX_NBNXN_SIMD_4XN
66 #include "gromacs/nbnxm/kernels_simd_4xm/kernels.h"
67 #endif
68 #undef INCLUDE_FUNCTION_TABLES
69
70 /*! \brief Clears the energy group output buffers
71  *
72  * \param[in,out] out  nbnxn kernel output struct
73  */
74 static void clearGroupEnergies(nbnxn_atomdata_output_t *out)
75 {
76     std::fill(out->Vvdw.begin(), out->Vvdw.end(), 0.0_real);
77     std::fill(out->Vc.begin(), out->Vc.end(), 0.0_real);
78     std::fill(out->VSvdw.begin(), out->VSvdw.end(), 0.0_real);
79     std::fill(out->VSc.begin(), out->VSc.end(), 0.0_real);
80 }
81
82 /*! \brief Reduce the group-pair energy buffers produced by a SIMD kernel
83  * to single terms in the output buffers.
84  *
85  * The SIMD kernels produce a large number of energy buffer in SIMD registers
86  * to avoid scattered reads and writes.
87  *
88  * \tparam        unrollj         The unroll size for j-particles in the SIMD kernel
89  * \param[in]     numGroups       The number of energy groups
90  * \param[in]     numGroups_2log  Log2 of numGroups, rounded up
91  * \param[in,out] out             Struct with energy buffers
92  */
93 template <int unrollj> static void
94 reduceGroupEnergySimdBuffers(int                       numGroups,
95                              int                       numGroups_2log,
96                              nbnxn_atomdata_output_t  *out)
97 {
98     const int                 unrollj_half     = unrollj/2;
99     /* Energies are stored in SIMD registers with size 2^numGroups_2log */
100     const int                 numGroupsStorage = (1 << numGroups_2log);
101
102     const real * gmx_restrict vVdwSimd     = out->VSvdw.data();
103     const real * gmx_restrict vCoulombSimd = out->VSc.data();
104     real * gmx_restrict       vVdw         = out->Vvdw.data();
105     real * gmx_restrict       vCoulomb     = out->Vc.data();
106
107     /* The size of the SIMD energy group buffer array is:
108      * numGroups*numGroups*numGroupsStorage*unrollj_half*simd_width
109      */
110     for (int i = 0; i < numGroups; i++)
111     {
112         for (int j1 = 0; j1 < numGroups; j1++)
113         {
114             for (int j0 = 0; j0 < numGroups; j0++)
115             {
116                 int c = ((i*numGroups + j1)*numGroupsStorage + j0)*unrollj_half*unrollj;
117                 for (int s = 0; s < unrollj_half; s++)
118                 {
119                     vVdw    [i*numGroups + j0] += vVdwSimd    [c + 0];
120                     vVdw    [i*numGroups + j1] += vVdwSimd    [c + 1];
121                     vCoulomb[i*numGroups + j0] += vCoulombSimd[c + 0];
122                     vCoulomb[i*numGroups + j1] += vCoulombSimd[c + 1];
123                     c                          += unrollj + 2;
124                 }
125             }
126         }
127     }
128 }
129
130 /*! \brief Dispatches the non-bonded N versus M atom cluster CPU kernels.
131  *
132  * OpenMP parallelization is performed within this function.
133  * Energy reduction, but not force and shift force reduction, is performed
134  * within this function.
135  *
136  * \param[in]     pairlistSet   Pairlists with local or non-local interactions to compute
137  * \param[in]     kernelSetup   The non-bonded kernel setup
138  * \param[in,out] nbat          The atomdata for the interactions
139  * \param[in]     ic            Non-bonded interaction constants
140  * \param[in]     shiftVectors  The PBC shift vectors
141  * \param[in]     forceFlags    Flags that tell what to compute
142  * \param[in]     clearF        Enum that tells if to clear the force output buffer
143  * \param[out]    fshift        Shift force output buffer
144  * \param[out]    vCoulomb      Output buffer for Coulomb energies
145  * \param[out]    vVdw          Output buffer for Van der Waals energies
146  */
147 static void
148 nbnxn_kernel_cpu(const nbnxn_pairlist_set_t     &pairlistSet,
149                  const Nbnxm::KernelSetup       &kernelSetup,
150                  nbnxn_atomdata_t               *nbat,
151                  const interaction_const_t      &ic,
152                  rvec                           *shiftVectors,
153                  int                             forceFlags,
154                  int                             clearF,
155                  real                           *fshift,
156                  real                           *vCoulomb,
157                  real                           *vVdw)
158 {
159
160     int                      coulkt;
161     if (EEL_RF(ic.eeltype) || ic.eeltype == eelCUT)
162     {
163         coulkt = coulktRF;
164     }
165     else
166     {
167         if (kernelSetup.ewaldExclusionType == Nbnxm::EwaldExclusionType::Table)
168         {
169             if (ic.rcoulomb == ic.rvdw)
170             {
171                 coulkt = coulktTAB;
172             }
173             else
174             {
175                 coulkt = coulktTAB_TWIN;
176             }
177         }
178         else
179         {
180             if (ic.rcoulomb == ic.rvdw)
181             {
182                 coulkt = coulktEWALD;
183             }
184             else
185             {
186                 coulkt = coulktEWALD_TWIN;
187             }
188         }
189     }
190
191     const nbnxn_atomdata_t::Params &nbatParams = nbat->params();
192
193     int vdwkt = 0;
194     if (ic.vdwtype == evdwCUT)
195     {
196         switch (ic.vdw_modifier)
197         {
198             case eintmodNONE:
199             case eintmodPOTSHIFT:
200                 switch (nbatParams.comb_rule)
201                 {
202                     case ljcrGEOM: vdwkt = vdwktLJCUT_COMBGEOM; break;
203                     case ljcrLB:   vdwkt = vdwktLJCUT_COMBLB;   break;
204                     case ljcrNONE: vdwkt = vdwktLJCUT_COMBNONE; break;
205                     default:
206                         GMX_RELEASE_ASSERT(false, "Unknown combination rule");
207                 }
208                 break;
209             case eintmodFORCESWITCH:
210                 vdwkt = vdwktLJFORCESWITCH;
211                 break;
212             case eintmodPOTSWITCH:
213                 vdwkt = vdwktLJPOTSWITCH;
214                 break;
215             default:
216                 GMX_RELEASE_ASSERT(false, "Unsupported VdW interaction modifier");
217         }
218     }
219     else if (ic.vdwtype == evdwPME)
220     {
221         if (ic.ljpme_comb_rule == eljpmeGEOM)
222         {
223             vdwkt = vdwktLJEWALDCOMBGEOM;
224         }
225         else
226         {
227             vdwkt = vdwktLJEWALDCOMBLB;
228             /* At setup we (should have) selected the C reference kernel */
229             GMX_RELEASE_ASSERT(kernelSetup.kernelType == Nbnxm::KernelType::Cpu4x4_PlainC, "Only the C reference nbnxn SIMD kernel supports LJ-PME with LB combination rules");
230         }
231     }
232     else
233     {
234         GMX_RELEASE_ASSERT(false, "Unsupported VdW interaction type");
235     }
236
237     int                        nnbl = pairlistSet.nnbl;
238     NbnxnPairlistCpu * const * nbl  = pairlistSet.nbl;
239
240     int gmx_unused             nthreads = gmx_omp_nthreads_get(emntNonbonded);
241 #pragma omp parallel for schedule(static) num_threads(nthreads)
242     for (int nb = 0; nb < nnbl; nb++)
243     {
244         // Presently, the kernels do not call C++ code that can throw,
245         // so no need for a try/catch pair in this OpenMP region.
246         nbnxn_atomdata_output_t *out = &nbat->out[nb];
247
248         if (clearF == enbvClearFYes)
249         {
250             clear_f(nbat, nb, out->f.data());
251         }
252
253         real *fshift_p;
254         if ((forceFlags & GMX_FORCE_VIRIAL) && nnbl == 1)
255         {
256             fshift_p = fshift;
257         }
258         else
259         {
260             fshift_p = out->fshift.data();
261
262             if (clearF == enbvClearFYes)
263             {
264                 clear_fshift(fshift_p);
265             }
266         }
267
268         if (!(forceFlags & GMX_FORCE_ENERGY))
269         {
270             /* Don't calculate energies */
271             switch (kernelSetup.kernelType)
272             {
273                 case Nbnxm::KernelType::Cpu4x4_PlainC:
274                     nbnxn_kernel_noener_ref[coulkt][vdwkt](nbl[nb], nbat,
275                                                            &ic,
276                                                            shiftVectors,
277                                                            out->f.data(),
278                                                            fshift_p);
279                     break;
280 #ifdef GMX_NBNXN_SIMD_2XNN
281                 case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
282                     nbnxm_kernel_noener_simd_2xmm[coulkt][vdwkt](nbl[nb], nbat,
283                                                                  &ic,
284                                                                  shiftVectors,
285                                                                  out->f.data(),
286                                                                  fshift_p);
287                     break;
288 #endif
289 #ifdef GMX_NBNXN_SIMD_4XN
290                 case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
291                     nbnxm_kernel_noener_simd_4xm[coulkt][vdwkt](nbl[nb], nbat,
292                                                                 &ic,
293                                                                 shiftVectors,
294                                                                 out->f.data(),
295                                                                 fshift_p);
296                     break;
297 #endif
298                 default:
299                     GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
300             }
301         }
302         else if (out->Vvdw.size() == 1)
303         {
304             /* A single energy group (pair) */
305             out->Vvdw[0] = 0;
306             out->Vc[0]   = 0;
307
308             switch (kernelSetup.kernelType)
309             {
310                 case Nbnxm::KernelType::Cpu4x4_PlainC:
311                     nbnxn_kernel_ener_ref[coulkt][vdwkt](nbl[nb], nbat,
312                                                          &ic,
313                                                          shiftVectors,
314                                                          out->f.data(),
315                                                          fshift_p,
316                                                          out->Vvdw.data(),
317                                                          out->Vc.data());
318                     break;
319 #ifdef GMX_NBNXN_SIMD_2XNN
320                 case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
321                     nbnxm_kernel_ener_simd_2xmm[coulkt][vdwkt](nbl[nb], nbat,
322                                                                &ic,
323                                                                shiftVectors,
324                                                                out->f.data(),
325                                                                fshift_p,
326                                                                out->Vvdw.data(),
327                                                                out->Vc.data());
328                     break;
329 #endif
330 #ifdef GMX_NBNXN_SIMD_4XN
331                 case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
332                     nbnxm_kernel_ener_simd_4xm[coulkt][vdwkt](nbl[nb], nbat,
333                                                               &ic,
334                                                               shiftVectors,
335                                                               out->f.data(),
336                                                               fshift_p,
337                                                               out->Vvdw.data(),
338                                                               out->Vc.data());
339                     break;
340 #endif
341                 default:
342                     GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
343             }
344         }
345         else
346         {
347             /* Calculate energy group contributions */
348             clearGroupEnergies(out);
349
350             int unrollj = 0;
351
352             switch (kernelSetup.kernelType)
353             {
354                 case Nbnxm::KernelType::Cpu4x4_PlainC:
355                     unrollj = c_nbnxnCpuIClusterSize;
356                     nbnxn_kernel_energrp_ref[coulkt][vdwkt](nbl[nb], nbat,
357                                                             &ic,
358                                                             shiftVectors,
359                                                             out->f.data(),
360                                                             fshift_p,
361                                                             out->Vvdw.data(),
362                                                             out->Vc.data());
363                     break;
364 #ifdef GMX_NBNXN_SIMD_2XNN
365                 case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
366                     unrollj = GMX_SIMD_REAL_WIDTH/2;
367                     nbnxm_kernel_energrp_simd_2xmm[coulkt][vdwkt](nbl[nb], nbat,
368                                                                   &ic,
369                                                                   shiftVectors,
370                                                                   out->f.data(),
371                                                                   fshift_p,
372                                                                   out->VSvdw.data(),
373                                                                   out->VSc.data());
374                     break;
375 #endif
376 #ifdef GMX_NBNXN_SIMD_4XN
377                 case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
378                     unrollj = GMX_SIMD_REAL_WIDTH;
379                     nbnxm_kernel_energrp_simd_4xm[coulkt][vdwkt](nbl[nb], nbat,
380                                                                  &ic,
381                                                                  shiftVectors,
382                                                                  out->f.data(),
383                                                                  fshift_p,
384                                                                  out->VSvdw.data(),
385                                                                  out->VSc.data());
386                     break;
387 #endif
388                 default:
389                     GMX_RELEASE_ASSERT(false, "Unsupported kernel architecture");
390             }
391
392             if (kernelSetup.kernelType != Nbnxm::KernelType::Cpu4x4_PlainC)
393             {
394                 switch (unrollj)
395                 {
396                     case 2:
397                         reduceGroupEnergySimdBuffers<2>(nbatParams.nenergrp,
398                                                         nbatParams.neg_2log,
399                                                         out);
400                         break;
401                     case 4:
402                         reduceGroupEnergySimdBuffers<4>(nbatParams.nenergrp,
403                                                         nbatParams.neg_2log,
404                                                         out);
405                         break;
406                     case 8:
407                         reduceGroupEnergySimdBuffers<8>(nbatParams.nenergrp,
408                                                         nbatParams.neg_2log,
409                                                         out);
410                         break;
411                     default:
412                         GMX_RELEASE_ASSERT(false, "Unsupported j-unroll size");
413                 }
414             }
415         }
416     }
417
418     if (forceFlags & GMX_FORCE_ENERGY)
419     {
420         reduce_energies_over_lists(nbat, nnbl, vVdw, vCoulomb);
421     }
422 }
423
424 static void accountFlops(t_nrnb                           *nrnb,
425                          const nbnxn_pairlist_set_t       &pairlistSet,
426                          const nonbonded_verlet_t         &nbv,
427                          const interaction_const_t        &ic,
428                          const int                         forceFlags)
429 {
430     const bool usingGpuKernels = nbv.useGpu();
431
432     int        enr_nbnxn_kernel_ljc;
433     if (EEL_RF(ic.eeltype) || ic.eeltype == eelCUT)
434     {
435         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_RF;
436     }
437     else if ((!usingGpuKernels && nbv.kernelSetup().ewaldExclusionType == Nbnxm::EwaldExclusionType::Analytical) ||
438              (usingGpuKernels && Nbnxm::gpu_is_kernel_ewald_analytical(nbv.gpu_nbv)))
439     {
440         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_EWALD;
441     }
442     else
443     {
444         enr_nbnxn_kernel_ljc = eNR_NBNXN_LJ_TAB;
445     }
446     int enr_nbnxn_kernel_lj = eNR_NBNXN_LJ;
447     if (forceFlags & GMX_FORCE_ENERGY)
448     {
449         /* In eNR_??? the nbnxn F+E kernels are always the F kernel + 1 */
450         enr_nbnxn_kernel_ljc += 1;
451         enr_nbnxn_kernel_lj  += 1;
452     }
453
454     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc,
455              pairlistSet.natpair_ljq);
456     inc_nrnb(nrnb, enr_nbnxn_kernel_lj,
457              pairlistSet.natpair_lj);
458     /* The Coulomb-only kernels are offset -eNR_NBNXN_LJ_RF+eNR_NBNXN_RF */
459     inc_nrnb(nrnb, enr_nbnxn_kernel_ljc-eNR_NBNXN_LJ_RF+eNR_NBNXN_RF,
460              pairlistSet.natpair_q);
461
462     const bool calcEnergy = ((forceFlags & GMX_FORCE_ENERGY) != 0);
463     if (ic.vdw_modifier == eintmodFORCESWITCH)
464     {
465         /* We add up the switch cost separately */
466         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_FSW + (calcEnergy ? 1 : 0),
467                  pairlistSet.natpair_ljq + pairlistSet.natpair_lj);
468     }
469     if (ic.vdw_modifier == eintmodPOTSWITCH)
470     {
471         /* We add up the switch cost separately */
472         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_PSW + (calcEnergy ? 1 : 0),
473                  pairlistSet.natpair_ljq + pairlistSet.natpair_lj);
474     }
475     if (ic.vdwtype == evdwPME)
476     {
477         /* We add up the LJ Ewald cost separately */
478         inc_nrnb(nrnb, eNR_NBNXN_ADD_LJ_EWALD + (calcEnergy ? 1 : 0),
479                  pairlistSet.natpair_ljq + pairlistSet.natpair_lj);
480     }
481 }
482
483 void
484 nonbonded_verlet_t::dispatchNonbondedKernel(Nbnxm::InteractionLocality iLocality,
485                                             const interaction_const_t &ic,
486                                             int                        forceFlags,
487                                             int                        clearF,
488                                             t_forcerec                *fr,
489                                             gmx_enerdata_t            *enerd,
490                                             t_nrnb                    *nrnb)
491 {
492     const nbnxn_pairlist_set_t &pairlistSet = pairlistSets().pairlistSet(iLocality);
493
494     switch (kernelSetup().kernelType)
495     {
496         case Nbnxm::KernelType::Cpu4x4_PlainC:
497         case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
498         case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
499             nbnxn_kernel_cpu(pairlistSet,
500                              kernelSetup(),
501                              nbat,
502                              ic,
503                              fr->shift_vec,
504                              forceFlags,
505                              clearF,
506                              fr->fshift[0],
507                              enerd->grpp.ener[egCOULSR],
508                              fr->bBHAM ?
509                              enerd->grpp.ener[egBHAMSR] :
510                              enerd->grpp.ener[egLJSR]);
511             break;
512
513         case Nbnxm::KernelType::Gpu8x8x8:
514             Nbnxm::gpu_launch_kernel(gpu_nbv, forceFlags, iLocality);
515             break;
516
517         case Nbnxm::KernelType::Cpu8x8x8_PlainC:
518             nbnxn_kernel_gpu_ref(pairlistSet.nblGpu[0],
519                                  nbat, &ic,
520                                  fr->shift_vec,
521                                  forceFlags,
522                                  clearF,
523                                  nbat->out[0].f,
524                                  fr->fshift[0],
525                                  enerd->grpp.ener[egCOULSR],
526                                  fr->bBHAM ?
527                                  enerd->grpp.ener[egBHAMSR] :
528                                  enerd->grpp.ener[egLJSR]);
529             break;
530
531         default:
532             GMX_RELEASE_ASSERT(false, "Invalid nonbonded kernel type passed!");
533
534     }
535
536     accountFlops(nrnb, pairlistSet, *this, ic, forceFlags);
537 }
538
539 void
540 nonbonded_verlet_t::dispatchFreeEnergyKernel(Nbnxm::InteractionLocality  iLocality,
541                                              t_forcerec                 *fr,
542                                              rvec                        x[],
543                                              rvec                        f[],
544                                              const t_mdatoms            &mdatoms,
545                                              t_lambda                   *fepvals,
546                                              real                       *lambda,
547                                              gmx_enerdata_t             *enerd,
548                                              const int                   forceFlags,
549                                              t_nrnb                     *nrnb)
550 {
551     const gmx::ArrayRef<t_nblist const * const > nbl_fep = pairlistSets().pairlistSet(iLocality).nbl_fep;
552
553     /* When the first list is empty, all are empty and there is nothing to do */
554     if (nbl_fep[0]->nrj == 0)
555     {
556         return;
557     }
558
559     int donb_flags = 0;
560     /* Add short-range interactions */
561     donb_flags |= GMX_NONBONDED_DO_SR;
562
563     /* Currently all group scheme kernels always calculate (shift-)forces */
564     if (forceFlags & GMX_FORCE_FORCES)
565     {
566         donb_flags |= GMX_NONBONDED_DO_FORCE;
567     }
568     if (forceFlags & GMX_FORCE_VIRIAL)
569     {
570         donb_flags |= GMX_NONBONDED_DO_SHIFTFORCE;
571     }
572     if (forceFlags & GMX_FORCE_ENERGY)
573     {
574         donb_flags |= GMX_NONBONDED_DO_POTENTIAL;
575     }
576
577     nb_kernel_data_t kernel_data;
578     real             dvdl_nb[efptNR] = { 0 };
579     kernel_data.flags  = donb_flags;
580     kernel_data.lambda = lambda;
581     kernel_data.dvdl   = dvdl_nb;
582
583     kernel_data.energygrp_elec = enerd->grpp.ener[egCOULSR];
584     kernel_data.energygrp_vdw  = enerd->grpp.ener[egLJSR];
585
586     GMX_ASSERT(gmx_omp_nthreads_get(emntNonbonded) == nbl_fep.ssize(), "Number of lists should be same as number of NB threads");
587
588 #pragma omp parallel for schedule(static) num_threads(nbl_fep.ssize())
589     for (int th = 0; th < nbl_fep.ssize(); th++)
590     {
591         try
592         {
593             gmx_nb_free_energy_kernel(nbl_fep[th],
594                                       x, f, fr, &mdatoms, &kernel_data, nrnb);
595         }
596         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
597     }
598
599     if (fepvals->sc_alpha != 0)
600     {
601         enerd->dvdl_nonlin[efptVDW]  += dvdl_nb[efptVDW];
602         enerd->dvdl_nonlin[efptCOUL] += dvdl_nb[efptCOUL];
603     }
604     else
605     {
606         enerd->dvdl_lin[efptVDW]  += dvdl_nb[efptVDW];
607         enerd->dvdl_lin[efptCOUL] += dvdl_nb[efptCOUL];
608     }
609
610     /* If we do foreign lambda and we have soft-core interactions
611      * we have to recalculate the (non-linear) energies contributions.
612      */
613     if (fepvals->n_lambda > 0 && (forceFlags & GMX_FORCE_DHDL) && fepvals->sc_alpha != 0)
614     {
615         real lam_i[efptNR];
616         kernel_data.flags          = (donb_flags & ~(GMX_NONBONDED_DO_FORCE | GMX_NONBONDED_DO_SHIFTFORCE)) | GMX_NONBONDED_DO_FOREIGNLAMBDA;
617         kernel_data.lambda         = lam_i;
618         kernel_data.energygrp_elec = enerd->foreign_grpp.ener[egCOULSR];
619         kernel_data.energygrp_vdw  = enerd->foreign_grpp.ener[egLJSR];
620         /* Note that we add to kernel_data.dvdl, but ignore the result */
621
622         for (int i = 0; i < enerd->n_lambda; i++)
623         {
624             for (int j = 0; j < efptNR; j++)
625             {
626                 lam_i[j] = (i == 0 ? lambda[j] : fepvals->all_lambda[j][i-1]);
627             }
628             reset_foreign_enerdata(enerd);
629 #pragma omp parallel for schedule(static) num_threads(nbl_fep.ssize())
630             for (int th = 0; th < nbl_fep.ssize(); th++)
631             {
632                 try
633                 {
634                     gmx_nb_free_energy_kernel(nbl_fep[th],
635                                               x, f, fr, &mdatoms, &kernel_data, nrnb);
636                 }
637                 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
638             }
639
640             sum_epot(&(enerd->foreign_grpp), enerd->foreign_term);
641             enerd->enerpart_lambda[i] += enerd->foreign_term[F_EPOT];
642         }
643     }
644 }