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.
41 #include "gromacs/legacyheaders/types/simple.h"
42 #include "gromacs/legacyheaders/typedefs.h"
43 #include "gromacs/legacyheaders/force.h"
45 #include "gromacs/math/utilities.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/pbcutil/ishift.h"
49 #include "nbnxn_kernel_gpu_ref.h"
50 #include "../nbnxn_consts.h"
51 #include "nbnxn_kernel_common.h"
52 #include "gromacs/mdlib/nb_verlet.h"
54 #define NCL_PER_SUPERCL (NBNXN_GPU_NCLUSTER_PER_SUPERCLUSTER)
55 #define CL_SIZE (NBNXN_GPU_CLUSTER_SIZE)
58 nbnxn_kernel_gpu_ref(const nbnxn_pairlist_t *nbl,
59 const nbnxn_atomdata_t *nbat,
60 const interaction_const_t *iconst,
69 const nbnxn_sci_t *nbln;
73 const real *Ftab = NULL;
74 real rcut2, rvdw2, rlist2;
80 int cj4_ind0, cj4_ind1, cj4_ind;
82 int ic, jc, ia, ja, is, ifs, js, jfs, im, jm;
86 real fscal, tx, ty, tz;
89 real qq, vcoul = 0, krsq, vctot;
95 real Vvdw_rep, Vvdw_disp;
96 real ix, iy, iz, fix, fiy, fiz;
98 real dx, dy, dz, rsq, rinv;
101 real c6, c12, cexp1, cexp2, br;
102 const real * shiftvec;
106 const nbnxn_excl_t *excl[2];
108 int npair_tot, npair;
109 int nhwu, nhwu_pruned;
111 if (nbl->na_ci != CL_SIZE)
113 gmx_fatal(FARGS, "The neighborlist cluster size in the GPU reference kernel is %d, expected it to be %d", nbl->na_ci, CL_SIZE);
116 if (clearF == enbvClearFYes)
121 bEner = (force_flags & GMX_FORCE_ENERGY);
123 bEwald = EEL_FULL(iconst->eeltype);
126 Ftab = iconst->tabq_coul_F;
129 rcut2 = iconst->rcoulomb*iconst->rcoulomb;
130 rvdw2 = iconst->rvdw*iconst->rvdw;
132 rlist2 = nbl->rlist*nbl->rlist;
135 facel = iconst->epsfac;
136 shiftvec = shift_vec[0];
137 vdwparam = nbat->nbfp;
146 for (n = 0; n < nbl->nsci; n++)
150 ish3 = 3*nbln->shift;
151 shX = shiftvec[ish3];
152 shY = shiftvec[ish3+1];
153 shZ = shiftvec[ish3+2];
154 cj4_ind0 = nbln->cj4_ind_start;
155 cj4_ind1 = nbln->cj4_ind_end;
160 if (nbln->shift == CENTRAL &&
161 nbl->cj4[cj4_ind0].cj[0] == sci*NCL_PER_SUPERCL)
163 /* we have the diagonal:
164 * add the charge self interaction energy term
166 for (im = 0; im < NCL_PER_SUPERCL; im++)
168 ci = sci*NCL_PER_SUPERCL + im;
169 for (ic = 0; ic < CL_SIZE; ic++)
171 ia = ci*CL_SIZE + ic;
172 iq = x[ia*nbat->xstride+3];
178 vctot *= -facel*0.5*iconst->c_rf;
182 /* last factor 1/sqrt(pi) */
183 vctot *= -facel*iconst->ewaldcoeff_q*M_1_SQRTPI;
187 for (cj4_ind = cj4_ind0; (cj4_ind < cj4_ind1); cj4_ind++)
189 excl[0] = &nbl->excl[nbl->cj4[cj4_ind].imei[0].excl_ind];
190 excl[1] = &nbl->excl[nbl->cj4[cj4_ind].imei[1].excl_ind];
192 for (jm = 0; jm < NBNXN_GPU_JGROUP_SIZE; jm++)
194 cj = nbl->cj4[cj4_ind].cj[jm];
196 for (im = 0; im < NCL_PER_SUPERCL; im++)
198 /* We're only using the first imask,
199 * but here imei[1].imask is identical.
201 if ((nbl->cj4[cj4_ind].imei[0].imask >> (jm*NCL_PER_SUPERCL+im)) & 1)
203 gmx_bool within_rlist;
205 ci = sci*NCL_PER_SUPERCL + im;
207 within_rlist = FALSE;
209 for (ic = 0; ic < CL_SIZE; ic++)
211 ia = ci*CL_SIZE + ic;
213 is = ia*nbat->xstride;
214 ifs = ia*nbat->fstride;
219 nti = ntype*2*type[ia];
225 for (jc = 0; jc < CL_SIZE; jc++)
227 ja = cj*CL_SIZE + jc;
229 if (nbln->shift == CENTRAL &&
230 ci == cj && ja <= ia)
235 int_bit = ((excl[jc>>2]->pair[(jc & 3)*CL_SIZE+ic] >> (jm*NCL_PER_SUPERCL+im)) & 1);
237 js = ja*nbat->xstride;
238 jfs = ja*nbat->fstride;
245 rsq = dx*dx + dy*dy + dz*dz;
255 if (type[ia] != ntype-1 && type[ja] != ntype-1)
260 /* avoid NaN for excluded pairs at r=0 */
261 rsq += (1.0 - int_bit)*NBNXN_AVOID_SING_R2_INC;
263 rinv = gmx_invsqrt(rsq);
271 krsq = iconst->k_rf*rsq;
272 fscal = qq*(int_bit*rinv - 2*krsq)*rinvsq;
275 vcoul = qq*(int_bit*rinv + krsq - iconst->c_rf);
281 rt = r*iconst->tabq_scale;
285 fexcl = (1 - eps)*Ftab[n0] + eps*Ftab[n0+1];
287 fscal = qq*(int_bit*rinvsq - fexcl)*rinv;
291 vcoul = qq*((int_bit - gmx_erf(iconst->ewaldcoeff_q*r))*rinv - int_bit*iconst->sh_ewald);
297 tj = nti + 2*type[ja];
299 /* Vanilla Lennard-Jones cutoff */
301 c12 = vdwparam[tj+1];
303 rinvsix = int_bit*rinvsq*rinvsq*rinvsq;
304 Vvdw_disp = c6*rinvsix;
305 Vvdw_rep = c12*rinvsix*rinvsix;
306 fscal += (Vvdw_rep - Vvdw_disp)*rinvsq;
313 (Vvdw_rep - int_bit*c12*iconst->sh_invrc6*iconst->sh_invrc6)/12 -
314 (Vvdw_disp - int_bit*c6*iconst->sh_invrc6)/6;
332 fshift[ish3] = fshift[ish3] + fix;
333 fshift[ish3+1] = fshift[ish3+1] + fiy;
334 fshift[ish3+2] = fshift[ish3+2] + fiz;
336 /* Count in half work-units.
337 * In CUDA one work-unit is 2 warps.
339 if ((ic+1) % (CL_SIZE/2) == 0)
349 within_rlist = FALSE;
361 Vc[ggid] = Vc[ggid] + vctot;
362 Vvdw[ggid] = Vvdw[ggid] + Vvdwtot;
368 fprintf(debug, "number of half %dx%d atom pairs: %d after pruning: %d fraction %4.2f\n",
369 nbl->na_ci, nbl->na_ci,
370 nhwu, nhwu_pruned, nhwu_pruned/(double)nhwu);
371 fprintf(debug, "generic kernel pair interactions: %d\n",
372 nhwu*nbl->na_ci/2*nbl->na_ci);
373 fprintf(debug, "generic kernel post-prune pair interactions: %d\n",
374 nhwu_pruned*nbl->na_ci/2*nbl->na_ci);
375 fprintf(debug, "generic kernel non-zero pair interactions: %d\n",
377 fprintf(debug, "ratio non-zero/post-prune pair interactions: %4.2f\n",
378 npair_tot/(double)(nhwu_pruned*nbl->na_ci/2*nbl->na_ci));