1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2012, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
45 #include "nbnxn_consts.h"
46 #include "nbnxn_internal.h"
47 #include "nbnxn_atomdata.h"
48 #include "nbnxn_search.h"
49 #include "gmx_cyclecounter.h"
51 #include "gmx_omp_nthreads.h"
55 /* Pair search box lower and upper corner in x,y,z.
56 * Store this in 4 iso 3 reals, which is useful with SSE.
57 * To avoid complicating the code we also use 4 without SSE.
60 #define NNBSBB_B (2*NNBSBB_C)
61 /* Pair search box lower and upper bound in z only. */
63 /* Pair search box lower and upper corner x,y,z indices */
72 #ifdef NBNXN_SEARCH_BB_SSE
73 /* We use SSE or AVX-128bit for bounding box calculations */
76 /* Single precision BBs + coordinates, we can also load coordinates using SSE */
77 #define NBNXN_SEARCH_SSE_SINGLE
80 /* Include basic SSE2 stuff */
81 #include <emmintrin.h>
83 #if defined NBNXN_SEARCH_SSE_SINGLE && (GPU_NSUBCELL == 4 || GPU_NSUBCELL == 8)
84 /* Store bounding boxes with x, y and z coordinates in packs of 4 */
88 /* The width of SSE/AVX128 with single precision for bounding boxes with GPU.
89 * Here AVX-256 turns out to be slightly slower than AVX-128.
92 #define STRIDE_PBB_2LOG 2
94 #endif /* NBNXN_SEARCH_BB_SSE */
98 /* The functions below are macros as they are performance sensitive */
100 /* 4x4 list, pack=4: no complex conversion required */
101 /* i-cluster to j-cluster conversion */
102 #define CI_TO_CJ_J4(ci) (ci)
103 /* cluster index to coordinate array index conversion */
104 #define X_IND_CI_J4(ci) ((ci)*STRIDE_P4)
105 #define X_IND_CJ_J4(cj) ((cj)*STRIDE_P4)
107 /* 4x2 list, pack=4: j-cluster size is half the packing width */
108 /* i-cluster to j-cluster conversion */
109 #define CI_TO_CJ_J2(ci) ((ci)<<1)
110 /* cluster index to coordinate array index conversion */
111 #define X_IND_CI_J2(ci) ((ci)*STRIDE_P4)
112 #define X_IND_CJ_J2(cj) (((cj)>>1)*STRIDE_P4 + ((cj) & 1)*(PACK_X4>>1))
114 /* 4x8 list, pack=8: i-cluster size is half the packing width */
115 /* i-cluster to j-cluster conversion */
116 #define CI_TO_CJ_J8(ci) ((ci)>>1)
117 /* cluster index to coordinate array index conversion */
118 #define X_IND_CI_J8(ci) (((ci)>>1)*STRIDE_P8 + ((ci) & 1)*(PACK_X8>>1))
119 #define X_IND_CJ_J8(cj) ((cj)*STRIDE_P8)
121 /* The j-cluster size is matched to the SIMD width */
122 #if GMX_NBNXN_SIMD_BITWIDTH == 128
124 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J2(ci)
125 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J2(ci)
126 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J2(cj)
128 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J4(ci)
129 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J4(ci)
130 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J4(cj)
133 #if GMX_NBNXN_SIMD_BITWIDTH == 256
135 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J4(ci)
136 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J4(ci)
137 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J4(cj)
139 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J8(ci)
140 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J8(ci)
141 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J8(cj)
142 /* Half SIMD with j-cluster size */
143 #define CI_TO_CJ_SIMD_2XNN(ci) CI_TO_CJ_J4(ci)
144 #define X_IND_CI_SIMD_2XNN(ci) X_IND_CI_J4(ci)
145 #define X_IND_CJ_SIMD_2XNN(cj) X_IND_CJ_J4(cj)
148 #error "unsupported GMX_NBNXN_SIMD_WIDTH"
152 #endif /* GMX_NBNXN_SIMD */
155 /* Interaction masks for 4xN atom interactions.
156 * Bit i*CJ_SIZE + j tells if atom i and j interact.
158 /* All interaction mask is the same for all kernels */
159 #define NBNXN_INT_MASK_ALL 0xffffffff
160 /* 4x4 kernel diagonal mask */
161 #define NBNXN_INT_MASK_DIAG 0x08ce
162 /* 4x2 kernel diagonal masks */
163 #define NBNXN_INT_MASK_DIAG_J2_0 0x0002
164 #define NBNXN_INT_MASK_DIAG_J2_1 0x002F
165 /* 4x8 kernel diagonal masks */
166 #define NBNXN_INT_MASK_DIAG_J8_0 0xf0f8fcfe
167 #define NBNXN_INT_MASK_DIAG_J8_1 0x0080c0e0
170 #ifdef NBNXN_SEARCH_BB_SSE
171 /* Store bounding boxes corners as quadruplets: xxxxyyyyzzzz */
173 /* Size of bounding box corners quadruplet */
174 #define NNBSBB_XXXX (NNBSBB_D*DIM*STRIDE_PBB)
177 /* We shift the i-particles backward for PBC.
178 * This leads to more conditionals than shifting forward.
179 * We do this to get more balanced pair lists.
181 #define NBNXN_SHIFT_BACKWARD
184 /* This define is a lazy way to avoid interdependence of the grid
185 * and searching data structures.
187 #define NBNXN_NA_SC_MAX (GPU_NSUBCELL*NBNXN_GPU_CLUSTER_SIZE)
190 static void nbs_cycle_clear(nbnxn_cycle_t *cc)
194 for (i = 0; i < enbsCCnr; i++)
201 static double Mcyc_av(const nbnxn_cycle_t *cc)
203 return (double)cc->c*1e-6/cc->count;
206 static void nbs_cycle_print(FILE *fp, const nbnxn_search_t nbs)
212 fprintf(fp, "ns %4d grid %4.1f search %4.1f red.f %5.3f",
213 nbs->cc[enbsCCgrid].count,
214 Mcyc_av(&nbs->cc[enbsCCgrid]),
215 Mcyc_av(&nbs->cc[enbsCCsearch]),
216 Mcyc_av(&nbs->cc[enbsCCreducef]));
218 if (nbs->nthread_max > 1)
220 if (nbs->cc[enbsCCcombine].count > 0)
222 fprintf(fp, " comb %5.2f",
223 Mcyc_av(&nbs->cc[enbsCCcombine]));
225 fprintf(fp, " s. th");
226 for (t = 0; t < nbs->nthread_max; t++)
228 fprintf(fp, " %4.1f",
229 Mcyc_av(&nbs->work[t].cc[enbsCCsearch]));
235 static void nbnxn_grid_init(nbnxn_grid_t * grid)
238 grid->cxy_ind = NULL;
239 grid->cxy_nalloc = 0;
245 static int get_2log(int n)
250 while ((1<<log2) < n)
256 gmx_fatal(FARGS, "nbnxn na_c (%d) is not a power of 2", n);
262 static int nbnxn_kernel_to_ci_size(int nb_kernel_type)
264 switch (nb_kernel_type)
266 case nbnxnk4x4_PlainC:
267 case nbnxnk4xN_SIMD_4xN:
268 case nbnxnk4xN_SIMD_2xNN:
269 return NBNXN_CPU_CLUSTER_I_SIZE;
270 case nbnxnk8x8x8_CUDA:
271 case nbnxnk8x8x8_PlainC:
272 /* The cluster size for super/sub lists is only set here.
273 * Any value should work for the pair-search and atomdata code.
274 * The kernels, of course, might require a particular value.
276 return NBNXN_GPU_CLUSTER_SIZE;
278 gmx_incons("unknown kernel type");
284 int nbnxn_kernel_to_cj_size(int nb_kernel_type)
286 int nbnxn_simd_width = 0;
289 #ifdef GMX_NBNXN_SIMD
290 nbnxn_simd_width = GMX_NBNXN_SIMD_BITWIDTH/(sizeof(real)*8);
293 switch (nb_kernel_type)
295 case nbnxnk4x4_PlainC:
296 cj_size = NBNXN_CPU_CLUSTER_I_SIZE;
298 case nbnxnk4xN_SIMD_4xN:
299 cj_size = nbnxn_simd_width;
301 case nbnxnk4xN_SIMD_2xNN:
302 cj_size = nbnxn_simd_width/2;
304 case nbnxnk8x8x8_CUDA:
305 case nbnxnk8x8x8_PlainC:
306 cj_size = nbnxn_kernel_to_ci_size(nb_kernel_type);
309 gmx_incons("unknown kernel type");
315 static int ci_to_cj(int na_cj_2log, int ci)
319 case 2: return ci; break;
320 case 1: return (ci<<1); break;
321 case 3: return (ci>>1); break;
327 gmx_bool nbnxn_kernel_pairlist_simple(int nb_kernel_type)
329 if (nb_kernel_type == nbnxnkNotSet)
331 gmx_fatal(FARGS, "Non-bonded kernel type not set for Verlet-style pair-list.");
334 switch (nb_kernel_type)
336 case nbnxnk8x8x8_CUDA:
337 case nbnxnk8x8x8_PlainC:
340 case nbnxnk4x4_PlainC:
341 case nbnxnk4xN_SIMD_4xN:
342 case nbnxnk4xN_SIMD_2xNN:
346 gmx_incons("Invalid nonbonded kernel type passed!");
351 void nbnxn_init_search(nbnxn_search_t * nbs_ptr,
353 gmx_domdec_zones_t *zones,
362 nbs->DomDec = (n_dd_cells != NULL);
364 clear_ivec(nbs->dd_dim);
370 for (d = 0; d < DIM; d++)
372 if ((*n_dd_cells)[d] > 1)
375 /* Each grid matches a DD zone */
381 snew(nbs->grid, nbs->ngrid);
382 for (g = 0; g < nbs->ngrid; g++)
384 nbnxn_grid_init(&nbs->grid[g]);
387 nbs->cell_nalloc = 0;
391 nbs->nthread_max = nthread_max;
393 /* Initialize the work data structures for each thread */
394 snew(nbs->work, nbs->nthread_max);
395 for (t = 0; t < nbs->nthread_max; t++)
397 nbs->work[t].cxy_na = NULL;
398 nbs->work[t].cxy_na_nalloc = 0;
399 nbs->work[t].sort_work = NULL;
400 nbs->work[t].sort_work_nalloc = 0;
403 /* Initialize detailed nbsearch cycle counting */
404 nbs->print_cycles = (getenv("GMX_NBNXN_CYCLE") != 0);
405 nbs->search_count = 0;
406 nbs_cycle_clear(nbs->cc);
407 for (t = 0; t < nbs->nthread_max; t++)
409 nbs_cycle_clear(nbs->work[t].cc);
413 static real grid_atom_density(int n, rvec corner0, rvec corner1)
417 rvec_sub(corner1, corner0, size);
419 return n/(size[XX]*size[YY]*size[ZZ]);
422 static int set_grid_size_xy(const nbnxn_search_t nbs,
425 int n, rvec corner0, rvec corner1,
431 real adens, tlen, tlen_x, tlen_y, nc_max;
434 rvec_sub(corner1, corner0, size);
438 /* target cell length */
441 /* To minimize the zero interactions, we should make
442 * the largest of the i/j cell cubic.
444 na_c = max(grid->na_c, grid->na_cj);
446 /* Approximately cubic cells */
447 tlen = pow(na_c/atom_density, 1.0/3.0);
453 /* Approximately cubic sub cells */
454 tlen = pow(grid->na_c/atom_density, 1.0/3.0);
455 tlen_x = tlen*GPU_NSUBCELL_X;
456 tlen_y = tlen*GPU_NSUBCELL_Y;
458 /* We round ncx and ncy down, because we get less cell pairs
459 * in the nbsist when the fixed cell dimensions (x,y) are
460 * larger than the variable one (z) than the other way around.
462 grid->ncx = max(1, (int)(size[XX]/tlen_x));
463 grid->ncy = max(1, (int)(size[YY]/tlen_y));
471 grid->sx = size[XX]/grid->ncx;
472 grid->sy = size[YY]/grid->ncy;
473 grid->inv_sx = 1/grid->sx;
474 grid->inv_sy = 1/grid->sy;
478 /* This is a non-home zone, add an extra row of cells
479 * for particles communicated for bonded interactions.
480 * These can be beyond the cut-off. It doesn't matter where
481 * they end up on the grid, but for performance it's better
482 * if they don't end up in cells that can be within cut-off range.
488 /* We need one additional cell entry for particles moved by DD */
489 if (grid->ncx*grid->ncy+1 > grid->cxy_nalloc)
491 grid->cxy_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
492 srenew(grid->cxy_na, grid->cxy_nalloc);
493 srenew(grid->cxy_ind, grid->cxy_nalloc+1);
495 for (t = 0; t < nbs->nthread_max; t++)
497 if (grid->ncx*grid->ncy+1 > nbs->work[t].cxy_na_nalloc)
499 nbs->work[t].cxy_na_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
500 srenew(nbs->work[t].cxy_na, nbs->work[t].cxy_na_nalloc);
504 /* Worst case scenario of 1 atom in each last cell */
505 if (grid->na_cj <= grid->na_c)
507 nc_max = n/grid->na_sc + grid->ncx*grid->ncy;
511 nc_max = n/grid->na_sc + grid->ncx*grid->ncy*grid->na_cj/grid->na_c;
514 if (nc_max > grid->nc_nalloc)
518 grid->nc_nalloc = over_alloc_large(nc_max);
519 srenew(grid->nsubc, grid->nc_nalloc);
520 srenew(grid->bbcz, grid->nc_nalloc*NNBSBB_D);
522 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX;
524 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL*NNBSBB_B;
526 sfree_aligned(grid->bb);
527 /* This snew also zeros the contents, this avoid possible
528 * floating exceptions in SSE with the unused bb elements.
530 snew_aligned(grid->bb, bb_nalloc, 16);
534 if (grid->na_cj == grid->na_c)
536 grid->bbj = grid->bb;
540 sfree_aligned(grid->bbj);
541 snew_aligned(grid->bbj, bb_nalloc*grid->na_c/grid->na_cj, 16);
545 srenew(grid->flags, grid->nc_nalloc);
548 copy_rvec(corner0, grid->c0);
549 copy_rvec(corner1, grid->c1);
554 /* We need to sort paricles in grid columns on z-coordinate.
555 * As particle are very often distributed homogeneously, we a sorting
556 * algorithm similar to pigeonhole sort. We multiply the z-coordinate
557 * by a factor, cast to an int and try to store in that hole. If the hole
558 * is full, we move this or another particle. A second pass is needed to make
559 * contiguous elements. SORT_GRID_OVERSIZE is the ratio of holes to particles.
560 * 4 is the optimal value for homogeneous particle distribution and allows
561 * for an O(#particles) sort up till distributions were all particles are
562 * concentrated in 1/4 of the space. No NlogN fallback is implemented,
563 * as it can be expensive to detect imhomogeneous particle distributions.
564 * SGSF is the maximum ratio of holes used, in the worst case all particles
565 * end up in the last hole and we need #particles extra holes at the end.
567 #define SORT_GRID_OVERSIZE 4
568 #define SGSF (SORT_GRID_OVERSIZE + 1)
570 /* Sort particle index a on coordinates x along dim.
571 * Backwards tells if we want decreasing iso increasing coordinates.
572 * h0 is the minimum of the coordinate range.
573 * invh is the inverse hole spacing.
574 * nsort, the theortical hole limit, is only used for debugging.
575 * sort is the sorting work array.
577 static void sort_atoms(int dim, gmx_bool Backwards,
578 int *a, int n, rvec *x,
579 real h0, real invh, int nsort, int *sort)
582 int zi, zim, zi_min, zi_max;
591 /* Determine the index range used, so we can limit it for the second pass */
595 /* Sort the particles using a simple index sort */
596 for (i = 0; i < n; i++)
598 /* The cast takes care of float-point rounding effects below zero.
599 * This code assumes particles are less than 1/SORT_GRID_OVERSIZE
600 * times the box height out of the box.
602 zi = (int)((x[a[i]][dim] - h0)*invh);
604 #ifdef DEBUG_NBNXN_GRIDDING
605 if (zi < 0 || zi >= nsort)
607 gmx_fatal(FARGS, "(int)((x[%d][%c]=%f - %f)*%f) = %d, not in 0 - %d\n",
608 a[i], 'x'+dim, x[a[i]][dim], h0, invh, zi, nsort);
612 /* Ideally this particle should go in sort cell zi,
613 * but that might already be in use,
614 * in that case find the first empty cell higher up
619 zi_min = min(zi_min, zi);
620 zi_max = max(zi_max, zi);
624 /* We have multiple atoms in the same sorting slot.
625 * Sort on real z for minimal bounding box size.
626 * There is an extra check for identical z to ensure
627 * well-defined output order, independent of input order
628 * to ensure binary reproducibility after restarts.
630 while (sort[zi] >= 0 && ( x[a[i]][dim] > x[sort[zi]][dim] ||
631 (x[a[i]][dim] == x[sort[zi]][dim] &&
639 /* Shift all elements by one slot until we find an empty slot */
642 while (sort[zim] >= 0)
650 zi_max = max(zi_max, zim);
653 zi_max = max(zi_max, zi);
660 for (zi = 0; zi < nsort; zi++)
671 for (zi = zi_max; zi >= zi_min; zi--)
682 gmx_incons("Lost particles while sorting");
687 #define R2F_D(x) ((float)((x) >= 0 ? ((1-GMX_FLOAT_EPS)*(x)) : ((1+GMX_FLOAT_EPS)*(x))))
688 #define R2F_U(x) ((float)((x) >= 0 ? ((1+GMX_FLOAT_EPS)*(x)) : ((1-GMX_FLOAT_EPS)*(x))))
694 /* Coordinate order x,y,z, bb order xyz0 */
695 static void calc_bounding_box(int na, int stride, const real *x, float *bb)
698 real xl, xh, yl, yh, zl, zh;
708 for (j = 1; j < na; j++)
710 xl = min(xl, x[i+XX]);
711 xh = max(xh, x[i+XX]);
712 yl = min(yl, x[i+YY]);
713 yh = max(yh, x[i+YY]);
714 zl = min(zl, x[i+ZZ]);
715 zh = max(zh, x[i+ZZ]);
718 /* Note: possible double to float conversion here */
719 bb[BBL_X] = R2F_D(xl);
720 bb[BBL_Y] = R2F_D(yl);
721 bb[BBL_Z] = R2F_D(zl);
722 bb[BBU_X] = R2F_U(xh);
723 bb[BBU_Y] = R2F_U(yh);
724 bb[BBU_Z] = R2F_U(zh);
727 /* Packed coordinates, bb order xyz0 */
728 static void calc_bounding_box_x_x4(int na, const real *x, float *bb)
731 real xl, xh, yl, yh, zl, zh;
739 for (j = 1; j < na; j++)
741 xl = min(xl, x[j+XX*PACK_X4]);
742 xh = max(xh, x[j+XX*PACK_X4]);
743 yl = min(yl, x[j+YY*PACK_X4]);
744 yh = max(yh, x[j+YY*PACK_X4]);
745 zl = min(zl, x[j+ZZ*PACK_X4]);
746 zh = max(zh, x[j+ZZ*PACK_X4]);
748 /* Note: possible double to float conversion here */
749 bb[BBL_X] = R2F_D(xl);
750 bb[BBL_Y] = R2F_D(yl);
751 bb[BBL_Z] = R2F_D(zl);
752 bb[BBU_X] = R2F_U(xh);
753 bb[BBU_Y] = R2F_U(yh);
754 bb[BBU_Z] = R2F_U(zh);
757 /* Packed coordinates, bb order xyz0 */
758 static void calc_bounding_box_x_x8(int na, const real *x, float *bb)
761 real xl, xh, yl, yh, zl, zh;
769 for (j = 1; j < na; j++)
771 xl = min(xl, x[j+XX*PACK_X8]);
772 xh = max(xh, x[j+XX*PACK_X8]);
773 yl = min(yl, x[j+YY*PACK_X8]);
774 yh = max(yh, x[j+YY*PACK_X8]);
775 zl = min(zl, x[j+ZZ*PACK_X8]);
776 zh = max(zh, x[j+ZZ*PACK_X8]);
778 /* Note: possible double to float conversion here */
779 bb[BBL_X] = R2F_D(xl);
780 bb[BBL_Y] = R2F_D(yl);
781 bb[BBL_Z] = R2F_D(zl);
782 bb[BBU_X] = R2F_U(xh);
783 bb[BBU_Y] = R2F_U(yh);
784 bb[BBU_Z] = R2F_U(zh);
787 #ifdef NBNXN_SEARCH_BB_SSE
789 /* Packed coordinates, bb order xyz0 */
790 static void calc_bounding_box_x_x4_halves(int na, const real *x,
791 float *bb, float *bbj)
793 calc_bounding_box_x_x4(min(na, 2), x, bbj);
797 calc_bounding_box_x_x4(min(na-2, 2), x+(PACK_X4>>1), bbj+NNBSBB_B);
801 /* Set the "empty" bounding box to the same as the first one,
802 * so we don't need to treat special cases in the rest of the code.
804 _mm_store_ps(bbj+NNBSBB_B, _mm_load_ps(bbj));
805 _mm_store_ps(bbj+NNBSBB_B+NNBSBB_C, _mm_load_ps(bbj+NNBSBB_C));
808 _mm_store_ps(bb, _mm_min_ps(_mm_load_ps(bbj),
809 _mm_load_ps(bbj+NNBSBB_B)));
810 _mm_store_ps(bb+NNBSBB_C, _mm_max_ps(_mm_load_ps(bbj+NNBSBB_C),
811 _mm_load_ps(bbj+NNBSBB_B+NNBSBB_C)));
814 /* Coordinate order xyz, bb order xxxxyyyyzzzz */
815 static void calc_bounding_box_xxxx(int na, int stride, const real *x, float *bb)
818 real xl, xh, yl, yh, zl, zh;
828 for (j = 1; j < na; j++)
830 xl = min(xl, x[i+XX]);
831 xh = max(xh, x[i+XX]);
832 yl = min(yl, x[i+YY]);
833 yh = max(yh, x[i+YY]);
834 zl = min(zl, x[i+ZZ]);
835 zh = max(zh, x[i+ZZ]);
838 /* Note: possible double to float conversion here */
839 bb[0*STRIDE_PBB] = R2F_D(xl);
840 bb[1*STRIDE_PBB] = R2F_D(yl);
841 bb[2*STRIDE_PBB] = R2F_D(zl);
842 bb[3*STRIDE_PBB] = R2F_U(xh);
843 bb[4*STRIDE_PBB] = R2F_U(yh);
844 bb[5*STRIDE_PBB] = R2F_U(zh);
847 #endif /* NBNXN_SEARCH_BB_SSE */
849 #ifdef NBNXN_SEARCH_SSE_SINGLE
851 /* Coordinate order xyz?, bb order xyz0 */
852 static void calc_bounding_box_sse(int na, const float *x, float *bb)
854 __m128 bb_0_SSE, bb_1_SSE;
859 bb_0_SSE = _mm_load_ps(x);
862 for (i = 1; i < na; i++)
864 x_SSE = _mm_load_ps(x+i*NNBSBB_C);
865 bb_0_SSE = _mm_min_ps(bb_0_SSE, x_SSE);
866 bb_1_SSE = _mm_max_ps(bb_1_SSE, x_SSE);
869 _mm_store_ps(bb, bb_0_SSE);
870 _mm_store_ps(bb+4, bb_1_SSE);
873 /* Coordinate order xyz?, bb order xxxxyyyyzzzz */
874 static void calc_bounding_box_xxxx_sse(int na, const float *x,
878 calc_bounding_box_sse(na, x, bb_work);
880 bb[0*STRIDE_PBB] = bb_work[BBL_X];
881 bb[1*STRIDE_PBB] = bb_work[BBL_Y];
882 bb[2*STRIDE_PBB] = bb_work[BBL_Z];
883 bb[3*STRIDE_PBB] = bb_work[BBU_X];
884 bb[4*STRIDE_PBB] = bb_work[BBU_Y];
885 bb[5*STRIDE_PBB] = bb_work[BBU_Z];
888 #endif /* NBNXN_SEARCH_SSE_SINGLE */
890 #ifdef NBNXN_SEARCH_BB_SSE
892 /* Combines pairs of consecutive bounding boxes */
893 static void combine_bounding_box_pairs(nbnxn_grid_t *grid, const float *bb)
895 int i, j, sc2, nc2, c2;
896 __m128 min_SSE, max_SSE;
898 for (i = 0; i < grid->ncx*grid->ncy; i++)
900 /* Starting bb in a column is expected to be 2-aligned */
901 sc2 = grid->cxy_ind[i]>>1;
902 /* For odd numbers skip the last bb here */
903 nc2 = (grid->cxy_na[i]+3)>>(2+1);
904 for (c2 = sc2; c2 < sc2+nc2; c2++)
906 min_SSE = _mm_min_ps(_mm_load_ps(bb+(c2*4+0)*NNBSBB_C),
907 _mm_load_ps(bb+(c2*4+2)*NNBSBB_C));
908 max_SSE = _mm_max_ps(_mm_load_ps(bb+(c2*4+1)*NNBSBB_C),
909 _mm_load_ps(bb+(c2*4+3)*NNBSBB_C));
910 _mm_store_ps(grid->bbj+(c2*2+0)*NNBSBB_C, min_SSE);
911 _mm_store_ps(grid->bbj+(c2*2+1)*NNBSBB_C, max_SSE);
913 if (((grid->cxy_na[i]+3)>>2) & 1)
915 /* Copy the last bb for odd bb count in this column */
916 for (j = 0; j < NNBSBB_C; j++)
918 grid->bbj[(c2*2+0)*NNBSBB_C+j] = bb[(c2*4+0)*NNBSBB_C+j];
919 grid->bbj[(c2*2+1)*NNBSBB_C+j] = bb[(c2*4+1)*NNBSBB_C+j];
928 /* Prints the average bb size, used for debug output */
929 static void print_bbsizes_simple(FILE *fp,
930 const nbnxn_search_t nbs,
931 const nbnxn_grid_t *grid)
937 for (c = 0; c < grid->nc; c++)
939 for (d = 0; d < DIM; d++)
941 ba[d] += grid->bb[c*NNBSBB_B+NNBSBB_C+d] - grid->bb[c*NNBSBB_B+d];
944 dsvmul(1.0/grid->nc, ba, ba);
946 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
947 nbs->box[XX][XX]/grid->ncx,
948 nbs->box[YY][YY]/grid->ncy,
949 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/grid->nc,
950 ba[XX], ba[YY], ba[ZZ],
951 ba[XX]*grid->ncx/nbs->box[XX][XX],
952 ba[YY]*grid->ncy/nbs->box[YY][YY],
953 ba[ZZ]*grid->nc/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
956 /* Prints the average bb size, used for debug output */
957 static void print_bbsizes_supersub(FILE *fp,
958 const nbnxn_search_t nbs,
959 const nbnxn_grid_t *grid)
966 for (c = 0; c < grid->nc; c++)
969 for (s = 0; s < grid->nsubc[c]; s += STRIDE_PBB)
973 cs_w = (c*GPU_NSUBCELL + s)/STRIDE_PBB;
974 for (i = 0; i < STRIDE_PBB; i++)
976 for (d = 0; d < DIM; d++)
979 grid->bb[cs_w*NNBSBB_XXXX+(DIM+d)*STRIDE_PBB+i] -
980 grid->bb[cs_w*NNBSBB_XXXX+ d *STRIDE_PBB+i];
985 for (s = 0; s < grid->nsubc[c]; s++)
989 cs = c*GPU_NSUBCELL + s;
990 for (d = 0; d < DIM; d++)
993 grid->bb[cs*NNBSBB_B+NNBSBB_C+d] -
994 grid->bb[cs*NNBSBB_B +d];
998 ns += grid->nsubc[c];
1000 dsvmul(1.0/ns, ba, ba);
1002 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
1003 nbs->box[XX][XX]/(grid->ncx*GPU_NSUBCELL_X),
1004 nbs->box[YY][YY]/(grid->ncy*GPU_NSUBCELL_Y),
1005 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z),
1006 ba[XX], ba[YY], ba[ZZ],
1007 ba[XX]*grid->ncx*GPU_NSUBCELL_X/nbs->box[XX][XX],
1008 ba[YY]*grid->ncy*GPU_NSUBCELL_Y/nbs->box[YY][YY],
1009 ba[ZZ]*grid->nc*GPU_NSUBCELL_Z/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
1012 /* Potentially sorts atoms on LJ coefficients !=0 and ==0.
1013 * Also sets interaction flags.
1015 void sort_on_lj(nbnxn_atomdata_t *nbat, int na_c,
1016 int a0, int a1, const int *atinfo,
1020 int subc, s, a, n1, n2, a_lj_max, i, j;
1021 int sort1[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1022 int sort2[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1028 for (s = a0; s < a1; s += na_c)
1030 /* Make lists for this (sub-)cell on atoms with and without LJ */
1035 for (a = s; a < min(s+na_c, a1); a++)
1037 haveQ = haveQ || GET_CGINFO_HAS_Q(atinfo[order[a]]);
1039 if (GET_CGINFO_HAS_VDW(atinfo[order[a]]))
1041 sort1[n1++] = order[a];
1046 sort2[n2++] = order[a];
1050 /* If we don't have atom with LJ, there's nothing to sort */
1053 *flags |= NBNXN_CI_DO_LJ(subc);
1057 /* Only sort when strictly necessary. Ordering particles
1058 * Ordering particles can lead to less accurate summation
1059 * due to rounding, both for LJ and Coulomb interactions.
1061 if (2*(a_lj_max - s) >= na_c)
1063 for (i = 0; i < n1; i++)
1065 order[a0+i] = sort1[i];
1067 for (j = 0; j < n2; j++)
1069 order[a0+n1+j] = sort2[j];
1073 *flags |= NBNXN_CI_HALF_LJ(subc);
1078 *flags |= NBNXN_CI_DO_COUL(subc);
1084 /* Fill a pair search cell with atoms.
1085 * Potentially sorts atoms and sets the interaction flags.
1087 void fill_cell(const nbnxn_search_t nbs,
1089 nbnxn_atomdata_t *nbat,
1093 int sx, int sy, int sz,
1104 sort_on_lj(nbat, grid->na_c, a0, a1, atinfo, nbs->a,
1105 grid->flags+(a0>>grid->na_c_2log)-grid->cell0);
1108 /* Now we have sorted the atoms, set the cell indices */
1109 for (a = a0; a < a1; a++)
1111 nbs->cell[nbs->a[a]] = a;
1114 copy_rvec_to_nbat_real(nbs->a+a0, a1-a0, grid->na_c, x,
1115 nbat->XFormat, nbat->x, a0,
1118 if (nbat->XFormat == nbatX4)
1120 /* Store the bounding boxes as xyz.xyz. */
1121 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1122 bb_ptr = grid->bb + offset;
1124 #if defined GMX_DOUBLE && defined NBNXN_SEARCH_BB_SSE
1125 if (2*grid->na_cj == grid->na_c)
1127 calc_bounding_box_x_x4_halves(na, nbat->x+X4_IND_A(a0), bb_ptr,
1128 grid->bbj+offset*2);
1133 calc_bounding_box_x_x4(na, nbat->x+X4_IND_A(a0), bb_ptr);
1136 else if (nbat->XFormat == nbatX8)
1138 /* Store the bounding boxes as xyz.xyz. */
1139 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1140 bb_ptr = grid->bb + offset;
1142 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(a0), bb_ptr);
1145 else if (!grid->bSimple)
1147 /* Store the bounding boxes in a format convenient
1148 * for SSE calculations: xxxxyyyyzzzz...
1152 ((a0-grid->cell0*grid->na_sc)>>(grid->na_c_2log+STRIDE_PBB_2LOG))*NNBSBB_XXXX +
1153 (((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log) & (STRIDE_PBB-1));
1155 #ifdef NBNXN_SEARCH_SSE_SINGLE
1156 if (nbat->XFormat == nbatXYZQ)
1158 calc_bounding_box_xxxx_sse(na, nbat->x+a0*nbat->xstride,
1164 calc_bounding_box_xxxx(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1169 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1171 bb_ptr[0*STRIDE_PBB], bb_ptr[3*STRIDE_PBB],
1172 bb_ptr[1*STRIDE_PBB], bb_ptr[4*STRIDE_PBB],
1173 bb_ptr[2*STRIDE_PBB], bb_ptr[5*STRIDE_PBB]);
1179 /* Store the bounding boxes as xyz.xyz. */
1180 bb_ptr = grid->bb+((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1182 calc_bounding_box(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1188 bbo = (a0 - grid->cell0*grid->na_sc)/grid->na_c;
1189 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1191 (grid->bb+bbo*NNBSBB_B)[BBL_X],
1192 (grid->bb+bbo*NNBSBB_B)[BBU_X],
1193 (grid->bb+bbo*NNBSBB_B)[BBL_Y],
1194 (grid->bb+bbo*NNBSBB_B)[BBU_Y],
1195 (grid->bb+bbo*NNBSBB_B)[BBL_Z],
1196 (grid->bb+bbo*NNBSBB_B)[BBU_Z]);
1201 /* Spatially sort the atoms within one grid column */
1202 static void sort_columns_simple(const nbnxn_search_t nbs,
1208 nbnxn_atomdata_t *nbat,
1209 int cxy_start, int cxy_end,
1213 int cx, cy, cz, ncz, cfilled, c;
1214 int na, ash, ind, a;
1219 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1220 grid->cell0, cxy_start, cxy_end, a0, a1);
1223 /* Sort the atoms within each x,y column in 3 dimensions */
1224 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1227 cy = cxy - cx*grid->ncy;
1229 na = grid->cxy_na[cxy];
1230 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1231 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1233 /* Sort the atoms within each x,y column on z coordinate */
1234 sort_atoms(ZZ, FALSE,
1237 ncz*grid->na_sc*SORT_GRID_OVERSIZE/nbs->box[ZZ][ZZ],
1238 ncz*grid->na_sc*SGSF, sort_work);
1240 /* Fill the ncz cells in this column */
1241 cfilled = grid->cxy_ind[cxy];
1242 for (cz = 0; cz < ncz; cz++)
1244 c = grid->cxy_ind[cxy] + cz;
1246 ash_c = ash + cz*grid->na_sc;
1247 na_c = min(grid->na_sc, na-(ash_c-ash));
1249 fill_cell(nbs, grid, nbat,
1250 ash_c, ash_c+na_c, atinfo, x,
1251 grid->na_sc*cx + (dd_zone >> 2),
1252 grid->na_sc*cy + (dd_zone & 3),
1256 /* This copy to bbcz is not really necessary.
1257 * But it allows to use the same grid search code
1258 * for the simple and supersub cell setups.
1264 grid->bbcz[c*NNBSBB_D ] = grid->bb[cfilled*NNBSBB_B+2];
1265 grid->bbcz[c*NNBSBB_D+1] = grid->bb[cfilled*NNBSBB_B+6];
1268 /* Set the unused atom indices to -1 */
1269 for (ind = na; ind < ncz*grid->na_sc; ind++)
1271 nbs->a[ash+ind] = -1;
1276 /* Spatially sort the atoms within one grid column */
1277 static void sort_columns_supersub(const nbnxn_search_t nbs,
1283 nbnxn_atomdata_t *nbat,
1284 int cxy_start, int cxy_end,
1288 int cx, cy, cz = -1, c = -1, ncz;
1289 int na, ash, na_c, ind, a;
1290 int subdiv_z, sub_z, na_z, ash_z;
1291 int subdiv_y, sub_y, na_y, ash_y;
1292 int subdiv_x, sub_x, na_x, ash_x;
1294 /* cppcheck-suppress unassignedVariable */
1295 float bb_work_array[NNBSBB_B+3], *bb_work_align;
1297 bb_work_align = (float *)(((size_t)(bb_work_array+3)) & (~((size_t)15)));
1301 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1302 grid->cell0, cxy_start, cxy_end, a0, a1);
1305 subdiv_x = grid->na_c;
1306 subdiv_y = GPU_NSUBCELL_X*subdiv_x;
1307 subdiv_z = GPU_NSUBCELL_Y*subdiv_y;
1309 /* Sort the atoms within each x,y column in 3 dimensions */
1310 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1313 cy = cxy - cx*grid->ncy;
1315 na = grid->cxy_na[cxy];
1316 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1317 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1319 /* Sort the atoms within each x,y column on z coordinate */
1320 sort_atoms(ZZ, FALSE,
1323 ncz*grid->na_sc*SORT_GRID_OVERSIZE/nbs->box[ZZ][ZZ],
1324 ncz*grid->na_sc*SGSF, sort_work);
1326 /* This loop goes over the supercells and subcells along z at once */
1327 for (sub_z = 0; sub_z < ncz*GPU_NSUBCELL_Z; sub_z++)
1329 ash_z = ash + sub_z*subdiv_z;
1330 na_z = min(subdiv_z, na-(ash_z-ash));
1332 /* We have already sorted on z */
1334 if (sub_z % GPU_NSUBCELL_Z == 0)
1336 cz = sub_z/GPU_NSUBCELL_Z;
1337 c = grid->cxy_ind[cxy] + cz;
1339 /* The number of atoms in this supercell */
1340 na_c = min(grid->na_sc, na-(ash_z-ash));
1342 grid->nsubc[c] = min(GPU_NSUBCELL, (na_c+grid->na_c-1)/grid->na_c);
1344 /* Store the z-boundaries of the super cell */
1345 grid->bbcz[c*NNBSBB_D ] = x[nbs->a[ash_z]][ZZ];
1346 grid->bbcz[c*NNBSBB_D+1] = x[nbs->a[ash_z+na_c-1]][ZZ];
1349 #if GPU_NSUBCELL_Y > 1
1350 /* Sort the atoms along y */
1351 sort_atoms(YY, (sub_z & 1),
1352 nbs->a+ash_z, na_z, x,
1353 grid->c0[YY]+cy*grid->sy, grid->inv_sy,
1354 subdiv_y*SGSF, sort_work);
1357 for (sub_y = 0; sub_y < GPU_NSUBCELL_Y; sub_y++)
1359 ash_y = ash_z + sub_y*subdiv_y;
1360 na_y = min(subdiv_y, na-(ash_y-ash));
1362 #if GPU_NSUBCELL_X > 1
1363 /* Sort the atoms along x */
1364 sort_atoms(XX, ((cz*GPU_NSUBCELL_Y + sub_y) & 1),
1365 nbs->a+ash_y, na_y, x,
1366 grid->c0[XX]+cx*grid->sx, grid->inv_sx,
1367 subdiv_x*SGSF, sort_work);
1370 for (sub_x = 0; sub_x < GPU_NSUBCELL_X; sub_x++)
1372 ash_x = ash_y + sub_x*subdiv_x;
1373 na_x = min(subdiv_x, na-(ash_x-ash));
1375 fill_cell(nbs, grid, nbat,
1376 ash_x, ash_x+na_x, atinfo, x,
1377 grid->na_c*(cx*GPU_NSUBCELL_X+sub_x) + (dd_zone >> 2),
1378 grid->na_c*(cy*GPU_NSUBCELL_Y+sub_y) + (dd_zone & 3),
1385 /* Set the unused atom indices to -1 */
1386 for (ind = na; ind < ncz*grid->na_sc; ind++)
1388 nbs->a[ash+ind] = -1;
1393 /* Determine in which grid column atoms should go */
1394 static void calc_column_indices(nbnxn_grid_t *grid,
1397 int dd_zone, const int *move,
1398 int thread, int nthread,
1405 /* We add one extra cell for particles which moved during DD */
1406 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1411 n0 = a0 + (int)((thread+0)*(a1 - a0))/nthread;
1412 n1 = a0 + (int)((thread+1)*(a1 - a0))/nthread;
1416 for (i = n0; i < n1; i++)
1418 if (move == NULL || move[i] >= 0)
1420 /* We need to be careful with rounding,
1421 * particles might be a few bits outside the local zone.
1422 * The int cast takes care of the lower bound,
1423 * we will explicitly take care of the upper bound.
1425 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1426 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1428 #ifdef DEBUG_NBNXN_GRIDDING
1429 if (cx < 0 || cx >= grid->ncx ||
1430 cy < 0 || cy >= grid->ncy)
1433 "grid cell cx %d cy %d out of range (max %d %d)\n"
1434 "atom %f %f %f, grid->c0 %f %f",
1435 cx, cy, grid->ncx, grid->ncy,
1436 x[i][XX], x[i][YY], x[i][ZZ], grid->c0[XX], grid->c0[YY]);
1439 /* Take care of potential rouding issues */
1440 cx = min(cx, grid->ncx - 1);
1441 cy = min(cy, grid->ncy - 1);
1443 /* For the moment cell will contain only the, grid local,
1444 * x and y indices, not z.
1446 cell[i] = cx*grid->ncy + cy;
1450 /* Put this moved particle after the end of the grid,
1451 * so we can process it later without using conditionals.
1453 cell[i] = grid->ncx*grid->ncy;
1462 for (i = n0; i < n1; i++)
1464 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1465 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1467 /* For non-home zones there could be particles outside
1468 * the non-bonded cut-off range, which have been communicated
1469 * for bonded interactions only. For the result it doesn't
1470 * matter where these end up on the grid. For performance
1471 * we put them in an extra row at the border.
1474 cx = min(cx, grid->ncx - 1);
1476 cy = min(cy, grid->ncy - 1);
1478 /* For the moment cell will contain only the, grid local,
1479 * x and y indices, not z.
1481 cell[i] = cx*grid->ncy + cy;
1488 /* Determine in which grid cells the atoms should go */
1489 static void calc_cell_indices(const nbnxn_search_t nbs,
1496 nbnxn_atomdata_t *nbat)
1499 int cx, cy, cxy, ncz_max, ncz;
1500 int nthread, thread;
1501 int *cxy_na, cxy_na_i;
1503 nthread = gmx_omp_nthreads_get(emntPairsearch);
1505 #pragma omp parallel for num_threads(nthread) schedule(static)
1506 for (thread = 0; thread < nthread; thread++)
1508 calc_column_indices(grid, a0, a1, x, dd_zone, move, thread, nthread,
1509 nbs->cell, nbs->work[thread].cxy_na);
1512 /* Make the cell index as a function of x and y */
1515 grid->cxy_ind[0] = 0;
1516 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1518 /* We set ncz_max at the beginning of the loop iso at the end
1519 * to skip i=grid->ncx*grid->ncy which are moved particles
1520 * that do not need to be ordered on the grid.
1526 cxy_na_i = nbs->work[0].cxy_na[i];
1527 for (thread = 1; thread < nthread; thread++)
1529 cxy_na_i += nbs->work[thread].cxy_na[i];
1531 ncz = (cxy_na_i + grid->na_sc - 1)/grid->na_sc;
1532 if (nbat->XFormat == nbatX8)
1534 /* Make the number of cell a multiple of 2 */
1535 ncz = (ncz + 1) & ~1;
1537 grid->cxy_ind[i+1] = grid->cxy_ind[i] + ncz;
1538 /* Clear cxy_na, so we can reuse the array below */
1539 grid->cxy_na[i] = 0;
1541 grid->nc = grid->cxy_ind[grid->ncx*grid->ncy] - grid->cxy_ind[0];
1543 nbat->natoms = (grid->cell0 + grid->nc)*grid->na_sc;
1547 fprintf(debug, "ns na_sc %d na_c %d super-cells: %d x %d y %d z %.1f maxz %d\n",
1548 grid->na_sc, grid->na_c, grid->nc,
1549 grid->ncx, grid->ncy, grid->nc/((double)(grid->ncx*grid->ncy)),
1554 for (cy = 0; cy < grid->ncy; cy++)
1556 for (cx = 0; cx < grid->ncx; cx++)
1558 fprintf(debug, " %2d", grid->cxy_ind[i+1]-grid->cxy_ind[i]);
1561 fprintf(debug, "\n");
1566 /* Make sure the work array for sorting is large enough */
1567 if (ncz_max*grid->na_sc*SGSF > nbs->work[0].sort_work_nalloc)
1569 for (thread = 0; thread < nbs->nthread_max; thread++)
1571 nbs->work[thread].sort_work_nalloc =
1572 over_alloc_large(ncz_max*grid->na_sc*SGSF);
1573 srenew(nbs->work[thread].sort_work,
1574 nbs->work[thread].sort_work_nalloc);
1575 /* When not in use, all elements should be -1 */
1576 for (i = 0; i < nbs->work[thread].sort_work_nalloc; i++)
1578 nbs->work[thread].sort_work[i] = -1;
1583 /* Now we know the dimensions we can fill the grid.
1584 * This is the first, unsorted fill. We sort the columns after this.
1586 for (i = a0; i < a1; i++)
1588 /* At this point nbs->cell contains the local grid x,y indices */
1590 nbs->a[(grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc + grid->cxy_na[cxy]++] = i;
1595 /* Set the cell indices for the moved particles */
1596 n0 = grid->nc*grid->na_sc;
1597 n1 = grid->nc*grid->na_sc+grid->cxy_na[grid->ncx*grid->ncy];
1600 for (i = n0; i < n1; i++)
1602 nbs->cell[nbs->a[i]] = i;
1607 /* Sort the super-cell columns along z into the sub-cells. */
1608 #pragma omp parallel for num_threads(nbs->nthread_max) schedule(static)
1609 for (thread = 0; thread < nbs->nthread_max; thread++)
1613 sort_columns_simple(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1614 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1615 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1616 nbs->work[thread].sort_work);
1620 sort_columns_supersub(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1621 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1622 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1623 nbs->work[thread].sort_work);
1627 #ifdef NBNXN_SEARCH_BB_SSE
1628 if (grid->bSimple && nbat->XFormat == nbatX8)
1630 combine_bounding_box_pairs(grid, grid->bb);
1636 grid->nsubc_tot = 0;
1637 for (i = 0; i < grid->nc; i++)
1639 grid->nsubc_tot += grid->nsubc[i];
1647 print_bbsizes_simple(debug, nbs, grid);
1651 fprintf(debug, "ns non-zero sub-cells: %d average atoms %.2f\n",
1652 grid->nsubc_tot, (a1-a0)/(double)grid->nsubc_tot);
1654 print_bbsizes_supersub(debug, nbs, grid);
1659 static void init_buffer_flags(nbnxn_buffer_flags_t *flags,
1664 flags->nflag = (natoms + NBNXN_BUFFERFLAG_SIZE - 1)/NBNXN_BUFFERFLAG_SIZE;
1665 if (flags->nflag > flags->flag_nalloc)
1667 flags->flag_nalloc = over_alloc_large(flags->nflag);
1668 srenew(flags->flag, flags->flag_nalloc);
1670 for (b = 0; b < flags->nflag; b++)
1676 /* Sets up a grid and puts the atoms on the grid.
1677 * This function only operates on one domain of the domain decompostion.
1678 * Note that without domain decomposition there is only one domain.
1680 void nbnxn_put_on_grid(nbnxn_search_t nbs,
1681 int ePBC, matrix box,
1683 rvec corner0, rvec corner1,
1688 int nmoved, int *move,
1690 nbnxn_atomdata_t *nbat)
1694 int nc_max_grid, nc_max;
1696 grid = &nbs->grid[dd_zone];
1698 nbs_cycle_start(&nbs->cc[enbsCCgrid]);
1700 grid->bSimple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
1702 grid->na_c = nbnxn_kernel_to_ci_size(nb_kernel_type);
1703 grid->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
1704 grid->na_sc = (grid->bSimple ? 1 : GPU_NSUBCELL)*grid->na_c;
1705 grid->na_c_2log = get_2log(grid->na_c);
1707 nbat->na_c = grid->na_c;
1716 (nbs->grid[dd_zone-1].cell0 + nbs->grid[dd_zone-1].nc)*
1717 nbs->grid[dd_zone-1].na_sc/grid->na_sc;
1725 copy_mat(box, nbs->box);
1727 if (atom_density >= 0)
1729 grid->atom_density = atom_density;
1733 grid->atom_density = grid_atom_density(n-nmoved, corner0, corner1);
1738 nbs->natoms_local = a1 - nmoved;
1739 /* We assume that nbnxn_put_on_grid is called first
1740 * for the local atoms (dd_zone=0).
1742 nbs->natoms_nonlocal = a1 - nmoved;
1746 nbs->natoms_nonlocal = max(nbs->natoms_nonlocal, a1);
1749 nc_max_grid = set_grid_size_xy(nbs, grid,
1750 dd_zone, n-nmoved, corner0, corner1,
1751 nbs->grid[0].atom_density,
1754 nc_max = grid->cell0 + nc_max_grid;
1756 if (a1 > nbs->cell_nalloc)
1758 nbs->cell_nalloc = over_alloc_large(a1);
1759 srenew(nbs->cell, nbs->cell_nalloc);
1762 /* To avoid conditionals we store the moved particles at the end of a,
1763 * make sure we have enough space.
1765 if (nc_max*grid->na_sc + nmoved > nbs->a_nalloc)
1767 nbs->a_nalloc = over_alloc_large(nc_max*grid->na_sc + nmoved);
1768 srenew(nbs->a, nbs->a_nalloc);
1771 /* We need padding up to a multiple of the buffer flag size: simply add */
1772 if (nc_max*grid->na_sc + NBNXN_BUFFERFLAG_SIZE > nbat->nalloc)
1774 nbnxn_atomdata_realloc(nbat, nc_max*grid->na_sc+NBNXN_BUFFERFLAG_SIZE);
1777 calc_cell_indices(nbs, dd_zone, grid, a0, a1, atinfo, x, move, nbat);
1781 nbat->natoms_local = nbat->natoms;
1784 nbs_cycle_stop(&nbs->cc[enbsCCgrid]);
1787 /* Calls nbnxn_put_on_grid for all non-local domains */
1788 void nbnxn_put_on_grid_nonlocal(nbnxn_search_t nbs,
1789 const gmx_domdec_zones_t *zones,
1793 nbnxn_atomdata_t *nbat)
1798 for (zone = 1; zone < zones->n; zone++)
1800 for (d = 0; d < DIM; d++)
1802 c0[d] = zones->size[zone].bb_x0[d];
1803 c1[d] = zones->size[zone].bb_x1[d];
1806 nbnxn_put_on_grid(nbs, nbs->ePBC, NULL,
1808 zones->cg_range[zone],
1809 zones->cg_range[zone+1],
1819 /* Add simple grid type information to the local super/sub grid */
1820 void nbnxn_grid_add_simple(nbnxn_search_t nbs,
1821 nbnxn_atomdata_t *nbat)
1827 grid = &nbs->grid[0];
1831 gmx_incons("nbnxn_grid_simple called with a simple grid");
1834 ncd = grid->na_sc/NBNXN_CPU_CLUSTER_I_SIZE;
1836 if (grid->nc*ncd > grid->nc_nalloc_simple)
1838 grid->nc_nalloc_simple = over_alloc_large(grid->nc*ncd);
1839 srenew(grid->bbcz_simple, grid->nc_nalloc_simple*NNBSBB_D);
1840 srenew(grid->bb_simple, grid->nc_nalloc_simple*NNBSBB_B);
1841 srenew(grid->flags_simple, grid->nc_nalloc_simple);
1844 sfree_aligned(grid->bbj);
1845 snew_aligned(grid->bbj, grid->nc_nalloc_simple/2, 16);
1849 bbcz = grid->bbcz_simple;
1850 bb = grid->bb_simple;
1852 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
1853 for (sc = 0; sc < grid->nc; sc++)
1857 for (c = 0; c < ncd; c++)
1861 na = NBNXN_CPU_CLUSTER_I_SIZE;
1863 nbat->type[tx*NBNXN_CPU_CLUSTER_I_SIZE+na-1] == nbat->ntype-1)
1870 switch (nbat->XFormat)
1873 /* PACK_X4==NBNXN_CPU_CLUSTER_I_SIZE, so this is simple */
1874 calc_bounding_box_x_x4(na, nbat->x+tx*STRIDE_P4,
1878 /* PACK_X8>NBNXN_CPU_CLUSTER_I_SIZE, more complicated */
1879 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(tx*NBNXN_CPU_CLUSTER_I_SIZE),
1883 calc_bounding_box(na, nbat->xstride,
1884 nbat->x+tx*NBNXN_CPU_CLUSTER_I_SIZE*nbat->xstride,
1888 bbcz[tx*NNBSBB_D+0] = bb[tx*NNBSBB_B +ZZ];
1889 bbcz[tx*NNBSBB_D+1] = bb[tx*NNBSBB_B+NNBSBB_C+ZZ];
1891 /* No interaction optimization yet here */
1892 grid->flags_simple[tx] = NBNXN_CI_DO_LJ(0) | NBNXN_CI_DO_COUL(0);
1896 grid->flags_simple[tx] = 0;
1901 #ifdef NBNXN_SEARCH_BB_SSE
1902 if (grid->bSimple && nbat->XFormat == nbatX8)
1904 combine_bounding_box_pairs(grid, grid->bb_simple);
1909 void nbnxn_get_ncells(nbnxn_search_t nbs, int *ncx, int *ncy)
1911 *ncx = nbs->grid[0].ncx;
1912 *ncy = nbs->grid[0].ncy;
1915 void nbnxn_get_atomorder(nbnxn_search_t nbs, int **a, int *n)
1917 const nbnxn_grid_t *grid;
1919 grid = &nbs->grid[0];
1921 /* Return the atom order for the home cell (index 0) */
1924 *n = grid->cxy_ind[grid->ncx*grid->ncy]*grid->na_sc;
1927 void nbnxn_set_atomorder(nbnxn_search_t nbs)
1930 int ao, cx, cy, cxy, cz, j;
1932 /* Set the atom order for the home cell (index 0) */
1933 grid = &nbs->grid[0];
1936 for (cx = 0; cx < grid->ncx; cx++)
1938 for (cy = 0; cy < grid->ncy; cy++)
1940 cxy = cx*grid->ncy + cy;
1941 j = grid->cxy_ind[cxy]*grid->na_sc;
1942 for (cz = 0; cz < grid->cxy_na[cxy]; cz++)
1953 /* Determines the cell range along one dimension that
1954 * the bounding box b0 - b1 sees.
1956 static void get_cell_range(real b0, real b1,
1957 int nc, real c0, real s, real invs,
1958 real d2, real r2, int *cf, int *cl)
1960 *cf = max((int)((b0 - c0)*invs), 0);
1962 while (*cf > 0 && d2 + sqr((b0 - c0) - (*cf-1+1)*s) < r2)
1967 *cl = min((int)((b1 - c0)*invs), nc-1);
1968 while (*cl < nc-1 && d2 + sqr((*cl+1)*s - (b1 - c0)) < r2)
1974 /* Reference code calculating the distance^2 between two bounding boxes */
1975 static float box_dist2(float bx0, float bx1, float by0,
1976 float by1, float bz0, float bz1,
1980 float dl, dh, dm, dm0;
1984 dl = bx0 - bb[BBU_X];
1985 dh = bb[BBL_X] - bx1;
1990 dl = by0 - bb[BBU_Y];
1991 dh = bb[BBL_Y] - by1;
1996 dl = bz0 - bb[BBU_Z];
1997 dh = bb[BBL_Z] - bz1;
2005 /* Plain C code calculating the distance^2 between two bounding boxes */
2006 static float subc_bb_dist2(int si, const float *bb_i_ci,
2007 int csj, const float *bb_j_all)
2009 const float *bb_i, *bb_j;
2011 float dl, dh, dm, dm0;
2013 bb_i = bb_i_ci + si*NNBSBB_B;
2014 bb_j = bb_j_all + csj*NNBSBB_B;
2018 dl = bb_i[BBL_X] - bb_j[BBU_X];
2019 dh = bb_j[BBL_X] - bb_i[BBU_X];
2024 dl = bb_i[BBL_Y] - bb_j[BBU_Y];
2025 dh = bb_j[BBL_Y] - bb_i[BBU_Y];
2030 dl = bb_i[BBL_Z] - bb_j[BBU_Z];
2031 dh = bb_j[BBL_Z] - bb_i[BBU_Z];
2039 #ifdef NBNXN_SEARCH_BB_SSE
2041 /* SSE code for bb distance for bb format xyz0 */
2042 static float subc_bb_dist2_sse(int na_c,
2043 int si, const float *bb_i_ci,
2044 int csj, const float *bb_j_all)
2046 const float *bb_i, *bb_j;
2048 __m128 bb_i_SSE0, bb_i_SSE1;
2049 __m128 bb_j_SSE0, bb_j_SSE1;
2055 #ifndef GMX_X86_SSE4_1
2056 float d2_array[7], *d2_align;
2058 d2_align = (float *)(((size_t)(d2_array+3)) & (~((size_t)15)));
2063 bb_i = bb_i_ci + si*NNBSBB_B;
2064 bb_j = bb_j_all + csj*NNBSBB_B;
2066 bb_i_SSE0 = _mm_load_ps(bb_i);
2067 bb_i_SSE1 = _mm_load_ps(bb_i+NNBSBB_C);
2068 bb_j_SSE0 = _mm_load_ps(bb_j);
2069 bb_j_SSE1 = _mm_load_ps(bb_j+NNBSBB_C);
2071 dl_SSE = _mm_sub_ps(bb_i_SSE0, bb_j_SSE1);
2072 dh_SSE = _mm_sub_ps(bb_j_SSE0, bb_i_SSE1);
2074 dm_SSE = _mm_max_ps(dl_SSE, dh_SSE);
2075 dm0_SSE = _mm_max_ps(dm_SSE, _mm_setzero_ps());
2076 #ifndef GMX_X86_SSE4_1
2077 d2_SSE = _mm_mul_ps(dm0_SSE, dm0_SSE);
2079 _mm_store_ps(d2_align, d2_SSE);
2081 return d2_align[0] + d2_align[1] + d2_align[2];
2083 /* SSE4.1 dot product of components 0,1,2 */
2084 d2_SSE = _mm_dp_ps(dm0_SSE, dm0_SSE, 0x71);
2086 _mm_store_ss(&d2, d2_SSE);
2092 /* Calculate bb bounding distances of bb_i[si,...,si+3] and store them in d2 */
2093 #define SUBC_BB_DIST2_SSE_XXXX_INNER(si, bb_i, d2) \
2097 __m128 dx_0, dy_0, dz_0; \
2098 __m128 dx_1, dy_1, dz_1; \
2100 __m128 mx, my, mz; \
2101 __m128 m0x, m0y, m0z; \
2103 __m128 d2x, d2y, d2z; \
2106 shi = si*NNBSBB_D*DIM; \
2108 xi_l = _mm_load_ps(bb_i+shi+0*STRIDE_PBB); \
2109 yi_l = _mm_load_ps(bb_i+shi+1*STRIDE_PBB); \
2110 zi_l = _mm_load_ps(bb_i+shi+2*STRIDE_PBB); \
2111 xi_h = _mm_load_ps(bb_i+shi+3*STRIDE_PBB); \
2112 yi_h = _mm_load_ps(bb_i+shi+4*STRIDE_PBB); \
2113 zi_h = _mm_load_ps(bb_i+shi+5*STRIDE_PBB); \
2115 dx_0 = _mm_sub_ps(xi_l, xj_h); \
2116 dy_0 = _mm_sub_ps(yi_l, yj_h); \
2117 dz_0 = _mm_sub_ps(zi_l, zj_h); \
2119 dx_1 = _mm_sub_ps(xj_l, xi_h); \
2120 dy_1 = _mm_sub_ps(yj_l, yi_h); \
2121 dz_1 = _mm_sub_ps(zj_l, zi_h); \
2123 mx = _mm_max_ps(dx_0, dx_1); \
2124 my = _mm_max_ps(dy_0, dy_1); \
2125 mz = _mm_max_ps(dz_0, dz_1); \
2127 m0x = _mm_max_ps(mx, zero); \
2128 m0y = _mm_max_ps(my, zero); \
2129 m0z = _mm_max_ps(mz, zero); \
2131 d2x = _mm_mul_ps(m0x, m0x); \
2132 d2y = _mm_mul_ps(m0y, m0y); \
2133 d2z = _mm_mul_ps(m0z, m0z); \
2135 d2s = _mm_add_ps(d2x, d2y); \
2136 d2t = _mm_add_ps(d2s, d2z); \
2138 _mm_store_ps(d2+si, d2t); \
2141 /* SSE code for nsi bb distances for bb format xxxxyyyyzzzz */
2142 static void subc_bb_dist2_sse_xxxx(const float *bb_j,
2143 int nsi, const float *bb_i,
2146 __m128 xj_l, yj_l, zj_l;
2147 __m128 xj_h, yj_h, zj_h;
2148 __m128 xi_l, yi_l, zi_l;
2149 __m128 xi_h, yi_h, zi_h;
2153 zero = _mm_setzero_ps();
2155 xj_l = _mm_set1_ps(bb_j[0*STRIDE_PBB]);
2156 yj_l = _mm_set1_ps(bb_j[1*STRIDE_PBB]);
2157 zj_l = _mm_set1_ps(bb_j[2*STRIDE_PBB]);
2158 xj_h = _mm_set1_ps(bb_j[3*STRIDE_PBB]);
2159 yj_h = _mm_set1_ps(bb_j[4*STRIDE_PBB]);
2160 zj_h = _mm_set1_ps(bb_j[5*STRIDE_PBB]);
2162 /* Here we "loop" over si (0,STRIDE_PBB) from 0 to nsi with step STRIDE_PBB.
2163 * But as we know the number of iterations is 1 or 2, we unroll manually.
2165 SUBC_BB_DIST2_SSE_XXXX_INNER(0, bb_i, d2);
2166 if (STRIDE_PBB < nsi)
2168 SUBC_BB_DIST2_SSE_XXXX_INNER(STRIDE_PBB, bb_i, d2);
2172 #endif /* NBNXN_SEARCH_BB_SSE */
2174 /* Plain C function which determines if any atom pair between two cells
2175 * is within distance sqrt(rl2).
2177 static gmx_bool subc_in_range_x(int na_c,
2178 int si, const real *x_i,
2179 int csj, int stride, const real *x_j,
2185 for (i = 0; i < na_c; i++)
2187 i0 = (si*na_c + i)*DIM;
2188 for (j = 0; j < na_c; j++)
2190 j0 = (csj*na_c + j)*stride;
2192 d2 = sqr(x_i[i0 ] - x_j[j0 ]) +
2193 sqr(x_i[i0+1] - x_j[j0+1]) +
2194 sqr(x_i[i0+2] - x_j[j0+2]);
2206 /* SSE function which determines if any atom pair between two cells,
2207 * both with 8 atoms, is within distance sqrt(rl2).
2209 static gmx_bool subc_in_range_sse8(int na_c,
2210 int si, const real *x_i,
2211 int csj, int stride, const real *x_j,
2214 #ifdef NBNXN_SEARCH_SSE_SINGLE
2215 __m128 ix_SSE0, iy_SSE0, iz_SSE0;
2216 __m128 ix_SSE1, iy_SSE1, iz_SSE1;
2223 rc2_SSE = _mm_set1_ps(rl2);
2225 na_c_sse = NBNXN_GPU_CLUSTER_SIZE/STRIDE_PBB;
2226 ix_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+0)*STRIDE_PBB);
2227 iy_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+1)*STRIDE_PBB);
2228 iz_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+2)*STRIDE_PBB);
2229 ix_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+3)*STRIDE_PBB);
2230 iy_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+4)*STRIDE_PBB);
2231 iz_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+5)*STRIDE_PBB);
2233 /* We loop from the outer to the inner particles to maximize
2234 * the chance that we find a pair in range quickly and return.
2240 __m128 jx0_SSE, jy0_SSE, jz0_SSE;
2241 __m128 jx1_SSE, jy1_SSE, jz1_SSE;
2243 __m128 dx_SSE0, dy_SSE0, dz_SSE0;
2244 __m128 dx_SSE1, dy_SSE1, dz_SSE1;
2245 __m128 dx_SSE2, dy_SSE2, dz_SSE2;
2246 __m128 dx_SSE3, dy_SSE3, dz_SSE3;
2257 __m128 wco_any_SSE01, wco_any_SSE23, wco_any_SSE;
2259 jx0_SSE = _mm_load1_ps(x_j+j0*stride+0);
2260 jy0_SSE = _mm_load1_ps(x_j+j0*stride+1);
2261 jz0_SSE = _mm_load1_ps(x_j+j0*stride+2);
2263 jx1_SSE = _mm_load1_ps(x_j+j1*stride+0);
2264 jy1_SSE = _mm_load1_ps(x_j+j1*stride+1);
2265 jz1_SSE = _mm_load1_ps(x_j+j1*stride+2);
2267 /* Calculate distance */
2268 dx_SSE0 = _mm_sub_ps(ix_SSE0, jx0_SSE);
2269 dy_SSE0 = _mm_sub_ps(iy_SSE0, jy0_SSE);
2270 dz_SSE0 = _mm_sub_ps(iz_SSE0, jz0_SSE);
2271 dx_SSE1 = _mm_sub_ps(ix_SSE1, jx0_SSE);
2272 dy_SSE1 = _mm_sub_ps(iy_SSE1, jy0_SSE);
2273 dz_SSE1 = _mm_sub_ps(iz_SSE1, jz0_SSE);
2274 dx_SSE2 = _mm_sub_ps(ix_SSE0, jx1_SSE);
2275 dy_SSE2 = _mm_sub_ps(iy_SSE0, jy1_SSE);
2276 dz_SSE2 = _mm_sub_ps(iz_SSE0, jz1_SSE);
2277 dx_SSE3 = _mm_sub_ps(ix_SSE1, jx1_SSE);
2278 dy_SSE3 = _mm_sub_ps(iy_SSE1, jy1_SSE);
2279 dz_SSE3 = _mm_sub_ps(iz_SSE1, jz1_SSE);
2281 /* rsq = dx*dx+dy*dy+dz*dz */
2282 rsq_SSE0 = gmx_mm_calc_rsq_ps(dx_SSE0, dy_SSE0, dz_SSE0);
2283 rsq_SSE1 = gmx_mm_calc_rsq_ps(dx_SSE1, dy_SSE1, dz_SSE1);
2284 rsq_SSE2 = gmx_mm_calc_rsq_ps(dx_SSE2, dy_SSE2, dz_SSE2);
2285 rsq_SSE3 = gmx_mm_calc_rsq_ps(dx_SSE3, dy_SSE3, dz_SSE3);
2287 wco_SSE0 = _mm_cmplt_ps(rsq_SSE0, rc2_SSE);
2288 wco_SSE1 = _mm_cmplt_ps(rsq_SSE1, rc2_SSE);
2289 wco_SSE2 = _mm_cmplt_ps(rsq_SSE2, rc2_SSE);
2290 wco_SSE3 = _mm_cmplt_ps(rsq_SSE3, rc2_SSE);
2292 wco_any_SSE01 = _mm_or_ps(wco_SSE0, wco_SSE1);
2293 wco_any_SSE23 = _mm_or_ps(wco_SSE2, wco_SSE3);
2294 wco_any_SSE = _mm_or_ps(wco_any_SSE01, wco_any_SSE23);
2296 if (_mm_movemask_ps(wco_any_SSE))
2308 gmx_incons("SSE function called without SSE support");
2314 /* Returns the j sub-cell for index cj_ind */
2315 static int nbl_cj(const nbnxn_pairlist_t *nbl, int cj_ind)
2317 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].cj[cj_ind & (NBNXN_GPU_JGROUP_SIZE - 1)];
2320 /* Returns the i-interaction mask of the j sub-cell for index cj_ind */
2321 static unsigned nbl_imask0(const nbnxn_pairlist_t *nbl, int cj_ind)
2323 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].imei[0].imask;
2326 /* Ensures there is enough space for extra extra exclusion masks */
2327 static void check_excl_space(nbnxn_pairlist_t *nbl, int extra)
2329 if (nbl->nexcl+extra > nbl->excl_nalloc)
2331 nbl->excl_nalloc = over_alloc_small(nbl->nexcl+extra);
2332 nbnxn_realloc_void((void **)&nbl->excl,
2333 nbl->nexcl*sizeof(*nbl->excl),
2334 nbl->excl_nalloc*sizeof(*nbl->excl),
2335 nbl->alloc, nbl->free);
2339 /* Ensures there is enough space for ncell extra j-cells in the list */
2340 static void check_subcell_list_space_simple(nbnxn_pairlist_t *nbl,
2345 cj_max = nbl->ncj + ncell;
2347 if (cj_max > nbl->cj_nalloc)
2349 nbl->cj_nalloc = over_alloc_small(cj_max);
2350 nbnxn_realloc_void((void **)&nbl->cj,
2351 nbl->ncj*sizeof(*nbl->cj),
2352 nbl->cj_nalloc*sizeof(*nbl->cj),
2353 nbl->alloc, nbl->free);
2357 /* Ensures there is enough space for ncell extra j-subcells in the list */
2358 static void check_subcell_list_space_supersub(nbnxn_pairlist_t *nbl,
2361 int ncj4_max, j4, j, w, t;
2364 #define WARP_SIZE 32
2366 /* We can have maximally nsupercell*GPU_NSUBCELL sj lists */
2367 /* We can store 4 j-subcell - i-supercell pairs in one struct.
2368 * since we round down, we need one extra entry.
2370 ncj4_max = ((nbl->work->cj_ind + nsupercell*GPU_NSUBCELL + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2372 if (ncj4_max > nbl->cj4_nalloc)
2374 nbl->cj4_nalloc = over_alloc_small(ncj4_max);
2375 nbnxn_realloc_void((void **)&nbl->cj4,
2376 nbl->work->cj4_init*sizeof(*nbl->cj4),
2377 nbl->cj4_nalloc*sizeof(*nbl->cj4),
2378 nbl->alloc, nbl->free);
2381 if (ncj4_max > nbl->work->cj4_init)
2383 for (j4 = nbl->work->cj4_init; j4 < ncj4_max; j4++)
2385 /* No i-subcells and no excl's in the list initially */
2386 for (w = 0; w < NWARP; w++)
2388 nbl->cj4[j4].imei[w].imask = 0U;
2389 nbl->cj4[j4].imei[w].excl_ind = 0;
2393 nbl->work->cj4_init = ncj4_max;
2397 /* Set all excl masks for one GPU warp no exclusions */
2398 static void set_no_excls(nbnxn_excl_t *excl)
2402 for (t = 0; t < WARP_SIZE; t++)
2404 /* Turn all interaction bits on */
2405 excl->pair[t] = NBNXN_INT_MASK_ALL;
2409 /* Initializes a single nbnxn_pairlist_t data structure */
2410 static void nbnxn_init_pairlist(nbnxn_pairlist_t *nbl,
2412 nbnxn_alloc_t *alloc,
2417 nbl->alloc = nbnxn_alloc_aligned;
2425 nbl->free = nbnxn_free_aligned;
2432 nbl->bSimple = bSimple;
2443 /* We need one element extra in sj, so alloc initially with 1 */
2444 nbl->cj4_nalloc = 0;
2451 nbl->excl_nalloc = 0;
2453 check_excl_space(nbl, 1);
2455 set_no_excls(&nbl->excl[0]);
2460 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX, NBNXN_MEM_ALIGN);
2462 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL*NNBSBB_B, NBNXN_MEM_ALIGN);
2464 snew_aligned(nbl->work->x_ci, NBNXN_NA_SC_MAX*DIM, NBNXN_MEM_ALIGN);
2465 #ifdef GMX_NBNXN_SIMD
2466 snew_aligned(nbl->work->x_ci_simd_4xn, 1, NBNXN_MEM_ALIGN);
2467 snew_aligned(nbl->work->x_ci_simd_2xnn, 1, NBNXN_MEM_ALIGN);
2469 snew_aligned(nbl->work->d2, GPU_NSUBCELL, NBNXN_MEM_ALIGN);
2471 nbl->work->sort = NULL;
2472 nbl->work->sort_nalloc = 0;
2473 nbl->work->sci_sort = NULL;
2474 nbl->work->sci_sort_nalloc = 0;
2477 void nbnxn_init_pairlist_set(nbnxn_pairlist_set_t *nbl_list,
2478 gmx_bool bSimple, gmx_bool bCombined,
2479 nbnxn_alloc_t *alloc,
2484 nbl_list->bSimple = bSimple;
2485 nbl_list->bCombined = bCombined;
2487 nbl_list->nnbl = gmx_omp_nthreads_get(emntNonbonded);
2489 if (!nbl_list->bCombined &&
2490 nbl_list->nnbl > NBNXN_BUFFERFLAG_MAX_THREADS)
2492 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.",
2493 nbl_list->nnbl, NBNXN_BUFFERFLAG_MAX_THREADS, NBNXN_BUFFERFLAG_MAX_THREADS);
2496 snew(nbl_list->nbl, nbl_list->nnbl);
2497 /* Execute in order to avoid memory interleaving between threads */
2498 #pragma omp parallel for num_threads(nbl_list->nnbl) schedule(static)
2499 for (i = 0; i < nbl_list->nnbl; i++)
2501 /* Allocate the nblist data structure locally on each thread
2502 * to optimize memory access for NUMA architectures.
2504 snew(nbl_list->nbl[i], 1);
2506 /* Only list 0 is used on the GPU, use normal allocation for i>0 */
2509 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, alloc, free);
2513 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, NULL, NULL);
2518 /* Print statistics of a pair list, used for debug output */
2519 static void print_nblist_statistics_simple(FILE *fp, const nbnxn_pairlist_t *nbl,
2520 const nbnxn_search_t nbs, real rl)
2522 const nbnxn_grid_t *grid;
2527 /* This code only produces correct statistics with domain decomposition */
2528 grid = &nbs->grid[0];
2530 fprintf(fp, "nbl nci %d ncj %d\n",
2531 nbl->nci, nbl->ncj);
2532 fprintf(fp, "nbl na_sc %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2533 nbl->na_sc, rl, nbl->ncj, nbl->ncj/(double)grid->nc,
2534 nbl->ncj/(double)grid->nc*grid->na_sc,
2535 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)));
2537 fprintf(fp, "nbl average j cell list length %.1f\n",
2538 0.25*nbl->ncj/(double)nbl->nci);
2540 for (s = 0; s < SHIFTS; s++)
2545 for (i = 0; i < nbl->nci; i++)
2547 cs[nbl->ci[i].shift & NBNXN_CI_SHIFT] +=
2548 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start;
2550 j = nbl->ci[i].cj_ind_start;
2551 while (j < nbl->ci[i].cj_ind_end &&
2552 nbl->cj[j].excl != NBNXN_INT_MASK_ALL)
2558 fprintf(fp, "nbl cell pairs, total: %d excl: %d %.1f%%\n",
2559 nbl->ncj, npexcl, 100*npexcl/(double)nbl->ncj);
2560 for (s = 0; s < SHIFTS; s++)
2564 fprintf(fp, "nbl shift %2d ncj %3d\n", s, cs[s]);
2569 /* Print statistics of a pair lists, used for debug output */
2570 static void print_nblist_statistics_supersub(FILE *fp, const nbnxn_pairlist_t *nbl,
2571 const nbnxn_search_t nbs, real rl)
2573 const nbnxn_grid_t *grid;
2574 int i, j4, j, si, b;
2575 int c[GPU_NSUBCELL+1];
2577 /* This code only produces correct statistics with domain decomposition */
2578 grid = &nbs->grid[0];
2580 fprintf(fp, "nbl nsci %d ncj4 %d nsi %d excl4 %d\n",
2581 nbl->nsci, nbl->ncj4, nbl->nci_tot, nbl->nexcl);
2582 fprintf(fp, "nbl na_c %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2583 nbl->na_ci, rl, nbl->nci_tot, nbl->nci_tot/(double)grid->nsubc_tot,
2584 nbl->nci_tot/(double)grid->nsubc_tot*grid->na_c,
2585 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)));
2587 fprintf(fp, "nbl average j super cell list length %.1f\n",
2588 0.25*nbl->ncj4/(double)nbl->nsci);
2589 fprintf(fp, "nbl average i sub cell list length %.1f\n",
2590 nbl->nci_tot/((double)nbl->ncj4));
2592 for (si = 0; si <= GPU_NSUBCELL; si++)
2596 for (i = 0; i < nbl->nsci; i++)
2598 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
2600 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
2603 for (si = 0; si < GPU_NSUBCELL; si++)
2605 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
2614 for (b = 0; b <= GPU_NSUBCELL; b++)
2616 fprintf(fp, "nbl j-list #i-subcell %d %7d %4.1f\n",
2617 b, c[b], 100.0*c[b]/(double)(nbl->ncj4*NBNXN_GPU_JGROUP_SIZE));
2621 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp */
2622 static void low_get_nbl_exclusions(nbnxn_pairlist_t *nbl, int cj4,
2623 int warp, nbnxn_excl_t **excl)
2625 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2627 /* No exclusions set, make a new list entry */
2628 nbl->cj4[cj4].imei[warp].excl_ind = nbl->nexcl;
2630 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2631 set_no_excls(*excl);
2635 /* We already have some exclusions, new ones can be added to the list */
2636 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2640 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp,
2641 * allocates extra memory, if necessary.
2643 static void get_nbl_exclusions_1(nbnxn_pairlist_t *nbl, int cj4,
2644 int warp, nbnxn_excl_t **excl)
2646 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2648 /* We need to make a new list entry, check if we have space */
2649 check_excl_space(nbl, 1);
2651 low_get_nbl_exclusions(nbl, cj4, warp, excl);
2654 /* Returns pointers to the exclusion mask for cj4-unit cj4 for both warps,
2655 * allocates extra memory, if necessary.
2657 static void get_nbl_exclusions_2(nbnxn_pairlist_t *nbl, int cj4,
2658 nbnxn_excl_t **excl_w0,
2659 nbnxn_excl_t **excl_w1)
2661 /* Check for space we might need */
2662 check_excl_space(nbl, 2);
2664 low_get_nbl_exclusions(nbl, cj4, 0, excl_w0);
2665 low_get_nbl_exclusions(nbl, cj4, 1, excl_w1);
2668 /* Sets the self exclusions i=j and pair exclusions i>j */
2669 static void set_self_and_newton_excls_supersub(nbnxn_pairlist_t *nbl,
2670 int cj4_ind, int sj_offset,
2673 nbnxn_excl_t *excl[2];
2676 /* Here we only set the set self and double pair exclusions */
2678 get_nbl_exclusions_2(nbl, cj4_ind, &excl[0], &excl[1]);
2680 /* Only minor < major bits set */
2681 for (ej = 0; ej < nbl->na_ci; ej++)
2684 for (ei = ej; ei < nbl->na_ci; ei++)
2686 excl[w]->pair[(ej & (NBNXN_GPU_JGROUP_SIZE-1))*nbl->na_ci + ei] &=
2687 ~(1U << (sj_offset*GPU_NSUBCELL + si));
2692 /* Returns a diagonal or off-diagonal interaction mask for plain C lists */
2693 static unsigned int get_imask(gmx_bool rdiag, int ci, int cj)
2695 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2698 /* Returns a diagonal or off-diagonal interaction mask for SIMD128 lists */
2699 static unsigned int get_imask_x86_simd128(gmx_bool rdiag, int ci, int cj)
2701 #ifndef GMX_DOUBLE /* cj-size = 4 */
2702 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2703 #else /* cj-size = 2 */
2704 return (rdiag && ci*2 == cj ? NBNXN_INT_MASK_DIAG_J2_0 :
2705 (rdiag && ci*2+1 == cj ? NBNXN_INT_MASK_DIAG_J2_1 :
2706 NBNXN_INT_MASK_ALL));
2710 /* Returns a diagonal or off-diagonal interaction mask for SIMD256 lists */
2711 static unsigned int get_imask_x86_simd256(gmx_bool rdiag, int ci, int cj)
2713 #ifndef GMX_DOUBLE /* cj-size = 8 */
2714 return (rdiag && ci == cj*2 ? NBNXN_INT_MASK_DIAG_J8_0 :
2715 (rdiag && ci == cj*2+1 ? NBNXN_INT_MASK_DIAG_J8_1 :
2716 NBNXN_INT_MASK_ALL));
2717 #else /* cj-size = 4 */
2718 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2722 #ifdef GMX_NBNXN_SIMD
2723 #if GMX_NBNXN_SIMD_BITWIDTH == 128
2724 #define get_imask_x86_simd_4xn get_imask_x86_simd128
2726 #if GMX_NBNXN_SIMD_BITWIDTH == 256
2727 #define get_imask_x86_simd_4xn get_imask_x86_simd256
2728 #define get_imask_x86_simd_2xnn get_imask_x86_simd128
2730 #error "unsupported GMX_NBNXN_SIMD_BITWIDTH"
2735 /* Plain C code for making a pair list of cell ci vs cell cjf-cjl.
2736 * Checks bounding box distances and possibly atom pair distances.
2738 static void make_cluster_list_simple(const nbnxn_grid_t *gridj,
2739 nbnxn_pairlist_t *nbl,
2740 int ci, int cjf, int cjl,
2741 gmx_bool remove_sub_diag,
2743 real rl2, float rbb2,
2746 const nbnxn_list_work_t *work;
2753 int cjf_gl, cjl_gl, cj;
2757 bb_ci = nbl->work->bb_ci;
2758 x_ci = nbl->work->x_ci;
2761 while (!InRange && cjf <= cjl)
2763 d2 = subc_bb_dist2(0, bb_ci, cjf, gridj->bb);
2766 /* Check if the distance is within the distance where
2767 * we use only the bounding box distance rbb,
2768 * or within the cut-off and there is at least one atom pair
2769 * within the cut-off.
2779 cjf_gl = gridj->cell0 + cjf;
2780 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2782 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2784 InRange = InRange ||
2785 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2786 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2787 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2790 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2803 while (!InRange && cjl > cjf)
2805 d2 = subc_bb_dist2(0, bb_ci, cjl, gridj->bb);
2808 /* Check if the distance is within the distance where
2809 * we use only the bounding box distance rbb,
2810 * or within the cut-off and there is at least one atom pair
2811 * within the cut-off.
2821 cjl_gl = gridj->cell0 + cjl;
2822 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2824 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2826 InRange = InRange ||
2827 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2828 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2829 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2832 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2842 for (cj = cjf; cj <= cjl; cj++)
2844 /* Store cj and the interaction mask */
2845 nbl->cj[nbl->ncj].cj = gridj->cell0 + cj;
2846 nbl->cj[nbl->ncj].excl = get_imask(remove_sub_diag, ci, cj);
2849 /* Increase the closing index in i super-cell list */
2850 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
2854 #ifdef GMX_NBNXN_SIMD_4XN
2855 #include "nbnxn_search_simd_4xn.h"
2857 #ifdef GMX_NBNXN_SIMD_2XNN
2858 #include "nbnxn_search_simd_2xnn.h"
2861 /* Plain C or SSE code for making a pair list of super-cell sci vs scj.
2862 * Checks bounding box distances and possibly atom pair distances.
2864 static void make_cluster_list_supersub(const nbnxn_search_t nbs,
2865 const nbnxn_grid_t *gridi,
2866 const nbnxn_grid_t *gridj,
2867 nbnxn_pairlist_t *nbl,
2869 gmx_bool sci_equals_scj,
2870 int stride, const real *x,
2871 real rl2, float rbb2,
2876 int cjo, ci1, ci, cj, cj_gl;
2877 int cj4_ind, cj_offset;
2884 #define PRUNE_LIST_CPU_ONE
2885 #ifdef PRUNE_LIST_CPU_ONE
2889 d2l = nbl->work->d2;
2891 bb_ci = nbl->work->bb_ci;
2892 x_ci = nbl->work->x_ci;
2896 for (cjo = 0; cjo < gridj->nsubc[scj]; cjo++)
2898 cj4_ind = (nbl->work->cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2899 cj_offset = nbl->work->cj_ind - cj4_ind*NBNXN_GPU_JGROUP_SIZE;
2900 cj4 = &nbl->cj4[cj4_ind];
2902 cj = scj*GPU_NSUBCELL + cjo;
2904 cj_gl = gridj->cell0*GPU_NSUBCELL + cj;
2906 /* Initialize this j-subcell i-subcell list */
2907 cj4->cj[cj_offset] = cj_gl;
2916 ci1 = gridi->nsubc[sci];
2920 /* Determine all ci1 bb distances in one call with SSE */
2921 subc_bb_dist2_sse_xxxx(gridj->bb+(cj>>STRIDE_PBB_2LOG)*NNBSBB_XXXX+(cj & (STRIDE_PBB-1)),
2927 /* We use a fixed upper-bound instead of ci1 to help optimization */
2928 for (ci = 0; ci < GPU_NSUBCELL; ci++)
2935 #ifndef NBNXN_BBXXXX
2936 /* Determine the bb distance between ci and cj */
2937 d2l[ci] = subc_bb_dist2(ci, bb_ci, cj, gridj->bb);
2942 #ifdef PRUNE_LIST_CPU_ALL
2943 /* Check if the distance is within the distance where
2944 * we use only the bounding box distance rbb,
2945 * or within the cut-off and there is at least one atom pair
2946 * within the cut-off. This check is very costly.
2948 *ndistc += na_c*na_c;
2951 #ifdef NBNXN_PBB_SSE
2956 (na_c, ci, x_ci, cj_gl, stride, x, rl2)))
2958 /* Check if the distance between the two bounding boxes
2959 * in within the pair-list cut-off.
2964 /* Flag this i-subcell to be taken into account */
2965 imask |= (1U << (cj_offset*GPU_NSUBCELL+ci));
2967 #ifdef PRUNE_LIST_CPU_ONE
2975 #ifdef PRUNE_LIST_CPU_ONE
2976 /* If we only found 1 pair, check if any atoms are actually
2977 * within the cut-off, so we could get rid of it.
2979 if (npair == 1 && d2l[ci_last] >= rbb2)
2981 /* Avoid using function pointers here, as it's slower */
2983 #ifdef NBNXN_PBB_SSE
2988 (na_c, ci_last, x_ci, cj_gl, stride, x, rl2))
2990 imask &= ~(1U << (cj_offset*GPU_NSUBCELL+ci_last));
2998 /* We have a useful sj entry, close it now */
3000 /* Set the exclucions for the ci== sj entry.
3001 * Here we don't bother to check if this entry is actually flagged,
3002 * as it will nearly always be in the list.
3006 set_self_and_newton_excls_supersub(nbl, cj4_ind, cj_offset, cjo);
3009 /* Copy the cluster interaction mask to the list */
3010 for (w = 0; w < NWARP; w++)
3012 cj4->imei[w].imask |= imask;
3015 nbl->work->cj_ind++;
3017 /* Keep the count */
3018 nbl->nci_tot += npair;
3020 /* Increase the closing index in i super-cell list */
3021 nbl->sci[nbl->nsci].cj4_ind_end =
3022 ((nbl->work->cj_ind+NBNXN_GPU_JGROUP_SIZE-1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3027 /* Set all atom-pair exclusions from the topology stored in excl
3028 * as masks in the pair-list for simple list i-entry nbl_ci
3030 static void set_ci_top_excls(const nbnxn_search_t nbs,
3031 nbnxn_pairlist_t *nbl,
3032 gmx_bool diagRemoved,
3035 const nbnxn_ci_t *nbl_ci,
3036 const t_blocka *excl)
3040 int cj_ind_first, cj_ind_last;
3041 int cj_first, cj_last;
3043 int i, ai, aj, si, eind, ge, se;
3044 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3048 nbnxn_excl_t *nbl_excl;
3049 int inner_i, inner_e;
3053 if (nbl_ci->cj_ind_end == nbl_ci->cj_ind_start)
3061 cj_ind_first = nbl_ci->cj_ind_start;
3062 cj_ind_last = nbl->ncj - 1;
3064 cj_first = nbl->cj[cj_ind_first].cj;
3065 cj_last = nbl->cj[cj_ind_last].cj;
3067 /* Determine how many contiguous j-cells we have starting
3068 * from the first i-cell. This number can be used to directly
3069 * calculate j-cell indices for excluded atoms.
3072 if (na_ci_2log == na_cj_2log)
3074 while (cj_ind_first + ndirect <= cj_ind_last &&
3075 nbl->cj[cj_ind_first+ndirect].cj == ci + ndirect)
3080 #ifdef NBNXN_SEARCH_BB_SSE
3083 while (cj_ind_first + ndirect <= cj_ind_last &&
3084 nbl->cj[cj_ind_first+ndirect].cj == ci_to_cj(na_cj_2log, ci) + ndirect)
3091 /* Loop over the atoms in the i super-cell */
3092 for (i = 0; i < nbl->na_sc; i++)
3094 ai = nbs->a[ci*nbl->na_sc+i];
3097 si = (i>>na_ci_2log);
3099 /* Loop over the topology-based exclusions for this i-atom */
3100 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3106 /* The self exclusion are already set, save some time */
3112 /* Without shifts we only calculate interactions j>i
3113 * for one-way pair-lists.
3115 if (diagRemoved && ge <= ci*nbl->na_sc + i)
3120 se = (ge >> na_cj_2log);
3122 /* Could the cluster se be in our list? */
3123 if (se >= cj_first && se <= cj_last)
3125 if (se < cj_first + ndirect)
3127 /* We can calculate cj_ind directly from se */
3128 found = cj_ind_first + se - cj_first;
3132 /* Search for se using bisection */
3134 cj_ind_0 = cj_ind_first + ndirect;
3135 cj_ind_1 = cj_ind_last + 1;
3136 while (found == -1 && cj_ind_0 < cj_ind_1)
3138 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3140 cj_m = nbl->cj[cj_ind_m].cj;
3148 cj_ind_1 = cj_ind_m;
3152 cj_ind_0 = cj_ind_m + 1;
3159 inner_i = i - (si << na_ci_2log);
3160 inner_e = ge - (se << na_cj_2log);
3162 nbl->cj[found].excl &= ~(1U<<((inner_i<<na_cj_2log) + inner_e));
3170 /* Set all atom-pair exclusions from the topology stored in excl
3171 * as masks in the pair-list for i-super-cell entry nbl_sci
3173 static void set_sci_top_excls(const nbnxn_search_t nbs,
3174 nbnxn_pairlist_t *nbl,
3175 gmx_bool diagRemoved,
3177 const nbnxn_sci_t *nbl_sci,
3178 const t_blocka *excl)
3183 int cj_ind_first, cj_ind_last;
3184 int cj_first, cj_last;
3186 int i, ai, aj, si, eind, ge, se;
3187 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3191 nbnxn_excl_t *nbl_excl;
3192 int inner_i, inner_e, w;
3198 if (nbl_sci->cj4_ind_end == nbl_sci->cj4_ind_start)
3206 cj_ind_first = nbl_sci->cj4_ind_start*NBNXN_GPU_JGROUP_SIZE;
3207 cj_ind_last = nbl->work->cj_ind - 1;
3209 cj_first = nbl->cj4[nbl_sci->cj4_ind_start].cj[0];
3210 cj_last = nbl_cj(nbl, cj_ind_last);
3212 /* Determine how many contiguous j-clusters we have starting
3213 * from the first i-cluster. This number can be used to directly
3214 * calculate j-cluster indices for excluded atoms.
3217 while (cj_ind_first + ndirect <= cj_ind_last &&
3218 nbl_cj(nbl, cj_ind_first+ndirect) == sci*GPU_NSUBCELL + ndirect)
3223 /* Loop over the atoms in the i super-cell */
3224 for (i = 0; i < nbl->na_sc; i++)
3226 ai = nbs->a[sci*nbl->na_sc+i];
3229 si = (i>>na_c_2log);
3231 /* Loop over the topology-based exclusions for this i-atom */
3232 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3238 /* The self exclusion are already set, save some time */
3244 /* Without shifts we only calculate interactions j>i
3245 * for one-way pair-lists.
3247 if (diagRemoved && ge <= sci*nbl->na_sc + i)
3253 /* Could the cluster se be in our list? */
3254 if (se >= cj_first && se <= cj_last)
3256 if (se < cj_first + ndirect)
3258 /* We can calculate cj_ind directly from se */
3259 found = cj_ind_first + se - cj_first;
3263 /* Search for se using bisection */
3265 cj_ind_0 = cj_ind_first + ndirect;
3266 cj_ind_1 = cj_ind_last + 1;
3267 while (found == -1 && cj_ind_0 < cj_ind_1)
3269 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3271 cj_m = nbl_cj(nbl, cj_ind_m);
3279 cj_ind_1 = cj_ind_m;
3283 cj_ind_0 = cj_ind_m + 1;
3290 inner_i = i - si*na_c;
3291 inner_e = ge - se*na_c;
3293 /* Macro for getting the index of atom a within a cluster */
3294 #define AMODCJ4(a) ((a) & (NBNXN_GPU_JGROUP_SIZE - 1))
3295 /* Macro for converting an atom number to a cluster number */
3296 #define A2CJ4(a) ((a) >> NBNXN_GPU_JGROUP_SIZE_2LOG)
3297 /* Macro for getting the index of an i-atom within a warp */
3298 #define AMODWI(a) ((a) & (NBNXN_GPU_CLUSTER_SIZE/2 - 1))
3300 if (nbl_imask0(nbl, found) & (1U << (AMODCJ4(found)*GPU_NSUBCELL + si)))
3304 get_nbl_exclusions_1(nbl, A2CJ4(found), w, &nbl_excl);
3306 nbl_excl->pair[AMODWI(inner_e)*nbl->na_ci+inner_i] &=
3307 ~(1U << (AMODCJ4(found)*GPU_NSUBCELL + si));
3320 /* Reallocate the simple ci list for at least n entries */
3321 static void nb_realloc_ci(nbnxn_pairlist_t *nbl, int n)
3323 nbl->ci_nalloc = over_alloc_small(n);
3324 nbnxn_realloc_void((void **)&nbl->ci,
3325 nbl->nci*sizeof(*nbl->ci),
3326 nbl->ci_nalloc*sizeof(*nbl->ci),
3327 nbl->alloc, nbl->free);
3330 /* Reallocate the super-cell sci list for at least n entries */
3331 static void nb_realloc_sci(nbnxn_pairlist_t *nbl, int n)
3333 nbl->sci_nalloc = over_alloc_small(n);
3334 nbnxn_realloc_void((void **)&nbl->sci,
3335 nbl->nsci*sizeof(*nbl->sci),
3336 nbl->sci_nalloc*sizeof(*nbl->sci),
3337 nbl->alloc, nbl->free);
3340 /* Make a new ci entry at index nbl->nci */
3341 static void new_ci_entry(nbnxn_pairlist_t *nbl, int ci, int shift, int flags,
3342 nbnxn_list_work_t *work)
3344 if (nbl->nci + 1 > nbl->ci_nalloc)
3346 nb_realloc_ci(nbl, nbl->nci+1);
3348 nbl->ci[nbl->nci].ci = ci;
3349 nbl->ci[nbl->nci].shift = shift;
3350 /* Store the interaction flags along with the shift */
3351 nbl->ci[nbl->nci].shift |= flags;
3352 nbl->ci[nbl->nci].cj_ind_start = nbl->ncj;
3353 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
3356 /* Make a new sci entry at index nbl->nsci */
3357 static void new_sci_entry(nbnxn_pairlist_t *nbl, int sci, int shift, int flags,
3358 nbnxn_list_work_t *work)
3360 if (nbl->nsci + 1 > nbl->sci_nalloc)
3362 nb_realloc_sci(nbl, nbl->nsci+1);
3364 nbl->sci[nbl->nsci].sci = sci;
3365 nbl->sci[nbl->nsci].shift = shift;
3366 nbl->sci[nbl->nsci].cj4_ind_start = nbl->ncj4;
3367 nbl->sci[nbl->nsci].cj4_ind_end = nbl->ncj4;
3370 /* Sort the simple j-list cj on exclusions.
3371 * Entries with exclusions will all be sorted to the beginning of the list.
3373 static void sort_cj_excl(nbnxn_cj_t *cj, int ncj,
3374 nbnxn_list_work_t *work)
3378 if (ncj > work->cj_nalloc)
3380 work->cj_nalloc = over_alloc_large(ncj);
3381 srenew(work->cj, work->cj_nalloc);
3384 /* Make a list of the j-cells involving exclusions */
3386 for (j = 0; j < ncj; j++)
3388 if (cj[j].excl != NBNXN_INT_MASK_ALL)
3390 work->cj[jnew++] = cj[j];
3393 /* Check if there are exclusions at all or not just the first entry */
3394 if (!((jnew == 0) ||
3395 (jnew == 1 && cj[0].excl != NBNXN_INT_MASK_ALL)))
3397 for (j = 0; j < ncj; j++)
3399 if (cj[j].excl == NBNXN_INT_MASK_ALL)
3401 work->cj[jnew++] = cj[j];
3404 for (j = 0; j < ncj; j++)
3406 cj[j] = work->cj[j];
3411 /* Close this simple list i entry */
3412 static void close_ci_entry_simple(nbnxn_pairlist_t *nbl)
3416 /* All content of the new ci entry have already been filled correctly,
3417 * we only need to increase the count here (for non empty lists).
3419 jlen = nbl->ci[nbl->nci].cj_ind_end - nbl->ci[nbl->nci].cj_ind_start;
3422 sort_cj_excl(nbl->cj+nbl->ci[nbl->nci].cj_ind_start, jlen, nbl->work);
3424 /* The counts below are used for non-bonded pair/flop counts
3425 * and should therefore match the available kernel setups.
3427 if (!(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_COUL(0)))
3429 nbl->work->ncj_noq += jlen;
3431 else if ((nbl->ci[nbl->nci].shift & NBNXN_CI_HALF_LJ(0)) ||
3432 !(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_LJ(0)))
3434 nbl->work->ncj_hlj += jlen;
3441 /* Split sci entry for load balancing on the GPU.
3442 * Splitting ensures we have enough lists to fully utilize the whole GPU.
3443 * With progBal we generate progressively smaller lists, which improves
3444 * load balancing. As we only know the current count on our own thread,
3445 * we will need to estimate the current total amount of i-entries.
3446 * As the lists get concatenated later, this estimate depends
3447 * both on nthread and our own thread index.
3449 static void split_sci_entry(nbnxn_pairlist_t *nbl,
3450 int nsp_max_av, gmx_bool progBal, int nc_bal,
3451 int thread, int nthread)
3455 int cj4_start, cj4_end, j4len, cj4;
3457 int nsp, nsp_sci, nsp_cj4, nsp_cj4_e, nsp_cj4_p;
3462 /* Estimate the total numbers of ci's of the nblist combined
3463 * over all threads using the target number of ci's.
3465 nsci_est = nc_bal*thread/nthread + nbl->nsci;
3467 /* The first ci blocks should be larger, to avoid overhead.
3468 * The last ci blocks should be smaller, to improve load balancing.
3471 nsp_max_av*nc_bal*3/(2*(nsci_est - 1 + nc_bal)));
3475 nsp_max = nsp_max_av;
3478 cj4_start = nbl->sci[nbl->nsci-1].cj4_ind_start;
3479 cj4_end = nbl->sci[nbl->nsci-1].cj4_ind_end;
3480 j4len = cj4_end - cj4_start;
3482 if (j4len > 1 && j4len*GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE > nsp_max)
3484 /* Remove the last ci entry and process the cj4's again */
3492 for (cj4 = cj4_start; cj4 < cj4_end; cj4++)
3494 nsp_cj4_p = nsp_cj4;
3495 /* Count the number of cluster pairs in this cj4 group */
3497 for (p = 0; p < GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE; p++)
3499 nsp_cj4 += (nbl->cj4[cj4].imei[0].imask >> p) & 1;
3502 if (nsp_cj4 > 0 && nsp + nsp_cj4 > nsp_max)
3504 /* Split the list at cj4 */
3505 nbl->sci[sci].cj4_ind_end = cj4;
3506 /* Create a new sci entry */
3509 if (nbl->nsci+1 > nbl->sci_nalloc)
3511 nb_realloc_sci(nbl, nbl->nsci+1);
3513 nbl->sci[sci].sci = nbl->sci[nbl->nsci-1].sci;
3514 nbl->sci[sci].shift = nbl->sci[nbl->nsci-1].shift;
3515 nbl->sci[sci].cj4_ind_start = cj4;
3517 nsp_cj4_e = nsp_cj4_p;
3523 /* Put the remaining cj4's in the last sci entry */
3524 nbl->sci[sci].cj4_ind_end = cj4_end;
3526 /* Possibly balance out the last two sci's
3527 * by moving the last cj4 of the second last sci.
3529 if (nsp_sci - nsp_cj4_e >= nsp + nsp_cj4_e)
3531 nbl->sci[sci-1].cj4_ind_end--;
3532 nbl->sci[sci].cj4_ind_start--;
3539 /* Clost this super/sub list i entry */
3540 static void close_ci_entry_supersub(nbnxn_pairlist_t *nbl,
3542 gmx_bool progBal, int nc_bal,
3543 int thread, int nthread)
3548 /* All content of the new ci entry have already been filled correctly,
3549 * we only need to increase the count here (for non empty lists).
3551 j4len = nbl->sci[nbl->nsci].cj4_ind_end - nbl->sci[nbl->nsci].cj4_ind_start;
3554 /* We can only have complete blocks of 4 j-entries in a list,
3555 * so round the count up before closing.
3557 nbl->ncj4 = ((nbl->work->cj_ind + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3558 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3564 /* Measure the size of the new entry and potentially split it */
3565 split_sci_entry(nbl, nsp_max_av, progBal, nc_bal, thread, nthread);
3570 /* Syncs the working array before adding another grid pair to the list */
3571 static void sync_work(nbnxn_pairlist_t *nbl)
3575 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3576 nbl->work->cj4_init = nbl->ncj4;
3580 /* Clears an nbnxn_pairlist_t data structure */
3581 static void clear_pairlist(nbnxn_pairlist_t *nbl)
3590 nbl->work->ncj_noq = 0;
3591 nbl->work->ncj_hlj = 0;
3594 /* Sets a simple list i-cell bounding box, including PBC shift */
3595 static void set_icell_bb_simple(const float *bb, int ci,
3596 real shx, real shy, real shz,
3602 bb_ci[BBL_X] = bb[ia+BBL_X] + shx;
3603 bb_ci[BBL_Y] = bb[ia+BBL_Y] + shy;
3604 bb_ci[BBL_Z] = bb[ia+BBL_Z] + shz;
3605 bb_ci[BBU_X] = bb[ia+BBU_X] + shx;
3606 bb_ci[BBU_Y] = bb[ia+BBU_Y] + shy;
3607 bb_ci[BBU_Z] = bb[ia+BBU_Z] + shz;
3610 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
3611 static void set_icell_bb_supersub(const float *bb, int ci,
3612 real shx, real shy, real shz,
3618 ia = ci*(GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX;
3619 for (m = 0; m < (GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX; m += NNBSBB_XXXX)
3621 for (i = 0; i < STRIDE_PBB; i++)
3623 bb_ci[m+0*STRIDE_PBB+i] = bb[ia+m+0*STRIDE_PBB+i] + shx;
3624 bb_ci[m+1*STRIDE_PBB+i] = bb[ia+m+1*STRIDE_PBB+i] + shy;
3625 bb_ci[m+2*STRIDE_PBB+i] = bb[ia+m+2*STRIDE_PBB+i] + shz;
3626 bb_ci[m+3*STRIDE_PBB+i] = bb[ia+m+3*STRIDE_PBB+i] + shx;
3627 bb_ci[m+4*STRIDE_PBB+i] = bb[ia+m+4*STRIDE_PBB+i] + shy;
3628 bb_ci[m+5*STRIDE_PBB+i] = bb[ia+m+5*STRIDE_PBB+i] + shz;
3632 ia = ci*GPU_NSUBCELL*NNBSBB_B;
3633 for (i = 0; i < GPU_NSUBCELL*NNBSBB_B; i += NNBSBB_B)
3635 bb_ci[i+BBL_X] = bb[ia+i+BBL_X] + shx;
3636 bb_ci[i+BBL_Y] = bb[ia+i+BBL_Y] + shy;
3637 bb_ci[i+BBL_Z] = bb[ia+i+BBL_Z] + shz;
3638 bb_ci[i+BBU_X] = bb[ia+i+BBU_X] + shx;
3639 bb_ci[i+BBU_Y] = bb[ia+i+BBU_Y] + shy;
3640 bb_ci[i+BBU_Z] = bb[ia+i+BBU_Z] + shz;
3645 /* Copies PBC shifted i-cell atom coordinates x,y,z to working array */
3646 static void icell_set_x_simple(int ci,
3647 real shx, real shy, real shz,
3649 int stride, const real *x,
3650 nbnxn_list_work_t *work)
3654 ia = ci*NBNXN_CPU_CLUSTER_I_SIZE;
3656 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE; i++)
3658 work->x_ci[i*STRIDE_XYZ+XX] = x[(ia+i)*stride+XX] + shx;
3659 work->x_ci[i*STRIDE_XYZ+YY] = x[(ia+i)*stride+YY] + shy;
3660 work->x_ci[i*STRIDE_XYZ+ZZ] = x[(ia+i)*stride+ZZ] + shz;
3664 /* Copies PBC shifted super-cell atom coordinates x,y,z to working array */
3665 static void icell_set_x_supersub(int ci,
3666 real shx, real shy, real shz,
3668 int stride, const real *x,
3669 nbnxn_list_work_t *work)
3676 ia = ci*GPU_NSUBCELL*na_c;
3677 for (i = 0; i < GPU_NSUBCELL*na_c; i++)
3679 x_ci[i*DIM + XX] = x[(ia+i)*stride + XX] + shx;
3680 x_ci[i*DIM + YY] = x[(ia+i)*stride + YY] + shy;
3681 x_ci[i*DIM + ZZ] = x[(ia+i)*stride + ZZ] + shz;
3685 #ifdef NBNXN_SEARCH_BB_SSE
3686 /* Copies PBC shifted super-cell packed atom coordinates to working array */
3687 static void icell_set_x_supersub_sse8(int ci,
3688 real shx, real shy, real shz,
3690 int stride, const real *x,
3691 nbnxn_list_work_t *work)
3693 int si, io, ia, i, j;
3698 for (si = 0; si < GPU_NSUBCELL; si++)
3700 for (i = 0; i < na_c; i += STRIDE_PBB)
3703 ia = ci*GPU_NSUBCELL*na_c + io;
3704 for (j = 0; j < STRIDE_PBB; j++)
3706 x_ci[io*DIM + j + XX*STRIDE_PBB] = x[(ia+j)*stride+XX] + shx;
3707 x_ci[io*DIM + j + YY*STRIDE_PBB] = x[(ia+j)*stride+YY] + shy;
3708 x_ci[io*DIM + j + ZZ*STRIDE_PBB] = x[(ia+j)*stride+ZZ] + shz;
3715 static real nbnxn_rlist_inc_nonloc_fac = 0.6;
3717 /* Due to the cluster size the effective pair-list is longer than
3718 * that of a simple atom pair-list. This function gives the extra distance.
3720 real nbnxn_get_rlist_effective_inc(int cluster_size, real atom_density)
3722 return ((0.5 + nbnxn_rlist_inc_nonloc_fac)*sqr(((cluster_size) - 1.0)/(cluster_size))*pow((cluster_size)/(atom_density), 1.0/3.0));
3725 /* Estimates the interaction volume^2 for non-local interactions */
3726 static real nonlocal_vol2(const gmx_domdec_zones_t *zones, rvec ls, real r)
3735 /* Here we simply add up the volumes of 1, 2 or 3 1D decomposition
3736 * not home interaction volume^2. As these volumes are not additive,
3737 * this is an overestimate, but it would only be significant in the limit
3738 * of small cells, where we anyhow need to split the lists into
3739 * as small parts as possible.
3742 for (z = 0; z < zones->n; z++)
3744 if (zones->shift[z][XX] + zones->shift[z][YY] + zones->shift[z][ZZ] == 1)
3749 for (d = 0; d < DIM; d++)
3751 if (zones->shift[z][d] == 0)
3755 za *= zones->size[z].x1[d] - zones->size[z].x0[d];
3759 /* 4 octants of a sphere */
3760 vold_est = 0.25*M_PI*r*r*r*r;
3761 /* 4 quarter pie slices on the edges */
3762 vold_est += 4*cl*M_PI/6.0*r*r*r;
3763 /* One rectangular volume on a face */
3764 vold_est += ca*0.5*r*r;
3766 vol2_est_tot += vold_est*za;
3770 return vol2_est_tot;
3773 /* Estimates the average size of a full j-list for super/sub setup */
3774 static int get_nsubpair_max(const nbnxn_search_t nbs,
3777 int min_ci_balanced)
3779 const nbnxn_grid_t *grid;
3781 real xy_diag2, r_eff_sup, vol_est, nsp_est, nsp_est_nl;
3784 grid = &nbs->grid[0];
3786 ls[XX] = (grid->c1[XX] - grid->c0[XX])/(grid->ncx*GPU_NSUBCELL_X);
3787 ls[YY] = (grid->c1[YY] - grid->c0[YY])/(grid->ncy*GPU_NSUBCELL_Y);
3788 ls[ZZ] = (grid->c1[ZZ] - grid->c0[ZZ])*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z);
3790 /* The average squared length of the diagonal of a sub cell */
3791 xy_diag2 = ls[XX]*ls[XX] + ls[YY]*ls[YY] + ls[ZZ]*ls[ZZ];
3793 /* The formulas below are a heuristic estimate of the average nsj per si*/
3794 r_eff_sup = rlist + nbnxn_rlist_inc_nonloc_fac*sqr((grid->na_c - 1.0)/grid->na_c)*sqrt(xy_diag2/3);
3796 if (!nbs->DomDec || nbs->zones->n == 1)
3803 sqr(grid->atom_density/grid->na_c)*
3804 nonlocal_vol2(nbs->zones, ls, r_eff_sup);
3809 /* Sub-cell interacts with itself */
3810 vol_est = ls[XX]*ls[YY]*ls[ZZ];
3811 /* 6/2 rectangular volume on the faces */
3812 vol_est += (ls[XX]*ls[YY] + ls[XX]*ls[ZZ] + ls[YY]*ls[ZZ])*r_eff_sup;
3813 /* 12/2 quarter pie slices on the edges */
3814 vol_est += 2*(ls[XX] + ls[YY] + ls[ZZ])*0.25*M_PI*sqr(r_eff_sup);
3815 /* 4 octants of a sphere */
3816 vol_est += 0.5*4.0/3.0*M_PI*pow(r_eff_sup, 3);
3818 nsp_est = grid->nsubc_tot*vol_est*grid->atom_density/grid->na_c;
3820 /* Subtract the non-local pair count */
3821 nsp_est -= nsp_est_nl;
3825 fprintf(debug, "nsp_est local %5.1f non-local %5.1f\n",
3826 nsp_est, nsp_est_nl);
3831 nsp_est = nsp_est_nl;
3834 if (min_ci_balanced <= 0 || grid->nc >= min_ci_balanced || grid->nc == 0)
3836 /* We don't need to worry */
3841 /* Thus the (average) maximum j-list size should be as follows */
3842 nsubpair_max = max(1, (int)(nsp_est/min_ci_balanced+0.5));
3844 /* Since the target value is a maximum (this avoids high outliers,
3845 * which lead to load imbalance), not average, we add half the
3846 * number of pairs in a cj4 block to get the average about right.
3848 nsubpair_max += GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE/2;
3853 fprintf(debug, "nbl nsp estimate %.1f, nsubpair_max %d\n",
3854 nsp_est, nsubpair_max);
3857 return nsubpair_max;
3860 /* Debug list print function */
3861 static void print_nblist_ci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3865 for (i = 0; i < nbl->nci; i++)
3867 fprintf(fp, "ci %4d shift %2d ncj %3d\n",
3868 nbl->ci[i].ci, nbl->ci[i].shift,
3869 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start);
3871 for (j = nbl->ci[i].cj_ind_start; j < nbl->ci[i].cj_ind_end; j++)
3873 fprintf(fp, " cj %5d imask %x\n",
3880 /* Debug list print function */
3881 static void print_nblist_sci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3883 int i, j4, j, ncp, si;
3885 for (i = 0; i < nbl->nsci; i++)
3887 fprintf(fp, "ci %4d shift %2d ncj4 %2d\n",
3888 nbl->sci[i].sci, nbl->sci[i].shift,
3889 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start);
3892 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
3894 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
3896 fprintf(fp, " sj %5d imask %x\n",
3898 nbl->cj4[j4].imei[0].imask);
3899 for (si=0; si<GPU_NSUBCELL; si++)
3901 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
3908 fprintf(fp, "ci %4d shift %2d ncj4 %2d ncp %3d\n",
3909 nbl->sci[i].sci, nbl->sci[i].shift,
3910 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start,
3915 /* Combine pair lists *nbl generated on multiple threads nblc */
3916 static void combine_nblists(int nnbl, nbnxn_pairlist_t **nbl,
3917 nbnxn_pairlist_t *nblc)
3919 int nsci, ncj4, nexcl;
3924 gmx_incons("combine_nblists does not support simple lists");
3929 nexcl = nblc->nexcl;
3930 for (i = 0; i < nnbl; i++)
3932 nsci += nbl[i]->nsci;
3933 ncj4 += nbl[i]->ncj4;
3934 nexcl += nbl[i]->nexcl;
3937 if (nsci > nblc->sci_nalloc)
3939 nb_realloc_sci(nblc, nsci);
3941 if (ncj4 > nblc->cj4_nalloc)
3943 nblc->cj4_nalloc = over_alloc_small(ncj4);
3944 nbnxn_realloc_void((void **)&nblc->cj4,
3945 nblc->ncj4*sizeof(*nblc->cj4),
3946 nblc->cj4_nalloc*sizeof(*nblc->cj4),
3947 nblc->alloc, nblc->free);
3949 if (nexcl > nblc->excl_nalloc)
3951 nblc->excl_nalloc = over_alloc_small(nexcl);
3952 nbnxn_realloc_void((void **)&nblc->excl,
3953 nblc->nexcl*sizeof(*nblc->excl),
3954 nblc->excl_nalloc*sizeof(*nblc->excl),
3955 nblc->alloc, nblc->free);
3958 /* Each thread should copy its own data to the combined arrays,
3959 * as otherwise data will go back and forth between different caches.
3961 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
3962 for (n = 0; n < nnbl; n++)
3969 const nbnxn_pairlist_t *nbli;
3971 /* Determine the offset in the combined data for our thread */
3972 sci_offset = nblc->nsci;
3973 cj4_offset = nblc->ncj4;
3974 ci_offset = nblc->nci_tot;
3975 excl_offset = nblc->nexcl;
3977 for (i = 0; i < n; i++)
3979 sci_offset += nbl[i]->nsci;
3980 cj4_offset += nbl[i]->ncj4;
3981 ci_offset += nbl[i]->nci_tot;
3982 excl_offset += nbl[i]->nexcl;
3987 for (i = 0; i < nbli->nsci; i++)
3989 nblc->sci[sci_offset+i] = nbli->sci[i];
3990 nblc->sci[sci_offset+i].cj4_ind_start += cj4_offset;
3991 nblc->sci[sci_offset+i].cj4_ind_end += cj4_offset;
3994 for (j4 = 0; j4 < nbli->ncj4; j4++)
3996 nblc->cj4[cj4_offset+j4] = nbli->cj4[j4];
3997 nblc->cj4[cj4_offset+j4].imei[0].excl_ind += excl_offset;
3998 nblc->cj4[cj4_offset+j4].imei[1].excl_ind += excl_offset;
4001 for (j4 = 0; j4 < nbli->nexcl; j4++)
4003 nblc->excl[excl_offset+j4] = nbli->excl[j4];
4007 for (n = 0; n < nnbl; n++)
4009 nblc->nsci += nbl[n]->nsci;
4010 nblc->ncj4 += nbl[n]->ncj4;
4011 nblc->nci_tot += nbl[n]->nci_tot;
4012 nblc->nexcl += nbl[n]->nexcl;
4016 /* Returns the next ci to be processes by our thread */
4017 static gmx_bool next_ci(const nbnxn_grid_t *grid,
4019 int nth, int ci_block,
4020 int *ci_x, int *ci_y,
4026 if (*ci_b == ci_block)
4028 /* Jump to the next block assigned to this task */
4029 *ci += (nth - 1)*ci_block;
4033 if (*ci >= grid->nc*conv)
4038 while (*ci >= grid->cxy_ind[*ci_x*grid->ncy + *ci_y + 1]*conv)
4041 if (*ci_y == grid->ncy)
4051 /* Returns the distance^2 for which we put cell pairs in the list
4052 * without checking atom pair distances. This is usually < rlist^2.
4054 static float boundingbox_only_distance2(const nbnxn_grid_t *gridi,
4055 const nbnxn_grid_t *gridj,
4059 /* If the distance between two sub-cell bounding boxes is less
4060 * than this distance, do not check the distance between
4061 * all particle pairs in the sub-cell, since then it is likely
4062 * that the box pair has atom pairs within the cut-off.
4063 * We use the nblist cut-off minus 0.5 times the average x/y diagonal
4064 * spacing of the sub-cells. Around 40% of the checked pairs are pruned.
4065 * Using more than 0.5 gains at most 0.5%.
4066 * If forces are calculated more than twice, the performance gain
4067 * in the force calculation outweighs the cost of checking.
4068 * Note that with subcell lists, the atom-pair distance check
4069 * is only performed when only 1 out of 8 sub-cells in within range,
4070 * this is because the GPU is much faster than the cpu.
4075 bbx = 0.5*(gridi->sx + gridj->sx);
4076 bby = 0.5*(gridi->sy + gridj->sy);
4079 bbx /= GPU_NSUBCELL_X;
4080 bby /= GPU_NSUBCELL_Y;
4083 rbb2 = sqr(max(0, rlist - 0.5*sqrt(bbx*bbx + bby*bby)));
4088 return (float)((1+GMX_FLOAT_EPS)*rbb2);
4092 static int get_ci_block_size(const nbnxn_grid_t *gridi,
4093 gmx_bool bDomDec, int nth)
4095 const int ci_block_enum = 5;
4096 const int ci_block_denom = 11;
4097 const int ci_block_min_atoms = 16;
4100 /* Here we decide how to distribute the blocks over the threads.
4101 * We use prime numbers to try to avoid that the grid size becomes
4102 * a multiple of the number of threads, which would lead to some
4103 * threads getting "inner" pairs and others getting boundary pairs,
4104 * which in turns will lead to load imbalance between threads.
4105 * Set the block size as 5/11/ntask times the average number of cells
4106 * in a y,z slab. This should ensure a quite uniform distribution
4107 * of the grid parts of the different thread along all three grid
4108 * zone boundaries with 3D domain decomposition. At the same time
4109 * the blocks will not become too small.
4111 ci_block = (gridi->nc*ci_block_enum)/(ci_block_denom*gridi->ncx*nth);
4113 /* Ensure the blocks are not too small: avoids cache invalidation */
4114 if (ci_block*gridi->na_sc < ci_block_min_atoms)
4116 ci_block = (ci_block_min_atoms + gridi->na_sc - 1)/gridi->na_sc;
4119 /* Without domain decomposition
4120 * or with less than 3 blocks per task, divide in nth blocks.
4122 if (!bDomDec || ci_block*3*nth > gridi->nc)
4124 ci_block = (gridi->nc + nth - 1)/nth;
4130 /* Generates the part of pair-list nbl assigned to our thread */
4131 static void nbnxn_make_pairlist_part(const nbnxn_search_t nbs,
4132 const nbnxn_grid_t *gridi,
4133 const nbnxn_grid_t *gridj,
4134 nbnxn_search_work_t *work,
4135 const nbnxn_atomdata_t *nbat,
4136 const t_blocka *excl,
4140 gmx_bool bFBufferFlag,
4143 int min_ci_balanced,
4145 nbnxn_pairlist_t *nbl)
4152 int ci_b, ci, ci_x, ci_y, ci_xy, cj;
4158 int conv_i, cell0_i;
4159 const float *bb_i, *bbcz_i, *bbcz_j;
4161 real bx0, bx1, by0, by1, bz0, bz1;
4163 real d2cx, d2z, d2z_cx, d2z_cy, d2zx, d2zxy, d2xy;
4164 int cxf, cxl, cyf, cyf_x, cyl;
4166 int c0, c1, cs, cf, cl;
4169 int gridi_flag_shift = 0, gridj_flag_shift = 0;
4170 unsigned *gridj_flag = NULL;
4171 int ncj_old_i, ncj_old_j;
4173 nbs_cycle_start(&work->cc[enbsCCsearch]);
4175 if (gridj->bSimple != nbl->bSimple)
4177 gmx_incons("Grid incompatible with pair-list");
4181 nbl->na_sc = gridj->na_sc;
4182 nbl->na_ci = gridj->na_c;
4183 nbl->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
4184 na_cj_2log = get_2log(nbl->na_cj);
4190 /* Determine conversion of clusters to flag blocks */
4191 gridi_flag_shift = 0;
4192 while ((nbl->na_ci<<gridi_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4196 gridj_flag_shift = 0;
4197 while ((nbl->na_cj<<gridj_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4202 gridj_flag = work->buffer_flags.flag;
4205 copy_mat(nbs->box, box);
4207 rl2 = nbl->rlist*nbl->rlist;
4209 rbb2 = boundingbox_only_distance2(gridi, gridj, nbl->rlist, nbl->bSimple);
4213 fprintf(debug, "nbl bounding box only distance %f\n", sqrt(rbb2));
4216 /* Set the shift range */
4217 for (d = 0; d < DIM; d++)
4219 /* Check if we need periodicity shifts.
4220 * Without PBC or with domain decomposition we don't need them.
4222 if (d >= ePBC2npbcdim(nbs->ePBC) || nbs->dd_dim[d])
4229 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
4240 if (nbl->bSimple && !gridi->bSimple)
4242 conv_i = gridi->na_sc/gridj->na_sc;
4243 bb_i = gridi->bb_simple;
4244 bbcz_i = gridi->bbcz_simple;
4245 flags_i = gridi->flags_simple;
4251 bbcz_i = gridi->bbcz;
4252 flags_i = gridi->flags;
4254 cell0_i = gridi->cell0*conv_i;
4256 bbcz_j = gridj->bbcz;
4260 /* Blocks of the conversion factor - 1 give a large repeat count
4261 * combined with a small block size. This should result in good
4262 * load balancing for both small and large domains.
4264 ci_block = conv_i - 1;
4268 fprintf(debug, "nbl nc_i %d col.av. %.1f ci_block %d\n",
4269 gridi->nc, gridi->nc/(double)(gridi->ncx*gridi->ncy), ci_block);
4275 /* Initially ci_b and ci to 1 before where we want them to start,
4276 * as they will both be incremented in next_ci.
4279 ci = th*ci_block - 1;
4282 while (next_ci(gridi, conv_i, nth, ci_block, &ci_x, &ci_y, &ci_b, &ci))
4284 if (nbl->bSimple && flags_i[ci] == 0)
4289 ncj_old_i = nbl->ncj;
4292 if (gridj != gridi && shp[XX] == 0)
4296 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX];
4300 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx;
4302 if (bx1 < gridj->c0[XX])
4304 d2cx = sqr(gridj->c0[XX] - bx1);
4313 ci_xy = ci_x*gridi->ncy + ci_y;
4315 /* Loop over shift vectors in three dimensions */
4316 for (tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
4318 shz = tz*box[ZZ][ZZ];
4320 bz0 = bbcz_i[ci*NNBSBB_D ] + shz;
4321 bz1 = bbcz_i[ci*NNBSBB_D+1] + shz;
4333 d2z = sqr(bz0 - box[ZZ][ZZ]);
4336 d2z_cx = d2z + d2cx;
4344 bz1/((real)(gridi->cxy_ind[ci_xy+1] - gridi->cxy_ind[ci_xy]));
4349 /* The check with bz1_frac close to or larger than 1 comes later */
4351 for (ty = -shp[YY]; ty <= shp[YY]; ty++)
4353 shy = ty*box[YY][YY] + tz*box[ZZ][YY];
4357 by0 = bb_i[ci*NNBSBB_B +YY] + shy;
4358 by1 = bb_i[ci*NNBSBB_B+NNBSBB_C+YY] + shy;
4362 by0 = gridi->c0[YY] + (ci_y )*gridi->sy + shy;
4363 by1 = gridi->c0[YY] + (ci_y+1)*gridi->sy + shy;
4366 get_cell_range(by0, by1,
4367 gridj->ncy, gridj->c0[YY], gridj->sy, gridj->inv_sy,
4377 if (by1 < gridj->c0[YY])
4379 d2z_cy += sqr(gridj->c0[YY] - by1);
4381 else if (by0 > gridj->c1[YY])
4383 d2z_cy += sqr(by0 - gridj->c1[YY]);
4386 for (tx = -shp[XX]; tx <= shp[XX]; tx++)
4388 shift = XYZ2IS(tx, ty, tz);
4390 #ifdef NBNXN_SHIFT_BACKWARD
4391 if (gridi == gridj && shift > CENTRAL)
4397 shx = tx*box[XX][XX] + ty*box[YY][XX] + tz*box[ZZ][XX];
4401 bx0 = bb_i[ci*NNBSBB_B +XX] + shx;
4402 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX] + shx;
4406 bx0 = gridi->c0[XX] + (ci_x )*gridi->sx + shx;
4407 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx + shx;
4410 get_cell_range(bx0, bx1,
4411 gridj->ncx, gridj->c0[XX], gridj->sx, gridj->inv_sx,
4422 new_ci_entry(nbl, cell0_i+ci, shift, flags_i[ci],
4427 new_sci_entry(nbl, cell0_i+ci, shift, flags_i[ci],
4431 #ifndef NBNXN_SHIFT_BACKWARD
4434 if (shift == CENTRAL && gridi == gridj &&
4438 /* Leave the pairs with i > j.
4439 * x is the major index, so skip half of it.
4446 set_icell_bb_simple(bb_i, ci, shx, shy, shz,
4451 set_icell_bb_supersub(bb_i, ci, shx, shy, shz,
4455 nbs->icell_set_x(cell0_i+ci, shx, shy, shz,
4456 gridi->na_c, nbat->xstride, nbat->x,
4459 for (cx = cxf; cx <= cxl; cx++)
4462 if (gridj->c0[XX] + cx*gridj->sx > bx1)
4464 d2zx += sqr(gridj->c0[XX] + cx*gridj->sx - bx1);
4466 else if (gridj->c0[XX] + (cx+1)*gridj->sx < bx0)
4468 d2zx += sqr(gridj->c0[XX] + (cx+1)*gridj->sx - bx0);
4471 #ifndef NBNXN_SHIFT_BACKWARD
4472 if (gridi == gridj &&
4473 cx == 0 && cyf < ci_y)
4475 if (gridi == gridj &&
4476 cx == 0 && shift == CENTRAL && cyf < ci_y)
4479 /* Leave the pairs with i > j.
4480 * Skip half of y when i and j have the same x.
4489 for (cy = cyf_x; cy <= cyl; cy++)
4491 c0 = gridj->cxy_ind[cx*gridj->ncy+cy];
4492 c1 = gridj->cxy_ind[cx*gridj->ncy+cy+1];
4493 #ifdef NBNXN_SHIFT_BACKWARD
4494 if (gridi == gridj &&
4495 shift == CENTRAL && c0 < ci)
4502 if (gridj->c0[YY] + cy*gridj->sy > by1)
4504 d2zxy += sqr(gridj->c0[YY] + cy*gridj->sy - by1);
4506 else if (gridj->c0[YY] + (cy+1)*gridj->sy < by0)
4508 d2zxy += sqr(gridj->c0[YY] + (cy+1)*gridj->sy - by0);
4510 if (c1 > c0 && d2zxy < rl2)
4512 cs = c0 + (int)(bz1_frac*(c1 - c0));
4520 /* Find the lowest cell that can possibly
4525 (bbcz_j[cf*NNBSBB_D+1] >= bz0 ||
4526 d2xy + sqr(bbcz_j[cf*NNBSBB_D+1] - bz0) < rl2))
4531 /* Find the highest cell that can possibly
4536 (bbcz_j[cl*NNBSBB_D] <= bz1 ||
4537 d2xy + sqr(bbcz_j[cl*NNBSBB_D] - bz1) < rl2))
4542 #ifdef NBNXN_REFCODE
4544 /* Simple reference code, for debugging,
4545 * overrides the more complex code above.
4550 for (k = c0; k < c1; k++)
4552 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1,
4553 bb+k*NNBSBB_B) < rl2 &&
4558 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1,
4559 bb+k*NNBSBB_B) < rl2 &&
4570 /* We want each atom/cell pair only once,
4571 * only use cj >= ci.
4573 #ifndef NBNXN_SHIFT_BACKWARD
4576 if (shift == CENTRAL)
4585 /* For f buffer flags with simple lists */
4586 ncj_old_j = nbl->ncj;
4588 switch (nb_kernel_type)
4590 case nbnxnk4x4_PlainC:
4591 check_subcell_list_space_simple(nbl, cl-cf+1);
4593 make_cluster_list_simple(gridj,
4595 (gridi == gridj && shift == CENTRAL),
4600 #ifdef GMX_NBNXN_SIMD_4XN
4601 case nbnxnk4xN_SIMD_4xN:
4602 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4603 make_cluster_list_simd_4xn(gridj,
4605 (gridi == gridj && shift == CENTRAL),
4611 #ifdef GMX_NBNXN_SIMD_2XNN
4612 case nbnxnk4xN_SIMD_2xNN:
4613 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4614 make_cluster_list_simd_2xnn(gridj,
4616 (gridi == gridj && shift == CENTRAL),
4622 case nbnxnk8x8x8_PlainC:
4623 case nbnxnk8x8x8_CUDA:
4624 check_subcell_list_space_supersub(nbl, cl-cf+1);
4625 for (cj = cf; cj <= cl; cj++)
4627 make_cluster_list_supersub(nbs, gridi, gridj,
4629 (gridi == gridj && shift == CENTRAL && ci == cj),
4630 nbat->xstride, nbat->x,
4636 ncpcheck += cl - cf + 1;
4638 if (bFBufferFlag && nbl->ncj > ncj_old_j)
4642 cbf = nbl->cj[ncj_old_j].cj >> gridj_flag_shift;
4643 cbl = nbl->cj[nbl->ncj-1].cj >> gridj_flag_shift;
4644 for (cb = cbf; cb <= cbl; cb++)
4646 gridj_flag[cb] = 1U<<th;
4654 /* Set the exclusions for this ci list */
4657 set_ci_top_excls(nbs,
4659 shift == CENTRAL && gridi == gridj,
4662 &(nbl->ci[nbl->nci]),
4667 set_sci_top_excls(nbs,
4669 shift == CENTRAL && gridi == gridj,
4671 &(nbl->sci[nbl->nsci]),
4675 /* Close this ci list */
4678 close_ci_entry_simple(nbl);
4682 close_ci_entry_supersub(nbl,
4684 progBal, min_ci_balanced,
4691 if (bFBufferFlag && nbl->ncj > ncj_old_i)
4693 work->buffer_flags.flag[(gridi->cell0+ci)>>gridi_flag_shift] = 1U<<th;
4697 work->ndistc = ndistc;
4699 nbs_cycle_stop(&work->cc[enbsCCsearch]);
4703 fprintf(debug, "number of distance checks %d\n", ndistc);
4704 fprintf(debug, "ncpcheck %s %d\n", gridi == gridj ? "local" : "non-local",
4709 print_nblist_statistics_simple(debug, nbl, nbs, rlist);
4713 print_nblist_statistics_supersub(debug, nbl, nbs, rlist);
4719 static void reduce_buffer_flags(const nbnxn_search_t nbs,
4721 const nbnxn_buffer_flags_t *dest)
4724 const unsigned *flag;
4726 for (s = 0; s < nsrc; s++)
4728 flag = nbs->work[s].buffer_flags.flag;
4730 for (b = 0; b < dest->nflag; b++)
4732 dest->flag[b] |= flag[b];
4737 static void print_reduction_cost(const nbnxn_buffer_flags_t *flags, int nout)
4739 int nelem, nkeep, ncopy, nred, b, c, out;
4745 for (b = 0; b < flags->nflag; b++)
4747 if (flags->flag[b] == 1)
4749 /* Only flag 0 is set, no copy of reduction required */
4753 else if (flags->flag[b] > 0)
4756 for (out = 0; out < nout; out++)
4758 if (flags->flag[b] & (1U<<out))
4775 fprintf(debug, "nbnxn reduction: #flag %d #list %d elem %4.2f, keep %4.2f copy %4.2f red %4.2f\n",
4777 nelem/(double)(flags->nflag),
4778 nkeep/(double)(flags->nflag),
4779 ncopy/(double)(flags->nflag),
4780 nred/(double)(flags->nflag));
4783 /* Perform a count (linear) sort to sort the smaller lists to the end.
4784 * This avoids load imbalance on the GPU, as large lists will be
4785 * scheduled and executed first and the smaller lists later.
4786 * Load balancing between multi-processors only happens at the end
4787 * and there smaller lists lead to more effective load balancing.
4788 * The sorting is done on the cj4 count, not on the actual pair counts.
4789 * Not only does this make the sort faster, but it also results in
4790 * better load balancing than using a list sorted on exact load.
4791 * This function swaps the pointer in the pair list to avoid a copy operation.
4793 static void sort_sci(nbnxn_pairlist_t *nbl)
4795 nbnxn_list_work_t *work;
4796 int m, i, s, s0, s1;
4797 nbnxn_sci_t *sci_sort;
4799 if (nbl->ncj4 <= nbl->nsci)
4801 /* nsci = 0 or all sci have size 1, sorting won't change the order */
4807 /* We will distinguish differences up to double the average */
4808 m = (2*nbl->ncj4)/nbl->nsci;
4810 if (m + 1 > work->sort_nalloc)
4812 work->sort_nalloc = over_alloc_large(m + 1);
4813 srenew(work->sort, work->sort_nalloc);
4816 if (work->sci_sort_nalloc != nbl->sci_nalloc)
4818 work->sci_sort_nalloc = nbl->sci_nalloc;
4819 nbnxn_realloc_void((void **)&work->sci_sort,
4821 work->sci_sort_nalloc*sizeof(*work->sci_sort),
4822 nbl->alloc, nbl->free);
4825 /* Count the entries of each size */
4826 for(i = 0; i <= m; i++)
4830 for(s = 0; s < nbl->nsci; s++)
4832 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4835 /* Calculate the offset for each count */
4838 for(i = m - 1; i >= 0; i--)
4841 work->sort[i] = work->sort[i + 1] + s0;
4845 /* Sort entries directly into place */
4846 sci_sort = work->sci_sort;
4847 for(s = 0; s < nbl->nsci; s++)
4849 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4850 sci_sort[work->sort[i]++] = nbl->sci[s];
4853 /* Swap the sci pointers so we use the new, sorted list */
4854 work->sci_sort = nbl->sci;
4855 nbl->sci = sci_sort;
4858 /* Make a local or non-local pair-list, depending on iloc */
4859 void nbnxn_make_pairlist(const nbnxn_search_t nbs,
4860 nbnxn_atomdata_t *nbat,
4861 const t_blocka *excl,
4863 int min_ci_balanced,
4864 nbnxn_pairlist_set_t *nbl_list,
4869 nbnxn_grid_t *gridi, *gridj;
4871 int nzi, zi, zj0, zj1, zj;
4875 nbnxn_pairlist_t **nbl;
4877 gmx_bool CombineNBLists;
4879 int np_tot, np_noq, np_hlj, nap;
4881 /* Check if we are running hybrid GPU + CPU nbnxn mode */
4882 bGPUCPU = (!nbs->grid[0].bSimple && nbl_list->bSimple);
4884 nnbl = nbl_list->nnbl;
4885 nbl = nbl_list->nbl;
4886 CombineNBLists = nbl_list->bCombined;
4890 fprintf(debug, "ns making %d nblists\n", nnbl);
4893 nbat->bUseBufferFlags = (nbat->nout > 1);
4894 /* We should re-init the flags before making the first list */
4895 if (nbat->bUseBufferFlags && (LOCAL_I(iloc) || bGPUCPU))
4897 init_buffer_flags(&nbat->buffer_flags, nbat->natoms);
4900 if (nbl_list->bSimple)
4902 switch (nb_kernel_type)
4904 #ifdef GMX_NBNXN_SIMD_4XN
4905 case nbnxnk4xN_SIMD_4xN:
4906 nbs->icell_set_x = icell_set_x_simd_4xn;
4909 #ifdef GMX_NBNXN_SIMD_2XNN
4910 case nbnxnk4xN_SIMD_2xNN:
4911 nbs->icell_set_x = icell_set_x_simd_2xnn;
4915 nbs->icell_set_x = icell_set_x_simple;
4921 #ifdef NBNXN_SEARCH_BB_SSE
4922 nbs->icell_set_x = icell_set_x_supersub_sse8;
4924 nbs->icell_set_x = icell_set_x_supersub;
4930 /* Only zone (grid) 0 vs 0 */
4937 nzi = nbs->zones->nizone;
4940 if (!nbl_list->bSimple && min_ci_balanced > 0)
4942 nsubpair_max = get_nsubpair_max(nbs, iloc, rlist, min_ci_balanced);
4949 /* Clear all pair-lists */
4950 for (th = 0; th < nnbl; th++)
4952 clear_pairlist(nbl[th]);
4955 for (zi = 0; zi < nzi; zi++)
4957 gridi = &nbs->grid[zi];
4959 if (NONLOCAL_I(iloc))
4961 zj0 = nbs->zones->izone[zi].j0;
4962 zj1 = nbs->zones->izone[zi].j1;
4968 for (zj = zj0; zj < zj1; zj++)
4970 gridj = &nbs->grid[zj];
4974 fprintf(debug, "ns search grid %d vs %d\n", zi, zj);
4977 nbs_cycle_start(&nbs->cc[enbsCCsearch]);
4979 if (nbl[0]->bSimple && !gridi->bSimple)
4981 /* Hybrid list, determine blocking later */
4986 ci_block = get_ci_block_size(gridi, nbs->DomDec, nnbl);
4989 #pragma omp parallel for num_threads(nnbl) schedule(static)
4990 for (th = 0; th < nnbl; th++)
4992 /* Re-init the thread-local work flag data before making
4993 * the first list (not an elegant conditional).
4995 if (nbat->bUseBufferFlags && ((zi == 0 && zj == 0) ||
4996 (bGPUCPU && zi == 0 && zj == 1)))
4998 init_buffer_flags(&nbs->work[th].buffer_flags, nbat->natoms);
5001 if (CombineNBLists && th > 0)
5003 clear_pairlist(nbl[th]);
5006 /* With GPU: generate progressively smaller lists for
5007 * load balancing for local only or non-local with 2 zones.
5009 progBal = (LOCAL_I(iloc) || nbs->zones->n <= 2);
5011 /* Divide the i super cell equally over the nblists */
5012 nbnxn_make_pairlist_part(nbs, gridi, gridj,
5013 &nbs->work[th], nbat, excl,
5017 nbat->bUseBufferFlags,
5019 progBal, min_ci_balanced,
5023 nbs_cycle_stop(&nbs->cc[enbsCCsearch]);
5028 for (th = 0; th < nnbl; th++)
5030 inc_nrnb(nrnb, eNR_NBNXN_DIST2, nbs->work[th].ndistc);
5032 if (nbl_list->bSimple)
5034 np_tot += nbl[th]->ncj;
5035 np_noq += nbl[th]->work->ncj_noq;
5036 np_hlj += nbl[th]->work->ncj_hlj;
5040 /* This count ignores potential subsequent pair pruning */
5041 np_tot += nbl[th]->nci_tot;
5044 nap = nbl[0]->na_ci*nbl[0]->na_cj;
5045 nbl_list->natpair_ljq = (np_tot - np_noq)*nap - np_hlj*nap/2;
5046 nbl_list->natpair_lj = np_noq*nap;
5047 nbl_list->natpair_q = np_hlj*nap/2;
5049 if (CombineNBLists && nnbl > 1)
5051 nbs_cycle_start(&nbs->cc[enbsCCcombine]);
5053 combine_nblists(nnbl-1, nbl+1, nbl[0]);
5055 nbs_cycle_stop(&nbs->cc[enbsCCcombine]);
5060 if (!nbl_list->bSimple)
5062 /* Sort the entries on size, large ones first */
5063 if (CombineNBLists || nnbl == 1)
5069 #pragma omp parallel for num_threads(nnbl) schedule(static)
5070 for (th = 0; th < nnbl; th++)
5077 if (nbat->bUseBufferFlags)
5079 reduce_buffer_flags(nbs, nnbl, &nbat->buffer_flags);
5082 /* Special performance logging stuff (env.var. GMX_NBNXN_CYCLE) */
5085 nbs->search_count++;
5087 if (nbs->print_cycles &&
5088 (!nbs->DomDec || (nbs->DomDec && !LOCAL_I(iloc))) &&
5089 nbs->search_count % 100 == 0)
5091 nbs_cycle_print(stderr, nbs);
5094 if (debug && (CombineNBLists && nnbl > 1))
5096 if (nbl[0]->bSimple)
5098 print_nblist_statistics_simple(debug, nbl[0], nbs, rlist);
5102 print_nblist_statistics_supersub(debug, nbl[0], nbs, rlist);
5110 if (nbl[0]->bSimple)
5112 print_nblist_ci_cj(debug, nbl[0]);
5116 print_nblist_sci_cj(debug, nbl[0]);
5120 if (nbat->bUseBufferFlags)
5122 print_reduction_cost(&nbat->buffer_flags, nnbl);