2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2016,2017 by the GROMACS development team.
5 * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Utility constant and function declaration for the OpenCL non-bonded kernels.
39 * This header should be included once at the top level, just before the
40 * kernels are included (has to be preceded by nbnxn_ocl_types.h).
42 * \author Szilárd Páll <pall.szilard@gmail.com>
43 * \ingroup module_nbnxm
48 #include "gromacs/gpu_utils/device_utils.clh"
49 #include "gromacs/gpu_utils/vectype_ops.clh"
50 #include "gromacs/pbcutil/ishift.h"
52 #include "nbnxm_ocl_consts.h"
54 #define CL_SIZE (c_nbnxnGpuClusterSize)
56 #define WARP_SIZE (CL_SIZE * CL_SIZE / 2) // Currently only c_nbnxnGpuClusterpairSplit=2 supported
58 #if defined _NVIDIA_SOURCE_ || defined _AMD_SOURCE_
59 /* Currently we enable CJ prefetch for AMD/NVIDIA and disable it for other vendors
60 * Note that this should precede the kernel_utils include.
62 # define USE_CJ_PREFETCH 1
64 # define USE_CJ_PREFETCH 0
67 #if defined cl_intel_subgroups || defined cl_khr_subgroups \
68 || (defined __OPENCL_VERSION__ && __OPENCL_VERSION__ >= 210)
69 # define HAVE_SUBGROUP 1
71 # define HAVE_SUBGROUP 0
74 #ifdef cl_intel_subgroups
75 # define HAVE_INTEL_SUBGROUP 1
77 # define HAVE_INTEL_SUBGROUP 0
80 #if defined _INTEL_SOURCE_
81 # define SUBGROUP_SIZE 8
82 #elif defined _AMD_SOURCE_
83 # define SUBGROUP_SIZE 64
85 # define SUBGROUP_SIZE 32
88 #define REDUCE_SHUFFLE (HAVE_INTEL_SUBGROUP && CL_SIZE == 4 && SUBGROUP_SIZE == WARP_SIZE)
89 #define USE_SUBGROUP_ANY (HAVE_SUBGROUP && SUBGROUP_SIZE == WARP_SIZE)
90 #define USE_SUBGROUP_PRELOAD HAVE_INTEL_SUBGROUP
92 /* 1.0 / sqrt(M_PI) */
93 #define M_FLOAT_1_SQRTPI 0.564189583547756F
97 #ifndef NBNXN_OPENCL_KERNEL_UTILS_CLH
98 # define NBNXN_OPENCL_KERNEL_UTILS_CLH
101 # define WARP_SIZE_LOG2 (5)
102 # define CL_SIZE_LOG2 (3)
104 # define WARP_SIZE_LOG2 (3)
105 # define CL_SIZE_LOG2 (2)
107 # error unsupported CL_SIZE
110 # define CL_SIZE_SQ (CL_SIZE * CL_SIZE)
111 # define FBUF_STRIDE (CL_SIZE_SQ)
113 # define ONE_SIXTH_F 0.16666667F
114 # define ONE_TWELVETH_F 0.08333333F
120 # define gmx_unused __attribute__((unused))
125 /*! \brief Single precision floating point short vector type (as rvec in the CPU codebase).
126 * Currently only used to avoid float3 register arrays.
128 typedef float fvec[3];
130 // Data structures shared between OpenCL device code and OpenCL host code
131 // TODO: review, improve
132 // Replaced real by float for now, to avoid including any other header
140 /* Used with potential switching:
141 * rsw = max(r - r_switch, 0)
142 * sw = 1 + c3*rsw^3 + c4*rsw^4 + c5*rsw^5
143 * dsw = 3*c3*rsw^2 + 4*c4*rsw^3 + 5*c5*rsw^4
144 * force = force*dsw - potential*sw
154 // Data structure shared between the OpenCL device code and OpenCL host code
155 // Must not contain OpenCL objects (buffers)
156 typedef struct cl_nbparam_params
159 //! type of electrostatics, takes values from #ElecType
161 //! type of VdW impl., takes values from #VdwType
164 //! charge multiplication factor
166 //! Reaction-field/plain cutoff electrostatics const.
168 //! Reaction-field electrostatics constant
170 //! Ewald/PME parameter
172 //! Ewald/PME correction term substracted from the direct-space potential
174 //! LJ-Ewald/PME correction term added to the correction potential
176 //! LJ-Ewald/PME coefficient
179 //! Coulomb cut-off squared
182 //! VdW cut-off squared
184 //! VdW switched cut-off
186 //! Full, outer pair-list cut-off squared
188 //! 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
191 //! VdW shift dispersion constants
192 shift_consts_t dispersion_shift;
193 //! VdW shift repulsion constants
194 shift_consts_t repulsion_shift;
195 //! VdW switch constants
196 switch_consts_t vdw_switch;
198 /* Ewald Coulomb force table data - accessed through texture memory */
199 //! table scale/spacing
200 float coulomb_tab_scale;
201 } cl_nbparam_params_t;
207 //! Shift vector index plus possible flags
209 //! Start index into cj4
211 //! End index into cj4
217 //! The i-cluster interactions mask for 1 warp
219 //! Index into the exclusion array for 1 warp
227 //! The i-cluster mask data for 2 warps
228 nbnxn_im_ei_t imei[2];
234 unsigned int pair[CL_SIZE * CL_SIZE / 2]; /* Topology exclusion interaction bits for one warp,
235 * each unsigned has bitS for 4*8 i clusters
239 /*! i-cluster interaction mask for a super-cluster with all c_nbnxnGpuNumClusterPerSupercluster bits set */
240 __constant unsigned supercl_interaction_mask = ((1U << c_nbnxnGpuNumClusterPerSupercluster) - 1U);
242 /*! Minimum single precision threshold for r^2 to avoid r^-12 overflow. */
243 __constant float c_nbnxnMinDistanceSquared = NBNXM_MIN_DISTANCE_SQUARED_VALUE_FLOAT;
245 gmx_opencl_inline void preloadCj4Generic(__local int* sm_cjPreload,
246 const __global int* gm_cj,
249 bool gmx_unused iMaskCond)
251 /* Pre-load cj into shared memory */
252 # if defined _AMD_SOURCE_ // TODO: fix by setting c_nbnxnGpuClusterpairSplit properly
253 if (tidxj == 0 & tidxi < c_nbnxnGpuJgroupSize)
255 sm_cjPreload[tidxi] = gm_cj[tidxi];
258 const int c_clSize = CL_SIZE;
259 const int c_nbnxnGpuClusterpairSplit = 2;
260 const int c_splitClSize = c_clSize / c_nbnxnGpuClusterpairSplit;
261 if ((tidxj == 0 | tidxj == c_splitClSize) & (tidxi < c_nbnxnGpuJgroupSize))
263 sm_cjPreload[tidxi + tidxj * c_nbnxnGpuJgroupSize / c_splitClSize] = gm_cj[tidxi];
269 # if USE_SUBGROUP_PRELOAD
270 gmx_opencl_inline int preloadCj4Subgroup(const __global int* gm_cj)
272 // loads subgroup-size # of elements (8) instead of the 4 required
273 // equivalent to *cjs = *gm_cj
274 return intel_sub_group_block_read((const __global uint*)gm_cj);
276 # endif // USE_SUBGROUP_PRELOAD
278 # if USE_SUBGROUP_PRELOAD
279 typedef size_t CjType;
281 typedef __local int* CjType;
284 /*! \brief Preload cj4
286 * - For AMD we load once for a wavefront of 64 threads (on 4 threads * NTHREAD_Z)
287 * - For NVIDIA once per warp (on 2x4 threads * NTHREAD_Z)
288 * - For Intel(/USE_SUBGROUP_PRELOAD) loads into private memory(/register) instead of local memory
290 * It is the caller's responsibility to make sure that data is consumed only when
291 * it's ready. This function does not call a barrier.
293 gmx_opencl_inline void preloadCj4(CjType gmx_unused* cjs,
294 const __global int gmx_unused* gm_cj,
295 int gmx_unused tidxi,
296 int gmx_unused tidxj,
297 bool gmx_unused iMaskCond)
299 # if USE_SUBGROUP_PRELOAD
300 *cjs = preloadCj4Subgroup(gm_cj);
301 # elif USE_CJ_PREFETCH
302 preloadCj4Generic(*cjs, gm_cj, tidxi, tidxj, iMaskCond);
308 gmx_opencl_inline int loadCjPreload(__local int* sm_cjPreload, int jm, int gmx_unused tidxi, int gmx_unused tidxj)
310 # if defined _AMD_SOURCE_
311 int warpLoadOffset = 0; // TODO: fix by setting c_nbnxnGpuClusterpairSplit properly
313 const int c_clSize = CL_SIZE;
314 const int c_nbnxnGpuClusterpairSplit = 2;
315 const int c_splitClSize = c_clSize / c_nbnxnGpuClusterpairSplit;
316 int warpLoadOffset = (tidxj & c_splitClSize) * c_nbnxnGpuJgroupSize / c_splitClSize;
318 return sm_cjPreload[jm + warpLoadOffset];
321 /* \brief Load a cj given a jm index.
323 * If cj4 preloading is enabled, it loads from the local memory, otherwise from global.
325 gmx_opencl_inline int
326 loadCj(CjType cjs, const __global int gmx_unused* gm_cj, int jm, int gmx_unused tidxi, int gmx_unused tidxj)
328 # if USE_SUBGROUP_PRELOAD
329 return sub_group_broadcast(cjs, jm);
330 # elif USE_CJ_PREFETCH
331 return loadCjPreload(cjs, jm, tidxi, tidxj);
337 /*! Convert LJ sigma,epsilon parameters to C6,C12. */
338 gmx_opencl_inline float2 convert_sigma_epsilon_to_c6_c12(const float sigma, const float epsilon)
340 const float sigma2 = sigma * sigma;
341 const float sigma6 = sigma2 * sigma2 * sigma2;
342 const float c6 = epsilon * sigma6;
343 const float2 c6c12 = (float2)(c6, /* c6 */
344 c6 * sigma6); /* c12 */
349 /*! Apply force switch, force + energy version. */
350 gmx_opencl_inline void calculate_force_switch_F(const cl_nbparam_params_t* nbparam,
357 /* force switch constants */
358 const float disp_shift_V2 = nbparam->dispersion_shift.c2;
359 const float disp_shift_V3 = nbparam->dispersion_shift.c3;
360 const float repu_shift_V2 = nbparam->repulsion_shift.c2;
361 const float repu_shift_V3 = nbparam->repulsion_shift.c3;
363 const float r = r2 * inv_r;
364 float r_switch = r - nbparam->rvdw_switch;
365 r_switch = r_switch >= 0.0F ? r_switch : 0.0F;
367 *F_invr += -c6 * (disp_shift_V2 + disp_shift_V3 * r_switch) * r_switch * r_switch * inv_r
368 + c12 * (repu_shift_V2 + repu_shift_V3 * r_switch) * r_switch * r_switch * inv_r;
371 /*! Apply force switch, force-only version. */
372 gmx_opencl_inline void calculate_force_switch_F_E(const cl_nbparam_params_t* nbparam,
380 /* force switch constants */
381 const float disp_shift_V2 = nbparam->dispersion_shift.c2;
382 const float disp_shift_V3 = nbparam->dispersion_shift.c3;
383 const float repu_shift_V2 = nbparam->repulsion_shift.c2;
384 const float repu_shift_V3 = nbparam->repulsion_shift.c3;
386 const float disp_shift_F2 = nbparam->dispersion_shift.c2 / 3;
387 const float disp_shift_F3 = nbparam->dispersion_shift.c3 / 4;
388 const float repu_shift_F2 = nbparam->repulsion_shift.c2 / 3;
389 const float repu_shift_F3 = nbparam->repulsion_shift.c3 / 4;
391 const float r = r2 * inv_r;
392 float r_switch = r - nbparam->rvdw_switch;
393 r_switch = r_switch >= 0.0F ? r_switch : 0.0F;
395 *F_invr += -c6 * (disp_shift_V2 + disp_shift_V3 * r_switch) * r_switch * r_switch * inv_r
396 + c12 * (repu_shift_V2 + repu_shift_V3 * r_switch) * r_switch * r_switch * inv_r;
397 *E_lj += c6 * (disp_shift_F2 + disp_shift_F3 * r_switch) * r_switch * r_switch * r_switch
398 - c12 * (repu_shift_F2 + repu_shift_F3 * r_switch) * r_switch * r_switch * r_switch;
401 /*! Apply potential switch, force-only version. */
402 gmx_opencl_inline void calculate_potential_switch_F(const cl_nbparam_params_t* nbparam,
408 /* potential switch constants */
409 const float switch_V3 = nbparam->vdw_switch.c3;
410 const float switch_V4 = nbparam->vdw_switch.c4;
411 const float switch_V5 = nbparam->vdw_switch.c5;
412 const float switch_F2 = nbparam->vdw_switch.c3;
413 const float switch_F3 = nbparam->vdw_switch.c4;
414 const float switch_F4 = nbparam->vdw_switch.c5;
416 const float r = r2 * inv_r;
417 const float r_switch = r - nbparam->rvdw_switch;
419 /* Unlike in the F+E kernel, conditional is faster here */
422 const float sw = 1.0F
423 + (switch_V3 + (switch_V4 + switch_V5 * r_switch) * r_switch) * r_switch
424 * r_switch * r_switch;
425 const float dsw = (switch_F2 + (switch_F3 + switch_F4 * r_switch) * r_switch) * r_switch * r_switch;
427 *F_invr = (*F_invr) * sw - inv_r * (*E_lj) * dsw;
431 /*! Apply potential switch, force + energy version. */
432 gmx_opencl_inline void calculate_potential_switch_F_E(const cl_nbparam_params_t* nbparam,
438 /* potential switch constants */
439 const float switch_V3 = nbparam->vdw_switch.c3;
440 const float switch_V4 = nbparam->vdw_switch.c4;
441 const float switch_V5 = nbparam->vdw_switch.c5;
442 const float switch_F2 = nbparam->vdw_switch.c3;
443 const float switch_F3 = nbparam->vdw_switch.c4;
444 const float switch_F4 = nbparam->vdw_switch.c5;
446 const float r = r2 * inv_r;
447 float r_switch = r - nbparam->rvdw_switch;
448 r_switch = r_switch >= 0.0F ? r_switch : 0.0F;
450 /* Unlike in the F-only kernel, masking is faster here */
452 1.0F + (switch_V3 + (switch_V4 + switch_V5 * r_switch) * r_switch) * r_switch * r_switch * r_switch;
453 const float dsw = (switch_F2 + (switch_F3 + switch_F4 * r_switch) * r_switch) * r_switch * r_switch;
455 *F_invr = (*F_invr) * sw - inv_r * (*E_lj) * dsw;
459 /*! Calculate LJ-PME grid force contribution with
460 * geometric combination rule.
462 gmx_opencl_inline void calculate_lj_ewald_comb_geom_F(__constant const float* nbfp_comb,
471 const float c6grid = nbfp_comb[2 * typei] * nbfp_comb[2 * typej];
473 /* Recalculate inv_r6 without exclusion mask */
474 const float inv_r6_nm = inv_r2 * inv_r2 * inv_r2;
475 const float cr2 = lje_coeff2 * r2;
476 const float expmcr2 = exp(-cr2);
477 const float poly = 1.0F + cr2 + HALF_F * cr2 * cr2;
479 /* Subtract the grid force from the total LJ force */
480 *F_invr += c6grid * (inv_r6_nm - expmcr2 * (inv_r6_nm * poly + lje_coeff6_6)) * inv_r2;
483 /*! Calculate LJ-PME grid force + energy contribution with
484 * geometric combination rule.
486 gmx_opencl_inline void calculate_lj_ewald_comb_geom_F_E(__constant const float* nbfp_comb,
487 const cl_nbparam_params_t* nbparam,
498 const float c6grid = nbfp_comb[2 * typei] * nbfp_comb[2 * typej];
500 /* Recalculate inv_r6 without exclusion mask */
501 const float inv_r6_nm = inv_r2 * inv_r2 * inv_r2;
502 const float cr2 = lje_coeff2 * r2;
503 const float expmcr2 = exp(-cr2);
504 const float poly = 1.0F + cr2 + HALF_F * cr2 * cr2;
506 /* Subtract the grid force from the total LJ force */
507 *F_invr += c6grid * (inv_r6_nm - expmcr2 * (inv_r6_nm * poly + lje_coeff6_6)) * inv_r2;
509 /* Shift should be applied only to real LJ pairs */
510 const float sh_mask = nbparam->sh_lj_ewald * int_bit;
511 *E_lj += ONE_SIXTH_F * c6grid * (inv_r6_nm * (1.0F - expmcr2 * poly) + sh_mask);
514 /*! Calculate LJ-PME grid force + energy contribution (if E_lj != NULL) with
515 * Lorentz-Berthelot combination rule.
516 * We use a single F+E kernel with conditional because the performance impact
517 * of this is pretty small and LB on the CPU is anyway very slow.
519 gmx_opencl_inline void calculate_lj_ewald_comb_LB_F_E(__constant const float* nbfp_comb,
520 const cl_nbparam_params_t* nbparam,
532 /* sigma and epsilon are scaled to give 6*C6 */
533 const float sigma = nbfp_comb[2 * typei] + nbfp_comb[2 * typej];
535 const float epsilon = nbfp_comb[2 * typei + 1] * nbfp_comb[2 * typej + 1];
537 const float sigma2 = sigma * sigma;
538 const float c6grid = epsilon * sigma2 * sigma2 * sigma2;
540 /* Recalculate inv_r6 without exclusion mask */
541 const float inv_r6_nm = inv_r2 * inv_r2 * inv_r2;
542 const float cr2 = lje_coeff2 * r2;
543 const float expmcr2 = exp(-cr2);
544 const float poly = 1.0F + cr2 + HALF_F * cr2 * cr2;
546 /* Subtract the grid force from the total LJ force */
547 *F_invr += c6grid * (inv_r6_nm - expmcr2 * (inv_r6_nm * poly + lje_coeff6_6)) * inv_r2;
552 /* Shift should be applied only to real LJ pairs */
553 const float sh_mask = nbparam->sh_lj_ewald * int_bit;
554 *E_lj += ONE_SIXTH_F * c6grid * (inv_r6_nm * (1.0F - expmcr2 * poly) + sh_mask);
558 /*! Interpolate Ewald coulomb force using the table through the tex_nbfp texture.
559 * Original idea: from the OpenMM project
561 gmx_opencl_inline float interpolate_coulomb_force_r(__constant const float* coulomb_tab, float r, float scale)
563 float normalized = scale * r;
564 int index = (int)normalized;
565 float fract2 = normalized - (float)index;
566 float fract1 = 1.0F - fract2;
568 return fract1 * coulomb_tab[index] + fract2 * coulomb_tab[index + 1];
571 /*! Calculate analytical Ewald correction term. */
572 gmx_opencl_inline float pmecorrF(float z2)
574 const float FN6 = -1.7357322914161492954e-8F;
575 const float FN5 = 1.4703624142580877519e-6F;
576 const float FN4 = -0.000053401640219807709149F;
577 const float FN3 = 0.0010054721316683106153F;
578 const float FN2 = -0.019278317264888380590F;
579 const float FN1 = 0.069670166153766424023F;
580 const float FN0 = -0.75225204789749321333F;
582 const float FD4 = 0.0011193462567257629232F;
583 const float FD3 = 0.014866955030185295499F;
584 const float FD2 = 0.11583842382862377919F;
585 const float FD1 = 0.50736591960530292870F;
586 const float FD0 = 1.0F;
588 const float z4 = z2 * z2;
590 float polyFD0 = FD4 * z4 + FD2;
591 float polyFD1 = FD3 * z4 + FD1;
592 polyFD0 = polyFD0 * z4 + FD0;
593 polyFD0 = polyFD1 * z2 + polyFD0;
595 polyFD0 = 1.0F / polyFD0;
597 float polyFN0 = FN6 * z4 + FN4;
598 float polyFN1 = FN5 * z4 + FN3;
599 polyFN0 = polyFN0 * z4 + FN2;
600 polyFN1 = polyFN1 * z4 + FN1;
601 polyFN0 = polyFN0 * z4 + FN0;
602 polyFN0 = polyFN1 * z2 + polyFN0;
604 return polyFN0 * polyFD0;
608 gmx_opencl_inline void
609 reduce_force_j_shfl(float3 fin, __global float* fout, int gmx_unused tidxi, int gmx_unused tidxj, int aidx)
611 /* Only does reduction over 4 elements in cluster. Needs to be changed
612 * for CL_SIZE>4. See CUDA code for required code */
613 fin.x += intel_sub_group_shuffle_down(fin.x, fin.x, 1);
614 fin.y += intel_sub_group_shuffle_up(fin.y, fin.y, 1);
615 fin.z += intel_sub_group_shuffle_down(fin.z, fin.z, 1);
616 if ((tidxi & 1) == 1)
620 fin.x += intel_sub_group_shuffle_down(fin.x, fin.x, 2);
621 fin.z += intel_sub_group_shuffle_up(fin.z, fin.z, 2);
628 atomicAdd_g_f(&fout[3 * aidx + tidxi], fin.x);
633 gmx_opencl_inline void
634 reduce_force_j_generic(__local float* f_buf, float3 fcj_buf, __global float* fout, int tidxi, int tidxj, int aidx)
636 int tidx = tidxi + tidxj * CL_SIZE;
637 f_buf[tidx] = fcj_buf.x;
638 f_buf[FBUF_STRIDE + tidx] = fcj_buf.y;
639 f_buf[2 * FBUF_STRIDE + tidx] = fcj_buf.z;
641 /* Split the reduction between the first 3 column threads
642 Threads with column id 0 will do the reduction for (float3).x components
643 Threads with column id 1 will do the reduction for (float3).y components
644 Threads with column id 2 will do the reduction for (float3).z components.
645 The reduction is performed for each line tidxj of f_buf. */
649 for (int j = tidxj * CL_SIZE; j < (tidxj + 1) * CL_SIZE; j++)
651 f += f_buf[FBUF_STRIDE * tidxi + j];
654 atomicAdd_g_f(&fout[3 * aidx + tidxi], f);
658 /*! Final j-force reduction
660 gmx_opencl_inline void reduce_force_j(__local float gmx_unused* f_buf,
662 __global float* fout,
668 reduce_force_j_shfl(fcj_buf, fout, tidxi, tidxj, aidx);
670 reduce_force_j_generic(f_buf, fcj_buf, fout, tidxi, tidxj, aidx);
675 gmx_opencl_inline void reduce_force_i_and_shift_shfl(__private fvec fci_buf[],
676 __global float* fout,
682 __global float* fshift)
684 /* Only does reduction over 4 elements in cluster (2 per warp). Needs to be changed
686 float2 fshift_buf = 0;
687 for (int ci_offset = 0; ci_offset < c_nbnxnGpuNumClusterPerSupercluster; ci_offset++)
689 int aidx = (sci * c_nbnxnGpuNumClusterPerSupercluster + ci_offset) * CL_SIZE + tidxi;
690 float3 fin = (float3)(fci_buf[ci_offset][0], fci_buf[ci_offset][1], fci_buf[ci_offset][2]);
691 fin.x += intel_sub_group_shuffle_down(fin.x, fin.x, CL_SIZE);
692 fin.y += intel_sub_group_shuffle_up(fin.y, fin.y, CL_SIZE);
693 fin.z += intel_sub_group_shuffle_down(fin.z, fin.z, CL_SIZE);
699 /* Threads 0,1 and 2,3 increment x,y for their warp */
700 atomicAdd_g_f(&fout[3 * aidx + (tidxj & 1)], fin.x);
703 fshift_buf[0] += fin.x;
705 /* Threads 0 and 2 increment z for their warp */
706 if ((tidxj & 1) == 0)
708 atomicAdd_g_f(&fout[3 * aidx + 2], fin.z);
711 fshift_buf[1] += fin.z;
715 /* add up local shift forces into global mem */
718 // Threads 0,1 and 2,3 update x,y
719 atomicAdd_g_f(&(fshift[3 * shift + (tidxj & 1)]), fshift_buf[0]);
720 // Threads 0 and 2 update z
721 if ((tidxj & 1) == 0)
723 atomicAdd_g_f(&(fshift[3 * shift + 2]), fshift_buf[1]);
729 /*! Final i-force reduction; this implementation works only with power of two
732 gmx_opencl_inline void reduce_force_i_and_shift_pow2(volatile __local float* f_buf,
733 __private fvec fci_buf[],
734 __global float* fout,
740 __global float* fshift)
742 float fshift_buf = 0;
743 for (int ci_offset = 0; ci_offset < c_nbnxnGpuNumClusterPerSupercluster; ci_offset++)
745 int aidx = (sci * c_nbnxnGpuNumClusterPerSupercluster + ci_offset) * CL_SIZE + tidxi;
746 int tidx = tidxi + tidxj * CL_SIZE;
747 /* store i forces in shmem */
748 f_buf[tidx] = fci_buf[ci_offset][0];
749 f_buf[FBUF_STRIDE + tidx] = fci_buf[ci_offset][1];
750 f_buf[2 * FBUF_STRIDE + tidx] = fci_buf[ci_offset][2];
751 barrier(CLK_LOCAL_MEM_FENCE);
753 /* Reduce the initial CL_SIZE values for each i atom to half
754 * every step by using CL_SIZE * i threads.
755 * Can't just use i as loop variable because than nvcc refuses to unroll.
758 for (int j = CL_SIZE_LOG2 - 1; j > 0; j--)
763 f_buf[tidxj * CL_SIZE + tidxi] += f_buf[(tidxj + i) * CL_SIZE + tidxi];
764 f_buf[FBUF_STRIDE + tidxj * CL_SIZE + tidxi] +=
765 f_buf[FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
766 f_buf[2 * FBUF_STRIDE + tidxj * CL_SIZE + tidxi] +=
767 f_buf[2 * FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
772 * a) for CL_SIZE<8: id 2 (doing z in next block) is in 2nd warp
773 * b) for all CL_SIZE a barrier is needed before f_buf is reused by next reduce_force_i call
774 * TODO: Test on Nvidia for performance difference between having the barrier here or after the atomicAdd
776 barrier(CLK_LOCAL_MEM_FENCE);
778 /* i == 1, last reduction step, writing to global mem */
779 /* Split the reduction between the first 3 line threads
780 Threads with line id 0 will do the reduction for (float3).x components
781 Threads with line id 1 will do the reduction for (float3).y components
782 Threads with line id 2 will do the reduction for (float3).z components. */
785 float f = f_buf[tidxj * FBUF_STRIDE + tidxi] + f_buf[tidxj * FBUF_STRIDE + i * CL_SIZE + tidxi];
787 atomicAdd_g_f(&fout[3 * aidx + tidxj], f);
795 /* add up local shift forces into global mem */
798 /* Only threads with tidxj < 3 will update fshift.
799 The threads performing the update, must be the same as the threads
800 storing the reduction result above.
804 atomicAdd_g_f(&(fshift[3 * shift + tidxj]), fshift_buf);
809 /*! Final i-force reduction
811 gmx_opencl_inline void reduce_force_i_and_shift(__local float gmx_unused* f_buf,
812 __private fvec fci_buf[],
819 __global float* fshift)
822 reduce_force_i_and_shift_shfl(fci_buf, f, bCalcFshift, tidxi, tidxj, sci, shift, fshift);
824 reduce_force_i_and_shift_pow2(f_buf, fci_buf, f, bCalcFshift, tidxi, tidxj, sci, shift, fshift);
830 gmx_opencl_inline void reduce_energy_shfl(float E_lj,
832 volatile __global float* e_lj,
833 volatile __global float* e_el,
836 E_lj = sub_group_reduce_add(E_lj);
837 E_el = sub_group_reduce_add(E_el);
838 /* Should be get_sub_group_local_id()==0. Doesn't work with Intel Classic driver.
839 * To make it possible to use REDUCE_SHUFFLE with single subgroup per i-j pair
840 * (e.g. subgroup size 16 with CL_SIZE 4), either this "if" needs to be changed or
841 * the definition of WARP_SIZE (currently CL_SIZE*CL_SIZE/2) needs to be changed
842 * (by supporting c_nbnxnGpuClusterpairSplit=1). */
843 if (tidx == 0 || tidx == WARP_SIZE)
845 atomicAdd_g_f(e_lj, E_lj);
846 atomicAdd_g_f(e_el, E_el);
851 /*! Energy reduction; this implementation works only with power of two
854 gmx_opencl_inline void reduce_energy_pow2(volatile __local float* buf,
855 volatile __global float* e_lj,
856 volatile __global float* e_el,
859 int i = WARP_SIZE / 2;
861 /* Can't just use i as loop variable because than nvcc refuses to unroll. */
862 for (int j = WARP_SIZE_LOG2 - 1; j > 0; j--)
866 buf[tidx] += buf[tidx + i];
867 buf[FBUF_STRIDE + tidx] += buf[FBUF_STRIDE + tidx + i];
872 /* last reduction step, writing to global mem */
875 float e1 = buf[tidx] + buf[tidx + i];
876 float e2 = buf[FBUF_STRIDE + tidx] + buf[FBUF_STRIDE + tidx + i];
878 atomicAdd_g_f(e_lj, e1);
879 atomicAdd_g_f(e_el, e2);
883 gmx_opencl_inline void reduce_energy(volatile __local float gmx_unused* buf,
886 volatile __global float* e_lj,
887 volatile __global float* e_el,
891 reduce_energy_shfl(E_lj, E_el, e_lj, e_el, tidx);
893 /* flush the energies to shmem and reduce them */
895 buf[FBUF_STRIDE + tidx] = E_el;
896 reduce_energy_pow2(buf + (tidx & WARP_SIZE), e_lj, e_el, tidx & ~WARP_SIZE);
900 gmx_opencl_inline bool gmx_sub_group_any_localmem(volatile __local int* warp_any, int widx, bool pred)
907 bool ret = warp_any[widx];
914 //! Returns a true if predicate is true for any work item in warp
915 gmx_opencl_inline bool gmx_sub_group_any(volatile __local int gmx_unused* warp_any, int gmx_unused widx, bool pred)
917 # if USE_SUBGROUP_ANY
918 return sub_group_any(pred);
920 return gmx_sub_group_any_localmem(warp_any, widx, pred);
924 #endif /* NBNXN_OPENCL_KERNEL_UTILS_CLH */