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
48 #if defined HAVE_CUDA_TEXOBJ_SUPPORT && __CUDA_ARCH__ >= 300
49 /* Note: convenience macro, needs to be undef-ed at the end of the file. */
53 #define WARP_SIZE_POW2_EXPONENT (5)
54 #define CL_SIZE_POW2_EXPONENT (3) /* change this together with GPU_NS_CLUSTER_SIZE !*/
55 #define CL_SIZE_SQ (CL_SIZE * CL_SIZE)
56 #define FBUF_STRIDE (CL_SIZE_SQ)
58 #define ONE_SIXTH_F 0.16666667f
59 #define ONE_TWELVETH_F 0.08333333f
62 /*! i-cluster interaction mask for a super-cluster with all NCL_PER_SUPERCL bits set */
63 const unsigned supercl_interaction_mask = ((1U << NCL_PER_SUPERCL) - 1U);
65 /*! Apply force switch, force + energy version. */
66 static inline __device__
67 void calculate_force_switch_F(const cu_nbparam_t nbparam,
76 /* force switch constants */
77 float disp_shift_V2 = nbparam.dispersion_shift.c2;
78 float disp_shift_V3 = nbparam.dispersion_shift.c3;
79 float repu_shift_V2 = nbparam.repulsion_shift.c2;
80 float repu_shift_V3 = nbparam.repulsion_shift.c3;
83 r_switch = r - nbparam.rvdw_switch;
84 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
87 -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
88 c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
91 /*! Apply force switch, force-only version. */
92 static inline __device__
93 void calculate_force_switch_F_E(const cu_nbparam_t nbparam,
103 /* force switch constants */
104 float disp_shift_V2 = nbparam.dispersion_shift.c2;
105 float disp_shift_V3 = nbparam.dispersion_shift.c3;
106 float repu_shift_V2 = nbparam.repulsion_shift.c2;
107 float repu_shift_V3 = nbparam.repulsion_shift.c3;
109 float disp_shift_F2 = nbparam.dispersion_shift.c2/3;
110 float disp_shift_F3 = nbparam.dispersion_shift.c3/4;
111 float repu_shift_F2 = nbparam.repulsion_shift.c2/3;
112 float repu_shift_F3 = nbparam.repulsion_shift.c3/4;
115 r_switch = r - nbparam.rvdw_switch;
116 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
119 -c6*(disp_shift_V2 + disp_shift_V3*r_switch)*r_switch*r_switch*inv_r +
120 c12*(-repu_shift_V2 + repu_shift_V3*r_switch)*r_switch*r_switch*inv_r;
122 c6*(disp_shift_F2 + disp_shift_F3*r_switch)*r_switch*r_switch*r_switch -
123 c12*(repu_shift_F2 + repu_shift_F3*r_switch)*r_switch*r_switch*r_switch;
126 /*! Apply potential switch, force-only version. */
127 static inline __device__
128 void calculate_potential_switch_F(const cu_nbparam_t nbparam,
139 /* potential switch constants */
140 float switch_V3 = nbparam.vdw_switch.c3;
141 float switch_V4 = nbparam.vdw_switch.c4;
142 float switch_V5 = nbparam.vdw_switch.c5;
143 float switch_F2 = 3*nbparam.vdw_switch.c3;
144 float switch_F3 = 4*nbparam.vdw_switch.c4;
145 float switch_F4 = 5*nbparam.vdw_switch.c5;
148 r_switch = r - nbparam.rvdw_switch;
150 /* Unlike in the F+E kernel, conditional is faster here */
153 sw = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
154 dsw = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
156 *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
160 /*! Apply potential switch, force + energy version. */
161 static inline __device__
162 void calculate_potential_switch_F_E(const cu_nbparam_t nbparam,
173 /* potential switch constants */
174 float switch_V3 = nbparam.vdw_switch.c3;
175 float switch_V4 = nbparam.vdw_switch.c4;
176 float switch_V5 = nbparam.vdw_switch.c5;
177 float switch_F2 = 3*nbparam.vdw_switch.c3;
178 float switch_F3 = 4*nbparam.vdw_switch.c4;
179 float switch_F4 = 5*nbparam.vdw_switch.c5;
182 r_switch = r - nbparam.rvdw_switch;
183 r_switch = r_switch >= 0.0f ? r_switch : 0.0f;
185 /* Unlike in the F-only kernel, masking is faster here */
186 sw = 1.0f + (switch_V3 + (switch_V4 + switch_V5*r_switch)*r_switch)*r_switch*r_switch*r_switch;
187 dsw = (switch_F2 + (switch_F3 + switch_F4*r_switch)*r_switch)*r_switch*r_switch;
189 *F_invr = (*F_invr)*sw - inv_r*(*E_lj)*dsw;
193 /*! Calculate LJ-PME grid force contribution with
194 * geometric combination rule.
196 static inline __device__
197 void calculate_lj_ewald_comb_geom_F(const cu_nbparam_t nbparam,
206 float c6grid, inv_r6_nm, cr2, expmcr2, poly;
209 c6grid = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei) * tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej);
211 c6grid = tex1Dfetch(nbfp_comb_texref, 2*typei) * tex1Dfetch(nbfp_comb_texref, 2*typej);
212 #endif /* USE_TEXOBJ */
214 /* Recalculate inv_r6 without exclusion mask */
215 inv_r6_nm = inv_r2*inv_r2*inv_r2;
217 expmcr2 = expf(-cr2);
218 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
220 /* Subtract the grid force from the total LJ force */
221 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
224 /*! Calculate LJ-PME grid force + energy contribution with
225 * geometric combination rule.
227 static inline __device__
228 void calculate_lj_ewald_comb_geom_F_E(const cu_nbparam_t nbparam,
239 float c6grid, inv_r6_nm, cr2, expmcr2, poly, sh_mask;
242 c6grid = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei) * tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej);
244 c6grid = tex1Dfetch(nbfp_comb_texref, 2*typei) * tex1Dfetch(nbfp_comb_texref, 2*typej);
245 #endif /* USE_TEXOBJ */
247 /* Recalculate inv_r6 without exclusion mask */
248 inv_r6_nm = inv_r2*inv_r2*inv_r2;
250 expmcr2 = expf(-cr2);
251 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
253 /* Subtract the grid force from the total LJ force */
254 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
256 /* Shift should be applied only to real LJ pairs */
257 sh_mask = nbparam.sh_lj_ewald*int_bit;
258 *E_lj += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
261 /*! Calculate LJ-PME grid force + energy contribution (if E_lj != NULL) with
262 * Lorentz-Berthelot combination rule.
263 * We use a single F+E kernel with conditional because the performance impact
264 * of this is pretty small and LB on the CPU is anyway very slow.
266 static inline __device__
267 void calculate_lj_ewald_comb_LB_F_E(const cu_nbparam_t nbparam,
278 float c6grid, inv_r6_nm, cr2, expmcr2, poly;
279 float sigma, sigma2, epsilon;
281 /* sigma and epsilon are scaled to give 6*C6 */
283 sigma = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei ) + tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej );
284 epsilon = tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typei + 1) * tex1Dfetch<float>(nbparam.nbfp_comb_texobj, 2*typej + 1);
286 sigma = tex1Dfetch(nbfp_comb_texref, 2*typei ) + tex1Dfetch(nbfp_comb_texref, 2*typej );
287 epsilon = tex1Dfetch(nbfp_comb_texref, 2*typei + 1) * tex1Dfetch(nbfp_comb_texref, 2*typej + 1);
288 #endif /* USE_TEXOBJ */
289 sigma2 = sigma*sigma;
290 c6grid = epsilon*sigma2*sigma2*sigma2;
292 /* Recalculate inv_r6 without exclusion mask */
293 inv_r6_nm = inv_r2*inv_r2*inv_r2;
295 expmcr2 = expf(-cr2);
296 poly = 1.0f + cr2 + 0.5f*cr2*cr2;
298 /* Subtract the grid force from the total LJ force */
299 *F_invr += c6grid*(inv_r6_nm - expmcr2*(inv_r6_nm*poly + lje_coeff6_6))*inv_r2;
305 /* Shift should be applied only to real LJ pairs */
306 sh_mask = nbparam.sh_lj_ewald*int_bit;
307 *E_lj += ONE_SIXTH_F*c6grid*(inv_r6_nm*(1.0f - expmcr2*poly) + sh_mask);
311 /*! Interpolate Ewald coulomb force using the table through the tex_nbfp texture.
312 * Original idea: from the OpenMM project
314 static inline __device__
315 float interpolate_coulomb_force_r(float r, float scale)
317 float normalized = scale * r;
318 int index = (int) normalized;
319 float fract2 = normalized - index;
320 float fract1 = 1.0f - fract2;
322 return fract1 * tex1Dfetch(coulomb_tab_texref, index)
323 + fract2 * tex1Dfetch(coulomb_tab_texref, index + 1);
326 #ifdef HAVE_CUDA_TEXOBJ_SUPPORT
327 static inline __device__
328 float interpolate_coulomb_force_r(cudaTextureObject_t texobj_coulomb_tab,
329 float r, float scale)
331 float normalized = scale * r;
332 int index = (int) normalized;
333 float fract2 = normalized - index;
334 float fract1 = 1.0f - fract2;
336 return fract1 * tex1Dfetch<float>(texobj_coulomb_tab, index) +
337 fract2 * tex1Dfetch<float>(texobj_coulomb_tab, index + 1);
342 /*! Calculate analytical Ewald correction term. */
343 static inline __device__
344 float pmecorrF(float z2)
346 const float FN6 = -1.7357322914161492954e-8f;
347 const float FN5 = 1.4703624142580877519e-6f;
348 const float FN4 = -0.000053401640219807709149f;
349 const float FN3 = 0.0010054721316683106153f;
350 const float FN2 = -0.019278317264888380590f;
351 const float FN1 = 0.069670166153766424023f;
352 const float FN0 = -0.75225204789749321333f;
354 const float FD4 = 0.0011193462567257629232f;
355 const float FD3 = 0.014866955030185295499f;
356 const float FD2 = 0.11583842382862377919f;
357 const float FD1 = 0.50736591960530292870f;
358 const float FD0 = 1.0f;
361 float polyFN0, polyFN1, polyFD0, polyFD1;
365 polyFD0 = FD4*z4 + FD2;
366 polyFD1 = FD3*z4 + FD1;
367 polyFD0 = polyFD0*z4 + FD0;
368 polyFD0 = polyFD1*z2 + polyFD0;
370 polyFD0 = 1.0f/polyFD0;
372 polyFN0 = FN6*z4 + FN4;
373 polyFN1 = FN5*z4 + FN3;
374 polyFN0 = polyFN0*z4 + FN2;
375 polyFN1 = polyFN1*z4 + FN1;
376 polyFN0 = polyFN0*z4 + FN0;
377 polyFN0 = polyFN1*z2 + polyFN0;
379 return polyFN0*polyFD0;
382 /*! Final j-force reduction; this generic implementation works with
383 * arbitrary array sizes.
385 static inline __device__
386 void reduce_force_j_generic(float *f_buf, float3 *fout,
387 int tidxi, int tidxj, int aidx)
392 for (int j = tidxj * CL_SIZE; j < (tidxj + 1) * CL_SIZE; j++)
394 f += f_buf[FBUF_STRIDE * tidxi + j];
397 atomicAdd((&fout[aidx].x)+tidxi, f);
401 /*! Final j-force reduction; this implementation only with power of two
402 * array sizes and with sm >= 3.0
404 #if __CUDA_ARCH__ >= 300
405 static inline __device__
406 void reduce_force_j_warp_shfl(float3 f, float3 *fout,
409 f.x += __shfl_down(f.x, 1);
410 f.y += __shfl_up (f.y, 1);
411 f.z += __shfl_down(f.z, 1);
418 f.x += __shfl_down(f.x, 2);
419 f.z += __shfl_up (f.z, 2);
426 f.x += __shfl_down(f.x, 4);
430 atomicAdd((&fout[aidx].x) + tidxi, f.x);
435 /*! Final i-force reduction; this generic implementation works with
436 * arbitrary array sizes.
437 * TODO: add the tidxi < 3 trick
439 static inline __device__
440 void reduce_force_i_generic(float *f_buf, float3 *fout,
441 float *fshift_buf, bool bCalcFshift,
442 int tidxi, int tidxj, int aidx)
447 for (int j = tidxi; j < CL_SIZE_SQ; j += CL_SIZE)
449 f += f_buf[tidxj * FBUF_STRIDE + j];
452 atomicAdd(&fout[aidx].x + tidxj, f);
461 /*! Final i-force reduction; this implementation works only with power of two
464 static inline __device__
465 void reduce_force_i_pow2(volatile float *f_buf, float3 *fout,
466 float *fshift_buf, bool bCalcFshift,
467 int tidxi, int tidxj, int aidx)
472 /* Reduce the initial CL_SIZE values for each i atom to half
473 * every step by using CL_SIZE * i threads.
474 * Can't just use i as loop variable because than nvcc refuses to unroll.
478 for (j = CL_SIZE_POW2_EXPONENT - 1; j > 0; j--)
483 f_buf[ tidxj * CL_SIZE + tidxi] += f_buf[ (tidxj + i) * CL_SIZE + tidxi];
484 f_buf[ FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[ FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
485 f_buf[2 * FBUF_STRIDE + tidxj * CL_SIZE + tidxi] += f_buf[2 * FBUF_STRIDE + (tidxj + i) * CL_SIZE + tidxi];
490 /* i == 1, last reduction step, writing to global mem */
493 /* tidxj*FBUF_STRIDE selects x, y or z */
494 f = f_buf[tidxj * FBUF_STRIDE + tidxi] +
495 f_buf[tidxj * FBUF_STRIDE + i * CL_SIZE + tidxi];
497 atomicAdd(&(fout[aidx].x) + tidxj, f);
507 /*! Final i-force reduction wrapper; calls the generic or pow2 reduction depending
508 * on whether the size of the array to be reduced is power of two or not.
510 static inline __device__
511 void reduce_force_i(float *f_buf, float3 *f,
512 float *fshift_buf, bool bCalcFshift,
513 int tidxi, int tidxj, int ai)
515 if ((CL_SIZE & (CL_SIZE - 1)))
517 reduce_force_i_generic(f_buf, f, fshift_buf, bCalcFshift, tidxi, tidxj, ai);
521 reduce_force_i_pow2(f_buf, f, fshift_buf, bCalcFshift, tidxi, tidxj, ai);
525 /*! Final i-force reduction; this implementation works only with power of two
526 * array sizes and with sm >= 3.0
528 #if __CUDA_ARCH__ >= 300
529 static inline __device__
530 void reduce_force_i_warp_shfl(float3 fin, float3 *fout,
531 float *fshift_buf, bool bCalcFshift,
534 fin.x += __shfl_down(fin.x, CL_SIZE);
535 fin.y += __shfl_up (fin.y, CL_SIZE);
536 fin.z += __shfl_down(fin.z, CL_SIZE);
543 fin.x += __shfl_down(fin.x, 2*CL_SIZE);
544 fin.z += __shfl_up (fin.z, 2*CL_SIZE);
551 /* Threads 0,1,2 and 4,5,6 increment x,y,z for their warp */
554 atomicAdd(&fout[aidx].x + (tidxj & ~4), fin.x);
558 *fshift_buf += fin.x;
564 /*! Energy reduction; this implementation works only with power of two
567 static inline __device__
568 void reduce_energy_pow2(volatile float *buf,
569 float *e_lj, float *e_el,
577 /* Can't just use i as loop variable because than nvcc refuses to unroll. */
579 for (j = WARP_SIZE_POW2_EXPONENT - 1; j > 0; j--)
583 buf[ tidx] += buf[ tidx + i];
584 buf[FBUF_STRIDE + tidx] += buf[FBUF_STRIDE + tidx + i];
589 // TODO do this on two threads - requires e_lj and e_el to be stored on adjascent
590 // memory locations to make sense
591 /* last reduction step, writing to global mem */
594 e1 = buf[ tidx] + buf[ tidx + i];
595 e2 = buf[FBUF_STRIDE + tidx] + buf[FBUF_STRIDE + tidx + i];
602 /*! Energy reduction; this implementation works only with power of two
603 * array sizes and with sm >= 3.0
605 #if __CUDA_ARCH__ >= 300
606 static inline __device__
607 void reduce_energy_warp_shfl(float E_lj, float E_el,
608 float *e_lj, float *e_el,
615 for (i = 0; i < 5; i++)
617 E_lj += __shfl_down(E_lj, sh);
618 E_el += __shfl_down(E_el, sh);
622 /* The first thread in the warp writes the reduced energies */
623 if (tidx == 0 || tidx == WARP_SIZE)
625 atomicAdd(e_lj, E_lj);
626 atomicAdd(e_el, E_el);
629 #endif /* __CUDA_ARCH__ */
633 #endif /* NBNXN_CUDA_KERNEL_UTILS_CUH */