2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
36 #include "device_utils.clh"
37 #include "vectype_ops.clh"
39 #define CL_SIZE (NBNXN_GPU_CLUSTER_SIZE)
40 #define NCL_PER_SUPERCL (NBNXN_GPU_NCLUSTER_PER_SUPERCLUSTER)
42 #define WARP_SIZE (CL_SIZE*CL_SIZE/2)
44 /* 1.0 / sqrt(M_PI) */
45 #define M_FLOAT_1_SQRTPI 0.564189583547756f
49 #ifndef NBNXN_OPENCL_KERNEL_UTILS_CLH
50 #define NBNXN_OPENCL_KERNEL_UTILS_CLH
52 __constant sampler_t generic_sampler = (CLK_NORMALIZED_COORDS_FALSE /* Natural coords */
53 | CLK_ADDRESS_NONE /* No clamp/repeat*/
54 | CLK_FILTER_NEAREST); /* No interpolation */
57 #define WARP_SIZE_LOG2 (5)
58 #define CL_SIZE_LOG2 (3)
60 #define WARP_SIZE_LOG2 (3)
61 #define CL_SIZE_LOG2 (2)
63 #error unsupported CL_SIZE
66 #define CL_SIZE_SQ (CL_SIZE * CL_SIZE)
67 #define FBUF_STRIDE (CL_SIZE_SQ)
69 #define ONE_SIXTH_F 0.16666667f
70 #define ONE_TWELVETH_F 0.08333333f
73 // Data structures shared between OpenCL device code and OpenCL host code
74 // TODO: review, improve
75 // Replaced real by float for now, to avoid including any other header
82 /* Used with potential switching:
83 * rsw = max(r - r_switch, 0)
84 * sw = 1 + c3*rsw^3 + c4*rsw^4 + c5*rsw^5
85 * dsw = 3*c3*rsw^2 + 4*c4*rsw^3 + 5*c5*rsw^4
86 * force = force*dsw - potential*sw
95 // Data structure shared between the OpenCL device code and OpenCL host code
96 // Must not contain OpenCL objects (buffers)
97 typedef struct cl_nbparam_params
100 int eeltype; /**< type of electrostatics, takes values from #eelCu */
101 int vdwtype; /**< type of VdW impl., takes values from #evdwCu */
103 float epsfac; /**< charge multiplication factor */
104 float c_rf; /**< Reaction-field/plain cutoff electrostatics const. */
105 float two_k_rf; /**< Reaction-field electrostatics constant */
106 float ewald_beta; /**< Ewald/PME parameter */
107 float sh_ewald; /**< Ewald/PME correction term substracted from the direct-space potential */
108 float sh_lj_ewald; /**< LJ-Ewald/PME correction term added to the correction potential */
109 float ewaldcoeff_lj; /**< LJ-Ewald/PME coefficient */
111 float rcoulomb_sq; /**< Coulomb cut-off squared */
113 float rvdw_sq; /**< VdW cut-off squared */
114 float rvdw_switch; /**< VdW switched cut-off */
115 float rlistOuter_sq; /**< Full, outer pair-list cut-off squared */
116 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 */
118 shift_consts_t dispersion_shift; /**< VdW shift dispersion constants */
119 shift_consts_t repulsion_shift; /**< VdW shift repulsion constants */
120 switch_consts_t vdw_switch; /**< VdW switch constants */
122 /* Ewald Coulomb force table data - accessed through texture memory */
123 float coulomb_tab_scale; /**< table scale/spacing */
124 }cl_nbparam_params_t;
127 int sci; /* i-super-cluster */
128 int shift; /* Shift vector index plus possible flags */
129 int cj4_ind_start; /* Start index into cj4 */
130 int cj4_ind_end; /* End index into cj4 */
134 unsigned int imask; /* The i-cluster interactions mask for 1 warp */
135 int excl_ind; /* Index into the exclusion array for 1 warp */
139 int cj[4]; /* The 4 j-clusters */
140 nbnxn_im_ei_t imei[2]; /* The i-cluster mask data for 2 warps */
145 unsigned int pair[CL_SIZE*CL_SIZE/2]; /* Topology exclusion interaction bits for one warp,
146 * each unsigned has bitS for 4*8 i clusters
150 /*! i-cluster interaction mask for a super-cluster with all NCL_PER_SUPERCL bits set */
151 __constant unsigned supercl_interaction_mask = ((1U << NCL_PER_SUPERCL) - 1U);
154 /*! \brief Preload cj4 into local memory.
156 * - For AMD we load once for a wavefront of 64 threads (on 4 threads * NTHREAD_Z)
157 * - For NVIDIA once per warp (on 2x4 threads * NTHREAD_Z)
158 * - Same as AMD in the nowarp kernel; we do not assume execution width and therefore
159 * the caller needs to sync.
161 * It is the caller's responsibility to make sure that data is consumed only when
162 * it's ready. This function does not call a barrier.
165 void preloadCj4(__local int *sm_cjPreload,
166 const __global int *gm_cj,
176 const int c_clSize = CL_SIZE;
177 const int c_nbnxnGpuJgroupSize = NBNXN_GPU_JGROUP_SIZE;
178 const int c_nbnxnGpuClusterpairSplit = 2;
179 const int c_splitClSize = c_clSize/c_nbnxnGpuClusterpairSplit;
181 /* Pre-load cj into shared memory */
182 #if defined _NVIDIA_SOURCE_
183 /* on both warps separately for NVIDIA */
184 if ((tidxj == 0 | tidxj == 4) & (tidxi < c_nbnxnGpuJgroupSize))
186 sm_cjPreload[tidxi + tidxj * c_nbnxnGpuJgroupSize/c_splitClSize] = gm_cj[tidxi];
188 #else // AMD or nowarp
189 /* Note that with "nowarp" / on hardware with wavefronts <64 a barrier is needed after preload. */
190 if (tidxj == 0 & tidxi < c_nbnxnGpuJgroupSize)
192 sm_cjPreload[tidxi] = gm_cj[tidxi];
197 /* \brief Load a cj given a jm index.
199 * If cj4 preloading is enabled, it loads from the local memory, otherwise from global.
202 int loadCj(__local int *sm_cjPreload,
203 const __global int *gm_cj,
208 const int c_clSize = CL_SIZE;
209 const int c_nbnxnGpuJgroupSize = NBNXN_GPU_JGROUP_SIZE;
210 const int c_nbnxnGpuClusterpairSplit = 2;
211 const int c_splitClSize = c_clSize/c_nbnxnGpuClusterpairSplit;
214 #if defined _NVIDIA_SOURCE_
215 int warpLoadOffset = (tidxj & 4) * c_nbnxnGpuJgroupSize/c_splitClSize;
216 #elif defined _AMD_SOURCE_
217 int warpLoadOffset = 0;
221 return sm_cjPreload[jm + warpLoadOffset];
228 /*! Convert LJ sigma,epsilon parameters to C6,C12. */
230 void convert_sigma_epsilon_to_c6_c12(const float sigma,
235 float sigma2, sigma6;
237 sigma2 = sigma * sigma;
238 sigma6 = sigma2 *sigma2 * sigma2;
239 *c6 = epsilon * sigma6;
244 /*! Apply force switch, force + energy version. */
246 void calculate_force_switch_F(cl_nbparam_params_t *nbparam,
255 /* force switch constants */
256 float disp_shift_V2 = nbparam->dispersion_shift.c2;
257 float disp_shift_V3 = nbparam->dispersion_shift.c3;
258 float repu_shift_V2 = nbparam->repulsion_shift.c2;
259 float repu_shift_V3 = nbparam->repulsion_shift.c3;
262 r_switch = r - nbparam->rvdw_switch;
263 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
266 -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
267 c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
270 /*! Apply force switch, force-only version. */
272 void calculate_force_switch_F_E(cl_nbparam_params_t *nbparam,
282 /* force switch constants */
283 float disp_shift_V2 = nbparam->dispersion_shift.c2;
284 float disp_shift_V3 = nbparam->dispersion_shift.c3;
285 float repu_shift_V2 = nbparam->repulsion_shift.c2;
286 float repu_shift_V3 = nbparam->repulsion_shift.c3;
288 float disp_shift_F2 = nbparam->dispersion_shift.c2/3;
289 float disp_shift_F3 = nbparam->dispersion_shift.c3/4;
290 float repu_shift_F2 = nbparam->repulsion_shift.c2/3;
291 float repu_shift_F3 = nbparam->repulsion_shift.c3/4;
294 r_switch = r - nbparam->rvdw_switch;
295 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
298 -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
299 c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
301 c6*(disp_shift_F2 + disp_shift_F3*r_switch)*r_switch*r_switch*r_switch -
302 c12*(repu_shift_F2 + repu_shift_F3*r_switch)*r_switch*r_switch*r_switch;
305 /*! Apply potential switch, force-only version. */
307 void calculate_potential_switch_F(cl_nbparam_params_t *nbparam,
316 /* potential switch constants */
317 float switch_V3 = nbparam->vdw_switch.c3;
318 float switch_V4 = nbparam->vdw_switch.c4;
319 float switch_V5 = nbparam->vdw_switch.c5;
320 float switch_F2 = nbparam->vdw_switch.c3;
321 float switch_F3 = nbparam->vdw_switch.c4;
322 float switch_F4 = nbparam->vdw_switch.c5;
325 r_switch = r - nbparam->rvdw_switch;
327 /* Unlike in the F+E kernel, conditional is faster here */
330 sw = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
331 dsw = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
333 *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
337 /*! Apply potential switch, force + energy version. */
339 void calculate_potential_switch_F_E(cl_nbparam_params_t *nbparam,
348 /* potential switch constants */
349 float switch_V3 = nbparam->vdw_switch.c3;
350 float switch_V4 = nbparam->vdw_switch.c4;
351 float switch_V5 = nbparam->vdw_switch.c5;
352 float switch_F2 = nbparam->vdw_switch.c3;
353 float switch_F3 = nbparam->vdw_switch.c4;
354 float switch_F4 = nbparam->vdw_switch.c5;
357 r_switch = r - nbparam->rvdw_switch;
358 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
360 /* Unlike in the F-only kernel, masking is faster here */
361 sw = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
362 dsw = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
364 *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
368 /*! Calculate LJ-PME grid force contribution with
369 * geometric combination rule.
372 void calculate_lj_ewald_comb_geom_F(__constant float * nbfp_comb_climg2d,
381 float c6grid, inv_r6_nm, cr2, expmcr2, poly;
383 c6grid = nbfp_comb_climg2d[2*typei]*nbfp_comb_climg2d[2*typej];
385 /* Recalculate inv_r6 without exclusion mask */
386 inv_r6_nm = inv_r2*inv_r2*inv_r2;
389 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
391 /* Subtract the grid force from the total LJ force */
392 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
395 /*! Calculate LJ-PME grid force + energy contribution with
396 * geometric combination rule.
399 void calculate_lj_ewald_comb_geom_F_E(__constant float *nbfp_comb_climg2d,
400 cl_nbparam_params_t *nbparam,
411 float c6grid, inv_r6_nm, cr2, expmcr2, poly, sh_mask;
413 c6grid = nbfp_comb_climg2d[2*typei]*nbfp_comb_climg2d[2*typej];
415 /* Recalculate inv_r6 without exclusion mask */
416 inv_r6_nm = inv_r2*inv_r2*inv_r2;
419 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
421 /* Subtract the grid force from the total LJ force */
422 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
424 /* Shift should be applied only to real LJ pairs */
425 sh_mask = nbparam->sh_lj_ewald*int_bit;
426 *E_lj += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
429 /*! Calculate LJ-PME grid force + energy contribution (if E_lj != NULL) with
430 * Lorentz-Berthelot combination rule.
431 * We use a single F+E kernel with conditional because the performance impact
432 * of this is pretty small and LB on the CPU is anyway very slow.
435 void calculate_lj_ewald_comb_LB_F_E(__constant float *nbfp_comb_climg2d,
436 cl_nbparam_params_t *nbparam,
448 float c6grid, inv_r6_nm, cr2, expmcr2, poly;
449 float sigma, sigma2, epsilon;
451 /* sigma and epsilon are scaled to give 6*C6 */
452 sigma = nbfp_comb_climg2d[2*typei] + nbfp_comb_climg2d[2*typej];
454 epsilon = nbfp_comb_climg2d[2*typei+1]*nbfp_comb_climg2d[2*typej+1];
456 sigma2 = sigma*sigma;
457 c6grid = epsilon*sigma2*sigma2*sigma2;
459 /* Recalculate inv_r6 without exclusion mask */
460 inv_r6_nm = inv_r2*inv_r2*inv_r2;
463 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
465 /* Subtract the grid force from the total LJ force */
466 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
468 if (with_E_lj == true)
472 /* Shift should be applied only to real LJ pairs */
473 sh_mask = nbparam->sh_lj_ewald*int_bit;
474 *E_lj += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
478 /*! Interpolate Ewald coulomb force using the table through the tex_nbfp texture.
479 * Original idea: from the OpenMM project
481 gmx_opencl_inline float
482 interpolate_coulomb_force_r(__constant float *coulomb_tab_climg2d,
486 float normalized = scale * r;
487 int index = (int) normalized;
488 float fract2 = normalized - index;
489 float fract1 = 1.0f - fract2;
491 return fract1*coulomb_tab_climg2d[index] +
492 fract2*coulomb_tab_climg2d[index + 1];
495 /*! Calculate analytical Ewald correction term. */
497 float pmecorrF(float z2)
499 const float FN6 = -1.7357322914161492954e-8f;
500 const float FN5 = 1.4703624142580877519e-6f;
501 const float FN4 = -0.000053401640219807709149f;
502 const float FN3 = 0.0010054721316683106153f;
503 const float FN2 = -0.019278317264888380590f;
504 const float FN1 = 0.069670166153766424023f;
505 const float FN0 = -0.75225204789749321333f;
507 const float FD4 = 0.0011193462567257629232f;
508 const float FD3 = 0.014866955030185295499f;
509 const float FD2 = 0.11583842382862377919f;
510 const float FD1 = 0.50736591960530292870f;
511 const float FD0 = 1.0f;
514 float polyFN0, polyFN1, polyFD0, polyFD1;
518 polyFD0 = FD4*z4 + FD2;
519 polyFD1 = FD3*z4 + FD1;
520 polyFD0 = polyFD0*z4 + FD0;
521 polyFD0 = polyFD1*z2 + polyFD0;
523 polyFD0 = 1.0f/polyFD0;
525 polyFN0 = FN6*z4 + FN4;
526 polyFN1 = FN5*z4 + FN3;
527 polyFN0 = polyFN0*z4 + FN2;
528 polyFN1 = polyFN1*z4 + FN1;
529 polyFN0 = polyFN0*z4 + FN0;
530 polyFN0 = polyFN1*z2 + polyFN0;
532 return polyFN0*polyFD0;
535 /*! Final j-force reduction; this generic implementation works with
536 * arbitrary array sizes.
539 void reduce_force_j_generic(__local float *f_buf, __global float *fout,
540 int tidxi, int tidxj, int aidx)
542 /* Split the reduction between the first 3 column threads
543 Threads with column id 0 will do the reduction for (float3).x components
544 Threads with column id 1 will do the reduction for (float3).y components
545 Threads with column id 2 will do the reduction for (float3).z components.
546 The reduction is performed for each line tidxj of f_buf. */
550 for (int j = tidxj * CL_SIZE; j < (tidxj + 1) * CL_SIZE; j++)
552 f += f_buf[FBUF_STRIDE * tidxi + j];
555 atomicAdd_g_f(&fout[3 * aidx + tidxi], f);
559 /*! Final i-force reduction; this generic implementation works with
560 * arbitrary array sizes.
563 void reduce_force_i_generic(__local float *f_buf, __global float *fout,
564 float *fshift_buf, bool bCalcFshift,
565 int tidxi, int tidxj, int aidx)
567 /* Split the reduction between the first 3 line threads
568 Threads with line id 0 will do the reduction for (float3).x components
569 Threads with line id 1 will do the reduction for (float3).y components
570 Threads with line id 2 will do the reduction for (float3).z components. */
574 for (int j = tidxi; j < CL_SIZE_SQ; j += CL_SIZE)
576 f += f_buf[tidxj * FBUF_STRIDE + j];
579 atomicAdd_g_f(&fout[3 * aidx + tidxj], f);
586 barrier(CLK_LOCAL_MEM_FENCE);
589 /*! Final i-force reduction; this implementation works only with power of two
593 void reduce_force_i_pow2(volatile __local float *f_buf, __global float *fout,
594 float *fshift_buf, bool bCalcFshift,
595 int tidxi, int tidxj, int aidx)
598 /* Reduce the initial CL_SIZE values for each i atom to half
599 * every step by using CL_SIZE * i threads.
600 * Can't just use i as loop variable because than nvcc refuses to unroll.
603 for (j = CL_SIZE_LOG2 - 1; j > 0; j--)
608 f_buf[ tidxj * CL_SIZE + tidxi] += f_buf[ (tidxj + i) * CL_SIZE + tidxi];
609 f_buf[ FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[ FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
610 f_buf[2 * FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[2 * FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
615 * a) for CL_SIZE<8: id 2 (doing z in next block) is in 2nd warp
616 * b) for all CL_SIZE a barrier is needed before f_buf is reused by next reduce_force_i call
618 barrier(CLK_LOCAL_MEM_FENCE);
620 /* i == 1, last reduction step, writing to global mem */
621 /* Split the reduction between the first 3 line threads
622 Threads with line id 0 will do the reduction for (float3).x components
623 Threads with line id 1 will do the reduction for (float3).y components
624 Threads with line id 2 will do the reduction for (float3).z components. */
627 float f = f_buf[tidxj * FBUF_STRIDE + tidxi] + f_buf[tidxj * FBUF_STRIDE + i * CL_SIZE + tidxi];
629 atomicAdd_g_f(&fout[3 * aidx + tidxj], f);
638 /*! Final i-force reduction wrapper; calls the generic or pow2 reduction depending
639 * on whether the size of the array to be reduced is power of two or not.
642 void reduce_force_i(__local float *f_buf, __global float *f,
643 float *fshift_buf, bool bCalcFshift,
644 int tidxi, int tidxj, int ai)
646 if ((CL_SIZE & (CL_SIZE - 1)))
648 reduce_force_i_generic(f_buf, f, fshift_buf, bCalcFshift, tidxi, tidxj, ai);
652 reduce_force_i_pow2(f_buf, f, fshift_buf, bCalcFshift, tidxi, tidxj, ai);
656 /*! Energy reduction; this implementation works only with power of two
660 void reduce_energy_pow2(volatile __local float *buf,
661 volatile __global float *e_lj,
662 volatile __global float *e_el,
670 /* Can't just use i as loop variable because than nvcc refuses to unroll. */
671 for (j = WARP_SIZE_LOG2 - 1; j > 0; j--)
675 buf[ tidx] += buf[ tidx + i];
676 buf[FBUF_STRIDE + tidx] += buf[FBUF_STRIDE + tidx + i];
681 /* last reduction step, writing to global mem */
684 e1 = buf[ tidx] + buf[ tidx + i];
685 e2 = buf[FBUF_STRIDE + tidx] + buf[FBUF_STRIDE + tidx + i];
687 atomicAdd_g_f(e_lj, e1);
688 atomicAdd_g_f(e_el, e2);
692 #endif /* NBNXN_OPENCL_KERNEL_UTILS_CLH */