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-2009, The GROMACS Development Team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
43 #include "gromacs/legacyheaders/types/simple.h"
45 #include "gromacs/math/vec.h"
46 #include "gromacs/utility/smalloc.h"
48 #include "nb_kernel_allvsall.h"
49 #include "gromacs/legacyheaders/nrnb.h"
55 int ** exclusion_mask;
60 calc_maxoffset(int i, int natoms)
64 if ((natoms % 2) == 1)
66 /* Odd number of atoms, easy */
69 else if ((natoms % 4) == 0)
71 /* Multiple of four is hard */
80 maxoffset = natoms/2-1;
91 maxoffset = natoms/2-1;
100 maxoffset = natoms/2;
104 maxoffset = natoms/2-1;
113 setup_exclusions_and_indices(gmx_allvsall_data_t * aadata,
123 /* This routine can appear to be a bit complex, but it is mostly book-keeping.
124 * To enable the fast all-vs-all kernel we need to be able to stream through all coordinates
125 * whether they should interact or not.
127 * To avoid looping over the exclusions, we create a simple mask that is 1 if the interaction
128 * should be present, otherwise 0. Since exclusions typically only occur when i & j are close,
129 * we create a jindex array with three elements per i atom: the starting point, the point to
130 * which we need to check exclusions, and the end point.
131 * This way we only have to allocate a short exclusion mask per i atom.
134 /* Allocate memory for our modified jindex array */
135 snew(aadata->jindex, 3*natoms);
137 /* Pointer to lists with exclusion masks */
138 snew(aadata->exclusion_mask, natoms);
140 for (i = 0; i < natoms; i++)
143 aadata->jindex[3*i] = i+1;
144 max_offset = calc_maxoffset(i, natoms);
147 nj0 = excl->index[i];
148 nj1 = excl->index[i+1];
150 /* first check the max range */
151 max_excl_offset = -1;
153 for (j = nj0; j < nj1; j++)
159 if (k+natoms <= max_offset)
164 max_excl_offset = (k > max_excl_offset) ? k : max_excl_offset;
167 max_excl_offset = (max_offset < max_excl_offset) ? max_offset : max_excl_offset;
169 aadata->jindex[3*i+1] = i+1+max_excl_offset;
172 snew(aadata->exclusion_mask[i], max_excl_offset);
173 /* Include everything by default */
174 for (j = 0; j < max_excl_offset; j++)
176 /* Use all-ones to mark interactions that should be present, compatible with SSE */
177 aadata->exclusion_mask[i][j] = 0xFFFFFFFF;
180 /* Go through exclusions again */
181 for (j = nj0; j < nj1; j++)
187 if (k+natoms <= max_offset)
192 if (k > 0 && k <= max_excl_offset)
194 /* Excluded, kill it! */
195 aadata->exclusion_mask[i][k-1] = 0;
200 aadata->jindex[3*i+2] = i+1+max_offset;
205 setup_aadata(gmx_allvsall_data_t ** p_aadata,
213 gmx_allvsall_data_t *aadata;
219 /* Generate vdw params */
220 snew(aadata->pvdwparam, ntype);
222 for (i = 0; i < ntype; i++)
224 snew(aadata->pvdwparam[i], 2*natoms);
225 p = aadata->pvdwparam[i];
227 /* Lets keep it simple and use multiple steps - first create temp. c6/c12 arrays */
228 for (j = 0; j < natoms; j++)
230 idx = i*ntype+type[j];
231 p[2*j] = pvdwparam[2*idx];
232 p[2*j+1] = pvdwparam[2*idx+1];
236 setup_exclusions_and_indices(aadata, excl, natoms);
242 nb_kernel_allvsall(t_nblist gmx_unused * nlist,
247 nb_kernel_data_t * kernel_data,
250 gmx_allvsall_data_t *aadata;
267 real rsq, rinv, rinvsq, rinvsix;
269 real c6, c12, Vvdw6, Vvdw12, Vvdwtot;
279 charge = mdatoms->chargeA;
280 type = mdatoms->typeA;
282 natoms = mdatoms->nr;
284 ni1 = mdatoms->homenr;
285 aadata = fr->AllvsAll_work;
286 excl = kernel_data->exclusions;
288 Vc = kernel_data->energygrp_elec;
289 Vvdw = kernel_data->energygrp_vdw;
293 setup_aadata(&aadata, excl, natoms, type, fr->ntype, fr->nbfp);
294 fr->AllvsAll_work = aadata;
297 for (i = ni0; i < ni1; i++)
299 /* We assume shifts are NOT used for all-vs-all interactions */
301 /* Load i atom data */
305 iq = facel*charge[i];
307 pvdw = aadata->pvdwparam[type[i]];
309 /* Zero the potential energy for this list */
313 /* Clear i atom forces */
318 /* Load limits for loop over neighbors */
319 nj0 = aadata->jindex[3*i];
320 nj1 = aadata->jindex[3*i+1];
321 nj2 = aadata->jindex[3*i+2];
323 mask = aadata->exclusion_mask[i];
325 /* Prologue part, including exclusion mask */
326 for (j = nj0; j < nj1; j++, mask++)
332 /* load j atom coordinates */
337 /* Calculate distance */
341 rsq = dx*dx+dy*dy+dz*dz;
343 /* Calculate 1/r and 1/r2 */
344 rinv = gmx_invsqrt(rsq);
347 /* Load parameters for j atom */
352 /* Coulomb interaction */
356 /* Lennard-Jones interaction */
357 rinvsix = rinvsq*rinvsq*rinvsq;
359 Vvdw12 = c12*rinvsix*rinvsix;
360 Vvdwtot = Vvdwtot+Vvdw12-Vvdw6;
361 fscal = (vcoul+12.0*Vvdw12-6.0*Vvdw6)*rinvsq;
363 /* Calculate temporary vectorial force */
368 /* Increment i atom force */
373 /* Decrement j atom force */
374 f[3*k] = f[3*k] - tx;
375 f[3*k+1] = f[3*k+1] - ty;
376 f[3*k+2] = f[3*k+2] - tz;
378 /* Inner loop uses 38 flops/iteration */
381 /* Main part, no exclusions */
382 for (j = nj1; j < nj2; j++)
386 /* load j atom coordinates */
391 /* Calculate distance */
395 rsq = dx*dx+dy*dy+dz*dz;
397 /* Calculate 1/r and 1/r2 */
398 rinv = gmx_invsqrt(rsq);
401 /* Load parameters for j atom */
406 /* Coulomb interaction */
410 /* Lennard-Jones interaction */
411 rinvsix = rinvsq*rinvsq*rinvsq;
413 Vvdw12 = c12*rinvsix*rinvsix;
414 Vvdwtot = Vvdwtot+Vvdw12-Vvdw6;
415 fscal = (vcoul+12.0*Vvdw12-6.0*Vvdw6)*rinvsq;
417 /* Calculate temporary vectorial force */
422 /* Increment i atom force */
427 /* Decrement j atom force */
428 f[3*k] = f[3*k] - tx;
429 f[3*k+1] = f[3*k+1] - ty;
430 f[3*k+2] = f[3*k+2] - tz;
432 /* Inner loop uses 38 flops/iteration */
439 /* Add potential energies to the group for this list */
442 Vc[ggid] = Vc[ggid] + vctot;
443 Vvdw[ggid] = Vvdw[ggid] + Vvdwtot;
445 /* Outer loop uses 6 flops/iteration */
448 /* 12 flops per outer iteration
449 * 19 flops per inner iteration
451 inc_nrnb(nrnb, eNR_NBKERNEL_ELEC_VDW_VF, (ni1-ni0)*12 + ((ni1-ni0)*natoms/2)*19);