2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2012, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
40 /* Note that floating-point constants in CUDA code should be suffixed
41 * with f (e.g. 0.5f), to stop the compiler producing intermediate
42 * code that is in double precision.
45 #if __CUDA_ARCH__ >= 300
46 #define REDUCE_SHUFFLE
47 /* On Kepler pre-loading i-atom types to shmem gives a few %,
48 but on Fermi it does not */
53 Kernel launch parameters:
54 - #blocks = #pair lists, blockId = pair list Id
55 - #threads = CL_SIZE^2
56 - shmem = CL_SIZE^2 * sizeof(float)
58 Each thread calculates an i force-component taking one pair of i-j atoms.
62 __global__ void NB_KERNEL_FUNC_NAME(k_nbnxn, _ener_prune)
64 __global__ void NB_KERNEL_FUNC_NAME(k_nbnxn, _prune)
68 __global__ void NB_KERNEL_FUNC_NAME(k_nbnxn, _ener)
70 __global__ void NB_KERNEL_FUNC_NAME(k_nbnxn)
73 (const cu_atomdata_t atdat,
74 const cu_nbparam_t nbparam,
75 const cu_plist_t plist,
78 /* convenience variables */
79 const nbnxn_sci_t *pl_sci = plist.sci;
83 nbnxn_cj4_t *pl_cj4 = plist.cj4;
84 const nbnxn_excl_t *excl = plist.excl;
85 const int *atom_types = atdat.atom_types;
86 int ntypes = atdat.ntypes;
87 const float4 *xq = atdat.xq;
89 const float3 *shift_vec = atdat.shift_vec;
90 float rcoulomb_sq = nbparam.rcoulomb_sq;
91 #ifdef VDW_CUTOFF_CHECK
92 float rvdw_sq = nbparam.rvdw_sq;
96 float two_k_rf = nbparam.two_k_rf;
99 float coulomb_tab_scale = nbparam.coulomb_tab_scale;
102 float rlist_sq = nbparam.rlist_sq;
106 float lj_shift = nbparam.sh_invrc6;
108 float beta = nbparam.ewald_beta;
109 float ewald_shift = nbparam.sh_ewald;
111 float c_rf = nbparam.c_rf;
113 float *e_lj = atdat.e_lj;
114 float *e_el = atdat.e_el;
117 /* thread/block/warp id-s */
118 unsigned int tidxi = threadIdx.x;
119 unsigned int tidxj = threadIdx.y;
120 unsigned int tidx = threadIdx.y * blockDim.x + threadIdx.x;
121 unsigned int bidx = blockIdx.x;
122 unsigned int widx = tidx / WARP_SIZE; /* warp index */
124 int sci, ci, cj, ci_offset,
126 cij4_start, cij4_end,
128 i, jm, j4, wexcl_idx;
130 r2, inv_r, inv_r2, inv_r6,
137 unsigned int wexcl, imask, mask_ji;
139 float3 xi, xj, rv, f_ij, fcj_buf, fshift_buf;
140 float3 fci_buf[NCL_PER_SUPERCL]; /* i force buffer */
143 /* shmem buffer for i x+q pre-loading */
144 extern __shared__ float4 xqib[];
145 /* shmem buffer for cj, for both warps separately */
146 int *cjs = (int *)(xqib + NCL_PER_SUPERCL * CL_SIZE);
148 /* shmem buffer for i atom-type pre-loading */
149 int *atib = (int *)(cjs + 2 * NBNXN_GPU_JGROUP_SIZE);
152 #ifndef REDUCE_SHUFFLE
153 /* shmem j force buffer */
155 float *f_buf = (float *)(atib + NCL_PER_SUPERCL * CL_SIZE);
157 float *f_buf = (float *)(cjs + 2 * NBNXN_GPU_JGROUP_SIZE);
161 nb_sci = pl_sci[bidx]; /* my i super-cluster's index = current bidx */
162 sci = nb_sci.sci; /* super-cluster */
163 cij4_start = nb_sci.cj4_ind_start; /* first ...*/
164 cij4_end = nb_sci.cj4_ind_end; /* and last index of j clusters */
166 /* Store the i-atom x and q in shared memory */
167 /* Note: the thread indexing here is inverted with respect to the
168 inner-loop as this results in slightly higher performance */
169 ci = sci * NCL_PER_SUPERCL + tidxi;
170 ai = ci * CL_SIZE + tidxj;
171 xqib[tidxi * CL_SIZE + tidxj] = xq[ai] + shift_vec[nb_sci.shift];
173 ci = sci * NCL_PER_SUPERCL + tidxj;
174 ai = ci * CL_SIZE + tidxi;
175 atib[tidxj * CL_SIZE + tidxi] = atom_types[ai];
179 for(ci_offset = 0; ci_offset < NCL_PER_SUPERCL; ci_offset++)
181 fci_buf[ci_offset] = make_float3(0.0f);
188 #if defined EL_EWALD || defined EL_RF
189 if (nb_sci.shift == CENTRAL && pl_cj4[cij4_start].cj[0] == sci*NCL_PER_SUPERCL)
191 /* we have the diagonal: add the charge self interaction energy term */
192 for (i = 0; i < NCL_PER_SUPERCL; i++)
194 qi = xqib[i * CL_SIZE + tidxi].w;
197 /* divide the self term equally over the j-threads */
200 E_el *= -nbparam.epsfac*0.5f*c_rf;
202 E_el *= -nbparam.epsfac*beta*M_FLOAT_1_SQRTPI; /* last factor 1/sqrt(pi) */
208 /* skip central shifts when summing shift forces */
209 if (nb_sci.shift == CENTRAL)
214 fshift_buf = make_float3(0.0f);
216 /* loop over the j clusters = seen by any of the atoms in the current super-cluster */
217 for (j4 = cij4_start; j4 < cij4_end; j4++)
219 wexcl_idx = pl_cj4[j4].imei[widx].excl_ind;
220 imask = pl_cj4[j4].imei[widx].imask;
221 wexcl = excl[wexcl_idx].pair[(tidx) & (WARP_SIZE - 1)];
227 /* Pre-load cj into shared memory on both warps separately */
228 if ((tidxj == 0 || tidxj == 4) && tidxi < NBNXN_GPU_JGROUP_SIZE)
230 cjs[tidxi + tidxj * NBNXN_GPU_JGROUP_SIZE / 4] = pl_cj4[j4].cj[tidxi];
233 /* Unrolling this loop
234 - with pruning leads to register spilling;
235 - on Kepler is much slower;
236 - doesn't work on CUDA <v4.1
237 Tested with nvcc 3.2 - 5.0.7 */
238 #if !defined PRUNE_NBL && __CUDA_ARCH__ < 300 && CUDA_VERSION >= 4010
241 for (jm = 0; jm < NBNXN_GPU_JGROUP_SIZE; jm++)
243 if (imask & (supercl_interaction_mask << (jm * NCL_PER_SUPERCL)))
245 mask_ji = (1U << (jm * NCL_PER_SUPERCL));
247 cj = cjs[jm + (tidxj & 4) * NBNXN_GPU_JGROUP_SIZE / 4];
248 aj = cj * CL_SIZE + tidxj;
250 /* load j atom data */
252 xj = make_float3(xqbuf.x, xqbuf.y, xqbuf.z);
253 qj_f = nbparam.epsfac * xqbuf.w;
254 typej = atom_types[aj];
256 fcj_buf = make_float3(0.0f);
258 /* The PME and RF kernels don't unroll with CUDA <v4.1. */
259 #if !defined PRUNE_NBL && !(CUDA_VERSION < 4010 && (defined EL_EWALD || defined EL_RF))
262 for(i = 0; i < NCL_PER_SUPERCL; i++)
266 ci_offset = i; /* i force buffer offset */
268 ci = sci * NCL_PER_SUPERCL + i; /* i cluster index */
269 ai = ci * CL_SIZE + tidxi; /* i atom index */
271 /* all threads load an atom from i cluster ci into shmem! */
272 xqbuf = xqib[i * CL_SIZE + tidxi];
273 xi = make_float3(xqbuf.x, xqbuf.y, xqbuf.z);
275 /* distance between i and j atoms */
280 /* If _none_ of the atoms pairs are in cutoff range,
281 the bit corresponding to the current
282 cluster-pair in imask gets set to 0. */
283 if (!__any(r2 < rlist_sq))
289 int_bit = (wexcl & mask_ji) ? 1.0f : 0.0f;
291 /* cutoff & exclusion check */
292 #if defined EL_EWALD || defined EL_RF
293 if (r2 < rcoulomb_sq *
294 (nb_sci.shift != CENTRAL || ci != cj || tidxj > tidxi))
296 if (r2 < rcoulomb_sq * int_bit)
299 /* load the rest of the i-atom parameters */
302 typei = atib[i * CL_SIZE + tidxi];
304 typei = atom_types[ai];
307 /* LJ 6*C6 and 12*C12 */
308 c6 = tex1Dfetch(tex_nbfp, 2 * (ntypes * typei + typej));
309 c12 = tex1Dfetch(tex_nbfp, 2 * (ntypes * typei + typej) + 1);
311 /* avoid NaN for excluded pairs at r=0 */
312 r2 += (1.0f - int_bit) * NBNXN_AVOID_SING_R2_INC;
315 inv_r2 = inv_r * inv_r;
316 inv_r6 = inv_r2 * inv_r2 * inv_r2;
317 #if defined EL_EWALD || defined EL_RF
318 /* We could mask inv_r2, but with Ewald
319 * masking both inv_r6 and F_invr is faster */
323 F_invr = inv_r6 * (c12 * inv_r6 - c6) * inv_r2;
326 E_lj_p = int_bit * (c12 * (inv_r6 * inv_r6 - lj_shift * lj_shift) * 0.08333333f - c6 * (inv_r6 - lj_shift) * 0.16666667f);
329 #ifdef VDW_CUTOFF_CHECK
330 /* this enables twin-range cut-offs (rvdw < rcoulomb <= rlist) */
331 vdw_in_range = (r2 < rvdw_sq) ? 1.0f : 0.0f;
332 F_invr *= vdw_in_range;
334 E_lj_p *= vdw_in_range;
343 F_invr += qi * qj_f * inv_r2 * inv_r;
346 F_invr += qi * qj_f * (int_bit*inv_r2 * inv_r - two_k_rf);
349 F_invr += qi * qj_f * (int_bit*inv_r2 - interpolate_coulomb_force_r(r2 * inv_r, coulomb_tab_scale)) * inv_r;
354 E_el += qi * qj_f * (inv_r - c_rf);
357 E_el += qi * qj_f * (int_bit*inv_r + 0.5f * two_k_rf * r2 - c_rf);
360 /* 1.0f - erff is faster than erfcf */
361 E_el += qi * qj_f * (inv_r * (int_bit - erff(r2 * inv_r * beta)) - int_bit * ewald_shift);
366 /* accumulate j forces in registers */
369 /* accumulate i forces in registers */
370 fci_buf[ci_offset] += f_ij;
374 /* shift the mask bit by 1 */
378 /* reduce j forces */
379 #ifdef REDUCE_SHUFFLE
380 reduce_force_j_warp_shfl(fcj_buf, f, tidxi, aj);
382 /* store j forces in shmem */
383 f_buf[ tidx] = fcj_buf.x;
384 f_buf[ FBUF_STRIDE + tidx] = fcj_buf.y;
385 f_buf[2 * FBUF_STRIDE + tidx] = fcj_buf.z;
387 reduce_force_j_generic(f_buf, f, tidxi, tidxj, aj);
392 /* Update the imask with the new one which does not contain the
393 out of range clusters anymore. */
394 pl_cj4[j4].imei[widx].imask = imask;
399 /* reduce i forces */
400 for(ci_offset = 0; ci_offset < NCL_PER_SUPERCL; ci_offset++)
402 ai = (sci * NCL_PER_SUPERCL + ci_offset) * CL_SIZE + tidxi;
403 #ifdef REDUCE_SHUFFLE
404 reduce_force_i_warp_shfl(fci_buf[ci_offset], f,
405 &fshift_buf, bCalcFshift,
408 f_buf[ tidx] = fci_buf[ci_offset].x;
409 f_buf[ FBUF_STRIDE + tidx] = fci_buf[ci_offset].y;
410 f_buf[2 * FBUF_STRIDE + tidx] = fci_buf[ci_offset].z;
412 reduce_force_i(f_buf, f,
413 &fshift_buf, bCalcFshift,
419 /* add up local shift forces into global mem */
420 #ifdef REDUCE_SHUFFLE
421 if (bCalcFshift && (tidxj == 0 || tidxj == 4))
423 if (bCalcFshift && tidxj == 0)
426 atomicAdd(&atdat.fshift[nb_sci.shift].x, fshift_buf.x);
427 atomicAdd(&atdat.fshift[nb_sci.shift].y, fshift_buf.y);
428 atomicAdd(&atdat.fshift[nb_sci.shift].z, fshift_buf.z);
432 #ifdef REDUCE_SHUFFLE
433 /* reduce the energies over warps and store into global memory */
434 reduce_energy_warp_shfl(E_lj, E_el, e_lj, e_el, tidx);
436 /* flush the energies to shmem and reduce them */
438 f_buf[FBUF_STRIDE + tidx] = E_el;
439 reduce_energy_pow2(f_buf + (tidx & WARP_SIZE), e_lj, e_el, tidx & ~WARP_SIZE);