Removed support for NVIDIA CC 2.x devices (codename Fermi)
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_cuda / nbnxn_cuda_kernel.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2017,2018, 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 /*! \internal \file
37  *  \brief
38  *  CUDA non-bonded kernel used through preprocessor-based code generation
39  *  of multiple kernel flavors, see nbnxn_cuda_kernels.cuh.
40  *
41  *  NOTE: No include fence as it is meant to be included multiple times.
42  *
43  *  \author Szilárd Páll <pall.szilard@gmail.com>
44  *  \author Berk Hess <hess@kth.se>
45  *  \ingroup module_mdlib
46  */
47
48 #include "gromacs/gpu_utils/cuda_arch_utils.cuh"
49 #include "gromacs/gpu_utils/cuda_kernel_utils.cuh"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/pbcutil/ishift.h"
52 /* Note that floating-point constants in CUDA code should be suffixed
53  * with f (e.g. 0.5f), to stop the compiler producing intermediate
54  * code that is in double precision.
55  */
56
57 #if defined EL_EWALD_ANA || defined EL_EWALD_TAB
58 /* Note: convenience macro, needs to be undef-ed at the end of the file. */
59 #define EL_EWALD_ANY
60 #endif
61
62 #if defined EL_EWALD_ANY || defined EL_RF || defined LJ_EWALD || (defined EL_CUTOFF && defined CALC_ENERGIES)
63 /* Macro to control the calculation of exclusion forces in the kernel
64  * We do that with Ewald (elec/vdw) and RF. Cut-off only has exclusion
65  * energy terms.
66  *
67  * Note: convenience macro, needs to be undef-ed at the end of the file.
68  */
69 #define EXCLUSION_FORCES
70 #endif
71
72 #if defined LJ_EWALD_COMB_GEOM || defined LJ_EWALD_COMB_LB
73 /* Note: convenience macro, needs to be undef-ed at the end of the file. */
74 #define LJ_EWALD
75 #endif
76
77 #if defined LJ_COMB_GEOM || defined LJ_COMB_LB
78 #define LJ_COMB
79 #endif
80
81 /*
82    Kernel launch parameters:
83     - #blocks   = #pair lists, blockId = pair list Id
84     - #threads  = NTHREAD_Z * c_clSize^2
85     - shmem     = see nbnxn_cuda.cu:calc_shmem_required_nonbonded()
86
87     Each thread calculates an i force-component taking one pair of i-j atoms.
88  */
89
90 /**@{*/
91 /*! \brief Compute capability dependent definition of kernel launch configuration parameters.
92  *
93  * NTHREAD_Z controls the number of j-clusters processed concurrently on NTHREAD_Z
94  * warp-pairs per block.
95  *
96  * - On CC 3.0-3.5, and >=5.0 NTHREAD_Z == 1, translating to 64 th/block with 16
97  * blocks/multiproc, is the fastest even though this setup gives low occupancy
98  * (except on 6.0).
99  * NTHREAD_Z > 1 results in excessive register spilling unless the minimum blocks
100  * per multiprocessor is reduced proportionally to get the original number of max
101  * threads in flight (and slightly lower performance).
102  * - On CC 3.7 there are enough registers to double the number of threads; using
103  * NTHREADS_Z == 2 is fastest with 16 blocks (TODO: test with RF and other kernels
104  * with low-register use).
105  *
106  * Note that the current kernel implementation only supports NTHREAD_Z > 1 with
107  * shuffle-based reduction, hence CC >= 3.0.
108  *
109  *
110  * NOTEs on Volta / CUDA 9 extensions:
111  *
112  * - While active thread masks are required for the warp collectives
113  *   (we use any and shfl), the kernel is designed such that all conditions
114  *   (other than the inner-most distance check) including loop trip counts
115  *   are warp-synchronous. Therefore, we don't need ballot to compute the
116  *   active masks as these are all full-warp masks.
117  *
118  * - TODO: reconsider the use of __syncwarp(): its only role is currently to prevent
119  *   WAR hazard due to the cj preload; we should try to replace it with direct
120  *   loads (which may be faster given the improved L1 on Volta).
121  */
122
123 /* Kernel launch bounds for different compute capabilities. The value of NTHREAD_Z
124  * determines the number of threads per block and it is chosen such that
125  * 16 blocks/multiprocessor can be kept in flight.
126  * - CC 3.0,3.5, and >=5.0: NTHREAD_Z=1, (64, 16) bounds
127  * - CC 3.7:                NTHREAD_Z=2, (128, 16) bounds
128  *
129  * Note: convenience macros, need to be undef-ed at the end of the file.
130  */
131 #if GMX_PTX_ARCH == 370
132     #define NTHREAD_Z           (2)
133     #define MIN_BLOCKS_PER_MP   (16)
134 #else
135     #define NTHREAD_Z           (1)
136     #define MIN_BLOCKS_PER_MP   (16)
137 #endif /* GMX_PTX_ARCH == 370 */
138 #define THREADS_PER_BLOCK   (c_clSize*c_clSize*NTHREAD_Z)
139
140 #if GMX_PTX_ARCH >= 350
141 /**@}*/
142 __launch_bounds__(THREADS_PER_BLOCK, MIN_BLOCKS_PER_MP)
143 #else
144 __launch_bounds__(THREADS_PER_BLOCK)
145 #endif /* GMX_PTX_ARCH >= 350 */
146 #ifdef PRUNE_NBL
147 #ifdef CALC_ENERGIES
148 __global__ void NB_KERNEL_FUNC_NAME(nbnxn_kernel, _VF_prune_cuda)
149 #else
150 __global__ void NB_KERNEL_FUNC_NAME(nbnxn_kernel, _F_prune_cuda)
151 #endif /* CALC_ENERGIES */
152 #else
153 #ifdef CALC_ENERGIES
154 __global__ void NB_KERNEL_FUNC_NAME(nbnxn_kernel, _VF_cuda)
155 #else
156 __global__ void NB_KERNEL_FUNC_NAME(nbnxn_kernel, _F_cuda)
157 #endif /* CALC_ENERGIES */
158 #endif /* PRUNE_NBL */
159 (const cu_atomdata_t atdat,
160  const cu_nbparam_t nbparam,
161  const cu_plist_t plist,
162  bool bCalcFshift)
163 #ifdef FUNCTION_DECLARATION_ONLY
164 ;     /* Only do function declaration, omit the function body. */
165 #else
166 {
167     /* convenience variables */
168     const nbnxn_sci_t *pl_sci       = plist.sci;
169 #ifndef PRUNE_NBL
170     const
171 #endif
172     nbnxn_cj4_t        *pl_cj4      = plist.cj4;
173     const nbnxn_excl_t *excl        = plist.excl;
174 #ifndef LJ_COMB
175     const int          *atom_types  = atdat.atom_types;
176     int                 ntypes      = atdat.ntypes;
177 #else
178     const float2       *lj_comb     = atdat.lj_comb;
179     float2              ljcp_i, ljcp_j;
180 #endif
181     const float4       *xq          = atdat.xq;
182     float3             *f           = atdat.f;
183     const float3       *shift_vec   = atdat.shift_vec;
184     float               rcoulomb_sq = nbparam.rcoulomb_sq;
185 #ifdef VDW_CUTOFF_CHECK
186     float               rvdw_sq     = nbparam.rvdw_sq;
187     float               vdw_in_range;
188 #endif
189 #ifdef LJ_EWALD
190     float               lje_coeff2, lje_coeff6_6;
191 #endif
192 #ifdef EL_RF
193     float two_k_rf              = nbparam.two_k_rf;
194 #endif
195 #ifdef EL_EWALD_ANA
196     float beta2                 = nbparam.ewald_beta*nbparam.ewald_beta;
197     float beta3                 = nbparam.ewald_beta*nbparam.ewald_beta*nbparam.ewald_beta;
198 #endif
199 #ifdef PRUNE_NBL
200     float rlist_sq              = nbparam.rlistOuter_sq;
201 #endif
202
203 #ifdef CALC_ENERGIES
204 #ifdef EL_EWALD_ANY
205     float  beta        = nbparam.ewald_beta;
206     float  ewald_shift = nbparam.sh_ewald;
207 #else
208     float  c_rf        = nbparam.c_rf;
209 #endif /* EL_EWALD_ANY */
210     float *e_lj        = atdat.e_lj;
211     float *e_el        = atdat.e_el;
212 #endif /* CALC_ENERGIES */
213
214     /* thread/block/warp id-s */
215     unsigned int tidxi  = threadIdx.x;
216     unsigned int tidxj  = threadIdx.y;
217     unsigned int tidx   = threadIdx.y * blockDim.x + threadIdx.x;
218 #if NTHREAD_Z == 1
219     unsigned int tidxz  = 0;
220 #else
221     unsigned int tidxz  = threadIdx.z;
222 #endif
223     unsigned int bidx   = blockIdx.x;
224     unsigned int widx   = tidx / warp_size; /* warp index */
225
226     int          sci, ci, cj,
227                  ai, aj,
228                  cij4_start, cij4_end;
229 #ifndef LJ_COMB
230     int          typei, typej;
231 #endif
232     int          i, jm, j4, wexcl_idx;
233     float        qi, qj_f,
234                  r2, inv_r, inv_r2;
235 #if !defined LJ_COMB_LB || defined CALC_ENERGIES
236     float        inv_r6, c6, c12;
237 #endif
238 #ifdef LJ_COMB_LB
239     float        sigma, epsilon;
240 #endif
241     float        int_bit,
242                  F_invr;
243 #ifdef CALC_ENERGIES
244     float        E_lj, E_el;
245 #endif
246 #if defined CALC_ENERGIES || defined LJ_POT_SWITCH
247     float        E_lj_p;
248 #endif
249     unsigned int wexcl, imask, mask_ji;
250     float4       xqbuf;
251     float3       xi, xj, rv, f_ij, fcj_buf;
252     float3       fci_buf[c_numClPerSupercl]; /* i force buffer */
253     nbnxn_sci_t  nb_sci;
254
255     /*! i-cluster interaction mask for a super-cluster with all c_numClPerSupercl=8 bits set */
256     const unsigned superClInteractionMask = ((1U << c_numClPerSupercl) - 1U);
257
258     /*********************************************************************
259      * Set up shared memory pointers.
260      * sm_nextSlotPtr should always be updated to point to the "next slot",
261      * that is past the last point where data has been stored.
262      */
263     extern __shared__  char sm_dynamicShmem[];
264     char                   *sm_nextSlotPtr = sm_dynamicShmem;
265     static_assert(sizeof(char) == 1, "The shared memory offset calculation assumes that char is 1 byte");
266
267     /* shmem buffer for i x+q pre-loading */
268     float4 *xqib    = (float4 *)sm_nextSlotPtr;
269     sm_nextSlotPtr += (c_numClPerSupercl * c_clSize * sizeof(*xqib));
270
271     /* shmem buffer for cj, for each warp separately */
272     int *cjs        = (int *)(sm_nextSlotPtr);
273     /* the cjs buffer's use expects a base pointer offset for pairs of warps in the j-concurrent execution */
274     cjs            += tidxz * c_nbnxnGpuClusterpairSplit * c_nbnxnGpuJgroupSize;
275     sm_nextSlotPtr += (NTHREAD_Z * c_nbnxnGpuClusterpairSplit * c_nbnxnGpuJgroupSize * sizeof(*cjs));
276
277 #ifndef LJ_COMB
278     /* shmem buffer for i atom-type pre-loading */
279     int *atib       = (int *)sm_nextSlotPtr;
280     sm_nextSlotPtr += (c_numClPerSupercl * c_clSize * sizeof(*atib));
281 #else
282     /* shmem buffer for i-atom LJ combination rule parameters */
283     float2 *ljcpib  = (float2 *)sm_nextSlotPtr;
284     sm_nextSlotPtr += (c_numClPerSupercl * c_clSize * sizeof(*ljcpib));
285 #endif
286     /*********************************************************************/
287
288     nb_sci      = pl_sci[bidx];         /* my i super-cluster's index = current bidx */
289     sci         = nb_sci.sci;           /* super-cluster */
290     cij4_start  = nb_sci.cj4_ind_start; /* first ...*/
291     cij4_end    = nb_sci.cj4_ind_end;   /* and last index of j clusters */
292
293     if (tidxz == 0)
294     {
295         /* Pre-load i-atom x and q into shared memory */
296         ci = sci * c_numClPerSupercl + tidxj;
297         ai = ci * c_clSize + tidxi;
298
299         float  *shiftptr = (float *)&shift_vec[nb_sci.shift];
300         xqbuf    = xq[ai] + make_float4(LDG(shiftptr), LDG(shiftptr + 1), LDG(shiftptr + 2), 0.0f);
301         xqbuf.w *= nbparam.epsfac;
302         xqib[tidxj * c_clSize + tidxi] = xqbuf;
303
304 #ifndef LJ_COMB
305         /* Pre-load the i-atom types into shared memory */
306         atib[tidxj * c_clSize + tidxi] = atom_types[ai];
307 #else
308         /* Pre-load the LJ combination parameters into shared memory */
309         ljcpib[tidxj * c_clSize + tidxi] = lj_comb[ai];
310 #endif
311     }
312     __syncthreads();
313
314     for (i = 0; i < c_numClPerSupercl; i++)
315     {
316         fci_buf[i] = make_float3(0.0f);
317     }
318
319 #ifdef LJ_EWALD
320     /* TODO: we are trading registers with flops by keeping lje_coeff-s, try re-calculating it later */
321     lje_coeff2   = nbparam.ewaldcoeff_lj*nbparam.ewaldcoeff_lj;
322     lje_coeff6_6 = lje_coeff2*lje_coeff2*lje_coeff2*c_oneSixth;
323 #endif
324
325
326 #ifdef CALC_ENERGIES
327     E_lj = 0.0f;
328     E_el = 0.0f;
329
330 #ifdef EXCLUSION_FORCES /* Ewald or RF */
331     if (nb_sci.shift == CENTRAL && pl_cj4[cij4_start].cj[0] == sci*c_numClPerSupercl)
332     {
333         /* we have the diagonal: add the charge and LJ self interaction energy term */
334         for (i = 0; i < c_numClPerSupercl; i++)
335         {
336 #if defined EL_EWALD_ANY || defined EL_RF || defined EL_CUTOFF
337             qi    = xqib[i * c_clSize + tidxi].w;
338             E_el += qi*qi;
339 #endif
340
341 #ifdef LJ_EWALD
342     #if DISABLE_CUDA_TEXTURES
343             E_lj += LDG(&nbparam.nbfp[atom_types[(sci*c_numClPerSupercl + i)*c_clSize + tidxi]*(ntypes + 1)*2]);
344     #else
345             E_lj += tex1Dfetch<float>(nbparam.nbfp_texobj, atom_types[(sci*c_numClPerSupercl + i)*c_clSize + tidxi]*(ntypes + 1)*2);
346     #endif
347 #endif
348         }
349
350         /* divide the self term(s) equally over the j-threads, then multiply with the coefficients. */
351 #ifdef LJ_EWALD
352         E_lj /= c_clSize*NTHREAD_Z;
353         E_lj *= 0.5f*c_oneSixth*lje_coeff6_6;
354 #endif
355
356 #if defined EL_EWALD_ANY || defined EL_RF || defined EL_CUTOFF
357         /* Correct for epsfac^2 due to adding qi^2 */
358         E_el /= nbparam.epsfac*c_clSize*NTHREAD_Z;
359 #if defined EL_RF || defined EL_CUTOFF
360         E_el *= -0.5f*c_rf;
361 #else
362         E_el *= -beta*M_FLOAT_1_SQRTPI; /* last factor 1/sqrt(pi) */
363 #endif
364 #endif                                  /* EL_EWALD_ANY || defined EL_RF || defined EL_CUTOFF */
365     }
366 #endif                                  /* EXCLUSION_FORCES */
367
368 #endif                                  /* CALC_ENERGIES */
369
370 #ifdef EXCLUSION_FORCES
371     const int nonSelfInteraction = !(nb_sci.shift == CENTRAL & tidxj <= tidxi);
372 #endif
373
374     /* loop over the j clusters = seen by any of the atoms in the current super-cluster;
375      * The loop stride NTHREAD_Z ensures that consecutive warps-pairs are assigned
376      * consecutive j4's entries.
377      */
378     for (j4 = cij4_start + tidxz; j4 < cij4_end; j4 += NTHREAD_Z)
379     {
380         wexcl_idx   = pl_cj4[j4].imei[widx].excl_ind;
381         imask       = pl_cj4[j4].imei[widx].imask;
382         wexcl       = excl[wexcl_idx].pair[(tidx) & (warp_size - 1)];
383
384 #ifndef PRUNE_NBL
385         if (imask)
386 #endif
387         {
388             /* Pre-load cj into shared memory on both warps separately */
389             if ((tidxj == 0 | tidxj == 4) & (tidxi < c_nbnxnGpuJgroupSize))
390             {
391                 cjs[tidxi + tidxj * c_nbnxnGpuJgroupSize/c_splitClSize] = pl_cj4[j4].cj[tidxi];
392             }
393             gmx_syncwarp(c_fullWarpMask);
394
395             /* Unrolling this loop
396                - with pruning leads to register spilling;
397                - on Kepler and later it is much slower;
398                Tested with up to nvcc 7.5 */
399             for (jm = 0; jm < c_nbnxnGpuJgroupSize; jm++)
400             {
401                 if (imask & (superClInteractionMask << (jm * c_numClPerSupercl)))
402                 {
403                     mask_ji = (1U << (jm * c_numClPerSupercl));
404
405                     cj      = cjs[jm + (tidxj & 4) * c_nbnxnGpuJgroupSize/c_splitClSize];
406                     aj      = cj * c_clSize + tidxj;
407
408                     /* load j atom data */
409                     xqbuf   = xq[aj];
410                     xj      = make_float3(xqbuf.x, xqbuf.y, xqbuf.z);
411                     qj_f    = xqbuf.w;
412 #ifndef LJ_COMB
413                     typej   = atom_types[aj];
414 #else
415                     ljcp_j  = lj_comb[aj];
416 #endif
417
418                     fcj_buf = make_float3(0.0f);
419
420 #if !defined PRUNE_NBL
421 #pragma unroll 8
422 #endif
423                     for (i = 0; i < c_numClPerSupercl; i++)
424                     {
425                         if (imask & mask_ji)
426                         {
427                             ci      = sci * c_numClPerSupercl + i; /* i cluster index */
428
429                             /* all threads load an atom from i cluster ci into shmem! */
430                             xqbuf   = xqib[i * c_clSize + tidxi];
431                             xi      = make_float3(xqbuf.x, xqbuf.y, xqbuf.z);
432
433                             /* distance between i and j atoms */
434                             rv      = xi - xj;
435                             r2      = norm2(rv);
436
437 #ifdef PRUNE_NBL
438                             /* If _none_ of the atoms pairs are in cutoff range,
439                                the bit corresponding to the current
440                                cluster-pair in imask gets set to 0. */
441                             if (!gmx_any_sync(c_fullWarpMask, r2 < rlist_sq))
442                             {
443                                 imask &= ~mask_ji;
444                             }
445 #endif
446
447                             int_bit = (wexcl & mask_ji) ? 1.0f : 0.0f;
448
449                             /* cutoff & exclusion check */
450 #ifdef EXCLUSION_FORCES
451                             if ((r2 < rcoulomb_sq) * (nonSelfInteraction | (ci != cj)))
452 #else
453                             if ((r2 < rcoulomb_sq) * int_bit)
454 #endif
455                             {
456                                 /* load the rest of the i-atom parameters */
457                                 qi      = xqbuf.w;
458
459 #ifndef LJ_COMB
460                                 /* LJ 6*C6 and 12*C12 */
461                                 typei   = atib[i * c_clSize + tidxi];
462                                 fetch_nbfp_c6_c12(c6, c12, nbparam, ntypes * typei + typej);
463 #else
464                                 ljcp_i  = ljcpib[i * c_clSize + tidxi];
465 #ifdef LJ_COMB_GEOM
466                                 c6      = ljcp_i.x * ljcp_j.x;
467                                 c12     = ljcp_i.y * ljcp_j.y;
468 #else
469                                 /* LJ 2^(1/6)*sigma and 12*epsilon */
470                                 sigma   = ljcp_i.x + ljcp_j.x;
471                                 epsilon = ljcp_i.y * ljcp_j.y;
472 #if defined CALC_ENERGIES || defined LJ_FORCE_SWITCH || defined LJ_POT_SWITCH
473                                 convert_sigma_epsilon_to_c6_c12(sigma, epsilon, &c6, &c12);
474 #endif
475 #endif                          /* LJ_COMB_GEOM */
476 #endif                          /* LJ_COMB */
477
478                                 // Ensure distance do not become so small that r^-12 overflows
479                                 r2      = max(r2, NBNXN_MIN_RSQ);
480
481                                 inv_r   = rsqrt(r2);
482                                 inv_r2  = inv_r * inv_r;
483 #if !defined LJ_COMB_LB || defined CALC_ENERGIES
484                                 inv_r6  = inv_r2 * inv_r2 * inv_r2;
485 #ifdef EXCLUSION_FORCES
486                                 /* We could mask inv_r2, but with Ewald
487                                  * masking both inv_r6 and F_invr is faster */
488                                 inv_r6  *= int_bit;
489 #endif                          /* EXCLUSION_FORCES */
490
491                                 F_invr  = inv_r6 * (c12 * inv_r6 - c6) * inv_r2;
492 #if defined CALC_ENERGIES || defined LJ_POT_SWITCH
493                                 E_lj_p  = int_bit * (c12 * (inv_r6 * inv_r6 + nbparam.repulsion_shift.cpot)*c_oneTwelveth -
494                                                      c6 * (inv_r6 + nbparam.dispersion_shift.cpot)*c_oneSixth);
495 #endif
496 #else                           /* !LJ_COMB_LB || CALC_ENERGIES */
497                                 float sig_r  = sigma*inv_r;
498                                 float sig_r2 = sig_r*sig_r;
499                                 float sig_r6 = sig_r2*sig_r2*sig_r2;
500 #ifdef EXCLUSION_FORCES
501                                 sig_r6 *= int_bit;
502 #endif                          /* EXCLUSION_FORCES */
503
504                                 F_invr  = epsilon * sig_r6 * (sig_r6 - 1.0f) * inv_r2;
505 #endif                          /* !LJ_COMB_LB || CALC_ENERGIES */
506
507 #ifdef LJ_FORCE_SWITCH
508 #ifdef CALC_ENERGIES
509                                 calculate_force_switch_F_E(nbparam, c6, c12, inv_r, r2, &F_invr, &E_lj_p);
510 #else
511                                 calculate_force_switch_F(nbparam, c6, c12, inv_r, r2, &F_invr);
512 #endif /* CALC_ENERGIES */
513 #endif /* LJ_FORCE_SWITCH */
514
515
516 #ifdef LJ_EWALD
517 #ifdef LJ_EWALD_COMB_GEOM
518 #ifdef CALC_ENERGIES
519                                 calculate_lj_ewald_comb_geom_F_E(nbparam, typei, typej, r2, inv_r2, lje_coeff2, lje_coeff6_6, int_bit, &F_invr, &E_lj_p);
520 #else
521                                 calculate_lj_ewald_comb_geom_F(nbparam, typei, typej, r2, inv_r2, lje_coeff2, lje_coeff6_6, &F_invr);
522 #endif                          /* CALC_ENERGIES */
523 #elif defined LJ_EWALD_COMB_LB
524                                 calculate_lj_ewald_comb_LB_F_E(nbparam, typei, typej, r2, inv_r2, lje_coeff2, lje_coeff6_6,
525 #ifdef CALC_ENERGIES
526                                                                int_bit, &F_invr, &E_lj_p
527 #else
528                                                                0, &F_invr, nullptr
529 #endif /* CALC_ENERGIES */
530                                                                );
531 #endif /* LJ_EWALD_COMB_GEOM */
532 #endif /* LJ_EWALD */
533
534 #ifdef LJ_POT_SWITCH
535 #ifdef CALC_ENERGIES
536                                 calculate_potential_switch_F_E(nbparam, inv_r, r2, &F_invr, &E_lj_p);
537 #else
538                                 calculate_potential_switch_F(nbparam, inv_r, r2, &F_invr, &E_lj_p);
539 #endif /* CALC_ENERGIES */
540 #endif /* LJ_POT_SWITCH */
541
542 #ifdef VDW_CUTOFF_CHECK
543                                 /* Separate VDW cut-off check to enable twin-range cut-offs
544                                  * (rvdw < rcoulomb <= rlist)
545                                  */
546                                 vdw_in_range  = (r2 < rvdw_sq) ? 1.0f : 0.0f;
547                                 F_invr       *= vdw_in_range;
548 #ifdef CALC_ENERGIES
549                                 E_lj_p       *= vdw_in_range;
550 #endif
551 #endif                          /* VDW_CUTOFF_CHECK */
552
553 #ifdef CALC_ENERGIES
554                                 E_lj    += E_lj_p;
555 #endif
556
557
558 #ifdef EL_CUTOFF
559 #ifdef EXCLUSION_FORCES
560                                 F_invr  += qi * qj_f * int_bit * inv_r2 * inv_r;
561 #else
562                                 F_invr  += qi * qj_f * inv_r2 * inv_r;
563 #endif
564 #endif
565 #ifdef EL_RF
566                                 F_invr  += qi * qj_f * (int_bit*inv_r2 * inv_r - two_k_rf);
567 #endif
568 #if defined EL_EWALD_ANA
569                                 F_invr  += qi * qj_f * (int_bit*inv_r2*inv_r + pmecorrF(beta2*r2)*beta3);
570 #elif defined EL_EWALD_TAB
571                                 F_invr  += qi * qj_f * (int_bit*inv_r2 -
572                                                         interpolate_coulomb_force_r(nbparam, r2 * inv_r)) * inv_r;
573 #endif                          /* EL_EWALD_ANA/TAB */
574
575 #ifdef CALC_ENERGIES
576 #ifdef EL_CUTOFF
577                                 E_el    += qi * qj_f * (int_bit*inv_r - c_rf);
578 #endif
579 #ifdef EL_RF
580                                 E_el    += qi * qj_f * (int_bit*inv_r + 0.5f * two_k_rf * r2 - c_rf);
581 #endif
582 #ifdef EL_EWALD_ANY
583                                 /* 1.0f - erff is faster than erfcf */
584                                 E_el    += qi * qj_f * (inv_r * (int_bit - erff(r2 * inv_r * beta)) - int_bit * ewald_shift);
585 #endif                          /* EL_EWALD_ANY */
586 #endif
587                                 f_ij    = rv * F_invr;
588
589                                 /* accumulate j forces in registers */
590                                 fcj_buf -= f_ij;
591
592                                 /* accumulate i forces in registers */
593                                 fci_buf[i] += f_ij;
594                             }
595                         }
596
597                         /* shift the mask bit by 1 */
598                         mask_ji += mask_ji;
599                     }
600
601                     /* reduce j forces */
602                     reduce_force_j_warp_shfl(fcj_buf, f, tidxi, aj, c_fullWarpMask);
603                 }
604             }
605 #ifdef PRUNE_NBL
606             /* Update the imask with the new one which does not contain the
607                out of range clusters anymore. */
608             pl_cj4[j4].imei[widx].imask = imask;
609 #endif
610         }
611         // avoid shared memory WAR hazards between loop iterations
612         gmx_syncwarp(c_fullWarpMask);
613     }
614
615     /* skip central shifts when summing shift forces */
616     if (nb_sci.shift == CENTRAL)
617     {
618         bCalcFshift = false;
619     }
620
621     float fshift_buf = 0.0f;
622
623     /* reduce i forces */
624     for (i = 0; i < c_numClPerSupercl; i++)
625     {
626         ai  = (sci * c_numClPerSupercl + i) * c_clSize + tidxi;
627         reduce_force_i_warp_shfl(fci_buf[i], f,
628                                  &fshift_buf, bCalcFshift,
629                                  tidxj, ai, c_fullWarpMask);
630     }
631
632     /* add up local shift forces into global mem, tidxj indexes x,y,z */
633     if (bCalcFshift && (tidxj & 3) < 3)
634     {
635         atomicAdd(&(atdat.fshift[nb_sci.shift].x) + (tidxj & 3), fshift_buf);
636     }
637
638 #ifdef CALC_ENERGIES
639     /* reduce the energies over warps and store into global memory */
640     reduce_energy_warp_shfl(E_lj, E_el, e_lj, e_el, tidx, c_fullWarpMask);
641 #endif
642 }
643 #endif /* FUNCTION_DECLARATION_ONLY */
644
645 #undef NTHREAD_Z
646 #undef MIN_BLOCKS_PER_MP
647 #undef THREADS_PER_BLOCK
648
649 #undef EL_EWALD_ANY
650 #undef EXCLUSION_FORCES
651 #undef LJ_EWALD
652
653 #undef LJ_COMB