4d5e7c9ba2d2c400df1e9084e5cf247445deab1e
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_ocl / nbnxn_ocl_kernel_utils.clh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,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 #include "device_utils.clh"
37 #include "vectype_ops.clh"
38
39 #define CL_SIZE                 (NBNXN_GPU_CLUSTER_SIZE)
40 #define NCL_PER_SUPERCL         (NBNXN_GPU_NCLUSTER_PER_SUPERCLUSTER)
41
42 #define WARP_SIZE  (CL_SIZE*CL_SIZE/2) //Currently only c_nbnxnGpuClusterpairSplit=2 supported
43
44 #if defined _NVIDIA_SOURCE_ || defined _AMD_SOURCE_
45 /* Currently we enable CJ prefetch for AMD/NVIDIA and disable it for other vendors
46  * Note that this should precede the kernel_utils include.
47  */
48 #define USE_CJ_PREFETCH 1
49 #else
50 #define USE_CJ_PREFETCH 0
51 #endif
52
53 #if (defined cl_intel_subgroups || defined cl_khr_subgroups || __OPENCL_VERSION__ >= 210)
54 #define HAVE_SUBGROUP 1
55 #else
56 #define HAVE_SUBGROUP 0
57 #endif
58
59 #ifdef cl_intel_subgroups
60 #define HAVE_INTEL_SUBGROUP 1
61 #else
62 #define HAVE_INTEL_SUBGROUP 0
63 #endif
64
65 #if _INTEL_SOURCE_
66 #define SUBGROUP_SIZE 8
67 #elif _AMD_SOURCE_
68 #define SUBGROUP_SIZE 64
69 #else
70 #define SUBGROUP_SIZE 32
71 #endif
72
73 #define REDUCE_SHUFFLE (HAVE_INTEL_SUBGROUP && CL_SIZE == 4 && SUBGROUP_SIZE == WARP_SIZE)
74 #define USE_SUBGROUP_ANY (HAVE_SUBGROUP && SUBGROUP_SIZE == WARP_SIZE)
75 #define USE_SUBGROUP_PRELOAD HAVE_INTEL_SUBGROUP
76
77 /* 1.0 / sqrt(M_PI) */
78 #define M_FLOAT_1_SQRTPI 0.564189583547756f
79
80 //-------------------
81
82 #ifndef NBNXN_OPENCL_KERNEL_UTILS_CLH
83 #define NBNXN_OPENCL_KERNEL_UTILS_CLH
84
85 #if CL_SIZE == 8
86 #define WARP_SIZE_LOG2  (5)
87 #define CL_SIZE_LOG2    (3)
88 #elif CL_SIZE == 4
89 #define WARP_SIZE_LOG2  (3)
90 #define CL_SIZE_LOG2    (2)
91 #else
92 #error unsupported CL_SIZE
93 #endif
94
95 #define CL_SIZE_SQ      (CL_SIZE * CL_SIZE)
96 #define FBUF_STRIDE     (CL_SIZE_SQ)
97
98 #define ONE_SIXTH_F     0.16666667f
99 #define ONE_TWELVETH_F  0.08333333f
100
101
102 // Data structures shared between OpenCL device code and OpenCL host code
103 // TODO: review, improve
104 // Replaced real by float for now, to avoid including any other header
105 typedef struct {
106     float c2;
107     float c3;
108     float cpot;
109 } shift_consts_t;
110
111 /* Used with potential switching:
112  * rsw        = max(r - r_switch, 0)
113  * sw         = 1 + c3*rsw^3 + c4*rsw^4 + c5*rsw^5
114  * dsw        = 3*c3*rsw^2 + 4*c4*rsw^3 + 5*c5*rsw^4
115  * force      = force*dsw - potential*sw
116  * potential *= sw
117  */
118 typedef struct {
119     float c3;
120     float c4;
121     float c5;
122 } switch_consts_t;
123
124 // Data structure shared between the OpenCL device code and OpenCL host code
125 // Must not contain OpenCL objects (buffers)
126 typedef struct cl_nbparam_params
127 {
128
129     int             eeltype;          /**< type of electrostatics, takes values from #eelCu */
130     int             vdwtype;          /**< type of VdW impl., takes values from #evdwCu     */
131
132     float           epsfac;           /**< charge multiplication factor                      */
133     float           c_rf;             /**< Reaction-field/plain cutoff electrostatics const. */
134     float           two_k_rf;         /**< Reaction-field electrostatics constant            */
135     float           ewald_beta;       /**< Ewald/PME parameter                               */
136     float           sh_ewald;         /**< Ewald/PME correction term substracted from the direct-space potential */
137     float           sh_lj_ewald;      /**< LJ-Ewald/PME correction term added to the correction potential        */
138     float           ewaldcoeff_lj;    /**< LJ-Ewald/PME coefficient                          */
139
140     float           rcoulomb_sq;      /**< Coulomb cut-off squared                           */
141
142     float           rvdw_sq;          /**< VdW cut-off squared                               */
143     float           rvdw_switch;      /**< VdW switched cut-off                              */
144     float           rlistOuter_sq;    /**< Full, outer pair-list cut-off squared             */
145     float           rlistInner_sq;    /**< Inner, dynamic pruned pair-list cut-off squared  XXX: this is only needed in the pruning kernels, but for now we also pass it to the nonbondeds */
146
147     shift_consts_t  dispersion_shift; /**< VdW shift dispersion constants           */
148     shift_consts_t  repulsion_shift;  /**< VdW shift repulsion constants            */
149     switch_consts_t vdw_switch;       /**< VdW switch constants                     */
150
151     /* Ewald Coulomb force table data - accessed through texture memory */
152     float                  coulomb_tab_scale;  /**< table scale/spacing                        */
153 }cl_nbparam_params_t;
154
155 typedef struct {
156     int sci;            /* i-super-cluster       */
157     int shift;          /* Shift vector index plus possible flags */
158     int cj4_ind_start;  /* Start index into cj4  */
159     int cj4_ind_end;    /* End index into cj4    */
160 } nbnxn_sci_t;
161
162 typedef struct {
163     unsigned int imask;    /* The i-cluster interactions mask for 1 warp  */
164     int          excl_ind; /* Index into the exclusion array for 1 warp   */
165 } nbnxn_im_ei_t;
166
167 typedef struct {
168     int           cj[4];   /* The 4 j-clusters                            */
169     nbnxn_im_ei_t imei[2]; /* The i-cluster mask data       for 2 warps   */
170 } nbnxn_cj4_t;
171
172
173 typedef struct {
174     unsigned int pair[CL_SIZE*CL_SIZE/2]; /* Topology exclusion interaction bits for one warp,
175                                            * each unsigned has bitS for 4*8 i clusters
176                                            */
177 } nbnxn_excl_t;
178
179 /*! i-cluster interaction mask for a super-cluster with all NCL_PER_SUPERCL bits set */
180 __constant unsigned supercl_interaction_mask = ((1U << NCL_PER_SUPERCL) - 1U);
181
182 gmx_opencl_inline
183 void preloadCj4Generic(__local int        *sm_cjPreload,
184                        const __global int *gm_cj,
185                        int                 tidxi,
186                        int                 tidxj,
187                        bool                iMaskCond)
188
189 {
190     /* Pre-load cj into shared memory */
191 #if defined _AMD_SOURCE_ //TODO: fix by setting c_nbnxnGpuClusterpairSplit properly
192     if (tidxj == 0 & tidxi < NBNXN_GPU_JGROUP_SIZE)
193     {
194         sm_cjPreload[tidxi] = gm_cj[tidxi];
195     }
196 #else
197     const int c_clSize                   = CL_SIZE;
198     const int c_nbnxnGpuJgroupSize       = NBNXN_GPU_JGROUP_SIZE;
199     const int c_nbnxnGpuClusterpairSplit = 2;
200     const int c_splitClSize              = c_clSize/c_nbnxnGpuClusterpairSplit;
201
202     if ((tidxj == 0 | tidxj == c_splitClSize) & (tidxi < c_nbnxnGpuJgroupSize))
203     {
204         sm_cjPreload[tidxi + tidxj * c_nbnxnGpuJgroupSize/c_splitClSize] = gm_cj[tidxi];
205     }
206 #endif
207 }
208
209
210 #if USE_SUBGROUP_PRELOAD
211 gmx_opencl_inline
212 int  preloadCj4Subgroup(const __global int *gm_cj)
213 {
214     //loads subgroup-size # of elements (8) instead of the 4 required
215     //equivalent to *cjs = *gm_cj
216     return intel_sub_group_block_read((const __global uint *)gm_cj);
217 }
218 #endif //USE_SUBGROUP_PRELOAD
219
220 #if USE_SUBGROUP_PRELOAD
221 typedef int CjType;
222 #else
223 typedef __local int* CjType;
224 #endif
225
226 /*! \brief Preload cj4
227  *
228  * - For AMD we load once for a wavefront of 64 threads (on 4 threads * NTHREAD_Z)
229  * - For NVIDIA once per warp (on 2x4 threads * NTHREAD_Z)
230  * - For Intel(/USE_SUBGROUP_PRELOAD) loads into private memory(/register) instead of local memory
231  *
232  * It is the caller's responsibility to make sure that data is consumed only when
233  * it's ready. This function does not call a barrier.
234  */
235 gmx_opencl_inline
236 void preloadCj4(CjType             *cjs,
237                 const __global int *gm_cj,
238                 int                 tidxi,
239                 int                 tidxj,
240                 bool                iMaskCond)
241 {
242 #if USE_SUBGROUP_PRELOAD
243     *cjs = preloadCj4Subgroup(gm_cj);
244 #elif USE_CJ_PREFETCH
245     preloadCj4Generic(*cjs, gm_cj, tidxi, tidxj, iMaskCond);
246 #else
247     //nothing to do
248 #endif
249 }
250
251 gmx_opencl_inline
252 int loadCjPreload(__local int*        sm_cjPreload,
253                   int                 jm,
254                   int                 tidxi,
255                   int                 tidxj)
256 {
257 #if defined _AMD_SOURCE_
258     int       warpLoadOffset = 0; //TODO: fix by setting c_nbnxnGpuClusterpairSplit properly
259 #else
260     const int c_clSize                   = CL_SIZE;
261     const int c_nbnxnGpuJgroupSize       = NBNXN_GPU_JGROUP_SIZE;
262     const int c_nbnxnGpuClusterpairSplit = 2;
263     const int c_splitClSize              = c_clSize/c_nbnxnGpuClusterpairSplit;
264
265     int       warpLoadOffset = (tidxj & c_splitClSize) * c_nbnxnGpuJgroupSize/c_splitClSize;
266 #endif
267     return sm_cjPreload[jm + warpLoadOffset];
268 }
269
270 /* \brief Load a cj given a jm index.
271  *
272  * If cj4 preloading is enabled, it loads from the local memory, otherwise from global.
273  */
274 gmx_opencl_inline
275 int loadCj(CjType cjs, const __global int *gm_cj,
276            int jm, int tidxi, int tidxj)
277 {
278 #if USE_SUBGROUP_PRELOAD
279     return sub_group_broadcast(cjs, jm);
280 #elif USE_CJ_PREFETCH
281     return loadCjPreload(cjs, jm, tidxi, tidxj);
282 #else
283     return gm_cj[jm];
284 #endif
285 }
286
287 /*! Convert LJ sigma,epsilon parameters to C6,C12. */
288 gmx_opencl_inline
289 void convert_sigma_epsilon_to_c6_c12(const float  sigma,
290                                      const float  epsilon,
291                                      float       *c6,
292                                      float       *c12)
293 {
294     float sigma2, sigma6;
295
296     sigma2 = sigma * sigma;
297     sigma6 = sigma2 *sigma2 * sigma2;
298     *c6    = epsilon * sigma6;
299     *c12   = *c6 * sigma6;
300 }
301
302
303 /*! Apply force switch,  force + energy version. */
304 gmx_opencl_inline
305 void calculate_force_switch_F(cl_nbparam_params_t *nbparam,
306                               float                c6,
307                               float                c12,
308                               float                inv_r,
309                               float                r2,
310                               float               *F_invr)
311 {
312     float r, r_switch;
313
314     /* force switch constants */
315     float disp_shift_V2 = nbparam->dispersion_shift.c2;
316     float disp_shift_V3 = nbparam->dispersion_shift.c3;
317     float repu_shift_V2 = nbparam->repulsion_shift.c2;
318     float repu_shift_V3 = nbparam->repulsion_shift.c3;
319
320     r         = r2 * inv_r;
321     r_switch  = r - nbparam->rvdw_switch;
322     r_switch  = r_switch >= 0.0f ? r_switch : 0.0f;
323
324     *F_invr  +=
325         -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
326         c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
327 }
328
329 /*! Apply force switch, force-only version. */
330 gmx_opencl_inline
331 void calculate_force_switch_F_E(cl_nbparam_params_t *nbparam,
332                                 float                c6,
333                                 float                c12,
334                                 float                inv_r,
335                                 float                r2,
336                                 float               *F_invr,
337                                 float               *E_lj)
338 {
339     float r, r_switch;
340
341     /* force switch constants */
342     float disp_shift_V2 = nbparam->dispersion_shift.c2;
343     float disp_shift_V3 = nbparam->dispersion_shift.c3;
344     float repu_shift_V2 = nbparam->repulsion_shift.c2;
345     float repu_shift_V3 = nbparam->repulsion_shift.c3;
346
347     float disp_shift_F2 = nbparam->dispersion_shift.c2/3;
348     float disp_shift_F3 = nbparam->dispersion_shift.c3/4;
349     float repu_shift_F2 = nbparam->repulsion_shift.c2/3;
350     float repu_shift_F3 = nbparam->repulsion_shift.c3/4;
351
352     r         = r2 * inv_r;
353     r_switch  = r - nbparam->rvdw_switch;
354     r_switch  = r_switch >= 0.0f ? r_switch : 0.0f;
355
356     *F_invr  +=
357         -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
358         c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
359     *E_lj    +=
360         c6*(disp_shift_F2 + disp_shift_F3*r_switch)*r_switch*r_switch*r_switch -
361         c12*(repu_shift_F2 + repu_shift_F3*r_switch)*r_switch*r_switch*r_switch;
362 }
363
364 /*! Apply potential switch, force-only version. */
365 gmx_opencl_inline
366 void calculate_potential_switch_F(cl_nbparam_params_t *nbparam,
367                                   float                inv_r,
368                                   float                r2,
369                                   float               *F_invr,
370                                   float               *E_lj)
371 {
372     float r, r_switch;
373     float sw, dsw;
374
375     /* potential switch constants */
376     float switch_V3 = nbparam->vdw_switch.c3;
377     float switch_V4 = nbparam->vdw_switch.c4;
378     float switch_V5 = nbparam->vdw_switch.c5;
379     float switch_F2 = nbparam->vdw_switch.c3;
380     float switch_F3 = nbparam->vdw_switch.c4;
381     float switch_F4 = nbparam->vdw_switch.c5;
382
383     r        = r2 * inv_r;
384     r_switch = r - nbparam->rvdw_switch;
385
386     /* Unlike in the F+E kernel, conditional is faster here */
387     if (r_switch > 0.0f)
388     {
389         sw      = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
390         dsw     = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
391
392         *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
393     }
394 }
395
396 /*! Apply potential switch, force + energy version. */
397 gmx_opencl_inline
398 void calculate_potential_switch_F_E(cl_nbparam_params_t *nbparam,
399                                     float                inv_r,
400                                     float                r2,
401                                     float               *F_invr,
402                                     float               *E_lj)
403 {
404     float r, r_switch;
405     float sw, dsw;
406
407     /* potential switch constants */
408     float switch_V3 = nbparam->vdw_switch.c3;
409     float switch_V4 = nbparam->vdw_switch.c4;
410     float switch_V5 = nbparam->vdw_switch.c5;
411     float switch_F2 = nbparam->vdw_switch.c3;
412     float switch_F3 = nbparam->vdw_switch.c4;
413     float switch_F4 = nbparam->vdw_switch.c5;
414
415     r        = r2 * inv_r;
416     r_switch = r - nbparam->rvdw_switch;
417     r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
418
419     /* Unlike in the F-only kernel, masking is faster here */
420     sw       = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
421     dsw      = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
422
423     *F_invr  = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
424     *E_lj   *= sw;
425 }
426
427 /*! Calculate LJ-PME grid force contribution with
428  *  geometric combination rule.
429  */
430 gmx_opencl_inline
431 void calculate_lj_ewald_comb_geom_F(__constant float * nbfp_comb_climg2d,
432                                     int                typei,
433                                     int                typej,
434                                     float              r2,
435                                     float              inv_r2,
436                                     float              lje_coeff2,
437                                     float              lje_coeff6_6,
438                                     float             *F_invr)
439 {
440     float c6grid, inv_r6_nm, cr2, expmcr2, poly;
441
442     c6grid    = nbfp_comb_climg2d[2*typei]*nbfp_comb_climg2d[2*typej];
443
444     /* Recalculate inv_r6 without exclusion mask */
445     inv_r6_nm = inv_r2*inv_r2*inv_r2;
446     cr2       = lje_coeff2*r2;
447     expmcr2   = exp(-cr2);
448     poly      = 1.0f + cr2 + 0.5f*cr2*cr2;
449
450     /* Subtract the grid force from the total LJ force */
451     *F_invr  += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
452 }
453
454 /*! Calculate LJ-PME grid force + energy contribution with
455  *  geometric combination rule.
456  */
457 gmx_opencl_inline
458 void calculate_lj_ewald_comb_geom_F_E(__constant float    *nbfp_comb_climg2d,
459                                       cl_nbparam_params_t *nbparam,
460                                       int                  typei,
461                                       int                  typej,
462                                       float                r2,
463                                       float                inv_r2,
464                                       float                lje_coeff2,
465                                       float                lje_coeff6_6,
466                                       float                int_bit,
467                                       float               *F_invr,
468                                       float               *E_lj)
469 {
470     float c6grid, inv_r6_nm, cr2, expmcr2, poly, sh_mask;
471
472     c6grid    = nbfp_comb_climg2d[2*typei]*nbfp_comb_climg2d[2*typej];
473
474     /* Recalculate inv_r6 without exclusion mask */
475     inv_r6_nm = inv_r2*inv_r2*inv_r2;
476     cr2       = lje_coeff2*r2;
477     expmcr2   = exp(-cr2);
478     poly      = 1.0f + cr2 + 0.5f*cr2*cr2;
479
480     /* Subtract the grid force from the total LJ force */
481     *F_invr  += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
482
483     /* Shift should be applied only to real LJ pairs */
484     sh_mask   = nbparam->sh_lj_ewald*int_bit;
485     *E_lj    += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
486 }
487
488 /*! Calculate LJ-PME grid force + energy contribution (if E_lj != NULL) with
489  *  Lorentz-Berthelot combination rule.
490  *  We use a single F+E kernel with conditional because the performance impact
491  *  of this is pretty small and LB on the CPU is anyway very slow.
492  */
493 gmx_opencl_inline
494 void calculate_lj_ewald_comb_LB_F_E(__constant float    *nbfp_comb_climg2d,
495                                     cl_nbparam_params_t *nbparam,
496                                     int                  typei,
497                                     int                  typej,
498                                     float                r2,
499                                     float                inv_r2,
500                                     float                lje_coeff2,
501                                     float                lje_coeff6_6,
502                                     float                int_bit,
503                                     bool                 with_E_lj,
504                                     float               *F_invr,
505                                     float               *E_lj)
506 {
507     float c6grid, inv_r6_nm, cr2, expmcr2, poly;
508     float sigma, sigma2, epsilon;
509
510     /* sigma and epsilon are scaled to give 6*C6 */
511     sigma      = nbfp_comb_climg2d[2*typei] + nbfp_comb_climg2d[2*typej];
512
513     epsilon    = nbfp_comb_climg2d[2*typei+1]*nbfp_comb_climg2d[2*typej+1];
514
515     sigma2  = sigma*sigma;
516     c6grid  = epsilon*sigma2*sigma2*sigma2;
517
518     /* Recalculate inv_r6 without exclusion mask */
519     inv_r6_nm = inv_r2*inv_r2*inv_r2;
520     cr2       = lje_coeff2*r2;
521     expmcr2   = exp(-cr2);
522     poly      = 1.0f + cr2 + 0.5f*cr2*cr2;
523
524     /* Subtract the grid force from the total LJ force */
525     *F_invr  += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
526
527     if (with_E_lj)
528     {
529         float sh_mask;
530
531         /* Shift should be applied only to real LJ pairs */
532         sh_mask   = nbparam->sh_lj_ewald*int_bit;
533         *E_lj    += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
534     }
535 }
536
537 /*! Interpolate Ewald coulomb force using the table through the tex_nbfp texture.
538  *  Original idea: from the OpenMM project
539  */
540 gmx_opencl_inline float
541 interpolate_coulomb_force_r(__constant float *coulomb_tab_climg2d,
542                             float             r,
543                             float             scale)
544 {
545     float   normalized = scale * r;
546     int     index      = (int) normalized;
547     float   fract2     = normalized - index;
548     float   fract1     = 1.0f - fract2;
549
550     return fract1*coulomb_tab_climg2d[index] +
551            fract2*coulomb_tab_climg2d[index + 1];
552 }
553
554 /*! Calculate analytical Ewald correction term. */
555 gmx_opencl_inline
556 float pmecorrF(float z2)
557 {
558     const float FN6 = -1.7357322914161492954e-8f;
559     const float FN5 = 1.4703624142580877519e-6f;
560     const float FN4 = -0.000053401640219807709149f;
561     const float FN3 = 0.0010054721316683106153f;
562     const float FN2 = -0.019278317264888380590f;
563     const float FN1 = 0.069670166153766424023f;
564     const float FN0 = -0.75225204789749321333f;
565
566     const float FD4 = 0.0011193462567257629232f;
567     const float FD3 = 0.014866955030185295499f;
568     const float FD2 = 0.11583842382862377919f;
569     const float FD1 = 0.50736591960530292870f;
570     const float FD0 = 1.0f;
571
572     float       z4;
573     float       polyFN0, polyFN1, polyFD0, polyFD1;
574
575     z4          = z2*z2;
576
577     polyFD0     = FD4*z4 + FD2;
578     polyFD1     = FD3*z4 + FD1;
579     polyFD0     = polyFD0*z4 + FD0;
580     polyFD0     = polyFD1*z2 + polyFD0;
581
582     polyFD0     = 1.0f/polyFD0;
583
584     polyFN0     = FN6*z4 + FN4;
585     polyFN1     = FN5*z4 + FN3;
586     polyFN0     = polyFN0*z4 + FN2;
587     polyFN1     = polyFN1*z4 + FN1;
588     polyFN0     = polyFN0*z4 + FN0;
589     polyFN0     = polyFN1*z2 + polyFN0;
590
591     return polyFN0*polyFD0;
592 }
593
594 #if REDUCE_SHUFFLE
595 gmx_opencl_inline
596 void reduce_force_j_shfl(float3 fin, __global float *fout,
597                          int tidxi, int tidxj, int aidx)
598 {
599     /* Only does reduction over 4 elements in cluster. Needs to be changed
600      * for CL_SIZE>4. See CUDA code for required code */
601     fin.x += intel_sub_group_shuffle_down(fin.x, fin.x, 1);
602     fin.y += intel_sub_group_shuffle_up  (fin.y, fin.y, 1);
603     fin.z += intel_sub_group_shuffle_down(fin.z, fin.z, 1);
604     if ((tidxi & 1) == 1)
605     {
606         fin.x = fin.y;
607     }
608     fin.x += intel_sub_group_shuffle_down(fin.x, fin.x, 2);
609     fin.z += intel_sub_group_shuffle_up  (fin.z, fin.z, 2);
610     if (tidxi == 2)
611     {
612         fin.x = fin.z;
613     }
614     if (tidxi < 3)
615     {
616         atomicAdd_g_f(&fout[3 * aidx + tidxi], fin.x);
617     }
618 }
619 #endif
620
621 gmx_opencl_inline
622 void reduce_force_j_generic(__local float *f_buf, float3 fcj_buf, __global float *fout,
623                             int tidxi, int tidxj, int aidx)
624 {
625     int tidx = tidxi + tidxj*CL_SIZE;
626     f_buf[                  tidx] = fcj_buf.x;
627     f_buf[    FBUF_STRIDE + tidx] = fcj_buf.y;
628     f_buf[2 * FBUF_STRIDE + tidx] = fcj_buf.z;
629
630     /* Split the reduction between the first 3 column threads
631        Threads with column id 0 will do the reduction for (float3).x components
632        Threads with column id 1 will do the reduction for (float3).y components
633        Threads with column id 2 will do the reduction for (float3).z components.
634        The reduction is performed for each line tidxj of f_buf. */
635     if (tidxi < 3)
636     {
637         float f = 0.0f;
638         for (int j = tidxj * CL_SIZE; j < (tidxj + 1) * CL_SIZE; j++)
639         {
640             f += f_buf[FBUF_STRIDE * tidxi + j];
641         }
642
643         atomicAdd_g_f(&fout[3 * aidx + tidxi], f);
644     }
645 }
646
647 /*! Final j-force reduction
648  */
649 gmx_opencl_inline
650 void reduce_force_j(__local float *f_buf, float3 fcj_buf, __global float *fout,
651                     int tidxi, int tidxj, int aidx)
652 {
653 #if REDUCE_SHUFFLE
654     reduce_force_j_shfl(fcj_buf, fout, tidxi, tidxj, aidx);
655 #else
656     reduce_force_j_generic(f_buf, fcj_buf, fout, tidxi, tidxj, aidx);
657 #endif
658 }
659
660 #if REDUCE_SHUFFLE
661 gmx_opencl_inline
662 void reduce_force_i_and_shift_shfl(float3* fci_buf, __global float *fout,
663                                    bool bCalcFshift, int tidxi, int tidxj,
664                                    int sci, int shift, __global float *fshift)
665 {
666     /* Only does reduction over 4 elements in cluster (2 per warp). Needs to be changed
667      * for CL_SIZE>4.*/
668     float2 fshift_buf = 0;
669     for (int ci_offset = 0; ci_offset < NCL_PER_SUPERCL; ci_offset++)
670     {
671         int    aidx = (sci * NCL_PER_SUPERCL + ci_offset) * CL_SIZE + tidxi;
672         float3 fin  = fci_buf[ci_offset];
673         fin.x += intel_sub_group_shuffle_down(fin.x, fin.x, CL_SIZE);
674         fin.y += intel_sub_group_shuffle_up  (fin.y, fin.y, CL_SIZE);
675         fin.z += intel_sub_group_shuffle_down(fin.z, fin.z, CL_SIZE);
676
677         if (tidxj & 1)
678         {
679             fin.x = fin.y;
680         }
681         /* Threads 0,1 and 2,3 increment x,y for their warp */
682         atomicAdd_g_f(&fout[3*aidx + (tidxj & 1)], fin.x);
683         if (bCalcFshift)
684         {
685             fshift_buf[0] += fin.x;
686         }
687         /* Threads 0 and 2 increment z for their warp */
688         if ((tidxj & 1) == 0)
689         {
690             atomicAdd_g_f(&fout[3*aidx+2], fin.z);
691             if (bCalcFshift)
692             {
693                 fshift_buf[1] += fin.z;
694             }
695         }
696     }
697     /* add up local shift forces into global mem */
698     if (bCalcFshift)
699     {
700         //Threads 0,1 and 2,3 update x,y
701         atomicAdd_g_f(&(fshift[3 * shift + (tidxj&1)]), fshift_buf[0]);
702         //Threads 0 and 2 update z
703         if ((tidxj & 1) == 0)
704         {
705             atomicAdd_g_f(&(fshift[3 * shift + 2]), fshift_buf[1]);
706         }
707     }
708 }
709 #endif
710
711 /*! Final i-force reduction; this implementation works only with power of two
712  *  array sizes.
713  */
714 gmx_opencl_inline
715 void reduce_force_i_and_shift_pow2(volatile __local float *f_buf, float3* fci_buf,
716                                    __global float *fout,
717                                    bool bCalcFshift,
718                                    int tidxi, int tidxj,
719                                    int sci, int shift, __global float *fshift)
720 {
721     float fshift_buf = 0;
722     for (int ci_offset = 0; ci_offset < NCL_PER_SUPERCL; ci_offset++)
723     {
724         int aidx = (sci * NCL_PER_SUPERCL + ci_offset) * CL_SIZE + tidxi;
725         int tidx = tidxi + tidxj*CL_SIZE;
726         /* store i forces in shmem */
727         f_buf[                  tidx] = fci_buf[ci_offset].x;
728         f_buf[    FBUF_STRIDE + tidx] = fci_buf[ci_offset].y;
729         f_buf[2 * FBUF_STRIDE + tidx] = fci_buf[ci_offset].z;
730         barrier(CLK_LOCAL_MEM_FENCE);
731
732         int     i, j;
733         /* Reduce the initial CL_SIZE values for each i atom to half
734          * every step by using CL_SIZE * i threads.
735          * Can't just use i as loop variable because than nvcc refuses to unroll.
736          */
737         i = CL_SIZE/2;
738         for (j = CL_SIZE_LOG2 - 1; j > 0; j--)
739         {
740             if (tidxj < i)
741             {
742
743                 f_buf[                  tidxj * CL_SIZE + tidxi] += f_buf[                  (tidxj + i) * CL_SIZE + tidxi];
744                 f_buf[    FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[    FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
745                 f_buf[2 * FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[2 * FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
746             }
747             i >>= 1;
748         }
749         /* needed because
750          * a) for CL_SIZE<8: id 2 (doing z in next block) is in 2nd warp
751          * b) for all CL_SIZE a barrier is needed before f_buf is reused by next reduce_force_i call
752          * TODO: Test on Nvidia for performance difference between having the barrier here or after the atomicAdd
753          */
754         barrier(CLK_LOCAL_MEM_FENCE);
755
756         /* i == 1, last reduction step, writing to global mem */
757         /* Split the reduction between the first 3 line threads
758            Threads with line id 0 will do the reduction for (float3).x components
759            Threads with line id 1 will do the reduction for (float3).y components
760            Threads with line id 2 will do the reduction for (float3).z components. */
761         if (tidxj < 3)
762         {
763             float f = f_buf[tidxj * FBUF_STRIDE + tidxi] + f_buf[tidxj * FBUF_STRIDE + i * CL_SIZE + tidxi];
764
765             atomicAdd_g_f(&fout[3 * aidx + tidxj], f);
766
767             if (bCalcFshift)
768             {
769                 fshift_buf += f;
770             }
771         }
772     }
773     /* add up local shift forces into global mem */
774     if (bCalcFshift)
775     {
776         /* Only threads with tidxj < 3 will update fshift.
777            The threads performing the update, must be the same as the threads
778            storing the reduction result above.
779          */
780         if (tidxj < 3)
781         {
782             atomicAdd_g_f(&(fshift[3 * shift + tidxj]), fshift_buf);
783         }
784     }
785 }
786
787 /*! Final i-force reduction
788  */
789 gmx_opencl_inline
790 void reduce_force_i_and_shift(__local float *f_buf, float3* fci_buf, __global float *f,
791                               bool bCalcFshift, int tidxi, int tidxj, int sci,
792                               int shift, __global float *fshift)
793 {
794 #if REDUCE_SHUFFLE
795     reduce_force_i_and_shift_shfl(fci_buf, f, bCalcFshift, tidxi, tidxj,
796                                   sci, shift, fshift);
797 #else
798     reduce_force_i_and_shift_pow2(f_buf, fci_buf, f, bCalcFshift, tidxi, tidxj,
799                                   sci, shift, fshift);
800 #endif
801 }
802
803
804
805 #if REDUCE_SHUFFLE
806 gmx_opencl_inline
807 void reduce_energy_shfl(float E_lj, float E_el,
808                         volatile __global float *e_lj,
809                         volatile __global float *e_el,
810                         unsigned int    tidx)
811 {
812     E_lj = sub_group_reduce_add(E_lj);
813     E_el = sub_group_reduce_add(E_el);
814     /* Should be get_sub_group_local_id()==0. Doesn't work with Intel Classic driver.
815      * To make it possible to use REDUCE_SHUFFLE with single subgroup per i-j pair
816      * (e.g. subgroup size 16 with CL_SIZE 4), either this "if" needs to be changed or
817      * the definition of WARP_SIZE (currently CL_SIZE*CL_SIZE/2) needs to be changed
818      * (by supporting c_nbnxnGpuClusterpairSplit=1). */
819     if (tidx == 0 || tidx == WARP_SIZE)
820     {
821         atomicAdd_g_f(e_lj, E_lj);
822         atomicAdd_g_f(e_el, E_el);
823     }
824 }
825 #endif
826
827 /*! Energy reduction; this implementation works only with power of two
828  *  array sizes.
829  */
830 gmx_opencl_inline
831 void reduce_energy_pow2(volatile __local float  *buf,
832                         volatile __global float *e_lj,
833                         volatile __global float *e_el,
834                         unsigned int             tidx)
835 {
836     int     i, j;
837     float   e1, e2;
838
839     i = WARP_SIZE/2;
840
841     /* Can't just use i as loop variable because than nvcc refuses to unroll. */
842     for (j = WARP_SIZE_LOG2 - 1; j > 0; j--)
843     {
844         if (tidx < i)
845         {
846             buf[              tidx] += buf[              tidx + i];
847             buf[FBUF_STRIDE + tidx] += buf[FBUF_STRIDE + tidx + i];
848         }
849         i >>= 1;
850     }
851
852     /* last reduction step, writing to global mem */
853     if (tidx == 0)
854     {
855         e1 = buf[              tidx] + buf[              tidx + i];
856         e2 = buf[FBUF_STRIDE + tidx] + buf[FBUF_STRIDE + tidx + i];
857
858         atomicAdd_g_f(e_lj, e1);
859         atomicAdd_g_f(e_el, e2);
860     }
861 }
862
863 gmx_opencl_inline
864 void reduce_energy(volatile __local float  *buf,
865                    float E_lj, float E_el,
866                    volatile __global float *e_lj,
867                    volatile __global float *e_el,
868                    unsigned int             tidx)
869 {
870 #if REDUCE_SHUFFLE
871     reduce_energy_shfl(E_lj, E_el, e_lj, e_el, tidx);
872 #else
873     /* flush the energies to shmem and reduce them */
874     buf[              tidx] = E_lj;
875     buf[FBUF_STRIDE + tidx] = E_el;
876     reduce_energy_pow2(buf + (tidx & WARP_SIZE), e_lj, e_el, tidx & ~WARP_SIZE);
877 #endif
878 }
879
880 bool gmx_sub_group_any_localmem(volatile __local uint *warp_any, int widx, bool pred)
881 {
882     if (pred)
883     {
884         warp_any[widx] = 1;
885     }
886
887     bool ret = warp_any[widx];
888
889     warp_any[widx] = 0;
890
891     return ret;
892 }
893
894 //! Returns a true if predicate is true for any work item in warp
895 bool gmx_sub_group_any(volatile __local uint *warp_any, int widx, bool pred)
896 {
897 #if USE_SUBGROUP_ANY
898     return sub_group_any(pred);
899 #else
900     return gmx_sub_group_any_localmem(warp_any, widx, pred);
901 #endif
902 }
903
904 #endif /* NBNXN_OPENCL_KERNEL_UTILS_CLH */