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.
45 #include "gromacs/math/utilities.h"
48 #include "nbnxn_consts.h"
49 /* nbnxn_internal.h included gromacs/simd/macros.h */
50 #include "nbnxn_internal.h"
52 #include "gromacs/simd/vector_operations.h"
54 #include "nbnxn_atomdata.h"
55 #include "nbnxn_search.h"
56 #include "gmx_omp_nthreads.h"
59 #include "gromacs/fileio/gmxfio.h"
61 #ifdef NBNXN_SEARCH_BB_SIMD4
62 /* Always use 4-wide SIMD for bounding box calculations */
65 /* Single precision BBs + coordinates, we can also load coordinates with SIMD */
66 # define NBNXN_SEARCH_SIMD4_FLOAT_X_BB
69 # if defined NBNXN_SEARCH_SIMD4_FLOAT_X_BB && (GPU_NSUBCELL == 4 || GPU_NSUBCELL == 8)
70 /* Store bounding boxes with x, y and z coordinates in packs of 4 */
71 # define NBNXN_PBB_SIMD4
74 /* The packed bounding box coordinate stride is always set to 4.
75 * With AVX we could use 8, but that turns out not to be faster.
78 # define STRIDE_PBB_2LOG 2
80 #endif /* NBNXN_SEARCH_BB_SIMD4 */
84 /* The functions below are macros as they are performance sensitive */
86 /* 4x4 list, pack=4: no complex conversion required */
87 /* i-cluster to j-cluster conversion */
88 #define CI_TO_CJ_J4(ci) (ci)
89 /* cluster index to coordinate array index conversion */
90 #define X_IND_CI_J4(ci) ((ci)*STRIDE_P4)
91 #define X_IND_CJ_J4(cj) ((cj)*STRIDE_P4)
93 /* 4x2 list, pack=4: j-cluster size is half the packing width */
94 /* i-cluster to j-cluster conversion */
95 #define CI_TO_CJ_J2(ci) ((ci)<<1)
96 /* cluster index to coordinate array index conversion */
97 #define X_IND_CI_J2(ci) ((ci)*STRIDE_P4)
98 #define X_IND_CJ_J2(cj) (((cj)>>1)*STRIDE_P4 + ((cj) & 1)*(PACK_X4>>1))
100 /* 4x8 list, pack=8: i-cluster size is half the packing width */
101 /* i-cluster to j-cluster conversion */
102 #define CI_TO_CJ_J8(ci) ((ci)>>1)
103 /* cluster index to coordinate array index conversion */
104 #define X_IND_CI_J8(ci) (((ci)>>1)*STRIDE_P8 + ((ci) & 1)*(PACK_X8>>1))
105 #define X_IND_CJ_J8(cj) ((cj)*STRIDE_P8)
107 /* The j-cluster size is matched to the SIMD width */
108 #if GMX_SIMD_REAL_WIDTH == 2
109 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J2(ci)
110 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J2(ci)
111 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J2(cj)
113 #if GMX_SIMD_REAL_WIDTH == 4
114 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J4(ci)
115 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J4(ci)
116 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J4(cj)
118 #if GMX_SIMD_REAL_WIDTH == 8
119 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J8(ci)
120 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J8(ci)
121 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J8(cj)
122 /* Half SIMD with j-cluster size */
123 #define CI_TO_CJ_SIMD_2XNN(ci) CI_TO_CJ_J4(ci)
124 #define X_IND_CI_SIMD_2XNN(ci) X_IND_CI_J4(ci)
125 #define X_IND_CJ_SIMD_2XNN(cj) X_IND_CJ_J4(cj)
127 #if GMX_SIMD_REAL_WIDTH == 16
128 #define CI_TO_CJ_SIMD_2XNN(ci) CI_TO_CJ_J8(ci)
129 #define X_IND_CI_SIMD_2XNN(ci) X_IND_CI_J8(ci)
130 #define X_IND_CJ_SIMD_2XNN(cj) X_IND_CJ_J8(cj)
132 #error "unsupported GMX_SIMD_REAL_WIDTH"
138 #endif /* GMX_NBNXN_SIMD */
141 #ifdef NBNXN_SEARCH_BB_SIMD4
142 /* Store bounding boxes corners as quadruplets: xxxxyyyyzzzz */
144 /* Size of bounding box corners quadruplet */
145 #define NNBSBB_XXXX (NNBSBB_D*DIM*STRIDE_PBB)
148 /* We shift the i-particles backward for PBC.
149 * This leads to more conditionals than shifting forward.
150 * We do this to get more balanced pair lists.
152 #define NBNXN_SHIFT_BACKWARD
155 /* This define is a lazy way to avoid interdependence of the grid
156 * and searching data structures.
158 #define NBNXN_NA_SC_MAX (GPU_NSUBCELL*NBNXN_GPU_CLUSTER_SIZE)
161 static void nbs_cycle_clear(nbnxn_cycle_t *cc)
165 for (i = 0; i < enbsCCnr; i++)
172 static double Mcyc_av(const nbnxn_cycle_t *cc)
174 return (double)cc->c*1e-6/cc->count;
177 static void nbs_cycle_print(FILE *fp, const nbnxn_search_t nbs)
183 fprintf(fp, "ns %4d grid %4.1f search %4.1f red.f %5.3f",
184 nbs->cc[enbsCCgrid].count,
185 Mcyc_av(&nbs->cc[enbsCCgrid]),
186 Mcyc_av(&nbs->cc[enbsCCsearch]),
187 Mcyc_av(&nbs->cc[enbsCCreducef]));
189 if (nbs->nthread_max > 1)
191 if (nbs->cc[enbsCCcombine].count > 0)
193 fprintf(fp, " comb %5.2f",
194 Mcyc_av(&nbs->cc[enbsCCcombine]));
196 fprintf(fp, " s. th");
197 for (t = 0; t < nbs->nthread_max; t++)
199 fprintf(fp, " %4.1f",
200 Mcyc_av(&nbs->work[t].cc[enbsCCsearch]));
206 static void nbnxn_grid_init(nbnxn_grid_t * grid)
209 grid->cxy_ind = NULL;
210 grid->cxy_nalloc = 0;
216 static int get_2log(int n)
221 while ((1<<log2) < n)
227 gmx_fatal(FARGS, "nbnxn na_c (%d) is not a power of 2", n);
233 static int nbnxn_kernel_to_ci_size(int nb_kernel_type)
235 switch (nb_kernel_type)
237 case nbnxnk4x4_PlainC:
238 case nbnxnk4xN_SIMD_4xN:
239 case nbnxnk4xN_SIMD_2xNN:
240 return NBNXN_CPU_CLUSTER_I_SIZE;
241 case nbnxnk8x8x8_CUDA:
242 case nbnxnk8x8x8_PlainC:
243 /* The cluster size for super/sub lists is only set here.
244 * Any value should work for the pair-search and atomdata code.
245 * The kernels, of course, might require a particular value.
247 return NBNXN_GPU_CLUSTER_SIZE;
249 gmx_incons("unknown kernel type");
255 int nbnxn_kernel_to_cj_size(int nb_kernel_type)
257 int nbnxn_simd_width = 0;
260 #ifdef GMX_NBNXN_SIMD
261 nbnxn_simd_width = GMX_SIMD_REAL_WIDTH;
264 switch (nb_kernel_type)
266 case nbnxnk4x4_PlainC:
267 cj_size = NBNXN_CPU_CLUSTER_I_SIZE;
269 case nbnxnk4xN_SIMD_4xN:
270 cj_size = nbnxn_simd_width;
272 case nbnxnk4xN_SIMD_2xNN:
273 cj_size = nbnxn_simd_width/2;
275 case nbnxnk8x8x8_CUDA:
276 case nbnxnk8x8x8_PlainC:
277 cj_size = nbnxn_kernel_to_ci_size(nb_kernel_type);
280 gmx_incons("unknown kernel type");
286 static int ci_to_cj(int na_cj_2log, int ci)
290 case 2: return ci; break;
291 case 1: return (ci<<1); break;
292 case 3: return (ci>>1); break;
298 gmx_bool nbnxn_kernel_pairlist_simple(int nb_kernel_type)
300 if (nb_kernel_type == nbnxnkNotSet)
302 gmx_fatal(FARGS, "Non-bonded kernel type not set for Verlet-style pair-list.");
305 switch (nb_kernel_type)
307 case nbnxnk8x8x8_CUDA:
308 case nbnxnk8x8x8_PlainC:
311 case nbnxnk4x4_PlainC:
312 case nbnxnk4xN_SIMD_4xN:
313 case nbnxnk4xN_SIMD_2xNN:
317 gmx_incons("Invalid nonbonded kernel type passed!");
322 void nbnxn_init_search(nbnxn_search_t * nbs_ptr,
324 gmx_domdec_zones_t *zones,
333 nbs->DomDec = (n_dd_cells != NULL);
335 clear_ivec(nbs->dd_dim);
341 for (d = 0; d < DIM; d++)
343 if ((*n_dd_cells)[d] > 1)
346 /* Each grid matches a DD zone */
352 snew(nbs->grid, nbs->ngrid);
353 for (g = 0; g < nbs->ngrid; g++)
355 nbnxn_grid_init(&nbs->grid[g]);
358 nbs->cell_nalloc = 0;
362 nbs->nthread_max = nthread_max;
364 /* Initialize the work data structures for each thread */
365 snew(nbs->work, nbs->nthread_max);
366 for (t = 0; t < nbs->nthread_max; t++)
368 nbs->work[t].cxy_na = NULL;
369 nbs->work[t].cxy_na_nalloc = 0;
370 nbs->work[t].sort_work = NULL;
371 nbs->work[t].sort_work_nalloc = 0;
374 /* Initialize detailed nbsearch cycle counting */
375 nbs->print_cycles = (getenv("GMX_NBNXN_CYCLE") != 0);
376 nbs->search_count = 0;
377 nbs_cycle_clear(nbs->cc);
378 for (t = 0; t < nbs->nthread_max; t++)
380 nbs_cycle_clear(nbs->work[t].cc);
384 static real grid_atom_density(int n, rvec corner0, rvec corner1)
388 rvec_sub(corner1, corner0, size);
390 return n/(size[XX]*size[YY]*size[ZZ]);
393 static int set_grid_size_xy(const nbnxn_search_t nbs,
396 int n, rvec corner0, rvec corner1,
401 real adens, tlen, tlen_x, tlen_y, nc_max;
404 rvec_sub(corner1, corner0, size);
408 /* target cell length */
411 /* To minimize the zero interactions, we should make
412 * the largest of the i/j cell cubic.
414 na_c = max(grid->na_c, grid->na_cj);
416 /* Approximately cubic cells */
417 tlen = pow(na_c/atom_density, 1.0/3.0);
423 /* Approximately cubic sub cells */
424 tlen = pow(grid->na_c/atom_density, 1.0/3.0);
425 tlen_x = tlen*GPU_NSUBCELL_X;
426 tlen_y = tlen*GPU_NSUBCELL_Y;
428 /* We round ncx and ncy down, because we get less cell pairs
429 * in the nbsist when the fixed cell dimensions (x,y) are
430 * larger than the variable one (z) than the other way around.
432 grid->ncx = max(1, (int)(size[XX]/tlen_x));
433 grid->ncy = max(1, (int)(size[YY]/tlen_y));
441 grid->sx = size[XX]/grid->ncx;
442 grid->sy = size[YY]/grid->ncy;
443 grid->inv_sx = 1/grid->sx;
444 grid->inv_sy = 1/grid->sy;
448 /* This is a non-home zone, add an extra row of cells
449 * for particles communicated for bonded interactions.
450 * These can be beyond the cut-off. It doesn't matter where
451 * they end up on the grid, but for performance it's better
452 * if they don't end up in cells that can be within cut-off range.
458 /* We need one additional cell entry for particles moved by DD */
459 if (grid->ncx*grid->ncy+1 > grid->cxy_nalloc)
461 grid->cxy_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
462 srenew(grid->cxy_na, grid->cxy_nalloc);
463 srenew(grid->cxy_ind, grid->cxy_nalloc+1);
465 for (t = 0; t < nbs->nthread_max; t++)
467 if (grid->ncx*grid->ncy+1 > nbs->work[t].cxy_na_nalloc)
469 nbs->work[t].cxy_na_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
470 srenew(nbs->work[t].cxy_na, nbs->work[t].cxy_na_nalloc);
474 /* Worst case scenario of 1 atom in each last cell */
475 if (grid->na_cj <= grid->na_c)
477 nc_max = n/grid->na_sc + grid->ncx*grid->ncy;
481 nc_max = n/grid->na_sc + grid->ncx*grid->ncy*grid->na_cj/grid->na_c;
484 if (nc_max > grid->nc_nalloc)
486 grid->nc_nalloc = over_alloc_large(nc_max);
487 srenew(grid->nsubc, grid->nc_nalloc);
488 srenew(grid->bbcz, grid->nc_nalloc*NNBSBB_D);
490 sfree_aligned(grid->bb);
491 /* This snew also zeros the contents, this avoid possible
492 * floating exceptions in SIMD with the unused bb elements.
496 snew_aligned(grid->bb, grid->nc_nalloc, 16);
503 pbb_nalloc = grid->nc_nalloc*GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX;
504 snew_aligned(grid->pbb, pbb_nalloc, 16);
506 snew_aligned(grid->bb, grid->nc_nalloc*GPU_NSUBCELL, 16);
512 if (grid->na_cj == grid->na_c)
514 grid->bbj = grid->bb;
518 sfree_aligned(grid->bbj);
519 snew_aligned(grid->bbj, grid->nc_nalloc*grid->na_c/grid->na_cj, 16);
523 srenew(grid->flags, grid->nc_nalloc);
526 copy_rvec(corner0, grid->c0);
527 copy_rvec(corner1, grid->c1);
532 /* We need to sort paricles in grid columns on z-coordinate.
533 * As particle are very often distributed homogeneously, we a sorting
534 * algorithm similar to pigeonhole sort. We multiply the z-coordinate
535 * by a factor, cast to an int and try to store in that hole. If the hole
536 * is full, we move this or another particle. A second pass is needed to make
537 * contiguous elements. SORT_GRID_OVERSIZE is the ratio of holes to particles.
538 * 4 is the optimal value for homogeneous particle distribution and allows
539 * for an O(#particles) sort up till distributions were all particles are
540 * concentrated in 1/4 of the space. No NlogN fallback is implemented,
541 * as it can be expensive to detect imhomogeneous particle distributions.
542 * SGSF is the maximum ratio of holes used, in the worst case all particles
543 * end up in the last hole and we need #particles extra holes at the end.
545 #define SORT_GRID_OVERSIZE 4
546 #define SGSF (SORT_GRID_OVERSIZE + 1)
548 /* Sort particle index a on coordinates x along dim.
549 * Backwards tells if we want decreasing iso increasing coordinates.
550 * h0 is the minimum of the coordinate range.
551 * invh is the 1/length of the sorting range.
552 * n_per_h (>=n) is the expected average number of particles per 1/invh
553 * sort is the sorting work array.
554 * sort should have a size of at least n_per_h*SORT_GRID_OVERSIZE + n,
555 * or easier, allocate at least n*SGSF elements.
557 static void sort_atoms(int dim, gmx_bool Backwards,
558 int gmx_unused dd_zone,
559 int *a, int n, rvec *x,
560 real h0, real invh, int n_per_h,
564 int zi, zim, zi_min, zi_max;
576 gmx_incons("n > n_per_h");
580 /* Transform the inverse range height into the inverse hole height */
581 invh *= n_per_h*SORT_GRID_OVERSIZE;
583 /* Set nsort to the maximum possible number of holes used.
584 * In worst case all n elements end up in the last bin.
586 nsort = n_per_h*SORT_GRID_OVERSIZE + n;
588 /* Determine the index range used, so we can limit it for the second pass */
592 /* Sort the particles using a simple index sort */
593 for (i = 0; i < n; i++)
595 /* The cast takes care of float-point rounding effects below zero.
596 * This code assumes particles are less than 1/SORT_GRID_OVERSIZE
597 * times the box height out of the box.
599 zi = (int)((x[a[i]][dim] - h0)*invh);
602 /* As we can have rounding effect, we use > iso >= here */
603 if (zi < 0 || (dd_zone == 0 && zi > n_per_h*SORT_GRID_OVERSIZE))
605 gmx_fatal(FARGS, "(int)((x[%d][%c]=%f - %f)*%f) = %d, not in 0 - %d*%d\n",
606 a[i], 'x'+dim, x[a[i]][dim], h0, invh, zi,
607 n_per_h, SORT_GRID_OVERSIZE);
611 /* In a non-local domain, particles communcated for bonded interactions
612 * can be far beyond the grid size, which is set by the non-bonded
613 * cut-off distance. We sort such particles into the last cell.
615 if (zi > n_per_h*SORT_GRID_OVERSIZE)
617 zi = n_per_h*SORT_GRID_OVERSIZE;
620 /* Ideally this particle should go in sort cell zi,
621 * but that might already be in use,
622 * in that case find the first empty cell higher up
627 zi_min = min(zi_min, zi);
628 zi_max = max(zi_max, zi);
632 /* We have multiple atoms in the same sorting slot.
633 * Sort on real z for minimal bounding box size.
634 * There is an extra check for identical z to ensure
635 * well-defined output order, independent of input order
636 * to ensure binary reproducibility after restarts.
638 while (sort[zi] >= 0 && ( x[a[i]][dim] > x[sort[zi]][dim] ||
639 (x[a[i]][dim] == x[sort[zi]][dim] &&
647 /* Shift all elements by one slot until we find an empty slot */
650 while (sort[zim] >= 0)
658 zi_max = max(zi_max, zim);
661 zi_max = max(zi_max, zi);
668 for (zi = 0; zi < nsort; zi++)
679 for (zi = zi_max; zi >= zi_min; zi--)
690 gmx_incons("Lost particles while sorting");
695 #define R2F_D(x) ((float)((x) >= 0 ? ((1-GMX_FLOAT_EPS)*(x)) : ((1+GMX_FLOAT_EPS)*(x))))
696 #define R2F_U(x) ((float)((x) >= 0 ? ((1+GMX_FLOAT_EPS)*(x)) : ((1-GMX_FLOAT_EPS)*(x))))
702 /* Coordinate order x,y,z, bb order xyz0 */
703 static void calc_bounding_box(int na, int stride, const real *x, nbnxn_bb_t *bb)
706 real xl, xh, yl, yh, zl, zh;
716 for (j = 1; j < na; j++)
718 xl = min(xl, x[i+XX]);
719 xh = max(xh, x[i+XX]);
720 yl = min(yl, x[i+YY]);
721 yh = max(yh, x[i+YY]);
722 zl = min(zl, x[i+ZZ]);
723 zh = max(zh, x[i+ZZ]);
726 /* Note: possible double to float conversion here */
727 bb->lower[BB_X] = R2F_D(xl);
728 bb->lower[BB_Y] = R2F_D(yl);
729 bb->lower[BB_Z] = R2F_D(zl);
730 bb->upper[BB_X] = R2F_U(xh);
731 bb->upper[BB_Y] = R2F_U(yh);
732 bb->upper[BB_Z] = R2F_U(zh);
735 /* Packed coordinates, bb order xyz0 */
736 static void calc_bounding_box_x_x4(int na, const real *x, nbnxn_bb_t *bb)
739 real xl, xh, yl, yh, zl, zh;
747 for (j = 1; j < na; j++)
749 xl = min(xl, x[j+XX*PACK_X4]);
750 xh = max(xh, x[j+XX*PACK_X4]);
751 yl = min(yl, x[j+YY*PACK_X4]);
752 yh = max(yh, x[j+YY*PACK_X4]);
753 zl = min(zl, x[j+ZZ*PACK_X4]);
754 zh = max(zh, x[j+ZZ*PACK_X4]);
756 /* Note: possible double to float conversion here */
757 bb->lower[BB_X] = R2F_D(xl);
758 bb->lower[BB_Y] = R2F_D(yl);
759 bb->lower[BB_Z] = R2F_D(zl);
760 bb->upper[BB_X] = R2F_U(xh);
761 bb->upper[BB_Y] = R2F_U(yh);
762 bb->upper[BB_Z] = R2F_U(zh);
765 /* Packed coordinates, bb order xyz0 */
766 static void calc_bounding_box_x_x8(int na, const real *x, nbnxn_bb_t *bb)
769 real xl, xh, yl, yh, zl, zh;
777 for (j = 1; j < na; j++)
779 xl = min(xl, x[j+XX*PACK_X8]);
780 xh = max(xh, x[j+XX*PACK_X8]);
781 yl = min(yl, x[j+YY*PACK_X8]);
782 yh = max(yh, x[j+YY*PACK_X8]);
783 zl = min(zl, x[j+ZZ*PACK_X8]);
784 zh = max(zh, x[j+ZZ*PACK_X8]);
786 /* Note: possible double to float conversion here */
787 bb->lower[BB_X] = R2F_D(xl);
788 bb->lower[BB_Y] = R2F_D(yl);
789 bb->lower[BB_Z] = R2F_D(zl);
790 bb->upper[BB_X] = R2F_U(xh);
791 bb->upper[BB_Y] = R2F_U(yh);
792 bb->upper[BB_Z] = R2F_U(zh);
795 /* Packed coordinates, bb order xyz0 */
796 static void calc_bounding_box_x_x4_halves(int na, const real *x,
797 nbnxn_bb_t *bb, nbnxn_bb_t *bbj)
799 calc_bounding_box_x_x4(min(na, 2), x, bbj);
803 calc_bounding_box_x_x4(min(na-2, 2), x+(PACK_X4>>1), bbj+1);
807 /* Set the "empty" bounding box to the same as the first one,
808 * so we don't need to treat special cases in the rest of the code.
810 #ifdef NBNXN_SEARCH_BB_SIMD4
811 gmx_simd4_store_f(&bbj[1].lower[0], gmx_simd4_load_f(&bbj[0].lower[0]));
812 gmx_simd4_store_f(&bbj[1].upper[0], gmx_simd4_load_f(&bbj[0].upper[0]));
818 #ifdef NBNXN_SEARCH_BB_SIMD4
819 gmx_simd4_store_f(&bb->lower[0],
820 gmx_simd4_min_f(gmx_simd4_load_f(&bbj[0].lower[0]),
821 gmx_simd4_load_f(&bbj[1].lower[0])));
822 gmx_simd4_store_f(&bb->upper[0],
823 gmx_simd4_max_f(gmx_simd4_load_f(&bbj[0].upper[0]),
824 gmx_simd4_load_f(&bbj[1].upper[0])));
829 for (i = 0; i < NNBSBB_C; i++)
831 bb->lower[i] = min(bbj[0].lower[i], bbj[1].lower[i]);
832 bb->upper[i] = max(bbj[0].upper[i], bbj[1].upper[i]);
838 #ifdef NBNXN_SEARCH_BB_SIMD4
840 /* Coordinate order xyz, bb order xxxxyyyyzzzz */
841 static void calc_bounding_box_xxxx(int na, int stride, const real *x, float *bb)
844 real xl, xh, yl, yh, zl, zh;
854 for (j = 1; j < na; j++)
856 xl = min(xl, x[i+XX]);
857 xh = max(xh, x[i+XX]);
858 yl = min(yl, x[i+YY]);
859 yh = max(yh, x[i+YY]);
860 zl = min(zl, x[i+ZZ]);
861 zh = max(zh, x[i+ZZ]);
864 /* Note: possible double to float conversion here */
865 bb[0*STRIDE_PBB] = R2F_D(xl);
866 bb[1*STRIDE_PBB] = R2F_D(yl);
867 bb[2*STRIDE_PBB] = R2F_D(zl);
868 bb[3*STRIDE_PBB] = R2F_U(xh);
869 bb[4*STRIDE_PBB] = R2F_U(yh);
870 bb[5*STRIDE_PBB] = R2F_U(zh);
873 #endif /* NBNXN_SEARCH_BB_SIMD4 */
875 #ifdef NBNXN_SEARCH_SIMD4_FLOAT_X_BB
877 /* Coordinate order xyz?, bb order xyz0 */
878 static void calc_bounding_box_simd4(int na, const float *x, nbnxn_bb_t *bb)
880 gmx_simd4_float_t bb_0_S, bb_1_S;
881 gmx_simd4_float_t x_S;
885 bb_0_S = gmx_simd4_load_f(x);
888 for (i = 1; i < na; i++)
890 x_S = gmx_simd4_load_f(x+i*NNBSBB_C);
891 bb_0_S = gmx_simd4_min_f(bb_0_S, x_S);
892 bb_1_S = gmx_simd4_max_f(bb_1_S, x_S);
895 gmx_simd4_store_f(&bb->lower[0], bb_0_S);
896 gmx_simd4_store_f(&bb->upper[0], bb_1_S);
899 /* Coordinate order xyz?, bb order xxxxyyyyzzzz */
900 static void calc_bounding_box_xxxx_simd4(int na, const float *x,
901 nbnxn_bb_t *bb_work_aligned,
904 calc_bounding_box_simd4(na, x, bb_work_aligned);
906 bb[0*STRIDE_PBB] = bb_work_aligned->lower[BB_X];
907 bb[1*STRIDE_PBB] = bb_work_aligned->lower[BB_Y];
908 bb[2*STRIDE_PBB] = bb_work_aligned->lower[BB_Z];
909 bb[3*STRIDE_PBB] = bb_work_aligned->upper[BB_X];
910 bb[4*STRIDE_PBB] = bb_work_aligned->upper[BB_Y];
911 bb[5*STRIDE_PBB] = bb_work_aligned->upper[BB_Z];
914 #endif /* NBNXN_SEARCH_SIMD4_FLOAT_X_BB */
917 /* Combines pairs of consecutive bounding boxes */
918 static void combine_bounding_box_pairs(nbnxn_grid_t *grid, const nbnxn_bb_t *bb)
920 int i, j, sc2, nc2, c2;
922 for (i = 0; i < grid->ncx*grid->ncy; i++)
924 /* Starting bb in a column is expected to be 2-aligned */
925 sc2 = grid->cxy_ind[i]>>1;
926 /* For odd numbers skip the last bb here */
927 nc2 = (grid->cxy_na[i]+3)>>(2+1);
928 for (c2 = sc2; c2 < sc2+nc2; c2++)
930 #ifdef NBNXN_SEARCH_BB_SIMD4
931 gmx_simd4_float_t min_S, max_S;
933 min_S = gmx_simd4_min_f(gmx_simd4_load_f(&bb[c2*2+0].lower[0]),
934 gmx_simd4_load_f(&bb[c2*2+1].lower[0]));
935 max_S = gmx_simd4_max_f(gmx_simd4_load_f(&bb[c2*2+0].upper[0]),
936 gmx_simd4_load_f(&bb[c2*2+1].upper[0]));
937 gmx_simd4_store_f(&grid->bbj[c2].lower[0], min_S);
938 gmx_simd4_store_f(&grid->bbj[c2].upper[0], max_S);
940 for (j = 0; j < NNBSBB_C; j++)
942 grid->bbj[c2].lower[j] = min(bb[c2*2+0].lower[j],
943 bb[c2*2+1].lower[j]);
944 grid->bbj[c2].upper[j] = max(bb[c2*2+0].upper[j],
945 bb[c2*2+1].upper[j]);
949 if (((grid->cxy_na[i]+3)>>2) & 1)
951 /* The bb count in this column is odd: duplicate the last bb */
952 for (j = 0; j < NNBSBB_C; j++)
954 grid->bbj[c2].lower[j] = bb[c2*2].lower[j];
955 grid->bbj[c2].upper[j] = bb[c2*2].upper[j];
962 /* Prints the average bb size, used for debug output */
963 static void print_bbsizes_simple(FILE *fp,
964 const nbnxn_search_t nbs,
965 const nbnxn_grid_t *grid)
971 for (c = 0; c < grid->nc; c++)
973 for (d = 0; d < DIM; d++)
975 ba[d] += grid->bb[c].upper[d] - grid->bb[c].lower[d];
978 dsvmul(1.0/grid->nc, ba, ba);
980 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
981 nbs->box[XX][XX]/grid->ncx,
982 nbs->box[YY][YY]/grid->ncy,
983 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/grid->nc,
984 ba[XX], ba[YY], ba[ZZ],
985 ba[XX]*grid->ncx/nbs->box[XX][XX],
986 ba[YY]*grid->ncy/nbs->box[YY][YY],
987 ba[ZZ]*grid->nc/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
990 /* Prints the average bb size, used for debug output */
991 static void print_bbsizes_supersub(FILE *fp,
992 const nbnxn_search_t nbs,
993 const nbnxn_grid_t *grid)
1000 for (c = 0; c < grid->nc; c++)
1003 for (s = 0; s < grid->nsubc[c]; s += STRIDE_PBB)
1007 cs_w = (c*GPU_NSUBCELL + s)/STRIDE_PBB;
1008 for (i = 0; i < STRIDE_PBB; i++)
1010 for (d = 0; d < DIM; d++)
1013 grid->pbb[cs_w*NNBSBB_XXXX+(DIM+d)*STRIDE_PBB+i] -
1014 grid->pbb[cs_w*NNBSBB_XXXX+ d *STRIDE_PBB+i];
1019 for (s = 0; s < grid->nsubc[c]; s++)
1023 cs = c*GPU_NSUBCELL + s;
1024 for (d = 0; d < DIM; d++)
1026 ba[d] += grid->bb[cs].upper[d] - grid->bb[cs].lower[d];
1030 ns += grid->nsubc[c];
1032 dsvmul(1.0/ns, ba, ba);
1034 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
1035 nbs->box[XX][XX]/(grid->ncx*GPU_NSUBCELL_X),
1036 nbs->box[YY][YY]/(grid->ncy*GPU_NSUBCELL_Y),
1037 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z),
1038 ba[XX], ba[YY], ba[ZZ],
1039 ba[XX]*grid->ncx*GPU_NSUBCELL_X/nbs->box[XX][XX],
1040 ba[YY]*grid->ncy*GPU_NSUBCELL_Y/nbs->box[YY][YY],
1041 ba[ZZ]*grid->nc*GPU_NSUBCELL_Z/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
1044 /* Potentially sorts atoms on LJ coefficients !=0 and ==0.
1045 * Also sets interaction flags.
1047 void sort_on_lj(int na_c,
1048 int a0, int a1, const int *atinfo,
1052 int subc, s, a, n1, n2, a_lj_max, i, j;
1053 int sort1[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1054 int sort2[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1060 for (s = a0; s < a1; s += na_c)
1062 /* Make lists for this (sub-)cell on atoms with and without LJ */
1067 for (a = s; a < min(s+na_c, a1); a++)
1069 haveQ = haveQ || GET_CGINFO_HAS_Q(atinfo[order[a]]);
1071 if (GET_CGINFO_HAS_VDW(atinfo[order[a]]))
1073 sort1[n1++] = order[a];
1078 sort2[n2++] = order[a];
1082 /* If we don't have atom with LJ, there's nothing to sort */
1085 *flags |= NBNXN_CI_DO_LJ(subc);
1089 /* Only sort when strictly necessary. Ordering particles
1090 * Ordering particles can lead to less accurate summation
1091 * due to rounding, both for LJ and Coulomb interactions.
1093 if (2*(a_lj_max - s) >= na_c)
1095 for (i = 0; i < n1; i++)
1097 order[a0+i] = sort1[i];
1099 for (j = 0; j < n2; j++)
1101 order[a0+n1+j] = sort2[j];
1105 *flags |= NBNXN_CI_HALF_LJ(subc);
1110 *flags |= NBNXN_CI_DO_COUL(subc);
1116 /* Fill a pair search cell with atoms.
1117 * Potentially sorts atoms and sets the interaction flags.
1119 void fill_cell(const nbnxn_search_t nbs,
1121 nbnxn_atomdata_t *nbat,
1125 int sx, int sy, int sz,
1126 nbnxn_bb_t gmx_unused *bb_work_aligned)
1139 sort_on_lj(grid->na_c, a0, a1, atinfo, nbs->a,
1140 grid->flags+(a0>>grid->na_c_2log)-grid->cell0);
1143 /* Now we have sorted the atoms, set the cell indices */
1144 for (a = a0; a < a1; a++)
1146 nbs->cell[nbs->a[a]] = a;
1149 copy_rvec_to_nbat_real(nbs->a+a0, a1-a0, grid->na_c, x,
1150 nbat->XFormat, nbat->x, a0,
1153 if (nbat->XFormat == nbatX4)
1155 /* Store the bounding boxes as xyz.xyz. */
1156 offset = (a0 - grid->cell0*grid->na_sc) >> grid->na_c_2log;
1157 bb_ptr = grid->bb + offset;
1159 #if defined GMX_NBNXN_SIMD && GMX_SIMD_REAL_WIDTH == 2
1160 if (2*grid->na_cj == grid->na_c)
1162 calc_bounding_box_x_x4_halves(na, nbat->x+X4_IND_A(a0), bb_ptr,
1163 grid->bbj+offset*2);
1168 calc_bounding_box_x_x4(na, nbat->x+X4_IND_A(a0), bb_ptr);
1171 else if (nbat->XFormat == nbatX8)
1173 /* Store the bounding boxes as xyz.xyz. */
1174 offset = (a0 - grid->cell0*grid->na_sc) >> grid->na_c_2log;
1175 bb_ptr = grid->bb + offset;
1177 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(a0), bb_ptr);
1180 else if (!grid->bSimple)
1182 /* Store the bounding boxes in a format convenient
1183 * for SIMD4 calculations: xxxxyyyyzzzz...
1187 ((a0-grid->cell0*grid->na_sc)>>(grid->na_c_2log+STRIDE_PBB_2LOG))*NNBSBB_XXXX +
1188 (((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log) & (STRIDE_PBB-1));
1190 #ifdef NBNXN_SEARCH_SIMD4_FLOAT_X_BB
1191 if (nbat->XFormat == nbatXYZQ)
1193 calc_bounding_box_xxxx_simd4(na, nbat->x+a0*nbat->xstride,
1194 bb_work_aligned, pbb_ptr);
1199 calc_bounding_box_xxxx(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1204 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1206 pbb_ptr[0*STRIDE_PBB], pbb_ptr[3*STRIDE_PBB],
1207 pbb_ptr[1*STRIDE_PBB], pbb_ptr[4*STRIDE_PBB],
1208 pbb_ptr[2*STRIDE_PBB], pbb_ptr[5*STRIDE_PBB]);
1214 /* Store the bounding boxes as xyz.xyz. */
1215 bb_ptr = grid->bb+((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log);
1217 calc_bounding_box(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1223 bbo = (a0 - grid->cell0*grid->na_sc)/grid->na_c;
1224 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1226 grid->bb[bbo].lower[BB_X],
1227 grid->bb[bbo].lower[BB_Y],
1228 grid->bb[bbo].lower[BB_Z],
1229 grid->bb[bbo].upper[BB_X],
1230 grid->bb[bbo].upper[BB_Y],
1231 grid->bb[bbo].upper[BB_Z]);
1236 /* Spatially sort the atoms within one grid column */
1237 static void sort_columns_simple(const nbnxn_search_t nbs,
1243 nbnxn_atomdata_t *nbat,
1244 int cxy_start, int cxy_end,
1248 int cx, cy, cz, ncz, cfilled, c;
1249 int na, ash, ind, a;
1254 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1255 grid->cell0, cxy_start, cxy_end, a0, a1);
1258 /* Sort the atoms within each x,y column in 3 dimensions */
1259 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1262 cy = cxy - cx*grid->ncy;
1264 na = grid->cxy_na[cxy];
1265 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1266 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1268 /* Sort the atoms within each x,y column on z coordinate */
1269 sort_atoms(ZZ, FALSE, dd_zone,
1272 1.0/nbs->box[ZZ][ZZ], ncz*grid->na_sc,
1275 /* Fill the ncz cells in this column */
1276 cfilled = grid->cxy_ind[cxy];
1277 for (cz = 0; cz < ncz; cz++)
1279 c = grid->cxy_ind[cxy] + cz;
1281 ash_c = ash + cz*grid->na_sc;
1282 na_c = min(grid->na_sc, na-(ash_c-ash));
1284 fill_cell(nbs, grid, nbat,
1285 ash_c, ash_c+na_c, atinfo, x,
1286 grid->na_sc*cx + (dd_zone >> 2),
1287 grid->na_sc*cy + (dd_zone & 3),
1291 /* This copy to bbcz is not really necessary.
1292 * But it allows to use the same grid search code
1293 * for the simple and supersub cell setups.
1299 grid->bbcz[c*NNBSBB_D ] = grid->bb[cfilled].lower[BB_Z];
1300 grid->bbcz[c*NNBSBB_D+1] = grid->bb[cfilled].upper[BB_Z];
1303 /* Set the unused atom indices to -1 */
1304 for (ind = na; ind < ncz*grid->na_sc; ind++)
1306 nbs->a[ash+ind] = -1;
1311 /* Spatially sort the atoms within one grid column */
1312 static void sort_columns_supersub(const nbnxn_search_t nbs,
1318 nbnxn_atomdata_t *nbat,
1319 int cxy_start, int cxy_end,
1323 int cx, cy, cz = -1, c = -1, ncz;
1324 int na, ash, na_c, ind, a;
1325 int subdiv_z, sub_z, na_z, ash_z;
1326 int subdiv_y, sub_y, na_y, ash_y;
1327 int subdiv_x, sub_x, na_x, ash_x;
1329 /* cppcheck-suppress unassignedVariable */
1330 nbnxn_bb_t bb_work_array[2], *bb_work_aligned;
1332 bb_work_aligned = (nbnxn_bb_t *)(((size_t)(bb_work_array+1)) & (~((size_t)15)));
1336 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1337 grid->cell0, cxy_start, cxy_end, a0, a1);
1340 subdiv_x = grid->na_c;
1341 subdiv_y = GPU_NSUBCELL_X*subdiv_x;
1342 subdiv_z = GPU_NSUBCELL_Y*subdiv_y;
1344 /* Sort the atoms within each x,y column in 3 dimensions */
1345 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1348 cy = cxy - cx*grid->ncy;
1350 na = grid->cxy_na[cxy];
1351 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1352 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1354 /* Sort the atoms within each x,y column on z coordinate */
1355 sort_atoms(ZZ, FALSE, dd_zone,
1358 1.0/nbs->box[ZZ][ZZ], ncz*grid->na_sc,
1361 /* This loop goes over the supercells and subcells along z at once */
1362 for (sub_z = 0; sub_z < ncz*GPU_NSUBCELL_Z; sub_z++)
1364 ash_z = ash + sub_z*subdiv_z;
1365 na_z = min(subdiv_z, na-(ash_z-ash));
1367 /* We have already sorted on z */
1369 if (sub_z % GPU_NSUBCELL_Z == 0)
1371 cz = sub_z/GPU_NSUBCELL_Z;
1372 c = grid->cxy_ind[cxy] + cz;
1374 /* The number of atoms in this supercell */
1375 na_c = min(grid->na_sc, na-(ash_z-ash));
1377 grid->nsubc[c] = min(GPU_NSUBCELL, (na_c+grid->na_c-1)/grid->na_c);
1379 /* Store the z-boundaries of the super cell */
1380 grid->bbcz[c*NNBSBB_D ] = x[nbs->a[ash_z]][ZZ];
1381 grid->bbcz[c*NNBSBB_D+1] = x[nbs->a[ash_z+na_c-1]][ZZ];
1384 #if GPU_NSUBCELL_Y > 1
1385 /* Sort the atoms along y */
1386 sort_atoms(YY, (sub_z & 1), dd_zone,
1387 nbs->a+ash_z, na_z, x,
1388 grid->c0[YY]+cy*grid->sy,
1389 grid->inv_sy, subdiv_z,
1393 for (sub_y = 0; sub_y < GPU_NSUBCELL_Y; sub_y++)
1395 ash_y = ash_z + sub_y*subdiv_y;
1396 na_y = min(subdiv_y, na-(ash_y-ash));
1398 #if GPU_NSUBCELL_X > 1
1399 /* Sort the atoms along x */
1400 sort_atoms(XX, ((cz*GPU_NSUBCELL_Y + sub_y) & 1), dd_zone,
1401 nbs->a+ash_y, na_y, x,
1402 grid->c0[XX]+cx*grid->sx,
1403 grid->inv_sx, subdiv_y,
1407 for (sub_x = 0; sub_x < GPU_NSUBCELL_X; sub_x++)
1409 ash_x = ash_y + sub_x*subdiv_x;
1410 na_x = min(subdiv_x, na-(ash_x-ash));
1412 fill_cell(nbs, grid, nbat,
1413 ash_x, ash_x+na_x, atinfo, x,
1414 grid->na_c*(cx*GPU_NSUBCELL_X+sub_x) + (dd_zone >> 2),
1415 grid->na_c*(cy*GPU_NSUBCELL_Y+sub_y) + (dd_zone & 3),
1422 /* Set the unused atom indices to -1 */
1423 for (ind = na; ind < ncz*grid->na_sc; ind++)
1425 nbs->a[ash+ind] = -1;
1430 /* Determine in which grid column atoms should go */
1431 static void calc_column_indices(nbnxn_grid_t *grid,
1434 int dd_zone, const int *move,
1435 int thread, int nthread,
1442 /* We add one extra cell for particles which moved during DD */
1443 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1448 n0 = a0 + (int)((thread+0)*(a1 - a0))/nthread;
1449 n1 = a0 + (int)((thread+1)*(a1 - a0))/nthread;
1453 for (i = n0; i < n1; i++)
1455 if (move == NULL || move[i] >= 0)
1457 /* We need to be careful with rounding,
1458 * particles might be a few bits outside the local zone.
1459 * The int cast takes care of the lower bound,
1460 * we will explicitly take care of the upper bound.
1462 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1463 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1466 if (cx < 0 || cx > grid->ncx ||
1467 cy < 0 || cy > grid->ncy)
1470 "grid cell cx %d cy %d out of range (max %d %d)\n"
1471 "atom %f %f %f, grid->c0 %f %f",
1472 cx, cy, grid->ncx, grid->ncy,
1473 x[i][XX], x[i][YY], x[i][ZZ], grid->c0[XX], grid->c0[YY]);
1476 /* Take care of potential rouding issues */
1477 cx = min(cx, grid->ncx - 1);
1478 cy = min(cy, grid->ncy - 1);
1480 /* For the moment cell will contain only the, grid local,
1481 * x and y indices, not z.
1483 cell[i] = cx*grid->ncy + cy;
1487 /* Put this moved particle after the end of the grid,
1488 * so we can process it later without using conditionals.
1490 cell[i] = grid->ncx*grid->ncy;
1499 for (i = n0; i < n1; i++)
1501 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1502 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1504 /* For non-home zones there could be particles outside
1505 * the non-bonded cut-off range, which have been communicated
1506 * for bonded interactions only. For the result it doesn't
1507 * matter where these end up on the grid. For performance
1508 * we put them in an extra row at the border.
1511 cx = min(cx, grid->ncx - 1);
1513 cy = min(cy, grid->ncy - 1);
1515 /* For the moment cell will contain only the, grid local,
1516 * x and y indices, not z.
1518 cell[i] = cx*grid->ncy + cy;
1525 /* Determine in which grid cells the atoms should go */
1526 static void calc_cell_indices(const nbnxn_search_t nbs,
1533 nbnxn_atomdata_t *nbat)
1536 int cx, cy, cxy, ncz_max, ncz;
1537 int nthread, thread;
1538 int *cxy_na, cxy_na_i;
1540 nthread = gmx_omp_nthreads_get(emntPairsearch);
1542 #pragma omp parallel for num_threads(nthread) schedule(static)
1543 for (thread = 0; thread < nthread; thread++)
1545 calc_column_indices(grid, a0, a1, x, dd_zone, move, thread, nthread,
1546 nbs->cell, nbs->work[thread].cxy_na);
1549 /* Make the cell index as a function of x and y */
1552 grid->cxy_ind[0] = 0;
1553 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1555 /* We set ncz_max at the beginning of the loop iso at the end
1556 * to skip i=grid->ncx*grid->ncy which are moved particles
1557 * that do not need to be ordered on the grid.
1563 cxy_na_i = nbs->work[0].cxy_na[i];
1564 for (thread = 1; thread < nthread; thread++)
1566 cxy_na_i += nbs->work[thread].cxy_na[i];
1568 ncz = (cxy_na_i + grid->na_sc - 1)/grid->na_sc;
1569 if (nbat->XFormat == nbatX8)
1571 /* Make the number of cell a multiple of 2 */
1572 ncz = (ncz + 1) & ~1;
1574 grid->cxy_ind[i+1] = grid->cxy_ind[i] + ncz;
1575 /* Clear cxy_na, so we can reuse the array below */
1576 grid->cxy_na[i] = 0;
1578 grid->nc = grid->cxy_ind[grid->ncx*grid->ncy] - grid->cxy_ind[0];
1580 nbat->natoms = (grid->cell0 + grid->nc)*grid->na_sc;
1584 fprintf(debug, "ns na_sc %d na_c %d super-cells: %d x %d y %d z %.1f maxz %d\n",
1585 grid->na_sc, grid->na_c, grid->nc,
1586 grid->ncx, grid->ncy, grid->nc/((double)(grid->ncx*grid->ncy)),
1591 for (cy = 0; cy < grid->ncy; cy++)
1593 for (cx = 0; cx < grid->ncx; cx++)
1595 fprintf(debug, " %2d", grid->cxy_ind[i+1]-grid->cxy_ind[i]);
1598 fprintf(debug, "\n");
1603 /* Make sure the work array for sorting is large enough */
1604 if (ncz_max*grid->na_sc*SGSF > nbs->work[0].sort_work_nalloc)
1606 for (thread = 0; thread < nbs->nthread_max; thread++)
1608 nbs->work[thread].sort_work_nalloc =
1609 over_alloc_large(ncz_max*grid->na_sc*SGSF);
1610 srenew(nbs->work[thread].sort_work,
1611 nbs->work[thread].sort_work_nalloc);
1612 /* When not in use, all elements should be -1 */
1613 for (i = 0; i < nbs->work[thread].sort_work_nalloc; i++)
1615 nbs->work[thread].sort_work[i] = -1;
1620 /* Now we know the dimensions we can fill the grid.
1621 * This is the first, unsorted fill. We sort the columns after this.
1623 for (i = a0; i < a1; i++)
1625 /* At this point nbs->cell contains the local grid x,y indices */
1627 nbs->a[(grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc + grid->cxy_na[cxy]++] = i;
1632 /* Set the cell indices for the moved particles */
1633 n0 = grid->nc*grid->na_sc;
1634 n1 = grid->nc*grid->na_sc+grid->cxy_na[grid->ncx*grid->ncy];
1637 for (i = n0; i < n1; i++)
1639 nbs->cell[nbs->a[i]] = i;
1644 /* Sort the super-cell columns along z into the sub-cells. */
1645 #pragma omp parallel for num_threads(nbs->nthread_max) schedule(static)
1646 for (thread = 0; thread < nbs->nthread_max; thread++)
1650 sort_columns_simple(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1651 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1652 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1653 nbs->work[thread].sort_work);
1657 sort_columns_supersub(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1658 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1659 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1660 nbs->work[thread].sort_work);
1664 if (grid->bSimple && nbat->XFormat == nbatX8)
1666 combine_bounding_box_pairs(grid, grid->bb);
1671 grid->nsubc_tot = 0;
1672 for (i = 0; i < grid->nc; i++)
1674 grid->nsubc_tot += grid->nsubc[i];
1682 print_bbsizes_simple(debug, nbs, grid);
1686 fprintf(debug, "ns non-zero sub-cells: %d average atoms %.2f\n",
1687 grid->nsubc_tot, (a1-a0)/(double)grid->nsubc_tot);
1689 print_bbsizes_supersub(debug, nbs, grid);
1694 static void init_buffer_flags(nbnxn_buffer_flags_t *flags,
1699 flags->nflag = (natoms + NBNXN_BUFFERFLAG_SIZE - 1)/NBNXN_BUFFERFLAG_SIZE;
1700 if (flags->nflag > flags->flag_nalloc)
1702 flags->flag_nalloc = over_alloc_large(flags->nflag);
1703 srenew(flags->flag, flags->flag_nalloc);
1705 for (b = 0; b < flags->nflag; b++)
1711 /* Sets up a grid and puts the atoms on the grid.
1712 * This function only operates on one domain of the domain decompostion.
1713 * Note that without domain decomposition there is only one domain.
1715 void nbnxn_put_on_grid(nbnxn_search_t nbs,
1716 int ePBC, matrix box,
1718 rvec corner0, rvec corner1,
1723 int nmoved, int *move,
1725 nbnxn_atomdata_t *nbat)
1729 int nc_max_grid, nc_max;
1731 grid = &nbs->grid[dd_zone];
1733 nbs_cycle_start(&nbs->cc[enbsCCgrid]);
1735 grid->bSimple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
1737 grid->na_c = nbnxn_kernel_to_ci_size(nb_kernel_type);
1738 grid->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
1739 grid->na_sc = (grid->bSimple ? 1 : GPU_NSUBCELL)*grid->na_c;
1740 grid->na_c_2log = get_2log(grid->na_c);
1742 nbat->na_c = grid->na_c;
1751 (nbs->grid[dd_zone-1].cell0 + nbs->grid[dd_zone-1].nc)*
1752 nbs->grid[dd_zone-1].na_sc/grid->na_sc;
1760 copy_mat(box, nbs->box);
1762 if (atom_density >= 0)
1764 grid->atom_density = atom_density;
1768 grid->atom_density = grid_atom_density(n-nmoved, corner0, corner1);
1773 nbs->natoms_local = a1 - nmoved;
1774 /* We assume that nbnxn_put_on_grid is called first
1775 * for the local atoms (dd_zone=0).
1777 nbs->natoms_nonlocal = a1 - nmoved;
1781 nbs->natoms_nonlocal = max(nbs->natoms_nonlocal, a1);
1784 nc_max_grid = set_grid_size_xy(nbs, grid,
1785 dd_zone, n-nmoved, corner0, corner1,
1786 nbs->grid[0].atom_density);
1788 nc_max = grid->cell0 + nc_max_grid;
1790 if (a1 > nbs->cell_nalloc)
1792 nbs->cell_nalloc = over_alloc_large(a1);
1793 srenew(nbs->cell, nbs->cell_nalloc);
1796 /* To avoid conditionals we store the moved particles at the end of a,
1797 * make sure we have enough space.
1799 if (nc_max*grid->na_sc + nmoved > nbs->a_nalloc)
1801 nbs->a_nalloc = over_alloc_large(nc_max*grid->na_sc + nmoved);
1802 srenew(nbs->a, nbs->a_nalloc);
1805 /* We need padding up to a multiple of the buffer flag size: simply add */
1806 if (nc_max*grid->na_sc + NBNXN_BUFFERFLAG_SIZE > nbat->nalloc)
1808 nbnxn_atomdata_realloc(nbat, nc_max*grid->na_sc+NBNXN_BUFFERFLAG_SIZE);
1811 calc_cell_indices(nbs, dd_zone, grid, a0, a1, atinfo, x, move, nbat);
1815 nbat->natoms_local = nbat->natoms;
1818 nbs_cycle_stop(&nbs->cc[enbsCCgrid]);
1821 /* Calls nbnxn_put_on_grid for all non-local domains */
1822 void nbnxn_put_on_grid_nonlocal(nbnxn_search_t nbs,
1823 const gmx_domdec_zones_t *zones,
1827 nbnxn_atomdata_t *nbat)
1832 for (zone = 1; zone < zones->n; zone++)
1834 for (d = 0; d < DIM; d++)
1836 c0[d] = zones->size[zone].bb_x0[d];
1837 c1[d] = zones->size[zone].bb_x1[d];
1840 nbnxn_put_on_grid(nbs, nbs->ePBC, NULL,
1842 zones->cg_range[zone],
1843 zones->cg_range[zone+1],
1853 /* Add simple grid type information to the local super/sub grid */
1854 void nbnxn_grid_add_simple(nbnxn_search_t nbs,
1855 nbnxn_atomdata_t *nbat)
1862 grid = &nbs->grid[0];
1866 gmx_incons("nbnxn_grid_simple called with a simple grid");
1869 ncd = grid->na_sc/NBNXN_CPU_CLUSTER_I_SIZE;
1871 if (grid->nc*ncd > grid->nc_nalloc_simple)
1873 grid->nc_nalloc_simple = over_alloc_large(grid->nc*ncd);
1874 srenew(grid->bbcz_simple, grid->nc_nalloc_simple*NNBSBB_D);
1875 srenew(grid->bb_simple, grid->nc_nalloc_simple);
1876 srenew(grid->flags_simple, grid->nc_nalloc_simple);
1879 sfree_aligned(grid->bbj);
1880 snew_aligned(grid->bbj, grid->nc_nalloc_simple/2, 16);
1884 bbcz = grid->bbcz_simple;
1885 bb = grid->bb_simple;
1887 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
1888 for (sc = 0; sc < grid->nc; sc++)
1892 for (c = 0; c < ncd; c++)
1896 na = NBNXN_CPU_CLUSTER_I_SIZE;
1898 nbat->type[tx*NBNXN_CPU_CLUSTER_I_SIZE+na-1] == nbat->ntype-1)
1905 switch (nbat->XFormat)
1908 /* PACK_X4==NBNXN_CPU_CLUSTER_I_SIZE, so this is simple */
1909 calc_bounding_box_x_x4(na, nbat->x+tx*STRIDE_P4,
1913 /* PACK_X8>NBNXN_CPU_CLUSTER_I_SIZE, more complicated */
1914 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(tx*NBNXN_CPU_CLUSTER_I_SIZE),
1918 calc_bounding_box(na, nbat->xstride,
1919 nbat->x+tx*NBNXN_CPU_CLUSTER_I_SIZE*nbat->xstride,
1923 bbcz[tx*NNBSBB_D+0] = bb[tx].lower[BB_Z];
1924 bbcz[tx*NNBSBB_D+1] = bb[tx].upper[BB_Z];
1926 /* No interaction optimization yet here */
1927 grid->flags_simple[tx] = NBNXN_CI_DO_LJ(0) | NBNXN_CI_DO_COUL(0);
1931 grid->flags_simple[tx] = 0;
1936 if (grid->bSimple && nbat->XFormat == nbatX8)
1938 combine_bounding_box_pairs(grid, grid->bb_simple);
1942 void nbnxn_get_ncells(nbnxn_search_t nbs, int *ncx, int *ncy)
1944 *ncx = nbs->grid[0].ncx;
1945 *ncy = nbs->grid[0].ncy;
1948 void nbnxn_get_atomorder(nbnxn_search_t nbs, int **a, int *n)
1950 const nbnxn_grid_t *grid;
1952 grid = &nbs->grid[0];
1954 /* Return the atom order for the home cell (index 0) */
1957 *n = grid->cxy_ind[grid->ncx*grid->ncy]*grid->na_sc;
1960 void nbnxn_set_atomorder(nbnxn_search_t nbs)
1963 int ao, cx, cy, cxy, cz, j;
1965 /* Set the atom order for the home cell (index 0) */
1966 grid = &nbs->grid[0];
1969 for (cx = 0; cx < grid->ncx; cx++)
1971 for (cy = 0; cy < grid->ncy; cy++)
1973 cxy = cx*grid->ncy + cy;
1974 j = grid->cxy_ind[cxy]*grid->na_sc;
1975 for (cz = 0; cz < grid->cxy_na[cxy]; cz++)
1986 /* Determines the cell range along one dimension that
1987 * the bounding box b0 - b1 sees.
1989 static void get_cell_range(real b0, real b1,
1990 int nc, real c0, real s, real invs,
1991 real d2, real r2, int *cf, int *cl)
1993 *cf = max((int)((b0 - c0)*invs), 0);
1995 while (*cf > 0 && d2 + sqr((b0 - c0) - (*cf-1+1)*s) < r2)
2000 *cl = min((int)((b1 - c0)*invs), nc-1);
2001 while (*cl < nc-1 && d2 + sqr((*cl+1)*s - (b1 - c0)) < r2)
2007 /* Reference code calculating the distance^2 between two bounding boxes */
2008 static float box_dist2(float bx0, float bx1, float by0,
2009 float by1, float bz0, float bz1,
2010 const nbnxn_bb_t *bb)
2013 float dl, dh, dm, dm0;
2017 dl = bx0 - bb->upper[BB_X];
2018 dh = bb->lower[BB_X] - bx1;
2023 dl = by0 - bb->upper[BB_Y];
2024 dh = bb->lower[BB_Y] - by1;
2029 dl = bz0 - bb->upper[BB_Z];
2030 dh = bb->lower[BB_Z] - bz1;
2038 /* Plain C code calculating the distance^2 between two bounding boxes */
2039 static float subc_bb_dist2(int si, const nbnxn_bb_t *bb_i_ci,
2040 int csj, const nbnxn_bb_t *bb_j_all)
2042 const nbnxn_bb_t *bb_i, *bb_j;
2044 float dl, dh, dm, dm0;
2046 bb_i = bb_i_ci + si;
2047 bb_j = bb_j_all + csj;
2051 dl = bb_i->lower[BB_X] - bb_j->upper[BB_X];
2052 dh = bb_j->lower[BB_X] - bb_i->upper[BB_X];
2057 dl = bb_i->lower[BB_Y] - bb_j->upper[BB_Y];
2058 dh = bb_j->lower[BB_Y] - bb_i->upper[BB_Y];
2063 dl = bb_i->lower[BB_Z] - bb_j->upper[BB_Z];
2064 dh = bb_j->lower[BB_Z] - bb_i->upper[BB_Z];
2072 #ifdef NBNXN_SEARCH_BB_SIMD4
2074 /* 4-wide SIMD code for bb distance for bb format xyz0 */
2075 static float subc_bb_dist2_simd4(int si, const nbnxn_bb_t *bb_i_ci,
2076 int csj, const nbnxn_bb_t *bb_j_all)
2078 gmx_simd4_float_t bb_i_S0, bb_i_S1;
2079 gmx_simd4_float_t bb_j_S0, bb_j_S1;
2080 gmx_simd4_float_t dl_S;
2081 gmx_simd4_float_t dh_S;
2082 gmx_simd4_float_t dm_S;
2083 gmx_simd4_float_t dm0_S;
2085 bb_i_S0 = gmx_simd4_load_f(&bb_i_ci[si].lower[0]);
2086 bb_i_S1 = gmx_simd4_load_f(&bb_i_ci[si].upper[0]);
2087 bb_j_S0 = gmx_simd4_load_f(&bb_j_all[csj].lower[0]);
2088 bb_j_S1 = gmx_simd4_load_f(&bb_j_all[csj].upper[0]);
2090 dl_S = gmx_simd4_sub_f(bb_i_S0, bb_j_S1);
2091 dh_S = gmx_simd4_sub_f(bb_j_S0, bb_i_S1);
2093 dm_S = gmx_simd4_max_f(dl_S, dh_S);
2094 dm0_S = gmx_simd4_max_f(dm_S, gmx_simd4_setzero_f());
2096 return gmx_simd4_dotproduct3_f(dm0_S, dm0_S);
2099 /* Calculate bb bounding distances of bb_i[si,...,si+3] and store them in d2 */
2100 #define SUBC_BB_DIST2_SIMD4_XXXX_INNER(si, bb_i, d2) \
2104 gmx_simd4_float_t dx_0, dy_0, dz_0; \
2105 gmx_simd4_float_t dx_1, dy_1, dz_1; \
2107 gmx_simd4_float_t mx, my, mz; \
2108 gmx_simd4_float_t m0x, m0y, m0z; \
2110 gmx_simd4_float_t d2x, d2y, d2z; \
2111 gmx_simd4_float_t d2s, d2t; \
2113 shi = si*NNBSBB_D*DIM; \
2115 xi_l = gmx_simd4_load_f(bb_i+shi+0*STRIDE_PBB); \
2116 yi_l = gmx_simd4_load_f(bb_i+shi+1*STRIDE_PBB); \
2117 zi_l = gmx_simd4_load_f(bb_i+shi+2*STRIDE_PBB); \
2118 xi_h = gmx_simd4_load_f(bb_i+shi+3*STRIDE_PBB); \
2119 yi_h = gmx_simd4_load_f(bb_i+shi+4*STRIDE_PBB); \
2120 zi_h = gmx_simd4_load_f(bb_i+shi+5*STRIDE_PBB); \
2122 dx_0 = gmx_simd4_sub_f(xi_l, xj_h); \
2123 dy_0 = gmx_simd4_sub_f(yi_l, yj_h); \
2124 dz_0 = gmx_simd4_sub_f(zi_l, zj_h); \
2126 dx_1 = gmx_simd4_sub_f(xj_l, xi_h); \
2127 dy_1 = gmx_simd4_sub_f(yj_l, yi_h); \
2128 dz_1 = gmx_simd4_sub_f(zj_l, zi_h); \
2130 mx = gmx_simd4_max_f(dx_0, dx_1); \
2131 my = gmx_simd4_max_f(dy_0, dy_1); \
2132 mz = gmx_simd4_max_f(dz_0, dz_1); \
2134 m0x = gmx_simd4_max_f(mx, zero); \
2135 m0y = gmx_simd4_max_f(my, zero); \
2136 m0z = gmx_simd4_max_f(mz, zero); \
2138 d2x = gmx_simd4_mul_f(m0x, m0x); \
2139 d2y = gmx_simd4_mul_f(m0y, m0y); \
2140 d2z = gmx_simd4_mul_f(m0z, m0z); \
2142 d2s = gmx_simd4_add_f(d2x, d2y); \
2143 d2t = gmx_simd4_add_f(d2s, d2z); \
2145 gmx_simd4_store_f(d2+si, d2t); \
2148 /* 4-wide SIMD code for nsi bb distances for bb format xxxxyyyyzzzz */
2149 static void subc_bb_dist2_simd4_xxxx(const float *bb_j,
2150 int nsi, const float *bb_i,
2153 gmx_simd4_float_t xj_l, yj_l, zj_l;
2154 gmx_simd4_float_t xj_h, yj_h, zj_h;
2155 gmx_simd4_float_t xi_l, yi_l, zi_l;
2156 gmx_simd4_float_t xi_h, yi_h, zi_h;
2158 gmx_simd4_float_t zero;
2160 zero = gmx_simd4_setzero_f();
2162 xj_l = gmx_simd4_set1_f(bb_j[0*STRIDE_PBB]);
2163 yj_l = gmx_simd4_set1_f(bb_j[1*STRIDE_PBB]);
2164 zj_l = gmx_simd4_set1_f(bb_j[2*STRIDE_PBB]);
2165 xj_h = gmx_simd4_set1_f(bb_j[3*STRIDE_PBB]);
2166 yj_h = gmx_simd4_set1_f(bb_j[4*STRIDE_PBB]);
2167 zj_h = gmx_simd4_set1_f(bb_j[5*STRIDE_PBB]);
2169 /* Here we "loop" over si (0,STRIDE_PBB) from 0 to nsi with step STRIDE_PBB.
2170 * But as we know the number of iterations is 1 or 2, we unroll manually.
2172 SUBC_BB_DIST2_SIMD4_XXXX_INNER(0, bb_i, d2);
2173 if (STRIDE_PBB < nsi)
2175 SUBC_BB_DIST2_SIMD4_XXXX_INNER(STRIDE_PBB, bb_i, d2);
2179 #endif /* NBNXN_SEARCH_BB_SIMD4 */
2181 /* Plain C function which determines if any atom pair between two cells
2182 * is within distance sqrt(rl2).
2184 static gmx_bool subc_in_range_x(int na_c,
2185 int si, const real *x_i,
2186 int csj, int stride, const real *x_j,
2192 for (i = 0; i < na_c; i++)
2194 i0 = (si*na_c + i)*DIM;
2195 for (j = 0; j < na_c; j++)
2197 j0 = (csj*na_c + j)*stride;
2199 d2 = sqr(x_i[i0 ] - x_j[j0 ]) +
2200 sqr(x_i[i0+1] - x_j[j0+1]) +
2201 sqr(x_i[i0+2] - x_j[j0+2]);
2213 #ifdef NBNXN_SEARCH_SIMD4_FLOAT_X_BB
2215 /* 4-wide SIMD function which determines if any atom pair between two cells,
2216 * both with 8 atoms, is within distance sqrt(rl2).
2217 * Using 8-wide AVX is not faster on Intel Sandy Bridge.
2219 static gmx_bool subc_in_range_simd4(int na_c,
2220 int si, const real *x_i,
2221 int csj, int stride, const real *x_j,
2224 gmx_simd4_real_t ix_S0, iy_S0, iz_S0;
2225 gmx_simd4_real_t ix_S1, iy_S1, iz_S1;
2227 gmx_simd4_real_t rc2_S;
2232 rc2_S = gmx_simd4_set1_r(rl2);
2234 dim_stride = NBNXN_GPU_CLUSTER_SIZE/STRIDE_PBB*DIM;
2235 ix_S0 = gmx_simd4_load_r(x_i+(si*dim_stride+0)*STRIDE_PBB);
2236 iy_S0 = gmx_simd4_load_r(x_i+(si*dim_stride+1)*STRIDE_PBB);
2237 iz_S0 = gmx_simd4_load_r(x_i+(si*dim_stride+2)*STRIDE_PBB);
2238 ix_S1 = gmx_simd4_load_r(x_i+(si*dim_stride+3)*STRIDE_PBB);
2239 iy_S1 = gmx_simd4_load_r(x_i+(si*dim_stride+4)*STRIDE_PBB);
2240 iz_S1 = gmx_simd4_load_r(x_i+(si*dim_stride+5)*STRIDE_PBB);
2242 /* We loop from the outer to the inner particles to maximize
2243 * the chance that we find a pair in range quickly and return.
2249 gmx_simd4_real_t jx0_S, jy0_S, jz0_S;
2250 gmx_simd4_real_t jx1_S, jy1_S, jz1_S;
2252 gmx_simd4_real_t dx_S0, dy_S0, dz_S0;
2253 gmx_simd4_real_t dx_S1, dy_S1, dz_S1;
2254 gmx_simd4_real_t dx_S2, dy_S2, dz_S2;
2255 gmx_simd4_real_t dx_S3, dy_S3, dz_S3;
2257 gmx_simd4_real_t rsq_S0;
2258 gmx_simd4_real_t rsq_S1;
2259 gmx_simd4_real_t rsq_S2;
2260 gmx_simd4_real_t rsq_S3;
2262 gmx_simd4_bool_t wco_S0;
2263 gmx_simd4_bool_t wco_S1;
2264 gmx_simd4_bool_t wco_S2;
2265 gmx_simd4_bool_t wco_S3;
2266 gmx_simd4_bool_t wco_any_S01, wco_any_S23, wco_any_S;
2268 jx0_S = gmx_simd4_set1_r(x_j[j0*stride+0]);
2269 jy0_S = gmx_simd4_set1_r(x_j[j0*stride+1]);
2270 jz0_S = gmx_simd4_set1_r(x_j[j0*stride+2]);
2272 jx1_S = gmx_simd4_set1_r(x_j[j1*stride+0]);
2273 jy1_S = gmx_simd4_set1_r(x_j[j1*stride+1]);
2274 jz1_S = gmx_simd4_set1_r(x_j[j1*stride+2]);
2276 /* Calculate distance */
2277 dx_S0 = gmx_simd4_sub_r(ix_S0, jx0_S);
2278 dy_S0 = gmx_simd4_sub_r(iy_S0, jy0_S);
2279 dz_S0 = gmx_simd4_sub_r(iz_S0, jz0_S);
2280 dx_S1 = gmx_simd4_sub_r(ix_S1, jx0_S);
2281 dy_S1 = gmx_simd4_sub_r(iy_S1, jy0_S);
2282 dz_S1 = gmx_simd4_sub_r(iz_S1, jz0_S);
2283 dx_S2 = gmx_simd4_sub_r(ix_S0, jx1_S);
2284 dy_S2 = gmx_simd4_sub_r(iy_S0, jy1_S);
2285 dz_S2 = gmx_simd4_sub_r(iz_S0, jz1_S);
2286 dx_S3 = gmx_simd4_sub_r(ix_S1, jx1_S);
2287 dy_S3 = gmx_simd4_sub_r(iy_S1, jy1_S);
2288 dz_S3 = gmx_simd4_sub_r(iz_S1, jz1_S);
2290 /* rsq = dx*dx+dy*dy+dz*dz */
2291 rsq_S0 = gmx_simd4_calc_rsq_r(dx_S0, dy_S0, dz_S0);
2292 rsq_S1 = gmx_simd4_calc_rsq_r(dx_S1, dy_S1, dz_S1);
2293 rsq_S2 = gmx_simd4_calc_rsq_r(dx_S2, dy_S2, dz_S2);
2294 rsq_S3 = gmx_simd4_calc_rsq_r(dx_S3, dy_S3, dz_S3);
2296 wco_S0 = gmx_simd4_cmplt_r(rsq_S0, rc2_S);
2297 wco_S1 = gmx_simd4_cmplt_r(rsq_S1, rc2_S);
2298 wco_S2 = gmx_simd4_cmplt_r(rsq_S2, rc2_S);
2299 wco_S3 = gmx_simd4_cmplt_r(rsq_S3, rc2_S);
2301 wco_any_S01 = gmx_simd4_or_b(wco_S0, wco_S1);
2302 wco_any_S23 = gmx_simd4_or_b(wco_S2, wco_S3);
2303 wco_any_S = gmx_simd4_or_b(wco_any_S01, wco_any_S23);
2305 if (gmx_simd4_anytrue_b(wco_any_S))
2319 /* Returns the j sub-cell for index cj_ind */
2320 static int nbl_cj(const nbnxn_pairlist_t *nbl, int cj_ind)
2322 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].cj[cj_ind & (NBNXN_GPU_JGROUP_SIZE - 1)];
2325 /* Returns the i-interaction mask of the j sub-cell for index cj_ind */
2326 static unsigned nbl_imask0(const nbnxn_pairlist_t *nbl, int cj_ind)
2328 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].imei[0].imask;
2331 /* Ensures there is enough space for extra extra exclusion masks */
2332 static void check_excl_space(nbnxn_pairlist_t *nbl, int extra)
2334 if (nbl->nexcl+extra > nbl->excl_nalloc)
2336 nbl->excl_nalloc = over_alloc_small(nbl->nexcl+extra);
2337 nbnxn_realloc_void((void **)&nbl->excl,
2338 nbl->nexcl*sizeof(*nbl->excl),
2339 nbl->excl_nalloc*sizeof(*nbl->excl),
2340 nbl->alloc, nbl->free);
2344 /* Ensures there is enough space for ncell extra j-cells in the list */
2345 static void check_subcell_list_space_simple(nbnxn_pairlist_t *nbl,
2350 cj_max = nbl->ncj + ncell;
2352 if (cj_max > nbl->cj_nalloc)
2354 nbl->cj_nalloc = over_alloc_small(cj_max);
2355 nbnxn_realloc_void((void **)&nbl->cj,
2356 nbl->ncj*sizeof(*nbl->cj),
2357 nbl->cj_nalloc*sizeof(*nbl->cj),
2358 nbl->alloc, nbl->free);
2362 /* Ensures there is enough space for ncell extra j-subcells in the list */
2363 static void check_subcell_list_space_supersub(nbnxn_pairlist_t *nbl,
2366 int ncj4_max, j4, j, w, t;
2369 #define WARP_SIZE 32
2371 /* We can have maximally nsupercell*GPU_NSUBCELL sj lists */
2372 /* We can store 4 j-subcell - i-supercell pairs in one struct.
2373 * since we round down, we need one extra entry.
2375 ncj4_max = ((nbl->work->cj_ind + nsupercell*GPU_NSUBCELL + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2377 if (ncj4_max > nbl->cj4_nalloc)
2379 nbl->cj4_nalloc = over_alloc_small(ncj4_max);
2380 nbnxn_realloc_void((void **)&nbl->cj4,
2381 nbl->work->cj4_init*sizeof(*nbl->cj4),
2382 nbl->cj4_nalloc*sizeof(*nbl->cj4),
2383 nbl->alloc, nbl->free);
2386 if (ncj4_max > nbl->work->cj4_init)
2388 for (j4 = nbl->work->cj4_init; j4 < ncj4_max; j4++)
2390 /* No i-subcells and no excl's in the list initially */
2391 for (w = 0; w < NWARP; w++)
2393 nbl->cj4[j4].imei[w].imask = 0U;
2394 nbl->cj4[j4].imei[w].excl_ind = 0;
2398 nbl->work->cj4_init = ncj4_max;
2402 /* Set all excl masks for one GPU warp no exclusions */
2403 static void set_no_excls(nbnxn_excl_t *excl)
2407 for (t = 0; t < WARP_SIZE; t++)
2409 /* Turn all interaction bits on */
2410 excl->pair[t] = NBNXN_INTERACTION_MASK_ALL;
2414 /* Initializes a single nbnxn_pairlist_t data structure */
2415 static void nbnxn_init_pairlist(nbnxn_pairlist_t *nbl,
2417 nbnxn_alloc_t *alloc,
2422 nbl->alloc = nbnxn_alloc_aligned;
2430 nbl->free = nbnxn_free_aligned;
2437 nbl->bSimple = bSimple;
2448 /* We need one element extra in sj, so alloc initially with 1 */
2449 nbl->cj4_nalloc = 0;
2456 nbl->excl_nalloc = 0;
2458 check_excl_space(nbl, 1);
2460 set_no_excls(&nbl->excl[0]);
2466 snew_aligned(nbl->work->bb_ci, 1, NBNXN_SEARCH_BB_MEM_ALIGN);
2471 snew_aligned(nbl->work->pbb_ci, GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX, NBNXN_SEARCH_BB_MEM_ALIGN);
2473 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL, NBNXN_SEARCH_BB_MEM_ALIGN);
2476 snew_aligned(nbl->work->x_ci, NBNXN_NA_SC_MAX*DIM, NBNXN_SEARCH_BB_MEM_ALIGN);
2477 #ifdef GMX_NBNXN_SIMD
2478 snew_aligned(nbl->work->x_ci_simd_4xn, 1, NBNXN_MEM_ALIGN);
2479 snew_aligned(nbl->work->x_ci_simd_2xnn, 1, NBNXN_MEM_ALIGN);
2481 snew_aligned(nbl->work->d2, GPU_NSUBCELL, NBNXN_SEARCH_BB_MEM_ALIGN);
2483 nbl->work->sort = NULL;
2484 nbl->work->sort_nalloc = 0;
2485 nbl->work->sci_sort = NULL;
2486 nbl->work->sci_sort_nalloc = 0;
2489 void nbnxn_init_pairlist_set(nbnxn_pairlist_set_t *nbl_list,
2490 gmx_bool bSimple, gmx_bool bCombined,
2491 nbnxn_alloc_t *alloc,
2496 nbl_list->bSimple = bSimple;
2497 nbl_list->bCombined = bCombined;
2499 nbl_list->nnbl = gmx_omp_nthreads_get(emntNonbonded);
2501 if (!nbl_list->bCombined &&
2502 nbl_list->nnbl > NBNXN_BUFFERFLAG_MAX_THREADS)
2504 gmx_fatal(FARGS, "%d OpenMP threads were requested. Since the non-bonded force buffer reduction is prohibitively slow with more than %d threads, we do not allow this. Use %d or less OpenMP threads.",
2505 nbl_list->nnbl, NBNXN_BUFFERFLAG_MAX_THREADS, NBNXN_BUFFERFLAG_MAX_THREADS);
2508 snew(nbl_list->nbl, nbl_list->nnbl);
2509 /* Execute in order to avoid memory interleaving between threads */
2510 #pragma omp parallel for num_threads(nbl_list->nnbl) schedule(static)
2511 for (i = 0; i < nbl_list->nnbl; i++)
2513 /* Allocate the nblist data structure locally on each thread
2514 * to optimize memory access for NUMA architectures.
2516 snew(nbl_list->nbl[i], 1);
2518 /* Only list 0 is used on the GPU, use normal allocation for i>0 */
2521 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, alloc, free);
2525 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, NULL, NULL);
2530 /* Print statistics of a pair list, used for debug output */
2531 static void print_nblist_statistics_simple(FILE *fp, const nbnxn_pairlist_t *nbl,
2532 const nbnxn_search_t nbs, real rl)
2534 const nbnxn_grid_t *grid;
2539 /* This code only produces correct statistics with domain decomposition */
2540 grid = &nbs->grid[0];
2542 fprintf(fp, "nbl nci %d ncj %d\n",
2543 nbl->nci, nbl->ncj);
2544 fprintf(fp, "nbl na_sc %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2545 nbl->na_sc, rl, nbl->ncj, nbl->ncj/(double)grid->nc,
2546 nbl->ncj/(double)grid->nc*grid->na_sc,
2547 nbl->ncj/(double)grid->nc*grid->na_sc/(0.5*4.0/3.0*M_PI*rl*rl*rl*grid->nc*grid->na_sc/det(nbs->box)));
2549 fprintf(fp, "nbl average j cell list length %.1f\n",
2550 0.25*nbl->ncj/(double)nbl->nci);
2552 for (s = 0; s < SHIFTS; s++)
2557 for (i = 0; i < nbl->nci; i++)
2559 cs[nbl->ci[i].shift & NBNXN_CI_SHIFT] +=
2560 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start;
2562 j = nbl->ci[i].cj_ind_start;
2563 while (j < nbl->ci[i].cj_ind_end &&
2564 nbl->cj[j].excl != NBNXN_INTERACTION_MASK_ALL)
2570 fprintf(fp, "nbl cell pairs, total: %d excl: %d %.1f%%\n",
2571 nbl->ncj, npexcl, 100*npexcl/(double)nbl->ncj);
2572 for (s = 0; s < SHIFTS; s++)
2576 fprintf(fp, "nbl shift %2d ncj %3d\n", s, cs[s]);
2581 /* Print statistics of a pair lists, used for debug output */
2582 static void print_nblist_statistics_supersub(FILE *fp, const nbnxn_pairlist_t *nbl,
2583 const nbnxn_search_t nbs, real rl)
2585 const nbnxn_grid_t *grid;
2586 int i, j4, j, si, b;
2587 int c[GPU_NSUBCELL+1];
2589 /* This code only produces correct statistics with domain decomposition */
2590 grid = &nbs->grid[0];
2592 fprintf(fp, "nbl nsci %d ncj4 %d nsi %d excl4 %d\n",
2593 nbl->nsci, nbl->ncj4, nbl->nci_tot, nbl->nexcl);
2594 fprintf(fp, "nbl na_c %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2595 nbl->na_ci, rl, nbl->nci_tot, nbl->nci_tot/(double)grid->nsubc_tot,
2596 nbl->nci_tot/(double)grid->nsubc_tot*grid->na_c,
2597 nbl->nci_tot/(double)grid->nsubc_tot*grid->na_c/(0.5*4.0/3.0*M_PI*rl*rl*rl*grid->nsubc_tot*grid->na_c/det(nbs->box)));
2599 fprintf(fp, "nbl average j super cell list length %.1f\n",
2600 0.25*nbl->ncj4/(double)nbl->nsci);
2601 fprintf(fp, "nbl average i sub cell list length %.1f\n",
2602 nbl->nci_tot/((double)nbl->ncj4));
2604 for (si = 0; si <= GPU_NSUBCELL; si++)
2608 for (i = 0; i < nbl->nsci; i++)
2610 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
2612 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
2615 for (si = 0; si < GPU_NSUBCELL; si++)
2617 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
2626 for (b = 0; b <= GPU_NSUBCELL; b++)
2628 fprintf(fp, "nbl j-list #i-subcell %d %7d %4.1f\n",
2629 b, c[b], 100.0*c[b]/(double)(nbl->ncj4*NBNXN_GPU_JGROUP_SIZE));
2633 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp */
2634 static void low_get_nbl_exclusions(nbnxn_pairlist_t *nbl, int cj4,
2635 int warp, nbnxn_excl_t **excl)
2637 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2639 /* No exclusions set, make a new list entry */
2640 nbl->cj4[cj4].imei[warp].excl_ind = nbl->nexcl;
2642 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2643 set_no_excls(*excl);
2647 /* We already have some exclusions, new ones can be added to the list */
2648 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2652 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp,
2653 * allocates extra memory, if necessary.
2655 static void get_nbl_exclusions_1(nbnxn_pairlist_t *nbl, int cj4,
2656 int warp, nbnxn_excl_t **excl)
2658 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2660 /* We need to make a new list entry, check if we have space */
2661 check_excl_space(nbl, 1);
2663 low_get_nbl_exclusions(nbl, cj4, warp, excl);
2666 /* Returns pointers to the exclusion mask for cj4-unit cj4 for both warps,
2667 * allocates extra memory, if necessary.
2669 static void get_nbl_exclusions_2(nbnxn_pairlist_t *nbl, int cj4,
2670 nbnxn_excl_t **excl_w0,
2671 nbnxn_excl_t **excl_w1)
2673 /* Check for space we might need */
2674 check_excl_space(nbl, 2);
2676 low_get_nbl_exclusions(nbl, cj4, 0, excl_w0);
2677 low_get_nbl_exclusions(nbl, cj4, 1, excl_w1);
2680 /* Sets the self exclusions i=j and pair exclusions i>j */
2681 static void set_self_and_newton_excls_supersub(nbnxn_pairlist_t *nbl,
2682 int cj4_ind, int sj_offset,
2685 nbnxn_excl_t *excl[2];
2688 /* Here we only set the set self and double pair exclusions */
2690 get_nbl_exclusions_2(nbl, cj4_ind, &excl[0], &excl[1]);
2692 /* Only minor < major bits set */
2693 for (ej = 0; ej < nbl->na_ci; ej++)
2696 for (ei = ej; ei < nbl->na_ci; ei++)
2698 excl[w]->pair[(ej & (NBNXN_GPU_JGROUP_SIZE-1))*nbl->na_ci + ei] &=
2699 ~(1U << (sj_offset*GPU_NSUBCELL + si));
2704 /* Returns a diagonal or off-diagonal interaction mask for plain C lists */
2705 static unsigned int get_imask(gmx_bool rdiag, int ci, int cj)
2707 return (rdiag && ci == cj ? NBNXN_INTERACTION_MASK_DIAG : NBNXN_INTERACTION_MASK_ALL);
2710 /* Returns a diagonal or off-diagonal interaction mask for cj-size=2 */
2711 static unsigned int get_imask_simd_j2(gmx_bool rdiag, int ci, int cj)
2713 return (rdiag && ci*2 == cj ? NBNXN_INTERACTION_MASK_DIAG_J2_0 :
2714 (rdiag && ci*2+1 == cj ? NBNXN_INTERACTION_MASK_DIAG_J2_1 :
2715 NBNXN_INTERACTION_MASK_ALL));
2718 /* Returns a diagonal or off-diagonal interaction mask for cj-size=4 */
2719 static unsigned int get_imask_simd_j4(gmx_bool rdiag, int ci, int cj)
2721 return (rdiag && ci == cj ? NBNXN_INTERACTION_MASK_DIAG : NBNXN_INTERACTION_MASK_ALL);
2724 /* Returns a diagonal or off-diagonal interaction mask for cj-size=8 */
2725 static unsigned int get_imask_simd_j8(gmx_bool rdiag, int ci, int cj)
2727 return (rdiag && ci == cj*2 ? NBNXN_INTERACTION_MASK_DIAG_J8_0 :
2728 (rdiag && ci == cj*2+1 ? NBNXN_INTERACTION_MASK_DIAG_J8_1 :
2729 NBNXN_INTERACTION_MASK_ALL));
2732 #ifdef GMX_NBNXN_SIMD
2733 #if GMX_SIMD_REAL_WIDTH == 2
2734 #define get_imask_simd_4xn get_imask_simd_j2
2736 #if GMX_SIMD_REAL_WIDTH == 4
2737 #define get_imask_simd_4xn get_imask_simd_j4
2739 #if GMX_SIMD_REAL_WIDTH == 8
2740 #define get_imask_simd_4xn get_imask_simd_j8
2741 #define get_imask_simd_2xnn get_imask_simd_j4
2743 #if GMX_SIMD_REAL_WIDTH == 16
2744 #define get_imask_simd_2xnn get_imask_simd_j8
2748 /* Plain C code for making a pair list of cell ci vs cell cjf-cjl.
2749 * Checks bounding box distances and possibly atom pair distances.
2751 static void make_cluster_list_simple(const nbnxn_grid_t *gridj,
2752 nbnxn_pairlist_t *nbl,
2753 int ci, int cjf, int cjl,
2754 gmx_bool remove_sub_diag,
2756 real rl2, float rbb2,
2759 const nbnxn_list_work_t *work;
2761 const nbnxn_bb_t *bb_ci;
2766 int cjf_gl, cjl_gl, cj;
2770 bb_ci = nbl->work->bb_ci;
2771 x_ci = nbl->work->x_ci;
2774 while (!InRange && cjf <= cjl)
2776 d2 = subc_bb_dist2(0, bb_ci, cjf, gridj->bb);
2779 /* Check if the distance is within the distance where
2780 * we use only the bounding box distance rbb,
2781 * or within the cut-off and there is at least one atom pair
2782 * within the cut-off.
2792 cjf_gl = gridj->cell0 + cjf;
2793 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2795 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2797 InRange = InRange ||
2798 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2799 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2800 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2803 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2816 while (!InRange && cjl > cjf)
2818 d2 = subc_bb_dist2(0, bb_ci, cjl, gridj->bb);
2821 /* Check if the distance is within the distance where
2822 * we use only the bounding box distance rbb,
2823 * or within the cut-off and there is at least one atom pair
2824 * within the cut-off.
2834 cjl_gl = gridj->cell0 + cjl;
2835 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2837 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2839 InRange = InRange ||
2840 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2841 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2842 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2845 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2855 for (cj = cjf; cj <= cjl; cj++)
2857 /* Store cj and the interaction mask */
2858 nbl->cj[nbl->ncj].cj = gridj->cell0 + cj;
2859 nbl->cj[nbl->ncj].excl = get_imask(remove_sub_diag, ci, cj);
2862 /* Increase the closing index in i super-cell list */
2863 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
2867 #ifdef GMX_NBNXN_SIMD_4XN
2868 #include "nbnxn_search_simd_4xn.h"
2870 #ifdef GMX_NBNXN_SIMD_2XNN
2871 #include "nbnxn_search_simd_2xnn.h"
2874 /* Plain C or SIMD4 code for making a pair list of super-cell sci vs scj.
2875 * Checks bounding box distances and possibly atom pair distances.
2877 static void make_cluster_list_supersub(const nbnxn_grid_t *gridi,
2878 const nbnxn_grid_t *gridj,
2879 nbnxn_pairlist_t *nbl,
2881 gmx_bool sci_equals_scj,
2882 int stride, const real *x,
2883 real rl2, float rbb2,
2888 int cjo, ci1, ci, cj, cj_gl;
2889 int cj4_ind, cj_offset;
2893 const float *pbb_ci;
2895 const nbnxn_bb_t *bb_ci;
2900 #define PRUNE_LIST_CPU_ONE
2901 #ifdef PRUNE_LIST_CPU_ONE
2905 d2l = nbl->work->d2;
2908 pbb_ci = nbl->work->pbb_ci;
2910 bb_ci = nbl->work->bb_ci;
2912 x_ci = nbl->work->x_ci;
2916 for (cjo = 0; cjo < gridj->nsubc[scj]; cjo++)
2918 cj4_ind = (nbl->work->cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2919 cj_offset = nbl->work->cj_ind - cj4_ind*NBNXN_GPU_JGROUP_SIZE;
2920 cj4 = &nbl->cj4[cj4_ind];
2922 cj = scj*GPU_NSUBCELL + cjo;
2924 cj_gl = gridj->cell0*GPU_NSUBCELL + cj;
2926 /* Initialize this j-subcell i-subcell list */
2927 cj4->cj[cj_offset] = cj_gl;
2936 ci1 = gridi->nsubc[sci];
2940 /* Determine all ci1 bb distances in one call with SIMD4 */
2941 subc_bb_dist2_simd4_xxxx(gridj->pbb+(cj>>STRIDE_PBB_2LOG)*NNBSBB_XXXX+(cj & (STRIDE_PBB-1)),
2947 /* We use a fixed upper-bound instead of ci1 to help optimization */
2948 for (ci = 0; ci < GPU_NSUBCELL; ci++)
2955 #ifndef NBNXN_BBXXXX
2956 /* Determine the bb distance between ci and cj */
2957 d2l[ci] = subc_bb_dist2(ci, bb_ci, cj, gridj->bb);
2962 #ifdef PRUNE_LIST_CPU_ALL
2963 /* Check if the distance is within the distance where
2964 * we use only the bounding box distance rbb,
2965 * or within the cut-off and there is at least one atom pair
2966 * within the cut-off. This check is very costly.
2968 *ndistc += na_c*na_c;
2971 #ifdef NBNXN_PBB_SIMD4
2976 (na_c, ci, x_ci, cj_gl, stride, x, rl2)))
2978 /* Check if the distance between the two bounding boxes
2979 * in within the pair-list cut-off.
2984 /* Flag this i-subcell to be taken into account */
2985 imask |= (1U << (cj_offset*GPU_NSUBCELL+ci));
2987 #ifdef PRUNE_LIST_CPU_ONE
2995 #ifdef PRUNE_LIST_CPU_ONE
2996 /* If we only found 1 pair, check if any atoms are actually
2997 * within the cut-off, so we could get rid of it.
2999 if (npair == 1 && d2l[ci_last] >= rbb2)
3001 /* Avoid using function pointers here, as it's slower */
3003 #ifdef NBNXN_PBB_SIMD4
3004 !subc_in_range_simd4
3008 (na_c, ci_last, x_ci, cj_gl, stride, x, rl2))
3010 imask &= ~(1U << (cj_offset*GPU_NSUBCELL+ci_last));
3018 /* We have a useful sj entry, close it now */
3020 /* Set the exclucions for the ci== sj entry.
3021 * Here we don't bother to check if this entry is actually flagged,
3022 * as it will nearly always be in the list.
3026 set_self_and_newton_excls_supersub(nbl, cj4_ind, cj_offset, cjo);
3029 /* Copy the cluster interaction mask to the list */
3030 for (w = 0; w < NWARP; w++)
3032 cj4->imei[w].imask |= imask;
3035 nbl->work->cj_ind++;
3037 /* Keep the count */
3038 nbl->nci_tot += npair;
3040 /* Increase the closing index in i super-cell list */
3041 nbl->sci[nbl->nsci].cj4_ind_end =
3042 ((nbl->work->cj_ind+NBNXN_GPU_JGROUP_SIZE-1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3047 /* Set all atom-pair exclusions from the topology stored in excl
3048 * as masks in the pair-list for simple list i-entry nbl_ci
3050 static void set_ci_top_excls(const nbnxn_search_t nbs,
3051 nbnxn_pairlist_t *nbl,
3052 gmx_bool diagRemoved,
3055 const nbnxn_ci_t *nbl_ci,
3056 const t_blocka *excl)
3060 int cj_ind_first, cj_ind_last;
3061 int cj_first, cj_last;
3063 int i, ai, aj, si, eind, ge, se;
3064 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3068 nbnxn_excl_t *nbl_excl;
3069 int inner_i, inner_e;
3073 if (nbl_ci->cj_ind_end == nbl_ci->cj_ind_start)
3081 cj_ind_first = nbl_ci->cj_ind_start;
3082 cj_ind_last = nbl->ncj - 1;
3084 cj_first = nbl->cj[cj_ind_first].cj;
3085 cj_last = nbl->cj[cj_ind_last].cj;
3087 /* Determine how many contiguous j-cells we have starting
3088 * from the first i-cell. This number can be used to directly
3089 * calculate j-cell indices for excluded atoms.
3092 if (na_ci_2log == na_cj_2log)
3094 while (cj_ind_first + ndirect <= cj_ind_last &&
3095 nbl->cj[cj_ind_first+ndirect].cj == ci + ndirect)
3100 #ifdef NBNXN_SEARCH_BB_SIMD4
3103 while (cj_ind_first + ndirect <= cj_ind_last &&
3104 nbl->cj[cj_ind_first+ndirect].cj == ci_to_cj(na_cj_2log, ci) + ndirect)
3111 /* Loop over the atoms in the i super-cell */
3112 for (i = 0; i < nbl->na_sc; i++)
3114 ai = nbs->a[ci*nbl->na_sc+i];
3117 si = (i>>na_ci_2log);
3119 /* Loop over the topology-based exclusions for this i-atom */
3120 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3126 /* The self exclusion are already set, save some time */
3132 /* Without shifts we only calculate interactions j>i
3133 * for one-way pair-lists.
3135 if (diagRemoved && ge <= ci*nbl->na_sc + i)
3140 se = (ge >> na_cj_2log);
3142 /* Could the cluster se be in our list? */
3143 if (se >= cj_first && se <= cj_last)
3145 if (se < cj_first + ndirect)
3147 /* We can calculate cj_ind directly from se */
3148 found = cj_ind_first + se - cj_first;
3152 /* Search for se using bisection */
3154 cj_ind_0 = cj_ind_first + ndirect;
3155 cj_ind_1 = cj_ind_last + 1;
3156 while (found == -1 && cj_ind_0 < cj_ind_1)
3158 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3160 cj_m = nbl->cj[cj_ind_m].cj;
3168 cj_ind_1 = cj_ind_m;
3172 cj_ind_0 = cj_ind_m + 1;
3179 inner_i = i - (si << na_ci_2log);
3180 inner_e = ge - (se << na_cj_2log);
3182 nbl->cj[found].excl &= ~(1U<<((inner_i<<na_cj_2log) + inner_e));
3183 /* The next code line is usually not needed. We do not want to version
3184 * away the above line, because there is logic that relies on being
3185 * able to detect easily whether any exclusions exist. */
3186 #if (defined GMX_SIMD_IBM_QPX)
3187 nbl->cj[found].interaction_mask_indices[inner_i] &= ~(1U << inner_e);
3196 /* Set all atom-pair exclusions from the topology stored in excl
3197 * as masks in the pair-list for i-super-cell entry nbl_sci
3199 static void set_sci_top_excls(const nbnxn_search_t nbs,
3200 nbnxn_pairlist_t *nbl,
3201 gmx_bool diagRemoved,
3203 const nbnxn_sci_t *nbl_sci,
3204 const t_blocka *excl)
3209 int cj_ind_first, cj_ind_last;
3210 int cj_first, cj_last;
3212 int i, ai, aj, si, eind, ge, se;
3213 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3217 nbnxn_excl_t *nbl_excl;
3218 int inner_i, inner_e, w;
3224 if (nbl_sci->cj4_ind_end == nbl_sci->cj4_ind_start)
3232 cj_ind_first = nbl_sci->cj4_ind_start*NBNXN_GPU_JGROUP_SIZE;
3233 cj_ind_last = nbl->work->cj_ind - 1;
3235 cj_first = nbl->cj4[nbl_sci->cj4_ind_start].cj[0];
3236 cj_last = nbl_cj(nbl, cj_ind_last);
3238 /* Determine how many contiguous j-clusters we have starting
3239 * from the first i-cluster. This number can be used to directly
3240 * calculate j-cluster indices for excluded atoms.
3243 while (cj_ind_first + ndirect <= cj_ind_last &&
3244 nbl_cj(nbl, cj_ind_first+ndirect) == sci*GPU_NSUBCELL + ndirect)
3249 /* Loop over the atoms in the i super-cell */
3250 for (i = 0; i < nbl->na_sc; i++)
3252 ai = nbs->a[sci*nbl->na_sc+i];
3255 si = (i>>na_c_2log);
3257 /* Loop over the topology-based exclusions for this i-atom */
3258 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3264 /* The self exclusion are already set, save some time */
3270 /* Without shifts we only calculate interactions j>i
3271 * for one-way pair-lists.
3273 if (diagRemoved && ge <= sci*nbl->na_sc + i)
3279 /* Could the cluster se be in our list? */
3280 if (se >= cj_first && se <= cj_last)
3282 if (se < cj_first + ndirect)
3284 /* We can calculate cj_ind directly from se */
3285 found = cj_ind_first + se - cj_first;
3289 /* Search for se using bisection */
3291 cj_ind_0 = cj_ind_first + ndirect;
3292 cj_ind_1 = cj_ind_last + 1;
3293 while (found == -1 && cj_ind_0 < cj_ind_1)
3295 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3297 cj_m = nbl_cj(nbl, cj_ind_m);
3305 cj_ind_1 = cj_ind_m;
3309 cj_ind_0 = cj_ind_m + 1;
3316 inner_i = i - si*na_c;
3317 inner_e = ge - se*na_c;
3319 /* Macro for getting the index of atom a within a cluster */
3320 #define AMODCJ4(a) ((a) & (NBNXN_GPU_JGROUP_SIZE - 1))
3321 /* Macro for converting an atom number to a cluster number */
3322 #define A2CJ4(a) ((a) >> NBNXN_GPU_JGROUP_SIZE_2LOG)
3323 /* Macro for getting the index of an i-atom within a warp */
3324 #define AMODWI(a) ((a) & (NBNXN_GPU_CLUSTER_SIZE/2 - 1))
3326 if (nbl_imask0(nbl, found) & (1U << (AMODCJ4(found)*GPU_NSUBCELL + si)))
3330 get_nbl_exclusions_1(nbl, A2CJ4(found), w, &nbl_excl);
3332 nbl_excl->pair[AMODWI(inner_e)*nbl->na_ci+inner_i] &=
3333 ~(1U << (AMODCJ4(found)*GPU_NSUBCELL + si));
3346 /* Reallocate the simple ci list for at least n entries */
3347 static void nb_realloc_ci(nbnxn_pairlist_t *nbl, int n)
3349 nbl->ci_nalloc = over_alloc_small(n);
3350 nbnxn_realloc_void((void **)&nbl->ci,
3351 nbl->nci*sizeof(*nbl->ci),
3352 nbl->ci_nalloc*sizeof(*nbl->ci),
3353 nbl->alloc, nbl->free);
3356 /* Reallocate the super-cell sci list for at least n entries */
3357 static void nb_realloc_sci(nbnxn_pairlist_t *nbl, int n)
3359 nbl->sci_nalloc = over_alloc_small(n);
3360 nbnxn_realloc_void((void **)&nbl->sci,
3361 nbl->nsci*sizeof(*nbl->sci),
3362 nbl->sci_nalloc*sizeof(*nbl->sci),
3363 nbl->alloc, nbl->free);
3366 /* Make a new ci entry at index nbl->nci */
3367 static void new_ci_entry(nbnxn_pairlist_t *nbl, int ci, int shift, int flags)
3369 if (nbl->nci + 1 > nbl->ci_nalloc)
3371 nb_realloc_ci(nbl, nbl->nci+1);
3373 nbl->ci[nbl->nci].ci = ci;
3374 nbl->ci[nbl->nci].shift = shift;
3375 /* Store the interaction flags along with the shift */
3376 nbl->ci[nbl->nci].shift |= flags;
3377 nbl->ci[nbl->nci].cj_ind_start = nbl->ncj;
3378 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
3381 /* Make a new sci entry at index nbl->nsci */
3382 static void new_sci_entry(nbnxn_pairlist_t *nbl, int sci, int shift)
3384 if (nbl->nsci + 1 > nbl->sci_nalloc)
3386 nb_realloc_sci(nbl, nbl->nsci+1);
3388 nbl->sci[nbl->nsci].sci = sci;
3389 nbl->sci[nbl->nsci].shift = shift;
3390 nbl->sci[nbl->nsci].cj4_ind_start = nbl->ncj4;
3391 nbl->sci[nbl->nsci].cj4_ind_end = nbl->ncj4;
3394 /* Sort the simple j-list cj on exclusions.
3395 * Entries with exclusions will all be sorted to the beginning of the list.
3397 static void sort_cj_excl(nbnxn_cj_t *cj, int ncj,
3398 nbnxn_list_work_t *work)
3402 if (ncj > work->cj_nalloc)
3404 work->cj_nalloc = over_alloc_large(ncj);
3405 srenew(work->cj, work->cj_nalloc);
3408 /* Make a list of the j-cells involving exclusions */
3410 for (j = 0; j < ncj; j++)
3412 if (cj[j].excl != NBNXN_INTERACTION_MASK_ALL)
3414 work->cj[jnew++] = cj[j];
3417 /* Check if there are exclusions at all or not just the first entry */
3418 if (!((jnew == 0) ||
3419 (jnew == 1 && cj[0].excl != NBNXN_INTERACTION_MASK_ALL)))
3421 for (j = 0; j < ncj; j++)
3423 if (cj[j].excl == NBNXN_INTERACTION_MASK_ALL)
3425 work->cj[jnew++] = cj[j];
3428 for (j = 0; j < ncj; j++)
3430 cj[j] = work->cj[j];
3435 /* Close this simple list i entry */
3436 static void close_ci_entry_simple(nbnxn_pairlist_t *nbl)
3440 /* All content of the new ci entry have already been filled correctly,
3441 * we only need to increase the count here (for non empty lists).
3443 jlen = nbl->ci[nbl->nci].cj_ind_end - nbl->ci[nbl->nci].cj_ind_start;
3446 sort_cj_excl(nbl->cj+nbl->ci[nbl->nci].cj_ind_start, jlen, nbl->work);
3448 /* The counts below are used for non-bonded pair/flop counts
3449 * and should therefore match the available kernel setups.
3451 if (!(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_COUL(0)))
3453 nbl->work->ncj_noq += jlen;
3455 else if ((nbl->ci[nbl->nci].shift & NBNXN_CI_HALF_LJ(0)) ||
3456 !(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_LJ(0)))
3458 nbl->work->ncj_hlj += jlen;
3465 /* Split sci entry for load balancing on the GPU.
3466 * Splitting ensures we have enough lists to fully utilize the whole GPU.
3467 * With progBal we generate progressively smaller lists, which improves
3468 * load balancing. As we only know the current count on our own thread,
3469 * we will need to estimate the current total amount of i-entries.
3470 * As the lists get concatenated later, this estimate depends
3471 * both on nthread and our own thread index.
3473 static void split_sci_entry(nbnxn_pairlist_t *nbl,
3474 int nsp_max_av, gmx_bool progBal, int nc_bal,
3475 int thread, int nthread)
3479 int cj4_start, cj4_end, j4len, cj4;
3481 int nsp, nsp_sci, nsp_cj4, nsp_cj4_e, nsp_cj4_p;
3486 /* Estimate the total numbers of ci's of the nblist combined
3487 * over all threads using the target number of ci's.
3489 nsci_est = nc_bal*thread/nthread + nbl->nsci;
3491 /* The first ci blocks should be larger, to avoid overhead.
3492 * The last ci blocks should be smaller, to improve load balancing.
3495 nsp_max_av*nc_bal*3/(2*(nsci_est - 1 + nc_bal)));
3499 nsp_max = nsp_max_av;
3502 cj4_start = nbl->sci[nbl->nsci-1].cj4_ind_start;
3503 cj4_end = nbl->sci[nbl->nsci-1].cj4_ind_end;
3504 j4len = cj4_end - cj4_start;
3506 if (j4len > 1 && j4len*GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE > nsp_max)
3508 /* Remove the last ci entry and process the cj4's again */
3516 for (cj4 = cj4_start; cj4 < cj4_end; cj4++)
3518 nsp_cj4_p = nsp_cj4;
3519 /* Count the number of cluster pairs in this cj4 group */
3521 for (p = 0; p < GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE; p++)
3523 nsp_cj4 += (nbl->cj4[cj4].imei[0].imask >> p) & 1;
3526 if (nsp_cj4 > 0 && nsp + nsp_cj4 > nsp_max)
3528 /* Split the list at cj4 */
3529 nbl->sci[sci].cj4_ind_end = cj4;
3530 /* Create a new sci entry */
3533 if (nbl->nsci+1 > nbl->sci_nalloc)
3535 nb_realloc_sci(nbl, nbl->nsci+1);
3537 nbl->sci[sci].sci = nbl->sci[nbl->nsci-1].sci;
3538 nbl->sci[sci].shift = nbl->sci[nbl->nsci-1].shift;
3539 nbl->sci[sci].cj4_ind_start = cj4;
3541 nsp_cj4_e = nsp_cj4_p;
3547 /* Put the remaining cj4's in the last sci entry */
3548 nbl->sci[sci].cj4_ind_end = cj4_end;
3550 /* Possibly balance out the last two sci's
3551 * by moving the last cj4 of the second last sci.
3553 if (nsp_sci - nsp_cj4_e >= nsp + nsp_cj4_e)
3555 nbl->sci[sci-1].cj4_ind_end--;
3556 nbl->sci[sci].cj4_ind_start--;
3563 /* Clost this super/sub list i entry */
3564 static void close_ci_entry_supersub(nbnxn_pairlist_t *nbl,
3566 gmx_bool progBal, int nc_bal,
3567 int thread, int nthread)
3572 /* All content of the new ci entry have already been filled correctly,
3573 * we only need to increase the count here (for non empty lists).
3575 j4len = nbl->sci[nbl->nsci].cj4_ind_end - nbl->sci[nbl->nsci].cj4_ind_start;
3578 /* We can only have complete blocks of 4 j-entries in a list,
3579 * so round the count up before closing.
3581 nbl->ncj4 = ((nbl->work->cj_ind + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3582 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3588 /* Measure the size of the new entry and potentially split it */
3589 split_sci_entry(nbl, nsp_max_av, progBal, nc_bal, thread, nthread);
3594 /* Syncs the working array before adding another grid pair to the list */
3595 static void sync_work(nbnxn_pairlist_t *nbl)
3599 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3600 nbl->work->cj4_init = nbl->ncj4;
3604 /* Clears an nbnxn_pairlist_t data structure */
3605 static void clear_pairlist(nbnxn_pairlist_t *nbl)
3614 nbl->work->ncj_noq = 0;
3615 nbl->work->ncj_hlj = 0;
3618 /* Sets a simple list i-cell bounding box, including PBC shift */
3619 static gmx_inline void set_icell_bb_simple(const nbnxn_bb_t *bb, int ci,
3620 real shx, real shy, real shz,
3623 bb_ci->lower[BB_X] = bb[ci].lower[BB_X] + shx;
3624 bb_ci->lower[BB_Y] = bb[ci].lower[BB_Y] + shy;
3625 bb_ci->lower[BB_Z] = bb[ci].lower[BB_Z] + shz;
3626 bb_ci->upper[BB_X] = bb[ci].upper[BB_X] + shx;
3627 bb_ci->upper[BB_Y] = bb[ci].upper[BB_Y] + shy;
3628 bb_ci->upper[BB_Z] = bb[ci].upper[BB_Z] + shz;
3632 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
3633 static void set_icell_bbxxxx_supersub(const float *bb, int ci,
3634 real shx, real shy, real shz,
3639 ia = ci*(GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX;
3640 for (m = 0; m < (GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX; m += NNBSBB_XXXX)
3642 for (i = 0; i < STRIDE_PBB; i++)
3644 bb_ci[m+0*STRIDE_PBB+i] = bb[ia+m+0*STRIDE_PBB+i] + shx;
3645 bb_ci[m+1*STRIDE_PBB+i] = bb[ia+m+1*STRIDE_PBB+i] + shy;
3646 bb_ci[m+2*STRIDE_PBB+i] = bb[ia+m+2*STRIDE_PBB+i] + shz;
3647 bb_ci[m+3*STRIDE_PBB+i] = bb[ia+m+3*STRIDE_PBB+i] + shx;
3648 bb_ci[m+4*STRIDE_PBB+i] = bb[ia+m+4*STRIDE_PBB+i] + shy;
3649 bb_ci[m+5*STRIDE_PBB+i] = bb[ia+m+5*STRIDE_PBB+i] + shz;
3655 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
3656 static void set_icell_bb_supersub(const nbnxn_bb_t *bb, int ci,
3657 real shx, real shy, real shz,
3662 for (i = 0; i < GPU_NSUBCELL; i++)
3664 set_icell_bb_simple(bb, ci*GPU_NSUBCELL+i,
3670 /* Copies PBC shifted i-cell atom coordinates x,y,z to working array */
3671 static void icell_set_x_simple(int ci,
3672 real shx, real shy, real shz,
3673 int gmx_unused na_c,
3674 int stride, const real *x,
3675 nbnxn_list_work_t *work)
3679 ia = ci*NBNXN_CPU_CLUSTER_I_SIZE;
3681 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE; i++)
3683 work->x_ci[i*STRIDE_XYZ+XX] = x[(ia+i)*stride+XX] + shx;
3684 work->x_ci[i*STRIDE_XYZ+YY] = x[(ia+i)*stride+YY] + shy;
3685 work->x_ci[i*STRIDE_XYZ+ZZ] = x[(ia+i)*stride+ZZ] + shz;
3689 /* Copies PBC shifted super-cell atom coordinates x,y,z to working array */
3690 static void icell_set_x_supersub(int ci,
3691 real shx, real shy, real shz,
3693 int stride, const real *x,
3694 nbnxn_list_work_t *work)
3701 ia = ci*GPU_NSUBCELL*na_c;
3702 for (i = 0; i < GPU_NSUBCELL*na_c; i++)
3704 x_ci[i*DIM + XX] = x[(ia+i)*stride + XX] + shx;
3705 x_ci[i*DIM + YY] = x[(ia+i)*stride + YY] + shy;
3706 x_ci[i*DIM + ZZ] = x[(ia+i)*stride + ZZ] + shz;
3710 #ifdef NBNXN_SEARCH_BB_SIMD4
3711 /* Copies PBC shifted super-cell packed atom coordinates to working array */
3712 static void icell_set_x_supersub_simd4(int ci,
3713 real shx, real shy, real shz,
3715 int stride, const real *x,
3716 nbnxn_list_work_t *work)
3718 int si, io, ia, i, j;
3723 for (si = 0; si < GPU_NSUBCELL; si++)
3725 for (i = 0; i < na_c; i += STRIDE_PBB)
3728 ia = ci*GPU_NSUBCELL*na_c + io;
3729 for (j = 0; j < STRIDE_PBB; j++)
3731 x_ci[io*DIM + j + XX*STRIDE_PBB] = x[(ia+j)*stride+XX] + shx;
3732 x_ci[io*DIM + j + YY*STRIDE_PBB] = x[(ia+j)*stride+YY] + shy;
3733 x_ci[io*DIM + j + ZZ*STRIDE_PBB] = x[(ia+j)*stride+ZZ] + shz;
3740 /* Clusters at the cut-off only increase rlist by 60% of their size */
3741 static real nbnxn_rlist_inc_outside_fac = 0.6;
3743 /* Due to the cluster size the effective pair-list is longer than
3744 * that of a simple atom pair-list. This function gives the extra distance.
3746 real nbnxn_get_rlist_effective_inc(int cluster_size_j, real atom_density)
3749 real vol_inc_i, vol_inc_j;
3751 /* We should get this from the setup, but currently it's the same for
3752 * all setups, including GPUs.
3754 cluster_size_i = NBNXN_CPU_CLUSTER_I_SIZE;
3756 vol_inc_i = (cluster_size_i - 1)/atom_density;
3757 vol_inc_j = (cluster_size_j - 1)/atom_density;
3759 return nbnxn_rlist_inc_outside_fac*pow(vol_inc_i + vol_inc_j, 1.0/3.0);
3762 /* Estimates the interaction volume^2 for non-local interactions */
3763 static real nonlocal_vol2(const gmx_domdec_zones_t *zones, rvec ls, real r)
3772 /* Here we simply add up the volumes of 1, 2 or 3 1D decomposition
3773 * not home interaction volume^2. As these volumes are not additive,
3774 * this is an overestimate, but it would only be significant in the limit
3775 * of small cells, where we anyhow need to split the lists into
3776 * as small parts as possible.
3779 for (z = 0; z < zones->n; z++)
3781 if (zones->shift[z][XX] + zones->shift[z][YY] + zones->shift[z][ZZ] == 1)
3786 for (d = 0; d < DIM; d++)
3788 if (zones->shift[z][d] == 0)
3792 za *= zones->size[z].x1[d] - zones->size[z].x0[d];
3796 /* 4 octants of a sphere */
3797 vold_est = 0.25*M_PI*r*r*r*r;
3798 /* 4 quarter pie slices on the edges */
3799 vold_est += 4*cl*M_PI/6.0*r*r*r;
3800 /* One rectangular volume on a face */
3801 vold_est += ca*0.5*r*r;
3803 vol2_est_tot += vold_est*za;
3807 return vol2_est_tot;
3810 /* Estimates the average size of a full j-list for super/sub setup */
3811 static int get_nsubpair_max(const nbnxn_search_t nbs,
3814 int min_ci_balanced)
3816 const nbnxn_grid_t *grid;
3818 real xy_diag2, r_eff_sup, vol_est, nsp_est, nsp_est_nl;
3821 grid = &nbs->grid[0];
3823 ls[XX] = (grid->c1[XX] - grid->c0[XX])/(grid->ncx*GPU_NSUBCELL_X);
3824 ls[YY] = (grid->c1[YY] - grid->c0[YY])/(grid->ncy*GPU_NSUBCELL_Y);
3825 ls[ZZ] = (grid->c1[ZZ] - grid->c0[ZZ])*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z);
3827 /* The average squared length of the diagonal of a sub cell */
3828 xy_diag2 = ls[XX]*ls[XX] + ls[YY]*ls[YY] + ls[ZZ]*ls[ZZ];
3830 /* The formulas below are a heuristic estimate of the average nsj per si*/
3831 r_eff_sup = rlist + nbnxn_rlist_inc_outside_fac*sqr((grid->na_c - 1.0)/grid->na_c)*sqrt(xy_diag2/3);
3833 if (!nbs->DomDec || nbs->zones->n == 1)
3840 sqr(grid->atom_density/grid->na_c)*
3841 nonlocal_vol2(nbs->zones, ls, r_eff_sup);
3846 /* Sub-cell interacts with itself */
3847 vol_est = ls[XX]*ls[YY]*ls[ZZ];
3848 /* 6/2 rectangular volume on the faces */
3849 vol_est += (ls[XX]*ls[YY] + ls[XX]*ls[ZZ] + ls[YY]*ls[ZZ])*r_eff_sup;
3850 /* 12/2 quarter pie slices on the edges */
3851 vol_est += 2*(ls[XX] + ls[YY] + ls[ZZ])*0.25*M_PI*sqr(r_eff_sup);
3852 /* 4 octants of a sphere */
3853 vol_est += 0.5*4.0/3.0*M_PI*pow(r_eff_sup, 3);
3855 nsp_est = grid->nsubc_tot*vol_est*grid->atom_density/grid->na_c;
3857 /* Subtract the non-local pair count */
3858 nsp_est -= nsp_est_nl;
3862 fprintf(debug, "nsp_est local %5.1f non-local %5.1f\n",
3863 nsp_est, nsp_est_nl);
3868 nsp_est = nsp_est_nl;
3871 if (min_ci_balanced <= 0 || grid->nc >= min_ci_balanced || grid->nc == 0)
3873 /* We don't need to worry */
3878 /* Thus the (average) maximum j-list size should be as follows */
3879 nsubpair_max = max(1, (int)(nsp_est/min_ci_balanced+0.5));
3881 /* Since the target value is a maximum (this avoids high outliers,
3882 * which lead to load imbalance), not average, we add half the
3883 * number of pairs in a cj4 block to get the average about right.
3885 nsubpair_max += GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE/2;
3890 fprintf(debug, "nbl nsp estimate %.1f, nsubpair_max %d\n",
3891 nsp_est, nsubpair_max);
3894 return nsubpair_max;
3897 /* Debug list print function */
3898 static void print_nblist_ci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3902 for (i = 0; i < nbl->nci; i++)
3904 fprintf(fp, "ci %4d shift %2d ncj %3d\n",
3905 nbl->ci[i].ci, nbl->ci[i].shift,
3906 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start);
3908 for (j = nbl->ci[i].cj_ind_start; j < nbl->ci[i].cj_ind_end; j++)
3910 fprintf(fp, " cj %5d imask %x\n",
3917 /* Debug list print function */
3918 static void print_nblist_sci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3920 int i, j4, j, ncp, si;
3922 for (i = 0; i < nbl->nsci; i++)
3924 fprintf(fp, "ci %4d shift %2d ncj4 %2d\n",
3925 nbl->sci[i].sci, nbl->sci[i].shift,
3926 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start);
3929 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
3931 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
3933 fprintf(fp, " sj %5d imask %x\n",
3935 nbl->cj4[j4].imei[0].imask);
3936 for (si = 0; si < GPU_NSUBCELL; si++)
3938 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
3945 fprintf(fp, "ci %4d shift %2d ncj4 %2d ncp %3d\n",
3946 nbl->sci[i].sci, nbl->sci[i].shift,
3947 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start,
3952 /* Combine pair lists *nbl generated on multiple threads nblc */
3953 static void combine_nblists(int nnbl, nbnxn_pairlist_t **nbl,
3954 nbnxn_pairlist_t *nblc)
3956 int nsci, ncj4, nexcl;
3961 gmx_incons("combine_nblists does not support simple lists");
3966 nexcl = nblc->nexcl;
3967 for (i = 0; i < nnbl; i++)
3969 nsci += nbl[i]->nsci;
3970 ncj4 += nbl[i]->ncj4;
3971 nexcl += nbl[i]->nexcl;
3974 if (nsci > nblc->sci_nalloc)
3976 nb_realloc_sci(nblc, nsci);
3978 if (ncj4 > nblc->cj4_nalloc)
3980 nblc->cj4_nalloc = over_alloc_small(ncj4);
3981 nbnxn_realloc_void((void **)&nblc->cj4,
3982 nblc->ncj4*sizeof(*nblc->cj4),
3983 nblc->cj4_nalloc*sizeof(*nblc->cj4),
3984 nblc->alloc, nblc->free);
3986 if (nexcl > nblc->excl_nalloc)
3988 nblc->excl_nalloc = over_alloc_small(nexcl);
3989 nbnxn_realloc_void((void **)&nblc->excl,
3990 nblc->nexcl*sizeof(*nblc->excl),
3991 nblc->excl_nalloc*sizeof(*nblc->excl),
3992 nblc->alloc, nblc->free);
3995 /* Each thread should copy its own data to the combined arrays,
3996 * as otherwise data will go back and forth between different caches.
3998 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
3999 for (n = 0; n < nnbl; n++)
4006 const nbnxn_pairlist_t *nbli;
4008 /* Determine the offset in the combined data for our thread */
4009 sci_offset = nblc->nsci;
4010 cj4_offset = nblc->ncj4;
4011 ci_offset = nblc->nci_tot;
4012 excl_offset = nblc->nexcl;
4014 for (i = 0; i < n; i++)
4016 sci_offset += nbl[i]->nsci;
4017 cj4_offset += nbl[i]->ncj4;
4018 ci_offset += nbl[i]->nci_tot;
4019 excl_offset += nbl[i]->nexcl;
4024 for (i = 0; i < nbli->nsci; i++)
4026 nblc->sci[sci_offset+i] = nbli->sci[i];
4027 nblc->sci[sci_offset+i].cj4_ind_start += cj4_offset;
4028 nblc->sci[sci_offset+i].cj4_ind_end += cj4_offset;
4031 for (j4 = 0; j4 < nbli->ncj4; j4++)
4033 nblc->cj4[cj4_offset+j4] = nbli->cj4[j4];
4034 nblc->cj4[cj4_offset+j4].imei[0].excl_ind += excl_offset;
4035 nblc->cj4[cj4_offset+j4].imei[1].excl_ind += excl_offset;
4038 for (j4 = 0; j4 < nbli->nexcl; j4++)
4040 nblc->excl[excl_offset+j4] = nbli->excl[j4];
4044 for (n = 0; n < nnbl; n++)
4046 nblc->nsci += nbl[n]->nsci;
4047 nblc->ncj4 += nbl[n]->ncj4;
4048 nblc->nci_tot += nbl[n]->nci_tot;
4049 nblc->nexcl += nbl[n]->nexcl;
4053 /* Returns the next ci to be processes by our thread */
4054 static gmx_bool next_ci(const nbnxn_grid_t *grid,
4056 int nth, int ci_block,
4057 int *ci_x, int *ci_y,
4063 if (*ci_b == ci_block)
4065 /* Jump to the next block assigned to this task */
4066 *ci += (nth - 1)*ci_block;
4070 if (*ci >= grid->nc*conv)
4075 while (*ci >= grid->cxy_ind[*ci_x*grid->ncy + *ci_y + 1]*conv)
4078 if (*ci_y == grid->ncy)
4088 /* Returns the distance^2 for which we put cell pairs in the list
4089 * without checking atom pair distances. This is usually < rlist^2.
4091 static float boundingbox_only_distance2(const nbnxn_grid_t *gridi,
4092 const nbnxn_grid_t *gridj,
4096 /* If the distance between two sub-cell bounding boxes is less
4097 * than this distance, do not check the distance between
4098 * all particle pairs in the sub-cell, since then it is likely
4099 * that the box pair has atom pairs within the cut-off.
4100 * We use the nblist cut-off minus 0.5 times the average x/y diagonal
4101 * spacing of the sub-cells. Around 40% of the checked pairs are pruned.
4102 * Using more than 0.5 gains at most 0.5%.
4103 * If forces are calculated more than twice, the performance gain
4104 * in the force calculation outweighs the cost of checking.
4105 * Note that with subcell lists, the atom-pair distance check
4106 * is only performed when only 1 out of 8 sub-cells in within range,
4107 * this is because the GPU is much faster than the cpu.
4112 bbx = 0.5*(gridi->sx + gridj->sx);
4113 bby = 0.5*(gridi->sy + gridj->sy);
4116 bbx /= GPU_NSUBCELL_X;
4117 bby /= GPU_NSUBCELL_Y;
4120 rbb2 = sqr(max(0, rlist - 0.5*sqrt(bbx*bbx + bby*bby)));
4125 return (float)((1+GMX_FLOAT_EPS)*rbb2);
4129 static int get_ci_block_size(const nbnxn_grid_t *gridi,
4130 gmx_bool bDomDec, int nth)
4132 const int ci_block_enum = 5;
4133 const int ci_block_denom = 11;
4134 const int ci_block_min_atoms = 16;
4137 /* Here we decide how to distribute the blocks over the threads.
4138 * We use prime numbers to try to avoid that the grid size becomes
4139 * a multiple of the number of threads, which would lead to some
4140 * threads getting "inner" pairs and others getting boundary pairs,
4141 * which in turns will lead to load imbalance between threads.
4142 * Set the block size as 5/11/ntask times the average number of cells
4143 * in a y,z slab. This should ensure a quite uniform distribution
4144 * of the grid parts of the different thread along all three grid
4145 * zone boundaries with 3D domain decomposition. At the same time
4146 * the blocks will not become too small.
4148 ci_block = (gridi->nc*ci_block_enum)/(ci_block_denom*gridi->ncx*nth);
4150 /* Ensure the blocks are not too small: avoids cache invalidation */
4151 if (ci_block*gridi->na_sc < ci_block_min_atoms)
4153 ci_block = (ci_block_min_atoms + gridi->na_sc - 1)/gridi->na_sc;
4156 /* Without domain decomposition
4157 * or with less than 3 blocks per task, divide in nth blocks.
4159 if (!bDomDec || ci_block*3*nth > gridi->nc)
4161 ci_block = (gridi->nc + nth - 1)/nth;
4167 /* Generates the part of pair-list nbl assigned to our thread */
4168 static void nbnxn_make_pairlist_part(const nbnxn_search_t nbs,
4169 const nbnxn_grid_t *gridi,
4170 const nbnxn_grid_t *gridj,
4171 nbnxn_search_work_t *work,
4172 const nbnxn_atomdata_t *nbat,
4173 const t_blocka *excl,
4177 gmx_bool bFBufferFlag,
4180 int min_ci_balanced,
4182 nbnxn_pairlist_t *nbl)
4189 int ci_b, ci, ci_x, ci_y, ci_xy, cj;
4195 int conv_i, cell0_i;
4196 const nbnxn_bb_t *bb_i = NULL;
4198 const float *pbb_i = NULL;
4200 const float *bbcz_i, *bbcz_j;
4202 real bx0, bx1, by0, by1, bz0, bz1;
4204 real d2cx, d2z, d2z_cx, d2z_cy, d2zx, d2zxy, d2xy;
4205 int cxf, cxl, cyf, cyf_x, cyl;
4207 int c0, c1, cs, cf, cl;
4210 int gridi_flag_shift = 0, gridj_flag_shift = 0;
4211 unsigned *gridj_flag = NULL;
4212 int ncj_old_i, ncj_old_j;
4214 nbs_cycle_start(&work->cc[enbsCCsearch]);
4216 if (gridj->bSimple != nbl->bSimple)
4218 gmx_incons("Grid incompatible with pair-list");
4222 nbl->na_sc = gridj->na_sc;
4223 nbl->na_ci = gridj->na_c;
4224 nbl->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
4225 na_cj_2log = get_2log(nbl->na_cj);
4231 /* Determine conversion of clusters to flag blocks */
4232 gridi_flag_shift = 0;
4233 while ((nbl->na_ci<<gridi_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4237 gridj_flag_shift = 0;
4238 while ((nbl->na_cj<<gridj_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4243 gridj_flag = work->buffer_flags.flag;
4246 copy_mat(nbs->box, box);
4248 rl2 = nbl->rlist*nbl->rlist;
4250 rbb2 = boundingbox_only_distance2(gridi, gridj, nbl->rlist, nbl->bSimple);
4254 fprintf(debug, "nbl bounding box only distance %f\n", sqrt(rbb2));
4257 /* Set the shift range */
4258 for (d = 0; d < DIM; d++)
4260 /* Check if we need periodicity shifts.
4261 * Without PBC or with domain decomposition we don't need them.
4263 if (d >= ePBC2npbcdim(nbs->ePBC) || nbs->dd_dim[d])
4270 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
4281 if (nbl->bSimple && !gridi->bSimple)
4283 conv_i = gridi->na_sc/gridj->na_sc;
4284 bb_i = gridi->bb_simple;
4285 bbcz_i = gridi->bbcz_simple;
4286 flags_i = gridi->flags_simple;
4301 /* We use the normal bounding box format for both grid types */
4304 bbcz_i = gridi->bbcz;
4305 flags_i = gridi->flags;
4307 cell0_i = gridi->cell0*conv_i;
4309 bbcz_j = gridj->bbcz;
4313 /* Blocks of the conversion factor - 1 give a large repeat count
4314 * combined with a small block size. This should result in good
4315 * load balancing for both small and large domains.
4317 ci_block = conv_i - 1;
4321 fprintf(debug, "nbl nc_i %d col.av. %.1f ci_block %d\n",
4322 gridi->nc, gridi->nc/(double)(gridi->ncx*gridi->ncy), ci_block);
4328 /* Initially ci_b and ci to 1 before where we want them to start,
4329 * as they will both be incremented in next_ci.
4332 ci = th*ci_block - 1;
4335 while (next_ci(gridi, conv_i, nth, ci_block, &ci_x, &ci_y, &ci_b, &ci))
4337 if (nbl->bSimple && flags_i[ci] == 0)
4342 ncj_old_i = nbl->ncj;
4345 if (gridj != gridi && shp[XX] == 0)
4349 bx1 = bb_i[ci].upper[BB_X];
4353 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx;
4355 if (bx1 < gridj->c0[XX])
4357 d2cx = sqr(gridj->c0[XX] - bx1);
4366 ci_xy = ci_x*gridi->ncy + ci_y;
4368 /* Loop over shift vectors in three dimensions */
4369 for (tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
4371 shz = tz*box[ZZ][ZZ];
4373 bz0 = bbcz_i[ci*NNBSBB_D ] + shz;
4374 bz1 = bbcz_i[ci*NNBSBB_D+1] + shz;
4386 d2z = sqr(bz0 - box[ZZ][ZZ]);
4389 d2z_cx = d2z + d2cx;
4397 bz1/((real)(gridi->cxy_ind[ci_xy+1] - gridi->cxy_ind[ci_xy]));
4402 /* The check with bz1_frac close to or larger than 1 comes later */
4404 for (ty = -shp[YY]; ty <= shp[YY]; ty++)
4406 shy = ty*box[YY][YY] + tz*box[ZZ][YY];
4410 by0 = bb_i[ci].lower[BB_Y] + shy;
4411 by1 = bb_i[ci].upper[BB_Y] + shy;
4415 by0 = gridi->c0[YY] + (ci_y )*gridi->sy + shy;
4416 by1 = gridi->c0[YY] + (ci_y+1)*gridi->sy + shy;
4419 get_cell_range(by0, by1,
4420 gridj->ncy, gridj->c0[YY], gridj->sy, gridj->inv_sy,
4430 if (by1 < gridj->c0[YY])
4432 d2z_cy += sqr(gridj->c0[YY] - by1);
4434 else if (by0 > gridj->c1[YY])
4436 d2z_cy += sqr(by0 - gridj->c1[YY]);
4439 for (tx = -shp[XX]; tx <= shp[XX]; tx++)
4441 shift = XYZ2IS(tx, ty, tz);
4443 #ifdef NBNXN_SHIFT_BACKWARD
4444 if (gridi == gridj && shift > CENTRAL)
4450 shx = tx*box[XX][XX] + ty*box[YY][XX] + tz*box[ZZ][XX];
4454 bx0 = bb_i[ci].lower[BB_X] + shx;
4455 bx1 = bb_i[ci].upper[BB_X] + shx;
4459 bx0 = gridi->c0[XX] + (ci_x )*gridi->sx + shx;
4460 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx + shx;
4463 get_cell_range(bx0, bx1,
4464 gridj->ncx, gridj->c0[XX], gridj->sx, gridj->inv_sx,
4475 new_ci_entry(nbl, cell0_i+ci, shift, flags_i[ci]);
4479 new_sci_entry(nbl, cell0_i+ci, shift);
4482 #ifndef NBNXN_SHIFT_BACKWARD
4485 if (shift == CENTRAL && gridi == gridj &&
4489 /* Leave the pairs with i > j.
4490 * x is the major index, so skip half of it.
4497 set_icell_bb_simple(bb_i, ci, shx, shy, shz,
4503 set_icell_bbxxxx_supersub(pbb_i, ci, shx, shy, shz,
4506 set_icell_bb_supersub(bb_i, ci, shx, shy, shz,
4511 nbs->icell_set_x(cell0_i+ci, shx, shy, shz,
4512 gridi->na_c, nbat->xstride, nbat->x,
4515 for (cx = cxf; cx <= cxl; cx++)
4518 if (gridj->c0[XX] + cx*gridj->sx > bx1)
4520 d2zx += sqr(gridj->c0[XX] + cx*gridj->sx - bx1);
4522 else if (gridj->c0[XX] + (cx+1)*gridj->sx < bx0)
4524 d2zx += sqr(gridj->c0[XX] + (cx+1)*gridj->sx - bx0);
4527 #ifndef NBNXN_SHIFT_BACKWARD
4528 if (gridi == gridj &&
4529 cx == 0 && cyf < ci_y)
4531 if (gridi == gridj &&
4532 cx == 0 && shift == CENTRAL && cyf < ci_y)
4535 /* Leave the pairs with i > j.
4536 * Skip half of y when i and j have the same x.
4545 for (cy = cyf_x; cy <= cyl; cy++)
4547 c0 = gridj->cxy_ind[cx*gridj->ncy+cy];
4548 c1 = gridj->cxy_ind[cx*gridj->ncy+cy+1];
4549 #ifdef NBNXN_SHIFT_BACKWARD
4550 if (gridi == gridj &&
4551 shift == CENTRAL && c0 < ci)
4558 if (gridj->c0[YY] + cy*gridj->sy > by1)
4560 d2zxy += sqr(gridj->c0[YY] + cy*gridj->sy - by1);
4562 else if (gridj->c0[YY] + (cy+1)*gridj->sy < by0)
4564 d2zxy += sqr(gridj->c0[YY] + (cy+1)*gridj->sy - by0);
4566 if (c1 > c0 && d2zxy < rl2)
4568 cs = c0 + (int)(bz1_frac*(c1 - c0));
4576 /* Find the lowest cell that can possibly
4581 (bbcz_j[cf*NNBSBB_D+1] >= bz0 ||
4582 d2xy + sqr(bbcz_j[cf*NNBSBB_D+1] - bz0) < rl2))
4587 /* Find the highest cell that can possibly
4592 (bbcz_j[cl*NNBSBB_D] <= bz1 ||
4593 d2xy + sqr(bbcz_j[cl*NNBSBB_D] - bz1) < rl2))
4598 #ifdef NBNXN_REFCODE
4600 /* Simple reference code, for debugging,
4601 * overrides the more complex code above.
4606 for (k = c0; k < c1; k++)
4608 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1, bb+k) < rl2 &&
4613 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1, bb+k) < rl2 &&
4624 /* We want each atom/cell pair only once,
4625 * only use cj >= ci.
4627 #ifndef NBNXN_SHIFT_BACKWARD
4630 if (shift == CENTRAL)
4639 /* For f buffer flags with simple lists */
4640 ncj_old_j = nbl->ncj;
4642 switch (nb_kernel_type)
4644 case nbnxnk4x4_PlainC:
4645 check_subcell_list_space_simple(nbl, cl-cf+1);
4647 make_cluster_list_simple(gridj,
4649 (gridi == gridj && shift == CENTRAL),
4654 #ifdef GMX_NBNXN_SIMD_4XN
4655 case nbnxnk4xN_SIMD_4xN:
4656 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4657 make_cluster_list_simd_4xn(gridj,
4659 (gridi == gridj && shift == CENTRAL),
4665 #ifdef GMX_NBNXN_SIMD_2XNN
4666 case nbnxnk4xN_SIMD_2xNN:
4667 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4668 make_cluster_list_simd_2xnn(gridj,
4670 (gridi == gridj && shift == CENTRAL),
4676 case nbnxnk8x8x8_PlainC:
4677 case nbnxnk8x8x8_CUDA:
4678 check_subcell_list_space_supersub(nbl, cl-cf+1);
4679 for (cj = cf; cj <= cl; cj++)
4681 make_cluster_list_supersub(gridi, gridj,
4683 (gridi == gridj && shift == CENTRAL && ci == cj),
4684 nbat->xstride, nbat->x,
4690 ncpcheck += cl - cf + 1;
4692 if (bFBufferFlag && nbl->ncj > ncj_old_j)
4696 cbf = nbl->cj[ncj_old_j].cj >> gridj_flag_shift;
4697 cbl = nbl->cj[nbl->ncj-1].cj >> gridj_flag_shift;
4698 for (cb = cbf; cb <= cbl; cb++)
4700 gridj_flag[cb] = 1U<<th;
4708 /* Set the exclusions for this ci list */
4711 set_ci_top_excls(nbs,
4713 shift == CENTRAL && gridi == gridj,
4716 &(nbl->ci[nbl->nci]),
4721 set_sci_top_excls(nbs,
4723 shift == CENTRAL && gridi == gridj,
4725 &(nbl->sci[nbl->nsci]),
4729 /* Close this ci list */
4732 close_ci_entry_simple(nbl);
4736 close_ci_entry_supersub(nbl,
4738 progBal, min_ci_balanced,
4745 if (bFBufferFlag && nbl->ncj > ncj_old_i)
4747 work->buffer_flags.flag[(gridi->cell0+ci)>>gridi_flag_shift] = 1U<<th;
4751 work->ndistc = ndistc;
4753 nbs_cycle_stop(&work->cc[enbsCCsearch]);
4757 fprintf(debug, "number of distance checks %d\n", ndistc);
4758 fprintf(debug, "ncpcheck %s %d\n", gridi == gridj ? "local" : "non-local",
4763 print_nblist_statistics_simple(debug, nbl, nbs, rlist);
4767 print_nblist_statistics_supersub(debug, nbl, nbs, rlist);
4773 static void reduce_buffer_flags(const nbnxn_search_t nbs,
4775 const nbnxn_buffer_flags_t *dest)
4778 const unsigned *flag;
4780 for (s = 0; s < nsrc; s++)
4782 flag = nbs->work[s].buffer_flags.flag;
4784 for (b = 0; b < dest->nflag; b++)
4786 dest->flag[b] |= flag[b];
4791 static void print_reduction_cost(const nbnxn_buffer_flags_t *flags, int nout)
4793 int nelem, nkeep, ncopy, nred, b, c, out;
4799 for (b = 0; b < flags->nflag; b++)
4801 if (flags->flag[b] == 1)
4803 /* Only flag 0 is set, no copy of reduction required */
4807 else if (flags->flag[b] > 0)
4810 for (out = 0; out < nout; out++)
4812 if (flags->flag[b] & (1U<<out))
4829 fprintf(debug, "nbnxn reduction: #flag %d #list %d elem %4.2f, keep %4.2f copy %4.2f red %4.2f\n",
4831 nelem/(double)(flags->nflag),
4832 nkeep/(double)(flags->nflag),
4833 ncopy/(double)(flags->nflag),
4834 nred/(double)(flags->nflag));
4837 /* Perform a count (linear) sort to sort the smaller lists to the end.
4838 * This avoids load imbalance on the GPU, as large lists will be
4839 * scheduled and executed first and the smaller lists later.
4840 * Load balancing between multi-processors only happens at the end
4841 * and there smaller lists lead to more effective load balancing.
4842 * The sorting is done on the cj4 count, not on the actual pair counts.
4843 * Not only does this make the sort faster, but it also results in
4844 * better load balancing than using a list sorted on exact load.
4845 * This function swaps the pointer in the pair list to avoid a copy operation.
4847 static void sort_sci(nbnxn_pairlist_t *nbl)
4849 nbnxn_list_work_t *work;
4850 int m, i, s, s0, s1;
4851 nbnxn_sci_t *sci_sort;
4853 if (nbl->ncj4 <= nbl->nsci)
4855 /* nsci = 0 or all sci have size 1, sorting won't change the order */
4861 /* We will distinguish differences up to double the average */
4862 m = (2*nbl->ncj4)/nbl->nsci;
4864 if (m + 1 > work->sort_nalloc)
4866 work->sort_nalloc = over_alloc_large(m + 1);
4867 srenew(work->sort, work->sort_nalloc);
4870 if (work->sci_sort_nalloc != nbl->sci_nalloc)
4872 work->sci_sort_nalloc = nbl->sci_nalloc;
4873 nbnxn_realloc_void((void **)&work->sci_sort,
4875 work->sci_sort_nalloc*sizeof(*work->sci_sort),
4876 nbl->alloc, nbl->free);
4879 /* Count the entries of each size */
4880 for (i = 0; i <= m; i++)
4884 for (s = 0; s < nbl->nsci; s++)
4886 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4889 /* Calculate the offset for each count */
4892 for (i = m - 1; i >= 0; i--)
4895 work->sort[i] = work->sort[i + 1] + s0;
4899 /* Sort entries directly into place */
4900 sci_sort = work->sci_sort;
4901 for (s = 0; s < nbl->nsci; s++)
4903 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4904 sci_sort[work->sort[i]++] = nbl->sci[s];
4907 /* Swap the sci pointers so we use the new, sorted list */
4908 work->sci_sort = nbl->sci;
4909 nbl->sci = sci_sort;
4912 /* Make a local or non-local pair-list, depending on iloc */
4913 void nbnxn_make_pairlist(const nbnxn_search_t nbs,
4914 nbnxn_atomdata_t *nbat,
4915 const t_blocka *excl,
4917 int min_ci_balanced,
4918 nbnxn_pairlist_set_t *nbl_list,
4923 nbnxn_grid_t *gridi, *gridj;
4925 int nzi, zi, zj0, zj1, zj;
4929 nbnxn_pairlist_t **nbl;
4931 gmx_bool CombineNBLists;
4933 int np_tot, np_noq, np_hlj, nap;
4935 /* Check if we are running hybrid GPU + CPU nbnxn mode */
4936 bGPUCPU = (!nbs->grid[0].bSimple && nbl_list->bSimple);
4938 nnbl = nbl_list->nnbl;
4939 nbl = nbl_list->nbl;
4940 CombineNBLists = nbl_list->bCombined;
4944 fprintf(debug, "ns making %d nblists\n", nnbl);
4947 nbat->bUseBufferFlags = (nbat->nout > 1);
4948 /* We should re-init the flags before making the first list */
4949 if (nbat->bUseBufferFlags && (LOCAL_I(iloc) || bGPUCPU))
4951 init_buffer_flags(&nbat->buffer_flags, nbat->natoms);
4954 if (nbl_list->bSimple)
4956 switch (nb_kernel_type)
4958 #ifdef GMX_NBNXN_SIMD_4XN
4959 case nbnxnk4xN_SIMD_4xN:
4960 nbs->icell_set_x = icell_set_x_simd_4xn;
4963 #ifdef GMX_NBNXN_SIMD_2XNN
4964 case nbnxnk4xN_SIMD_2xNN:
4965 nbs->icell_set_x = icell_set_x_simd_2xnn;
4969 nbs->icell_set_x = icell_set_x_simple;
4975 #ifdef NBNXN_SEARCH_BB_SIMD4
4976 nbs->icell_set_x = icell_set_x_supersub_simd4;
4978 nbs->icell_set_x = icell_set_x_supersub;
4984 /* Only zone (grid) 0 vs 0 */
4991 nzi = nbs->zones->nizone;
4994 if (!nbl_list->bSimple && min_ci_balanced > 0)
4996 nsubpair_max = get_nsubpair_max(nbs, iloc, rlist, min_ci_balanced);
5003 /* Clear all pair-lists */
5004 for (th = 0; th < nnbl; th++)
5006 clear_pairlist(nbl[th]);
5009 for (zi = 0; zi < nzi; zi++)
5011 gridi = &nbs->grid[zi];
5013 if (NONLOCAL_I(iloc))
5015 zj0 = nbs->zones->izone[zi].j0;
5016 zj1 = nbs->zones->izone[zi].j1;
5022 for (zj = zj0; zj < zj1; zj++)
5024 gridj = &nbs->grid[zj];
5028 fprintf(debug, "ns search grid %d vs %d\n", zi, zj);
5031 nbs_cycle_start(&nbs->cc[enbsCCsearch]);
5033 if (nbl[0]->bSimple && !gridi->bSimple)
5035 /* Hybrid list, determine blocking later */
5040 ci_block = get_ci_block_size(gridi, nbs->DomDec, nnbl);
5043 /* With GPU: generate progressively smaller lists for
5044 * load balancing for local only or non-local with 2 zones.
5046 progBal = (LOCAL_I(iloc) || nbs->zones->n <= 2);
5048 #pragma omp parallel for num_threads(nnbl) schedule(static)
5049 for (th = 0; th < nnbl; th++)
5051 /* Re-init the thread-local work flag data before making
5052 * the first list (not an elegant conditional).
5054 if (nbat->bUseBufferFlags && ((zi == 0 && zj == 0) ||
5055 (bGPUCPU && zi == 0 && zj == 1)))
5057 init_buffer_flags(&nbs->work[th].buffer_flags, nbat->natoms);
5060 if (CombineNBLists && th > 0)
5062 clear_pairlist(nbl[th]);
5065 /* Divide the i super cell equally over the nblists */
5066 nbnxn_make_pairlist_part(nbs, gridi, gridj,
5067 &nbs->work[th], nbat, excl,
5071 nbat->bUseBufferFlags,
5073 progBal, min_ci_balanced,
5077 nbs_cycle_stop(&nbs->cc[enbsCCsearch]);
5082 for (th = 0; th < nnbl; th++)
5084 inc_nrnb(nrnb, eNR_NBNXN_DIST2, nbs->work[th].ndistc);
5086 if (nbl_list->bSimple)
5088 np_tot += nbl[th]->ncj;
5089 np_noq += nbl[th]->work->ncj_noq;
5090 np_hlj += nbl[th]->work->ncj_hlj;
5094 /* This count ignores potential subsequent pair pruning */
5095 np_tot += nbl[th]->nci_tot;
5098 nap = nbl[0]->na_ci*nbl[0]->na_cj;
5099 nbl_list->natpair_ljq = (np_tot - np_noq)*nap - np_hlj*nap/2;
5100 nbl_list->natpair_lj = np_noq*nap;
5101 nbl_list->natpair_q = np_hlj*nap/2;
5103 if (CombineNBLists && nnbl > 1)
5105 nbs_cycle_start(&nbs->cc[enbsCCcombine]);
5107 combine_nblists(nnbl-1, nbl+1, nbl[0]);
5109 nbs_cycle_stop(&nbs->cc[enbsCCcombine]);
5114 if (!nbl_list->bSimple)
5116 /* Sort the entries on size, large ones first */
5117 if (CombineNBLists || nnbl == 1)
5123 #pragma omp parallel for num_threads(nnbl) schedule(static)
5124 for (th = 0; th < nnbl; th++)
5131 if (nbat->bUseBufferFlags)
5133 reduce_buffer_flags(nbs, nnbl, &nbat->buffer_flags);
5136 /* Special performance logging stuff (env.var. GMX_NBNXN_CYCLE) */
5139 nbs->search_count++;
5141 if (nbs->print_cycles &&
5142 (!nbs->DomDec || (nbs->DomDec && !LOCAL_I(iloc))) &&
5143 nbs->search_count % 100 == 0)
5145 nbs_cycle_print(stderr, nbs);
5148 if (debug && (CombineNBLists && nnbl > 1))
5150 if (nbl[0]->bSimple)
5152 print_nblist_statistics_simple(debug, nbl[0], nbs, rlist);
5156 print_nblist_statistics_supersub(debug, nbl[0], nbs, rlist);
5164 if (nbl[0]->bSimple)
5166 print_nblist_ci_cj(debug, nbl[0]);
5170 print_nblist_sci_cj(debug, nbl[0]);
5174 if (nbat->bUseBufferFlags)
5176 print_reduction_cost(&nbat->buffer_flags, nnbl);