2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, 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 /* Note that floating-point constants in CUDA code should be suffixed
37 * with f (e.g. 0.5f), to stop the compiler producing intermediate
38 * code that is in double precision.
42 #include "gromacs/gmxlib/cuda_tools/vectype_ops.cuh"
44 #ifndef NBNXN_CUDA_KERNEL_UTILS_CUH
45 #define NBNXN_CUDA_KERNEL_UTILS_CUH
47 #define WARP_SIZE_POW2_EXPONENT (5)
48 #define CL_SIZE_POW2_EXPONENT (3) /* change this together with GPU_NS_CLUSTER_SIZE !*/
49 #define CL_SIZE_SQ (CL_SIZE * CL_SIZE)
50 #define FBUF_STRIDE (CL_SIZE_SQ)
52 #define ONE_SIXTH_F 0.16666667f
53 #define ONE_TWELVETH_F 0.08333333f
56 /*! i-cluster interaction mask for a super-cluster with all NCL_PER_SUPERCL bits set */
57 const unsigned supercl_interaction_mask = ((1U << NCL_PER_SUPERCL) - 1U);
59 /*! Apply force switch, force + energy version. */
60 static inline __device__
61 void calculate_force_switch_F(const cu_nbparam_t nbparam,
70 /* force switch constants */
71 float disp_shift_V2 = nbparam.dispersion_shift.c2;
72 float disp_shift_V3 = nbparam.dispersion_shift.c3;
73 float repu_shift_V2 = nbparam.repulsion_shift.c2;
74 float repu_shift_V3 = nbparam.repulsion_shift.c3;
77 r_switch = r - nbparam.rvdw_switch;
78 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
81 -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
82 c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
85 /*! Apply force switch, force-only version. */
86 static inline __device__
87 void calculate_force_switch_F_E(const cu_nbparam_t nbparam,
97 /* force switch constants */
98 float disp_shift_V2 = nbparam.dispersion_shift.c2;
99 float disp_shift_V3 = nbparam.dispersion_shift.c3;
100 float repu_shift_V2 = nbparam.repulsion_shift.c2;
101 float repu_shift_V3 = nbparam.repulsion_shift.c3;
103 float disp_shift_F2 = nbparam.dispersion_shift.c2/3;
104 float disp_shift_F3 = nbparam.dispersion_shift.c3/4;
105 float repu_shift_F2 = nbparam.repulsion_shift.c2/3;
106 float repu_shift_F3 = nbparam.repulsion_shift.c3/4;
109 r_switch = r - nbparam.rvdw_switch;
110 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
113 -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
114 c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
116 c6*(disp_shift_F2 + disp_shift_F3*r_switch)*r_switch*r_switch*r_switch -
117 c12*(repu_shift_F2 + repu_shift_F3*r_switch)*r_switch*r_switch*r_switch;
120 /*! Apply potential switch, force-only version. */
121 static inline __device__
122 void calculate_potential_switch_F(const cu_nbparam_t nbparam,
133 /* potential switch constants */
134 float switch_V3 = nbparam.vdw_switch.c3;
135 float switch_V4 = nbparam.vdw_switch.c4;
136 float switch_V5 = nbparam.vdw_switch.c5;
137 float switch_F2 = 3*nbparam.vdw_switch.c3;
138 float switch_F3 = 4*nbparam.vdw_switch.c4;
139 float switch_F4 = 5*nbparam.vdw_switch.c5;
142 r_switch = r - nbparam.rvdw_switch;
144 /* Unlike in the F+E kernel, conditional is faster here */
147 sw = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
148 dsw = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
150 *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
154 /*! Apply potential switch, force + energy version. */
155 static inline __device__
156 void calculate_potential_switch_F_E(const cu_nbparam_t nbparam,
167 /* potential switch constants */
168 float switch_V3 = nbparam.vdw_switch.c3;
169 float switch_V4 = nbparam.vdw_switch.c4;
170 float switch_V5 = nbparam.vdw_switch.c5;
171 float switch_F2 = 3*nbparam.vdw_switch.c3;
172 float switch_F3 = 4*nbparam.vdw_switch.c4;
173 float switch_F4 = 5*nbparam.vdw_switch.c5;
176 r_switch = r - nbparam.rvdw_switch;
177 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
179 /* Unlike in the F-only kernel, masking is faster here */
180 sw = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
181 dsw = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
183 *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
187 /*! Calculate LJ-PME grid force contribution with
188 * geometric combination rule.
190 static inline __device__
191 void calculate_lj_ewald_comb_geom_F(const cu_nbparam_t nbparam,
200 float c6grid, inv_r6_nm, cr2, expmcr2, poly;
203 c6grid = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei) * tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej);
205 c6grid = tex1Dfetch(nbfp_comb_texref, 2*typei) * tex1Dfetch(nbfp_comb_texref, 2*typej);
206 #endif /* USE_TEXOBJ */
208 /* Recalculate inv_r6 without exclusion mask */
209 inv_r6_nm = inv_r2*inv_r2*inv_r2;
211 expmcr2 = expf(-cr2);
212 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
214 /* Subtract the grid force from the total LJ force */
215 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
218 /*! Calculate LJ-PME grid force + energy contribution with
219 * geometric combination rule.
221 static inline __device__
222 void calculate_lj_ewald_comb_geom_F_E(const cu_nbparam_t nbparam,
233 float c6grid, inv_r6_nm, cr2, expmcr2, poly, sh_mask;
236 c6grid = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei) * tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej);
238 c6grid = tex1Dfetch(nbfp_comb_texref, 2*typei) * tex1Dfetch(nbfp_comb_texref, 2*typej);
239 #endif /* USE_TEXOBJ */
241 /* Recalculate inv_r6 without exclusion mask */
242 inv_r6_nm = inv_r2*inv_r2*inv_r2;
244 expmcr2 = expf(-cr2);
245 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
247 /* Subtract the grid force from the total LJ force */
248 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
250 /* Shift should be applied only to real LJ pairs */
251 sh_mask = nbparam.sh_lj_ewald*int_bit;
252 *E_lj += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
255 /*! Calculate LJ-PME grid force + energy contribution (if E_lj != NULL) with
256 * Lorentz-Berthelot combination rule.
257 * We use a single F+E kernel with conditional because the performance impact
258 * of this is pretty small and LB on the CPU is anyway very slow.
260 static inline __device__
261 void calculate_lj_ewald_comb_LB_F_E(const cu_nbparam_t nbparam,
272 float c6grid, inv_r6_nm, cr2, expmcr2, poly;
273 float sigma, sigma2, epsilon;
275 /* sigma and epsilon are scaled to give 6*C6 */
277 sigma = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei ) + tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej );
278 epsilon = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei + 1) * tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej + 1);
280 sigma = tex1Dfetch(nbfp_comb_texref, 2*typei ) + tex1Dfetch(nbfp_comb_texref, 2*typej );
281 epsilon = tex1Dfetch(nbfp_comb_texref, 2*typei + 1) * tex1Dfetch(nbfp_comb_texref, 2*typej + 1);
282 #endif /* USE_TEXOBJ */
283 sigma2 = sigma*sigma;
284 c6grid = epsilon*sigma2*sigma2*sigma2;
286 /* Recalculate inv_r6 without exclusion mask */
287 inv_r6_nm = inv_r2*inv_r2*inv_r2;
289 expmcr2 = expf(-cr2);
290 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
292 /* Subtract the grid force from the total LJ force */
293 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
299 /* Shift should be applied only to real LJ pairs */
300 sh_mask = nbparam.sh_lj_ewald*int_bit;
301 *E_lj += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
305 /*! Interpolate Ewald coulomb force using the table through the tex_nbfp texture.
306 * Original idea: from the OpenMM project
308 static inline __device__
309 float interpolate_coulomb_force_r(float r, float scale)
311 float normalized = scale * r;
312 int index = (int) normalized;
313 float fract2 = normalized - index;
314 float fract1 = 1.0f - fract2;
316 return fract1 * tex1Dfetch(coulomb_tab_texref, index)
317 + fract2 * tex1Dfetch(coulomb_tab_texref, index + 1);
320 #ifdef HAVE_CUDA_TEXOBJ_SUPPORT
321 static inline __device__
322 float interpolate_coulomb_force_r(cudaTextureObject_t texobj_coulomb_tab,
323 float r, float scale)
325 float normalized = scale * r;
326 int index = (int) normalized;
327 float fract2 = normalized - index;
328 float fract1 = 1.0f - fract2;
330 return fract1 * tex1Dfetch<float>(texobj_coulomb_tab, index) +
331 fract2 * tex1Dfetch<float>(texobj_coulomb_tab, index + 1);
336 /*! Calculate analytical Ewald correction term. */
337 static inline __device__
338 float pmecorrF(float z2)
340 const float FN6 = -1.7357322914161492954e-8f;
341 const float FN5 = 1.4703624142580877519e-6f;
342 const float FN4 = -0.000053401640219807709149f;
343 const float FN3 = 0.0010054721316683106153f;
344 const float FN2 = -0.019278317264888380590f;
345 const float FN1 = 0.069670166153766424023f;
346 const float FN0 = -0.75225204789749321333f;
348 const float FD4 = 0.0011193462567257629232f;
349 const float FD3 = 0.014866955030185295499f;
350 const float FD2 = 0.11583842382862377919f;
351 const float FD1 = 0.50736591960530292870f;
352 const float FD0 = 1.0f;
355 float polyFN0, polyFN1, polyFD0, polyFD1;
359 polyFD0 = FD4*z4 + FD2;
360 polyFD1 = FD3*z4 + FD1;
361 polyFD0 = polyFD0*z4 + FD0;
362 polyFD0 = polyFD1*z2 + polyFD0;
364 polyFD0 = 1.0f/polyFD0;
366 polyFN0 = FN6*z4 + FN4;
367 polyFN1 = FN5*z4 + FN3;
368 polyFN0 = polyFN0*z4 + FN2;
369 polyFN1 = polyFN1*z4 + FN1;
370 polyFN0 = polyFN0*z4 + FN0;
371 polyFN0 = polyFN1*z2 + polyFN0;
373 return polyFN0*polyFD0;
376 /*! Final j-force reduction; this generic implementation works with
377 * arbitrary array sizes.
379 static inline __device__
380 void reduce_force_j_generic(float *f_buf, float3 *fout,
381 int tidxi, int tidxj, int aidx)
386 for (int j = tidxj * CL_SIZE; j < (tidxj + 1) * CL_SIZE; j++)
388 f += f_buf[FBUF_STRIDE * tidxi + j];
391 atomicAdd((&fout[aidx].x)+tidxi, f);
395 /*! Final j-force reduction; this implementation only with power of two
396 * array sizes and with sm >= 3.0
398 #if __CUDA_ARCH__ >= 300
399 static inline __device__
400 void reduce_force_j_warp_shfl(float3 f, float3 *fout,
403 f.x += __shfl_down(f.x, 1);
404 f.y += __shfl_up (f.y, 1);
405 f.z += __shfl_down(f.z, 1);
412 f.x += __shfl_down(f.x, 2);
413 f.z += __shfl_up (f.z, 2);
420 f.x += __shfl_down(f.x, 4);
424 atomicAdd((&fout[aidx].x) + tidxi, f.x);
429 /*! Final i-force reduction; this generic implementation works with
430 * arbitrary array sizes.
431 * TODO: add the tidxi < 3 trick
433 static inline __device__
434 void reduce_force_i_generic(float *f_buf, float3 *fout,
435 float *fshift_buf, bool bCalcFshift,
436 int tidxi, int tidxj, int aidx)
441 for (int j = tidxi; j < CL_SIZE_SQ; j += CL_SIZE)
443 f += f_buf[tidxj * FBUF_STRIDE + j];
446 atomicAdd(&fout[aidx].x + tidxj, f);
455 /*! Final i-force reduction; this implementation works only with power of two
458 static inline __device__
459 void reduce_force_i_pow2(volatile float *f_buf, float3 *fout,
460 float *fshift_buf, bool bCalcFshift,
461 int tidxi, int tidxj, int aidx)
466 /* Reduce the initial CL_SIZE values for each i atom to half
467 * every step by using CL_SIZE * i threads.
468 * Can't just use i as loop variable because than nvcc refuses to unroll.
472 for (j = CL_SIZE_POW2_EXPONENT - 1; j > 0; j--)
477 f_buf[ tidxj * CL_SIZE + tidxi] += f_buf[ (tidxj + i) * CL_SIZE + tidxi];
478 f_buf[ FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[ FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
479 f_buf[2 * FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[2 * FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
484 /* i == 1, last reduction step, writing to global mem */
487 /* tidxj*FBUF_STRIDE selects x, y or z */
488 f = f_buf[tidxj * FBUF_STRIDE + tidxi] +
489 f_buf[tidxj * FBUF_STRIDE + i * CL_SIZE + tidxi];
491 atomicAdd(&(fout[aidx].x) + tidxj, f);
501 /*! Final i-force reduction wrapper; calls the generic or pow2 reduction depending
502 * on whether the size of the array to be reduced is power of two or not.
504 static inline __device__
505 void reduce_force_i(float *f_buf, float3 *f,
506 float *fshift_buf, bool bCalcFshift,
507 int tidxi, int tidxj, int ai)
509 if ((CL_SIZE & (CL_SIZE - 1)))
511 reduce_force_i_generic(f_buf, f, fshift_buf, bCalcFshift, tidxi, tidxj, ai);
515 reduce_force_i_pow2(f_buf, f, fshift_buf, bCalcFshift, tidxi, tidxj, ai);
519 /*! Final i-force reduction; this implementation works only with power of two
520 * array sizes and with sm >= 3.0
522 #if __CUDA_ARCH__ >= 300
523 static inline __device__
524 void reduce_force_i_warp_shfl(float3 fin, float3 *fout,
525 float *fshift_buf, bool bCalcFshift,
528 fin.x += __shfl_down(fin.x, CL_SIZE);
529 fin.y += __shfl_up (fin.y, CL_SIZE);
530 fin.z += __shfl_down(fin.z, CL_SIZE);
537 fin.x += __shfl_down(fin.x, 2*CL_SIZE);
538 fin.z += __shfl_up (fin.z, 2*CL_SIZE);
545 /* Threads 0,1,2 and 4,5,6 increment x,y,z for their warp */
548 atomicAdd(&fout[aidx].x + (tidxj & ~4), fin.x);
552 *fshift_buf += fin.x;
558 /*! Energy reduction; this implementation works only with power of two
561 static inline __device__
562 void reduce_energy_pow2(volatile float *buf,
563 float *e_lj, float *e_el,
571 /* Can't just use i as loop variable because than nvcc refuses to unroll. */
573 for (j = WARP_SIZE_POW2_EXPONENT - 1; j > 0; j--)
577 buf[ tidx] += buf[ tidx + i];
578 buf[FBUF_STRIDE + tidx] += buf[FBUF_STRIDE + tidx + i];
583 // TODO do this on two threads - requires e_lj and e_el to be stored on adjascent
584 // memory locations to make sense
585 /* last reduction step, writing to global mem */
588 e1 = buf[ tidx] + buf[ tidx + i];
589 e2 = buf[FBUF_STRIDE + tidx] + buf[FBUF_STRIDE + tidx + i];
596 /*! Energy reduction; this implementation works only with power of two
597 * array sizes and with sm >= 3.0
599 #if __CUDA_ARCH__ >= 300
600 static inline __device__
601 void reduce_energy_warp_shfl(float E_lj, float E_el,
602 float *e_lj, float *e_el,
609 for (i = 0; i < 5; i++)
611 E_lj += __shfl_down(E_lj, sh);
612 E_el += __shfl_down(E_el, sh);
616 /* The first thread in the warp writes the reduced energies */
617 if (tidx == 0 || tidx == WARP_SIZE)
619 atomicAdd(e_lj, E_lj);
620 atomicAdd(e_el, E_el);
623 #endif /* __CUDA_ARCH__ */
625 #endif /* NBNXN_CUDA_KERNEL_UTILS_CUH */