2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016,2017, 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.
38 #include "nbnxn_atomdata.h"
48 #include "thread_mpi/atomic.h"
50 #include "gromacs/math/functions.h"
51 #include "gromacs/math/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/mdlib/gmx_omp_nthreads.h"
54 #include "gromacs/mdlib/nb_verlet.h"
55 #include "gromacs/mdlib/nbnxn_consts.h"
56 #include "gromacs/mdlib/nbnxn_internal.h"
57 #include "gromacs/mdlib/nbnxn_search.h"
58 #include "gromacs/mdlib/nbnxn_util.h"
59 #include "gromacs/pbcutil/ishift.h"
60 #include "gromacs/simd/simd.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/fatalerror.h"
63 #include "gromacs/utility/gmxomp.h"
64 #include "gromacs/utility/smalloc.h"
66 using namespace gmx; // TODO: Remove when this file is moved into gmx namespace
68 /* Default nbnxn allocation routine, allocates NBNXN_MEM_ALIGN byte aligned */
69 void nbnxn_alloc_aligned(void **ptr, size_t nbytes)
71 *ptr = save_malloc_aligned("ptr", __FILE__, __LINE__, nbytes, 1, NBNXN_MEM_ALIGN);
74 /* Free function for memory allocated with nbnxn_alloc_aligned */
75 void nbnxn_free_aligned(void *ptr)
80 /* Reallocation wrapper function for nbnxn data structures */
81 void nbnxn_realloc_void(void **ptr,
82 int nbytes_copy, int nbytes_new,
88 ma(&ptr_new, nbytes_new);
90 if (nbytes_new > 0 && ptr_new == nullptr)
92 gmx_fatal(FARGS, "Allocation of %d bytes failed", nbytes_new);
97 if (nbytes_new < nbytes_copy)
99 gmx_incons("In nbnxn_realloc_void: new size less than copy size");
101 memcpy(ptr_new, *ptr, nbytes_copy);
110 /* Reallocate the nbnxn_atomdata_t for a size of n atoms */
111 void nbnxn_atomdata_realloc(nbnxn_atomdata_t *nbat, int n)
115 nbnxn_realloc_void((void **)&nbat->type,
116 nbat->natoms*sizeof(*nbat->type),
117 n*sizeof(*nbat->type),
118 nbat->alloc, nbat->free);
119 nbnxn_realloc_void((void **)&nbat->lj_comb,
120 nbat->natoms*2*sizeof(*nbat->lj_comb),
121 n*2*sizeof(*nbat->lj_comb),
122 nbat->alloc, nbat->free);
123 if (nbat->XFormat != nbatXYZQ)
125 nbnxn_realloc_void((void **)&nbat->q,
126 nbat->natoms*sizeof(*nbat->q),
128 nbat->alloc, nbat->free);
130 if (nbat->nenergrp > 1)
132 nbnxn_realloc_void((void **)&nbat->energrp,
133 nbat->natoms/nbat->na_c*sizeof(*nbat->energrp),
134 n/nbat->na_c*sizeof(*nbat->energrp),
135 nbat->alloc, nbat->free);
137 nbnxn_realloc_void((void **)&nbat->x,
138 nbat->natoms*nbat->xstride*sizeof(*nbat->x),
139 n*nbat->xstride*sizeof(*nbat->x),
140 nbat->alloc, nbat->free);
141 for (t = 0; t < nbat->nout; t++)
143 /* Allocate one element extra for possible signaling with GPUs */
144 nbnxn_realloc_void((void **)&nbat->out[t].f,
145 nbat->natoms*nbat->fstride*sizeof(*nbat->out[t].f),
146 n*nbat->fstride*sizeof(*nbat->out[t].f),
147 nbat->alloc, nbat->free);
152 /* Initializes an nbnxn_atomdata_output_t data structure */
153 static void nbnxn_atomdata_output_init(nbnxn_atomdata_output_t *out,
155 int nenergrp, int stride,
159 ma((void **)&out->fshift, SHIFTS*DIM*sizeof(*out->fshift));
160 out->nV = nenergrp*nenergrp;
161 ma((void **)&out->Vvdw, out->nV*sizeof(*out->Vvdw));
162 ma((void **)&out->Vc, out->nV*sizeof(*out->Vc ));
164 if (nb_kernel_type == nbnxnk4xN_SIMD_4xN ||
165 nb_kernel_type == nbnxnk4xN_SIMD_2xNN)
167 int cj_size = nbnxn_kernel_to_cluster_j_size(nb_kernel_type);
168 out->nVS = nenergrp*nenergrp*stride*(cj_size>>1)*cj_size;
169 ma((void **)&out->VSvdw, out->nVS*sizeof(*out->VSvdw));
170 ma((void **)&out->VSc, out->nVS*sizeof(*out->VSc ));
178 static void copy_int_to_nbat_int(const int *a, int na, int na_round,
179 const int *in, int fill, int *innb)
184 for (i = 0; i < na; i++)
186 innb[j++] = in[a[i]];
188 /* Complete the partially filled last cell with fill */
189 for (; i < na_round; i++)
195 void copy_rvec_to_nbat_real(const int *a, int na, int na_round,
196 const rvec *x, int nbatFormat,
199 /* We complete partially filled cells, can only be the last one in each
200 * column, with coordinates farAway. The actual coordinate value does
201 * not influence the results, since these filler particles do not interact.
202 * Clusters with normal atoms + fillers have a bounding box based only
203 * on the coordinates of the atoms. Clusters with only fillers have as
204 * the bounding box the coordinates of the first filler. Such clusters
205 * are not considered as i-entries, but they are considered as j-entries.
206 * So for performance it is better to have their bounding boxes far away,
207 * such that filler only clusters don't end up in the pair list.
209 const real farAway = -1000000;
217 for (i = 0; i < na; i++)
219 xnb[j++] = x[a[i]][XX];
220 xnb[j++] = x[a[i]][YY];
221 xnb[j++] = x[a[i]][ZZ];
223 /* Complete the partially filled last cell with farAway elements */
224 for (; i < na_round; i++)
233 for (i = 0; i < na; i++)
235 xnb[j++] = x[a[i]][XX];
236 xnb[j++] = x[a[i]][YY];
237 xnb[j++] = x[a[i]][ZZ];
240 /* Complete the partially filled last cell with zeros */
241 for (; i < na_round; i++)
250 j = atom_to_x_index<c_packX4>(a0);
251 c = a0 & (c_packX4-1);
252 for (i = 0; i < na; i++)
254 xnb[j+XX*c_packX4] = x[a[i]][XX];
255 xnb[j+YY*c_packX4] = x[a[i]][YY];
256 xnb[j+ZZ*c_packX4] = x[a[i]][ZZ];
261 j += (DIM-1)*c_packX4;
265 /* Complete the partially filled last cell with zeros */
266 for (; i < na_round; i++)
268 xnb[j+XX*c_packX4] = farAway;
269 xnb[j+YY*c_packX4] = farAway;
270 xnb[j+ZZ*c_packX4] = farAway;
275 j += (DIM-1)*c_packX4;
281 j = atom_to_x_index<c_packX8>(a0);
282 c = a0 & (c_packX8 - 1);
283 for (i = 0; i < na; i++)
285 xnb[j+XX*c_packX8] = x[a[i]][XX];
286 xnb[j+YY*c_packX8] = x[a[i]][YY];
287 xnb[j+ZZ*c_packX8] = x[a[i]][ZZ];
292 j += (DIM-1)*c_packX8;
296 /* Complete the partially filled last cell with zeros */
297 for (; i < na_round; i++)
299 xnb[j+XX*c_packX8] = farAway;
300 xnb[j+YY*c_packX8] = farAway;
301 xnb[j+ZZ*c_packX8] = farAway;
306 j += (DIM-1)*c_packX8;
312 gmx_incons("Unsupported nbnxn_atomdata_t format");
316 /* Stores the LJ parameter data in a format convenient for different kernels */
317 static void set_lj_parameter_data(nbnxn_atomdata_t *nbat, gmx_bool bSIMD)
321 int nt = nbat->ntype;
326 /* nbfp_aligned stores two parameters using the stride most suitable
327 * for the present SIMD architecture, as specified by the constant
328 * c_simdBestPairAlignment from the SIMD header.
329 * There's a slight inefficiency in allocating and initializing nbfp_aligned
330 * when it might not be used, but introducing the conditional code is not
333 nbat->alloc((void **)&nbat->nbfp_aligned,
334 nt*nt*c_simdBestPairAlignment*sizeof(*nbat->nbfp_aligned));
335 for (int i = 0; i < nt; i++)
337 for (int j = 0; j < nt; j++)
339 nbat->nbfp_aligned[(i*nt+j)*c_simdBestPairAlignment+0] = nbat->nbfp[(i*nt+j)*2+0];
340 nbat->nbfp_aligned[(i*nt+j)*c_simdBestPairAlignment+1] = nbat->nbfp[(i*nt+j)*2+1];
341 nbat->nbfp_aligned[(i*nt+j)*c_simdBestPairAlignment+2] = 0;
342 nbat->nbfp_aligned[(i*nt+j)*c_simdBestPairAlignment+3] = 0;
348 /* We use combination rule data for SIMD combination rule kernels
349 * and with LJ-PME kernels. We then only need parameters per atom type,
350 * not per pair of atom types.
352 switch (nbat->comb_rule)
355 nbat->comb_rule = ljcrGEOM;
357 for (int i = 0; i < nt; i++)
359 /* Store the sqrt of the diagonal from the nbfp matrix */
360 nbat->nbfp_comb[i*2 ] = std::sqrt(nbat->nbfp[(i*nt+i)*2 ]);
361 nbat->nbfp_comb[i*2+1] = std::sqrt(nbat->nbfp[(i*nt+i)*2+1]);
365 for (int i = 0; i < nt; i++)
367 /* Get 6*C6 and 12*C12 from the diagonal of the nbfp matrix */
368 c6 = nbat->nbfp[(i*nt+i)*2 ];
369 c12 = nbat->nbfp[(i*nt+i)*2+1];
370 if (c6 > 0 && c12 > 0)
372 /* We store 0.5*2^1/6*sigma and sqrt(4*3*eps),
373 * so we get 6*C6 and 12*C12 after combining.
375 nbat->nbfp_comb[i*2 ] = 0.5*gmx::sixthroot(c12/c6);
376 nbat->nbfp_comb[i*2+1] = std::sqrt(c6*c6/c12);
380 nbat->nbfp_comb[i*2 ] = 0;
381 nbat->nbfp_comb[i*2+1] = 0;
386 /* We always store the full matrix (see code above) */
389 gmx_incons("Unknown combination rule");
396 nbnxn_atomdata_init_simple_exclusion_masks(nbnxn_atomdata_t *nbat)
398 const int simd_width = GMX_SIMD_REAL_WIDTH;
400 /* Set the diagonal cluster pair exclusion mask setup data.
401 * In the kernel we check 0 < j - i to generate the masks.
402 * Here we store j - i for generating the mask for the first i,
403 * we substract 0.5 to avoid rounding issues.
404 * In the kernel we can subtract 1 to generate the subsequent mask.
406 int simd_4xn_diag_size;
408 simd_4xn_diag_size = std::max(NBNXN_CPU_CLUSTER_I_SIZE, simd_width);
409 snew_aligned(nbat->simd_4xn_diagonal_j_minus_i, simd_4xn_diag_size, NBNXN_MEM_ALIGN);
410 for (int j = 0; j < simd_4xn_diag_size; j++)
412 nbat->simd_4xn_diagonal_j_minus_i[j] = j - 0.5;
415 snew_aligned(nbat->simd_2xnn_diagonal_j_minus_i, simd_width, NBNXN_MEM_ALIGN);
416 for (int j = 0; j < simd_width/2; j++)
418 /* The j-cluster size is half the SIMD width */
419 nbat->simd_2xnn_diagonal_j_minus_i[j] = j - 0.5;
420 /* The next half of the SIMD width is for i + 1 */
421 nbat->simd_2xnn_diagonal_j_minus_i[simd_width/2+j] = j - 1 - 0.5;
424 /* We use up to 32 bits for exclusion masking.
425 * The same masks are used for the 4xN and 2x(N+N) kernels.
426 * The masks are read either into integer SIMD registers or into
427 * real SIMD registers (together with a cast).
428 * In single precision this means the real and integer SIMD registers
431 simd_excl_size = NBNXN_CPU_CLUSTER_I_SIZE*simd_width;
432 #if GMX_DOUBLE && !GMX_SIMD_HAVE_INT32_LOGICAL
433 snew_aligned(nbat->simd_exclusion_filter64, simd_excl_size, NBNXN_MEM_ALIGN);
435 snew_aligned(nbat->simd_exclusion_filter, simd_excl_size, NBNXN_MEM_ALIGN);
438 for (int j = 0; j < simd_excl_size; j++)
440 /* Set the consecutive bits for masking pair exclusions */
441 #if GMX_DOUBLE && !GMX_SIMD_HAVE_INT32_LOGICAL
442 nbat->simd_exclusion_filter64[j] = (1U << j);
444 nbat->simd_exclusion_filter[j] = (1U << j);
448 #if !GMX_SIMD_HAVE_LOGICAL && !GMX_SIMD_HAVE_INT32_LOGICAL
449 // If the SIMD implementation has no bitwise logical operation support
450 // whatsoever we cannot use the normal masking. Instead,
451 // we generate a vector of all 2^4 possible ways an i atom
452 // interacts with its 4 j atoms. Each array entry contains
453 // GMX_SIMD_REAL_WIDTH values that are read with a single aligned SIMD load.
454 // Since there is no logical value representation in this case, we use
455 // any nonzero value to indicate 'true', while zero mean 'false'.
456 // This can then be converted to a SIMD boolean internally in the SIMD
457 // module by comparing to zero.
458 // Each array entry encodes how this i atom will interact with the 4 j atoms.
459 // Matching code exists in set_ci_top_excls() to generate indices into this array.
460 // Those indices are used in the kernels.
462 simd_excl_size = NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
463 const real simdFalse = 0.0;
464 const real simdTrue = 1.0;
465 real *simd_interaction_array;
467 snew_aligned(simd_interaction_array, simd_excl_size * GMX_SIMD_REAL_WIDTH, NBNXN_MEM_ALIGN);
468 for (int j = 0; j < simd_excl_size; j++)
470 int index = j * GMX_SIMD_REAL_WIDTH;
471 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
473 simd_interaction_array[index + i] = (j & (1 << i)) ? simdTrue : simdFalse;
476 nbat->simd_interaction_array = simd_interaction_array;
481 /* Initializes an nbnxn_atomdata_t data structure */
482 void nbnxn_atomdata_init(FILE *fp,
483 nbnxn_atomdata_t *nbat,
485 int enbnxninitcombrule,
486 int ntype, const real *nbfp,
489 nbnxn_alloc_t *alloc,
495 gmx_bool simple, bCombGeom, bCombLB, bSIMD;
497 if (alloc == nullptr)
499 nbat->alloc = nbnxn_alloc_aligned;
507 nbat->free = nbnxn_free_aligned;
516 fprintf(debug, "There are %d atom types in the system, adding one for nbnxn_atomdata_t\n", ntype);
518 nbat->ntype = ntype + 1;
519 nbat->alloc((void **)&nbat->nbfp,
520 nbat->ntype*nbat->ntype*2*sizeof(*nbat->nbfp));
521 nbat->alloc((void **)&nbat->nbfp_comb, nbat->ntype*2*sizeof(*nbat->nbfp_comb));
523 /* A tolerance of 1e-5 seems reasonable for (possibly hand-typed)
524 * force-field floating point parameters.
527 ptr = getenv("GMX_LJCOMB_TOL");
532 sscanf(ptr, "%lf", &dbl);
538 /* Temporarily fill nbat->nbfp_comb with sigma and epsilon
539 * to check for the LB rule.
541 for (int i = 0; i < ntype; i++)
543 c6 = nbfp[(i*ntype+i)*2 ]/6.0;
544 c12 = nbfp[(i*ntype+i)*2+1]/12.0;
545 if (c6 > 0 && c12 > 0)
547 nbat->nbfp_comb[i*2 ] = gmx::sixthroot(c12/c6);
548 nbat->nbfp_comb[i*2+1] = 0.25*c6*c6/c12;
550 else if (c6 == 0 && c12 == 0)
552 nbat->nbfp_comb[i*2 ] = 0;
553 nbat->nbfp_comb[i*2+1] = 0;
557 /* Can not use LB rule with only dispersion or repulsion */
562 for (int i = 0; i < nbat->ntype; i++)
564 for (int j = 0; j < nbat->ntype; j++)
566 if (i < ntype && j < ntype)
568 /* fr->nbfp has been updated, so that array too now stores c6/c12 including
569 * the 6.0/12.0 prefactors to save 2 flops in the most common case (force-only).
571 c6 = nbfp[(i*ntype+j)*2 ];
572 c12 = nbfp[(i*ntype+j)*2+1];
573 nbat->nbfp[(i*nbat->ntype+j)*2 ] = c6;
574 nbat->nbfp[(i*nbat->ntype+j)*2+1] = c12;
576 /* Compare 6*C6 and 12*C12 for geometric cobination rule */
577 bCombGeom = bCombGeom &&
578 gmx_within_tol(c6*c6, nbfp[(i*ntype+i)*2 ]*nbfp[(j*ntype+j)*2 ], tol) &&
579 gmx_within_tol(c12*c12, nbfp[(i*ntype+i)*2+1]*nbfp[(j*ntype+j)*2+1], tol);
581 /* Compare C6 and C12 for Lorentz-Berthelot combination rule */
585 ((c6 == 0 && c12 == 0 &&
586 (nbat->nbfp_comb[i*2+1] == 0 || nbat->nbfp_comb[j*2+1] == 0)) ||
587 (c6 > 0 && c12 > 0 &&
588 gmx_within_tol(gmx::sixthroot(c12/c6),
589 0.5*(nbat->nbfp_comb[i*2]+nbat->nbfp_comb[j*2]), tol) &&
590 gmx_within_tol(0.25*c6*c6/c12, std::sqrt(nbat->nbfp_comb[i*2+1]*nbat->nbfp_comb[j*2+1]), tol)));
594 /* Add zero parameters for the additional dummy atom type */
595 nbat->nbfp[(i*nbat->ntype+j)*2 ] = 0;
596 nbat->nbfp[(i*nbat->ntype+j)*2+1] = 0;
602 fprintf(debug, "Combination rules: geometric %d Lorentz-Berthelot %d\n",
606 simple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
608 switch (enbnxninitcombrule)
610 case enbnxninitcombruleDETECT:
611 /* We prefer the geometic combination rule,
612 * as that gives a slightly faster kernel than the LB rule.
616 nbat->comb_rule = ljcrGEOM;
620 nbat->comb_rule = ljcrLB;
624 nbat->comb_rule = ljcrNONE;
626 nbat->free(nbat->nbfp_comb);
631 if (nbat->comb_rule == ljcrNONE)
633 fprintf(fp, "Using full Lennard-Jones parameter combination matrix\n\n");
637 fprintf(fp, "Using %s Lennard-Jones combination rule\n\n",
638 nbat->comb_rule == ljcrGEOM ? "geometric" : "Lorentz-Berthelot");
642 case enbnxninitcombruleGEOM:
643 nbat->comb_rule = ljcrGEOM;
645 case enbnxninitcombruleLB:
646 nbat->comb_rule = ljcrLB;
648 case enbnxninitcombruleNONE:
649 nbat->comb_rule = ljcrNONE;
651 nbat->free(nbat->nbfp_comb);
654 gmx_incons("Unknown enbnxninitcombrule");
657 bSIMD = (nb_kernel_type == nbnxnk4xN_SIMD_4xN ||
658 nb_kernel_type == nbnxnk4xN_SIMD_2xNN);
660 set_lj_parameter_data(nbat, bSIMD);
663 nbat->type = nullptr;
664 nbat->lj_comb = nullptr;
671 pack_x = std::max(NBNXN_CPU_CLUSTER_I_SIZE,
672 nbnxn_kernel_to_cluster_j_size(nb_kernel_type));
676 nbat->XFormat = nbatX4;
679 nbat->XFormat = nbatX8;
682 gmx_incons("Unsupported packing width");
687 nbat->XFormat = nbatXYZ;
690 nbat->FFormat = nbat->XFormat;
694 nbat->XFormat = nbatXYZQ;
695 nbat->FFormat = nbatXYZ;
698 nbat->nenergrp = n_energygroups;
701 /* Energy groups not supported yet for super-sub lists */
702 if (n_energygroups > 1 && fp != nullptr)
704 fprintf(fp, "\nNOTE: With GPUs, reporting energy group contributions is not supported\n\n");
708 /* Temporary storage goes as #grp^3*simd_width^2/2, so limit to 64 */
709 if (nbat->nenergrp > 64)
711 gmx_fatal(FARGS, "With NxN kernels not more than 64 energy groups are supported\n");
714 while (nbat->nenergrp > (1<<nbat->neg_2log))
718 nbat->energrp = nullptr;
719 nbat->alloc((void **)&nbat->shift_vec, SHIFTS*sizeof(*nbat->shift_vec));
720 nbat->xstride = (nbat->XFormat == nbatXYZQ ? STRIDE_XYZQ : DIM);
721 nbat->fstride = (nbat->FFormat == nbatXYZQ ? STRIDE_XYZQ : DIM);
727 nbnxn_atomdata_init_simple_exclusion_masks(nbat);
731 /* Initialize the output data structures */
733 snew(nbat->out, nbat->nout);
735 for (int i = 0; i < nbat->nout; i++)
737 nbnxn_atomdata_output_init(&nbat->out[i],
739 nbat->nenergrp, 1<<nbat->neg_2log,
742 nbat->buffer_flags.flag = nullptr;
743 nbat->buffer_flags.flag_nalloc = 0;
745 nth = gmx_omp_nthreads_get(emntNonbonded);
747 ptr = getenv("GMX_USE_TREEREDUCE");
750 nbat->bUseTreeReduce = strtol(ptr, nullptr, 10);
753 else if (nth > 8) /*on the CPU we currently don't benefit even at 32*/
755 nbat->bUseTreeReduce = 1;
760 nbat->bUseTreeReduce = 0;
762 if (nbat->bUseTreeReduce)
766 fprintf(fp, "Using tree force reduction\n\n");
768 snew(nbat->syncStep, nth);
772 template<int packSize>
773 static void copy_lj_to_nbat_lj_comb(const real *ljparam_type,
774 const int *type, int na,
777 /* The LJ params follow the combination rule:
778 * copy the params for the type array to the atom array.
780 for (int is = 0; is < na; is += packSize)
782 for (int k = 0; k < packSize; k++)
785 ljparam_at[is*2 + k] = ljparam_type[type[i]*2 ];
786 ljparam_at[is*2 + packSize + k] = ljparam_type[type[i]*2 + 1];
791 /* Sets the atom type in nbnxn_atomdata_t */
792 static void nbnxn_atomdata_set_atomtypes(nbnxn_atomdata_t *nbat,
793 const nbnxn_search_t nbs,
796 for (int g = 0; g < nbs->ngrid; g++)
798 const nbnxn_grid_t * grid = &nbs->grid[g];
800 /* Loop over all columns and copy and fill */
801 for (int i = 0; i < grid->ncx*grid->ncy; i++)
803 int ncz = grid->cxy_ind[i+1] - grid->cxy_ind[i];
804 int ash = (grid->cell0 + grid->cxy_ind[i])*grid->na_sc;
806 copy_int_to_nbat_int(nbs->a+ash, grid->cxy_na[i], ncz*grid->na_sc,
807 type, nbat->ntype-1, nbat->type+ash);
812 /* Sets the LJ combination rule parameters in nbnxn_atomdata_t */
813 static void nbnxn_atomdata_set_ljcombparams(nbnxn_atomdata_t *nbat,
814 const nbnxn_search_t nbs)
816 if (nbat->comb_rule != ljcrNONE)
818 for (int g = 0; g < nbs->ngrid; g++)
820 const nbnxn_grid_t * grid = &nbs->grid[g];
822 /* Loop over all columns and copy and fill */
823 for (int i = 0; i < grid->ncx*grid->ncy; i++)
825 int ncz = grid->cxy_ind[i+1] - grid->cxy_ind[i];
826 int ash = (grid->cell0 + grid->cxy_ind[i])*grid->na_sc;
828 if (nbat->XFormat == nbatX4)
830 copy_lj_to_nbat_lj_comb<c_packX4>(nbat->nbfp_comb,
833 nbat->lj_comb + ash*2);
835 else if (nbat->XFormat == nbatX8)
837 copy_lj_to_nbat_lj_comb<c_packX8>(nbat->nbfp_comb,
840 nbat->lj_comb + ash*2);
842 else if (nbat->XFormat == nbatXYZQ)
844 copy_lj_to_nbat_lj_comb<1>(nbat->nbfp_comb,
847 nbat->lj_comb + ash*2);
854 /* Sets the charges in nbnxn_atomdata_t *nbat */
855 static void nbnxn_atomdata_set_charges(nbnxn_atomdata_t *nbat,
856 const nbnxn_search_t nbs,
862 for (int g = 0; g < nbs->ngrid; g++)
864 const nbnxn_grid_t * grid = &nbs->grid[g];
866 /* Loop over all columns and copy and fill */
867 for (int cxy = 0; cxy < grid->ncx*grid->ncy; cxy++)
869 int ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
870 int na = grid->cxy_na[cxy];
871 int na_round = (grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy])*grid->na_sc;
873 if (nbat->XFormat == nbatXYZQ)
875 q = nbat->x + ash*STRIDE_XYZQ + ZZ + 1;
876 for (i = 0; i < na; i++)
878 *q = charge[nbs->a[ash+i]];
881 /* Complete the partially filled last cell with zeros */
882 for (; i < na_round; i++)
891 for (i = 0; i < na; i++)
893 *q = charge[nbs->a[ash+i]];
896 /* Complete the partially filled last cell with zeros */
897 for (; i < na_round; i++)
907 /* Set the charges of perturbed atoms in nbnxn_atomdata_t to 0.
908 * This is to automatically remove the RF/PME self term in the nbnxn kernels.
909 * Part of the zero interactions are still calculated in the normal kernels.
910 * All perturbed interactions are calculated in the free energy kernel,
911 * using the original charge and LJ data, not nbnxn_atomdata_t.
913 static void nbnxn_atomdata_mask_fep(nbnxn_atomdata_t *nbat,
914 const nbnxn_search_t nbs)
919 if (nbat->XFormat == nbatXYZQ)
921 q = nbat->x + ZZ + 1;
922 stride_q = STRIDE_XYZQ;
930 for (int g = 0; g < nbs->ngrid; g++)
932 const nbnxn_grid_t * grid = &nbs->grid[g];
939 nsubc = c_gpuNumClusterPerCell;
942 int c_offset = grid->cell0*grid->na_sc;
944 /* Loop over all columns and copy and fill */
945 for (int c = 0; c < grid->nc*nsubc; c++)
947 /* Does this cluster contain perturbed particles? */
948 if (grid->fep[c] != 0)
950 for (int i = 0; i < grid->na_c; i++)
952 /* Is this a perturbed particle? */
953 if (grid->fep[c] & (1 << i))
955 int ind = c_offset + c*grid->na_c + i;
956 /* Set atom type and charge to non-interacting */
957 nbat->type[ind] = nbat->ntype - 1;
966 /* Copies the energy group indices to a reordered and packed array */
967 static void copy_egp_to_nbat_egps(const int *a, int na, int na_round,
968 int na_c, int bit_shift,
969 const int *in, int *innb)
975 for (i = 0; i < na; i += na_c)
977 /* Store na_c energy group numbers into one int */
979 for (int sa = 0; sa < na_c; sa++)
984 comb |= (GET_CGINFO_GID(in[at]) << (sa*bit_shift));
989 /* Complete the partially filled last cell with fill */
990 for (; i < na_round; i += na_c)
996 /* Set the energy group indices for atoms in nbnxn_atomdata_t */
997 static void nbnxn_atomdata_set_energygroups(nbnxn_atomdata_t *nbat,
998 const nbnxn_search_t nbs,
1001 if (nbat->nenergrp == 1)
1006 for (int g = 0; g < nbs->ngrid; g++)
1008 const nbnxn_grid_t * grid = &nbs->grid[g];
1010 /* Loop over all columns and copy and fill */
1011 for (int i = 0; i < grid->ncx*grid->ncy; i++)
1013 int ncz = grid->cxy_ind[i+1] - grid->cxy_ind[i];
1014 int ash = (grid->cell0 + grid->cxy_ind[i])*grid->na_sc;
1016 copy_egp_to_nbat_egps(nbs->a+ash, grid->cxy_na[i], ncz*grid->na_sc,
1017 nbat->na_c, nbat->neg_2log,
1018 atinfo, nbat->energrp+(ash>>grid->na_c_2log));
1023 /* Sets all required atom parameter data in nbnxn_atomdata_t */
1024 void nbnxn_atomdata_set(nbnxn_atomdata_t *nbat,
1025 const nbnxn_search_t nbs,
1026 const t_mdatoms *mdatoms,
1029 nbnxn_atomdata_set_atomtypes(nbat, nbs, mdatoms->typeA);
1031 nbnxn_atomdata_set_charges(nbat, nbs, mdatoms->chargeA);
1035 nbnxn_atomdata_mask_fep(nbat, nbs);
1038 /* This must be done after masking types for FEP */
1039 nbnxn_atomdata_set_ljcombparams(nbat, nbs);
1041 nbnxn_atomdata_set_energygroups(nbat, nbs, atinfo);
1044 /* Copies the shift vector array to nbnxn_atomdata_t */
1045 void nbnxn_atomdata_copy_shiftvec(gmx_bool bDynamicBox,
1047 nbnxn_atomdata_t *nbat)
1051 nbat->bDynamicBox = bDynamicBox;
1052 for (i = 0; i < SHIFTS; i++)
1054 copy_rvec(shift_vec[i], nbat->shift_vec[i]);
1058 /* Copies (and reorders) the coordinates to nbnxn_atomdata_t */
1059 void nbnxn_atomdata_copy_x_to_nbat_x(const nbnxn_search_t nbs,
1063 nbnxn_atomdata_t *nbat)
1086 nbat->natoms_local = nbs->grid[0].nc*nbs->grid[0].na_sc;
1089 nth = gmx_omp_nthreads_get(emntPairsearch);
1091 #pragma omp parallel for num_threads(nth) schedule(static)
1092 for (th = 0; th < nth; th++)
1096 for (int g = g0; g < g1; g++)
1098 const nbnxn_grid_t *grid;
1101 grid = &nbs->grid[g];
1103 cxy0 = (grid->ncx*grid->ncy* th +nth-1)/nth;
1104 cxy1 = (grid->ncx*grid->ncy*(th+1)+nth-1)/nth;
1106 for (int cxy = cxy0; cxy < cxy1; cxy++)
1108 int na, ash, na_fill;
1110 na = grid->cxy_na[cxy];
1111 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1113 if (g == 0 && FillLocal)
1116 (grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy])*grid->na_sc;
1120 /* We fill only the real particle locations.
1121 * We assume the filling entries at the end have been
1122 * properly set before during pair-list generation.
1126 copy_rvec_to_nbat_real(nbs->a+ash, na, na_fill, x,
1127 nbat->XFormat, nbat->x, ash);
1131 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1136 nbnxn_atomdata_clear_reals(real * gmx_restrict dest,
1139 for (int i = i0; i < i1; i++)
1145 gmx_unused static void
1146 nbnxn_atomdata_reduce_reals(real * gmx_restrict dest,
1148 real ** gmx_restrict src,
1154 /* The destination buffer contains data, add to it */
1155 for (int i = i0; i < i1; i++)
1157 for (int s = 0; s < nsrc; s++)
1159 dest[i] += src[s][i];
1165 /* The destination buffer is unitialized, set it first */
1166 for (int i = i0; i < i1; i++)
1168 dest[i] = src[0][i];
1169 for (int s = 1; s < nsrc; s++)
1171 dest[i] += src[s][i];
1177 gmx_unused static void
1178 nbnxn_atomdata_reduce_reals_simd(real gmx_unused * gmx_restrict dest,
1179 gmx_bool gmx_unused bDestSet,
1180 real gmx_unused ** gmx_restrict src,
1181 int gmx_unused nsrc,
1182 int gmx_unused i0, int gmx_unused i1)
1185 /* The SIMD width here is actually independent of that in the kernels,
1186 * but we use the same width for simplicity (usually optimal anyhow).
1188 SimdReal dest_SSE, src_SSE;
1192 for (int i = i0; i < i1; i += GMX_SIMD_REAL_WIDTH)
1194 dest_SSE = load<SimdReal>(dest+i);
1195 for (int s = 0; s < nsrc; s++)
1197 src_SSE = load<SimdReal>(src[s]+i);
1198 dest_SSE = dest_SSE + src_SSE;
1200 store(dest+i, dest_SSE);
1205 for (int i = i0; i < i1; i += GMX_SIMD_REAL_WIDTH)
1207 dest_SSE = load<SimdReal>(src[0]+i);
1208 for (int s = 1; s < nsrc; s++)
1210 src_SSE = load<SimdReal>(src[s]+i);
1211 dest_SSE = dest_SSE + src_SSE;
1213 store(dest+i, dest_SSE);
1219 /* Add part of the force array(s) from nbnxn_atomdata_t to f */
1221 nbnxn_atomdata_add_nbat_f_to_f_part(const nbnxn_search_t nbs,
1222 const nbnxn_atomdata_t *nbat,
1223 nbnxn_atomdata_output_t *out,
1233 /* Loop over all columns and copy and fill */
1234 switch (nbat->FFormat)
1242 for (int a = a0; a < a1; a++)
1244 int i = cell[a]*nbat->fstride;
1247 f[a][YY] += fnb[i+1];
1248 f[a][ZZ] += fnb[i+2];
1253 for (int a = a0; a < a1; a++)
1255 int i = cell[a]*nbat->fstride;
1257 for (int fa = 0; fa < nfa; fa++)
1259 f[a][XX] += out[fa].f[i];
1260 f[a][YY] += out[fa].f[i+1];
1261 f[a][ZZ] += out[fa].f[i+2];
1271 for (int a = a0; a < a1; a++)
1273 int i = atom_to_x_index<c_packX4>(cell[a]);
1275 f[a][XX] += fnb[i+XX*c_packX4];
1276 f[a][YY] += fnb[i+YY*c_packX4];
1277 f[a][ZZ] += fnb[i+ZZ*c_packX4];
1282 for (int a = a0; a < a1; a++)
1284 int i = atom_to_x_index<c_packX4>(cell[a]);
1286 for (int fa = 0; fa < nfa; fa++)
1288 f[a][XX] += out[fa].f[i+XX*c_packX4];
1289 f[a][YY] += out[fa].f[i+YY*c_packX4];
1290 f[a][ZZ] += out[fa].f[i+ZZ*c_packX4];
1300 for (int a = a0; a < a1; a++)
1302 int i = atom_to_x_index<c_packX8>(cell[a]);
1304 f[a][XX] += fnb[i+XX*c_packX8];
1305 f[a][YY] += fnb[i+YY*c_packX8];
1306 f[a][ZZ] += fnb[i+ZZ*c_packX8];
1311 for (int a = a0; a < a1; a++)
1313 int i = atom_to_x_index<c_packX8>(cell[a]);
1315 for (int fa = 0; fa < nfa; fa++)
1317 f[a][XX] += out[fa].f[i+XX*c_packX8];
1318 f[a][YY] += out[fa].f[i+YY*c_packX8];
1319 f[a][ZZ] += out[fa].f[i+ZZ*c_packX8];
1325 gmx_incons("Unsupported nbnxn_atomdata_t format");
1329 static gmx_inline unsigned char reverse_bits(unsigned char b)
1331 /* http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64BitsDiv */
1332 return (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;
1335 static void nbnxn_atomdata_add_nbat_f_to_f_treereduce(const nbnxn_atomdata_t *nbat,
1338 const nbnxn_buffer_flags_t *flags = &nbat->buffer_flags;
1340 int next_pow2 = 1<<(gmx::log2I(nth-1)+1);
1342 assert(nbat->nout == nth); /* tree-reduce currently only works for nout==nth */
1344 memset(nbat->syncStep, 0, sizeof(*(nbat->syncStep))*nth);
1346 #pragma omp parallel num_threads(nth)
1354 th = gmx_omp_get_thread_num();
1356 for (group_size = 2; group_size < 2*next_pow2; group_size *= 2)
1358 int index[2], group_pos, partner_pos, wu;
1359 int partner_th = th ^ (group_size/2);
1364 /* wait on partner thread - replaces full barrier */
1365 int sync_th, sync_group_size;
1367 tMPI_Atomic_memory_barrier(); /* gurantee data is saved before marking work as done */
1368 tMPI_Atomic_set(&(nbat->syncStep[th]), group_size/2); /* mark previous step as completed */
1370 /* find thread to sync with. Equal to partner_th unless nth is not a power of two. */
1371 for (sync_th = partner_th, sync_group_size = group_size; sync_th >= nth && sync_group_size > 2; sync_group_size /= 2)
1373 sync_th &= ~(sync_group_size/4);
1375 if (sync_th < nth) /* otherwise nothing to sync index[1] will be >=nout */
1377 /* wait on the thread which computed input data in previous step */
1378 while (tMPI_Atomic_get((volatile tMPI_Atomic_t*)&(nbat->syncStep[sync_th])) < group_size/2)
1382 /* guarantee that no later load happens before wait loop is finisehd */
1383 tMPI_Atomic_memory_barrier();
1385 #else /* TMPI_ATOMICS */
1390 /* Calculate buffers to sum (result goes into first buffer) */
1391 group_pos = th % group_size;
1392 index[0] = th - group_pos;
1393 index[1] = index[0] + group_size/2;
1395 /* If no second buffer, nothing to do */
1396 if (index[1] >= nbat->nout && group_size > 2)
1401 #if NBNXN_BUFFERFLAG_MAX_THREADS > 256
1402 #error reverse_bits assumes max 256 threads
1404 /* Position is permuted so that one of the 2 vectors being added was computed on the same thread in the previous step.
1405 This improves locality and enables to sync with just a single thread between steps (=the levels in the btree).
1406 The permutation which allows this corresponds to reversing the bits of the group position.
1408 group_pos = reverse_bits(group_pos)/(256/group_size);
1410 partner_pos = group_pos ^ 1;
1412 /* loop over two work-units (own and partner) */
1413 for (wu = 0; wu < 2; wu++)
1417 if (partner_th < nth)
1419 break; /* partner exists we don't have to do his work */
1423 group_pos = partner_pos;
1427 /* Calculate the cell-block range for our thread */
1428 b0 = (flags->nflag* group_pos )/group_size;
1429 b1 = (flags->nflag*(group_pos+1))/group_size;
1431 for (b = b0; b < b1; b++)
1433 i0 = b *NBNXN_BUFFERFLAG_SIZE*nbat->fstride;
1434 i1 = (b+1)*NBNXN_BUFFERFLAG_SIZE*nbat->fstride;
1436 if (bitmask_is_set(flags->flag[b], index[1]) || group_size > 2)
1439 nbnxn_atomdata_reduce_reals_simd
1441 nbnxn_atomdata_reduce_reals
1443 (nbat->out[index[0]].f,
1444 bitmask_is_set(flags->flag[b], index[0]) || group_size > 2,
1445 &(nbat->out[index[1]].f), 1, i0, i1);
1448 else if (!bitmask_is_set(flags->flag[b], index[0]))
1450 nbnxn_atomdata_clear_reals(nbat->out[index[0]].f,
1457 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1462 static void nbnxn_atomdata_add_nbat_f_to_f_stdreduce(const nbnxn_atomdata_t *nbat,
1465 #pragma omp parallel for num_threads(nth) schedule(static)
1466 for (int th = 0; th < nth; th++)
1470 const nbnxn_buffer_flags_t *flags;
1472 real *fptr[NBNXN_BUFFERFLAG_MAX_THREADS];
1474 flags = &nbat->buffer_flags;
1476 /* Calculate the cell-block range for our thread */
1477 int b0 = (flags->nflag* th )/nth;
1478 int b1 = (flags->nflag*(th+1))/nth;
1480 for (int b = b0; b < b1; b++)
1482 int i0 = b *NBNXN_BUFFERFLAG_SIZE*nbat->fstride;
1483 int i1 = (b+1)*NBNXN_BUFFERFLAG_SIZE*nbat->fstride;
1486 for (int out = 1; out < nbat->nout; out++)
1488 if (bitmask_is_set(flags->flag[b], out))
1490 fptr[nfptr++] = nbat->out[out].f;
1496 nbnxn_atomdata_reduce_reals_simd
1498 nbnxn_atomdata_reduce_reals
1501 bitmask_is_set(flags->flag[b], 0),
1505 else if (!bitmask_is_set(flags->flag[b], 0))
1507 nbnxn_atomdata_clear_reals(nbat->out[0].f,
1512 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1516 /* Add the force array(s) from nbnxn_atomdata_t to f */
1517 void nbnxn_atomdata_add_nbat_f_to_f(const nbnxn_search_t nbs,
1519 const nbnxn_atomdata_t *nbat,
1524 nbs_cycle_start(&nbs->cc[enbsCCreducef]);
1530 na = nbs->natoms_nonlocal;
1534 na = nbs->natoms_local;
1537 a0 = nbs->natoms_local;
1538 na = nbs->natoms_nonlocal - nbs->natoms_local;
1542 int nth = gmx_omp_nthreads_get(emntNonbonded);
1546 if (locality != eatAll)
1548 gmx_incons("add_f_to_f called with nout>1 and locality!=eatAll");
1551 /* Reduce the force thread output buffers into buffer 0, before adding
1552 * them to the, differently ordered, "real" force buffer.
1554 if (nbat->bUseTreeReduce)
1556 nbnxn_atomdata_add_nbat_f_to_f_treereduce(nbat, nth);
1560 nbnxn_atomdata_add_nbat_f_to_f_stdreduce(nbat, nth);
1563 #pragma omp parallel for num_threads(nth) schedule(static)
1564 for (int th = 0; th < nth; th++)
1568 nbnxn_atomdata_add_nbat_f_to_f_part(nbs, nbat,
1575 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
1578 nbs_cycle_stop(&nbs->cc[enbsCCreducef]);
1581 /* Adds the shift forces from nbnxn_atomdata_t to fshift */
1582 void nbnxn_atomdata_add_nbat_fshift_to_fshift(const nbnxn_atomdata_t *nbat,
1585 const nbnxn_atomdata_output_t * out = nbat->out;
1587 for (int s = 0; s < SHIFTS; s++)
1591 for (int th = 0; th < nbat->nout; th++)
1593 sum[XX] += out[th].fshift[s*DIM+XX];
1594 sum[YY] += out[th].fshift[s*DIM+YY];
1595 sum[ZZ] += out[th].fshift[s*DIM+ZZ];
1597 rvec_inc(fshift[s], sum);