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,
430 real adens, tlen, tlen_x, tlen_y, nc_max;
433 rvec_sub(corner1, corner0, size);
437 /* target cell length */
440 /* To minimize the zero interactions, we should make
441 * the largest of the i/j cell cubic.
443 na_c = max(grid->na_c, grid->na_cj);
445 /* Approximately cubic cells */
446 tlen = pow(na_c/atom_density, 1.0/3.0);
452 /* Approximately cubic sub cells */
453 tlen = pow(grid->na_c/atom_density, 1.0/3.0);
454 tlen_x = tlen*GPU_NSUBCELL_X;
455 tlen_y = tlen*GPU_NSUBCELL_Y;
457 /* We round ncx and ncy down, because we get less cell pairs
458 * in the nbsist when the fixed cell dimensions (x,y) are
459 * larger than the variable one (z) than the other way around.
461 grid->ncx = max(1, (int)(size[XX]/tlen_x));
462 grid->ncy = max(1, (int)(size[YY]/tlen_y));
470 grid->sx = size[XX]/grid->ncx;
471 grid->sy = size[YY]/grid->ncy;
472 grid->inv_sx = 1/grid->sx;
473 grid->inv_sy = 1/grid->sy;
477 /* This is a non-home zone, add an extra row of cells
478 * for particles communicated for bonded interactions.
479 * These can be beyond the cut-off. It doesn't matter where
480 * they end up on the grid, but for performance it's better
481 * if they don't end up in cells that can be within cut-off range.
487 /* We need one additional cell entry for particles moved by DD */
488 if (grid->ncx*grid->ncy+1 > grid->cxy_nalloc)
490 grid->cxy_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
491 srenew(grid->cxy_na, grid->cxy_nalloc);
492 srenew(grid->cxy_ind, grid->cxy_nalloc+1);
494 for (t = 0; t < nbs->nthread_max; t++)
496 if (grid->ncx*grid->ncy+1 > nbs->work[t].cxy_na_nalloc)
498 nbs->work[t].cxy_na_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
499 srenew(nbs->work[t].cxy_na, nbs->work[t].cxy_na_nalloc);
503 /* Worst case scenario of 1 atom in each last cell */
504 if (grid->na_cj <= grid->na_c)
506 nc_max = n/grid->na_sc + grid->ncx*grid->ncy;
510 nc_max = n/grid->na_sc + grid->ncx*grid->ncy*grid->na_cj/grid->na_c;
513 if (nc_max > grid->nc_nalloc)
517 grid->nc_nalloc = over_alloc_large(nc_max);
518 srenew(grid->nsubc, grid->nc_nalloc);
519 srenew(grid->bbcz, grid->nc_nalloc*NNBSBB_D);
521 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX;
523 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL*NNBSBB_B;
525 sfree_aligned(grid->bb);
526 /* This snew also zeros the contents, this avoid possible
527 * floating exceptions in SSE with the unused bb elements.
529 snew_aligned(grid->bb, bb_nalloc, 16);
533 if (grid->na_cj == grid->na_c)
535 grid->bbj = grid->bb;
539 sfree_aligned(grid->bbj);
540 snew_aligned(grid->bbj, bb_nalloc*grid->na_c/grid->na_cj, 16);
544 srenew(grid->flags, grid->nc_nalloc);
547 copy_rvec(corner0, grid->c0);
548 copy_rvec(corner1, grid->c1);
553 /* We need to sort paricles in grid columns on z-coordinate.
554 * As particle are very often distributed homogeneously, we a sorting
555 * algorithm similar to pigeonhole sort. We multiply the z-coordinate
556 * by a factor, cast to an int and try to store in that hole. If the hole
557 * is full, we move this or another particle. A second pass is needed to make
558 * contiguous elements. SORT_GRID_OVERSIZE is the ratio of holes to particles.
559 * 4 is the optimal value for homogeneous particle distribution and allows
560 * for an O(#particles) sort up till distributions were all particles are
561 * concentrated in 1/4 of the space. No NlogN fallback is implemented,
562 * as it can be expensive to detect imhomogeneous particle distributions.
563 * SGSF is the maximum ratio of holes used, in the worst case all particles
564 * end up in the last hole and we need #particles extra holes at the end.
566 #define SORT_GRID_OVERSIZE 4
567 #define SGSF (SORT_GRID_OVERSIZE + 1)
569 /* Sort particle index a on coordinates x along dim.
570 * Backwards tells if we want decreasing iso increasing coordinates.
571 * h0 is the minimum of the coordinate range.
572 * invh is the 1/length of the sorting range.
573 * n_per_h (>=n) is the expected average number of particles per 1/invh
574 * sort is the sorting work array.
575 * sort should have a size of at least n_per_h*SORT_GRID_OVERSIZE + n,
576 * or easier, allocate at least n*SGSF elements.
578 static void sort_atoms(int dim, gmx_bool Backwards,
579 int *a, int n, rvec *x,
580 real h0, real invh, int n_per_h,
584 int zi, zim, zi_min, zi_max;
596 gmx_incons("n > n_per_h");
600 /* Transform the inverse range height into the inverse hole height */
601 invh *= n_per_h*SORT_GRID_OVERSIZE;
603 /* Set nsort to the maximum possible number of holes used.
604 * In worst case all n elements end up in the last bin.
606 nsort = n_per_h*SORT_GRID_OVERSIZE + n;
608 /* Determine the index range used, so we can limit it for the second pass */
612 /* Sort the particles using a simple index sort */
613 for (i = 0; i < n; i++)
615 /* The cast takes care of float-point rounding effects below zero.
616 * This code assumes particles are less than 1/SORT_GRID_OVERSIZE
617 * times the box height out of the box.
619 zi = (int)((x[a[i]][dim] - h0)*invh);
622 /* As we can have rounding effect, we use > iso >= here */
623 if (zi < 0 || zi > n_per_h*SORT_GRID_OVERSIZE)
625 gmx_fatal(FARGS, "(int)((x[%d][%c]=%f - %f)*%f) = %d, not in 0 - %d*%d\n",
626 a[i], 'x'+dim, x[a[i]][dim], h0, invh, zi,
627 n_per_h, SORT_GRID_OVERSIZE);
631 /* Ideally this particle should go in sort cell zi,
632 * but that might already be in use,
633 * in that case find the first empty cell higher up
638 zi_min = min(zi_min, zi);
639 zi_max = max(zi_max, zi);
643 /* We have multiple atoms in the same sorting slot.
644 * Sort on real z for minimal bounding box size.
645 * There is an extra check for identical z to ensure
646 * well-defined output order, independent of input order
647 * to ensure binary reproducibility after restarts.
649 while (sort[zi] >= 0 && ( x[a[i]][dim] > x[sort[zi]][dim] ||
650 (x[a[i]][dim] == x[sort[zi]][dim] &&
658 /* Shift all elements by one slot until we find an empty slot */
661 while (sort[zim] >= 0)
669 zi_max = max(zi_max, zim);
672 zi_max = max(zi_max, zi);
679 for (zi = 0; zi < nsort; zi++)
690 for (zi = zi_max; zi >= zi_min; zi--)
701 gmx_incons("Lost particles while sorting");
706 #define R2F_D(x) ((float)((x) >= 0 ? ((1-GMX_FLOAT_EPS)*(x)) : ((1+GMX_FLOAT_EPS)*(x))))
707 #define R2F_U(x) ((float)((x) >= 0 ? ((1+GMX_FLOAT_EPS)*(x)) : ((1-GMX_FLOAT_EPS)*(x))))
713 /* Coordinate order x,y,z, bb order xyz0 */
714 static void calc_bounding_box(int na, int stride, const real *x, float *bb)
717 real xl, xh, yl, yh, zl, zh;
727 for (j = 1; j < na; j++)
729 xl = min(xl, x[i+XX]);
730 xh = max(xh, x[i+XX]);
731 yl = min(yl, x[i+YY]);
732 yh = max(yh, x[i+YY]);
733 zl = min(zl, x[i+ZZ]);
734 zh = max(zh, x[i+ZZ]);
737 /* Note: possible double to float conversion here */
738 bb[BBL_X] = R2F_D(xl);
739 bb[BBL_Y] = R2F_D(yl);
740 bb[BBL_Z] = R2F_D(zl);
741 bb[BBU_X] = R2F_U(xh);
742 bb[BBU_Y] = R2F_U(yh);
743 bb[BBU_Z] = R2F_U(zh);
746 /* Packed coordinates, bb order xyz0 */
747 static void calc_bounding_box_x_x4(int na, const real *x, float *bb)
750 real xl, xh, yl, yh, zl, zh;
758 for (j = 1; j < na; j++)
760 xl = min(xl, x[j+XX*PACK_X4]);
761 xh = max(xh, x[j+XX*PACK_X4]);
762 yl = min(yl, x[j+YY*PACK_X4]);
763 yh = max(yh, x[j+YY*PACK_X4]);
764 zl = min(zl, x[j+ZZ*PACK_X4]);
765 zh = max(zh, x[j+ZZ*PACK_X4]);
767 /* Note: possible double to float conversion here */
768 bb[BBL_X] = R2F_D(xl);
769 bb[BBL_Y] = R2F_D(yl);
770 bb[BBL_Z] = R2F_D(zl);
771 bb[BBU_X] = R2F_U(xh);
772 bb[BBU_Y] = R2F_U(yh);
773 bb[BBU_Z] = R2F_U(zh);
776 /* Packed coordinates, bb order xyz0 */
777 static void calc_bounding_box_x_x8(int na, const real *x, float *bb)
780 real xl, xh, yl, yh, zl, zh;
788 for (j = 1; j < na; j++)
790 xl = min(xl, x[j+XX*PACK_X8]);
791 xh = max(xh, x[j+XX*PACK_X8]);
792 yl = min(yl, x[j+YY*PACK_X8]);
793 yh = max(yh, x[j+YY*PACK_X8]);
794 zl = min(zl, x[j+ZZ*PACK_X8]);
795 zh = max(zh, x[j+ZZ*PACK_X8]);
797 /* Note: possible double to float conversion here */
798 bb[BBL_X] = R2F_D(xl);
799 bb[BBL_Y] = R2F_D(yl);
800 bb[BBL_Z] = R2F_D(zl);
801 bb[BBU_X] = R2F_U(xh);
802 bb[BBU_Y] = R2F_U(yh);
803 bb[BBU_Z] = R2F_U(zh);
806 #ifdef NBNXN_SEARCH_BB_SSE
808 /* Packed coordinates, bb order xyz0 */
809 static void calc_bounding_box_x_x4_halves(int na, const real *x,
810 float *bb, float *bbj)
812 calc_bounding_box_x_x4(min(na, 2), x, bbj);
816 calc_bounding_box_x_x4(min(na-2, 2), x+(PACK_X4>>1), bbj+NNBSBB_B);
820 /* Set the "empty" bounding box to the same as the first one,
821 * so we don't need to treat special cases in the rest of the code.
823 _mm_store_ps(bbj+NNBSBB_B, _mm_load_ps(bbj));
824 _mm_store_ps(bbj+NNBSBB_B+NNBSBB_C, _mm_load_ps(bbj+NNBSBB_C));
827 _mm_store_ps(bb, _mm_min_ps(_mm_load_ps(bbj),
828 _mm_load_ps(bbj+NNBSBB_B)));
829 _mm_store_ps(bb+NNBSBB_C, _mm_max_ps(_mm_load_ps(bbj+NNBSBB_C),
830 _mm_load_ps(bbj+NNBSBB_B+NNBSBB_C)));
833 /* Coordinate order xyz, bb order xxxxyyyyzzzz */
834 static void calc_bounding_box_xxxx(int na, int stride, const real *x, float *bb)
837 real xl, xh, yl, yh, zl, zh;
847 for (j = 1; j < na; j++)
849 xl = min(xl, x[i+XX]);
850 xh = max(xh, x[i+XX]);
851 yl = min(yl, x[i+YY]);
852 yh = max(yh, x[i+YY]);
853 zl = min(zl, x[i+ZZ]);
854 zh = max(zh, x[i+ZZ]);
857 /* Note: possible double to float conversion here */
858 bb[0*STRIDE_PBB] = R2F_D(xl);
859 bb[1*STRIDE_PBB] = R2F_D(yl);
860 bb[2*STRIDE_PBB] = R2F_D(zl);
861 bb[3*STRIDE_PBB] = R2F_U(xh);
862 bb[4*STRIDE_PBB] = R2F_U(yh);
863 bb[5*STRIDE_PBB] = R2F_U(zh);
866 #endif /* NBNXN_SEARCH_BB_SSE */
868 #ifdef NBNXN_SEARCH_SSE_SINGLE
870 /* Coordinate order xyz?, bb order xyz0 */
871 static void calc_bounding_box_sse(int na, const float *x, float *bb)
873 __m128 bb_0_SSE, bb_1_SSE;
878 bb_0_SSE = _mm_load_ps(x);
881 for (i = 1; i < na; i++)
883 x_SSE = _mm_load_ps(x+i*NNBSBB_C);
884 bb_0_SSE = _mm_min_ps(bb_0_SSE, x_SSE);
885 bb_1_SSE = _mm_max_ps(bb_1_SSE, x_SSE);
888 _mm_store_ps(bb, bb_0_SSE);
889 _mm_store_ps(bb+4, bb_1_SSE);
892 /* Coordinate order xyz?, bb order xxxxyyyyzzzz */
893 static void calc_bounding_box_xxxx_sse(int na, const float *x,
897 calc_bounding_box_sse(na, x, bb_work);
899 bb[0*STRIDE_PBB] = bb_work[BBL_X];
900 bb[1*STRIDE_PBB] = bb_work[BBL_Y];
901 bb[2*STRIDE_PBB] = bb_work[BBL_Z];
902 bb[3*STRIDE_PBB] = bb_work[BBU_X];
903 bb[4*STRIDE_PBB] = bb_work[BBU_Y];
904 bb[5*STRIDE_PBB] = bb_work[BBU_Z];
907 #endif /* NBNXN_SEARCH_SSE_SINGLE */
909 #ifdef NBNXN_SEARCH_BB_SSE
911 /* Combines pairs of consecutive bounding boxes */
912 static void combine_bounding_box_pairs(nbnxn_grid_t *grid, const float *bb)
914 int i, j, sc2, nc2, c2;
915 __m128 min_SSE, max_SSE;
917 for (i = 0; i < grid->ncx*grid->ncy; i++)
919 /* Starting bb in a column is expected to be 2-aligned */
920 sc2 = grid->cxy_ind[i]>>1;
921 /* For odd numbers skip the last bb here */
922 nc2 = (grid->cxy_na[i]+3)>>(2+1);
923 for (c2 = sc2; c2 < sc2+nc2; c2++)
925 min_SSE = _mm_min_ps(_mm_load_ps(bb+(c2*4+0)*NNBSBB_C),
926 _mm_load_ps(bb+(c2*4+2)*NNBSBB_C));
927 max_SSE = _mm_max_ps(_mm_load_ps(bb+(c2*4+1)*NNBSBB_C),
928 _mm_load_ps(bb+(c2*4+3)*NNBSBB_C));
929 _mm_store_ps(grid->bbj+(c2*2+0)*NNBSBB_C, min_SSE);
930 _mm_store_ps(grid->bbj+(c2*2+1)*NNBSBB_C, max_SSE);
932 if (((grid->cxy_na[i]+3)>>2) & 1)
934 /* Copy the last bb for odd bb count in this column */
935 for (j = 0; j < NNBSBB_C; j++)
937 grid->bbj[(c2*2+0)*NNBSBB_C+j] = bb[(c2*4+0)*NNBSBB_C+j];
938 grid->bbj[(c2*2+1)*NNBSBB_C+j] = bb[(c2*4+1)*NNBSBB_C+j];
947 /* Prints the average bb size, used for debug output */
948 static void print_bbsizes_simple(FILE *fp,
949 const nbnxn_search_t nbs,
950 const nbnxn_grid_t *grid)
956 for (c = 0; c < grid->nc; c++)
958 for (d = 0; d < DIM; d++)
960 ba[d] += grid->bb[c*NNBSBB_B+NNBSBB_C+d] - grid->bb[c*NNBSBB_B+d];
963 dsvmul(1.0/grid->nc, ba, ba);
965 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
966 nbs->box[XX][XX]/grid->ncx,
967 nbs->box[YY][YY]/grid->ncy,
968 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/grid->nc,
969 ba[XX], ba[YY], ba[ZZ],
970 ba[XX]*grid->ncx/nbs->box[XX][XX],
971 ba[YY]*grid->ncy/nbs->box[YY][YY],
972 ba[ZZ]*grid->nc/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
975 /* Prints the average bb size, used for debug output */
976 static void print_bbsizes_supersub(FILE *fp,
977 const nbnxn_search_t nbs,
978 const nbnxn_grid_t *grid)
985 for (c = 0; c < grid->nc; c++)
988 for (s = 0; s < grid->nsubc[c]; s += STRIDE_PBB)
992 cs_w = (c*GPU_NSUBCELL + s)/STRIDE_PBB;
993 for (i = 0; i < STRIDE_PBB; i++)
995 for (d = 0; d < DIM; d++)
998 grid->bb[cs_w*NNBSBB_XXXX+(DIM+d)*STRIDE_PBB+i] -
999 grid->bb[cs_w*NNBSBB_XXXX+ d *STRIDE_PBB+i];
1004 for (s = 0; s < grid->nsubc[c]; s++)
1008 cs = c*GPU_NSUBCELL + s;
1009 for (d = 0; d < DIM; d++)
1012 grid->bb[cs*NNBSBB_B+NNBSBB_C+d] -
1013 grid->bb[cs*NNBSBB_B +d];
1017 ns += grid->nsubc[c];
1019 dsvmul(1.0/ns, ba, ba);
1021 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
1022 nbs->box[XX][XX]/(grid->ncx*GPU_NSUBCELL_X),
1023 nbs->box[YY][YY]/(grid->ncy*GPU_NSUBCELL_Y),
1024 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z),
1025 ba[XX], ba[YY], ba[ZZ],
1026 ba[XX]*grid->ncx*GPU_NSUBCELL_X/nbs->box[XX][XX],
1027 ba[YY]*grid->ncy*GPU_NSUBCELL_Y/nbs->box[YY][YY],
1028 ba[ZZ]*grid->nc*GPU_NSUBCELL_Z/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
1031 /* Potentially sorts atoms on LJ coefficients !=0 and ==0.
1032 * Also sets interaction flags.
1034 void sort_on_lj(int na_c,
1035 int a0, int a1, const int *atinfo,
1039 int subc, s, a, n1, n2, a_lj_max, i, j;
1040 int sort1[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1041 int sort2[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1047 for (s = a0; s < a1; s += na_c)
1049 /* Make lists for this (sub-)cell on atoms with and without LJ */
1054 for (a = s; a < min(s+na_c, a1); a++)
1056 haveQ = haveQ || GET_CGINFO_HAS_Q(atinfo[order[a]]);
1058 if (GET_CGINFO_HAS_VDW(atinfo[order[a]]))
1060 sort1[n1++] = order[a];
1065 sort2[n2++] = order[a];
1069 /* If we don't have atom with LJ, there's nothing to sort */
1072 *flags |= NBNXN_CI_DO_LJ(subc);
1076 /* Only sort when strictly necessary. Ordering particles
1077 * Ordering particles can lead to less accurate summation
1078 * due to rounding, both for LJ and Coulomb interactions.
1080 if (2*(a_lj_max - s) >= na_c)
1082 for (i = 0; i < n1; i++)
1084 order[a0+i] = sort1[i];
1086 for (j = 0; j < n2; j++)
1088 order[a0+n1+j] = sort2[j];
1092 *flags |= NBNXN_CI_HALF_LJ(subc);
1097 *flags |= NBNXN_CI_DO_COUL(subc);
1103 /* Fill a pair search cell with atoms.
1104 * Potentially sorts atoms and sets the interaction flags.
1106 void fill_cell(const nbnxn_search_t nbs,
1108 nbnxn_atomdata_t *nbat,
1112 int sx, int sy, int sz,
1123 sort_on_lj(grid->na_c, a0, a1, atinfo, nbs->a,
1124 grid->flags+(a0>>grid->na_c_2log)-grid->cell0);
1127 /* Now we have sorted the atoms, set the cell indices */
1128 for (a = a0; a < a1; a++)
1130 nbs->cell[nbs->a[a]] = a;
1133 copy_rvec_to_nbat_real(nbs->a+a0, a1-a0, grid->na_c, x,
1134 nbat->XFormat, nbat->x, a0,
1137 if (nbat->XFormat == nbatX4)
1139 /* Store the bounding boxes as xyz.xyz. */
1140 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1141 bb_ptr = grid->bb + offset;
1143 #if defined GMX_DOUBLE && defined NBNXN_SEARCH_BB_SSE
1144 if (2*grid->na_cj == grid->na_c)
1146 calc_bounding_box_x_x4_halves(na, nbat->x+X4_IND_A(a0), bb_ptr,
1147 grid->bbj+offset*2);
1152 calc_bounding_box_x_x4(na, nbat->x+X4_IND_A(a0), bb_ptr);
1155 else if (nbat->XFormat == nbatX8)
1157 /* Store the bounding boxes as xyz.xyz. */
1158 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1159 bb_ptr = grid->bb + offset;
1161 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(a0), bb_ptr);
1164 else if (!grid->bSimple)
1166 /* Store the bounding boxes in a format convenient
1167 * for SSE calculations: xxxxyyyyzzzz...
1171 ((a0-grid->cell0*grid->na_sc)>>(grid->na_c_2log+STRIDE_PBB_2LOG))*NNBSBB_XXXX +
1172 (((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log) & (STRIDE_PBB-1));
1174 #ifdef NBNXN_SEARCH_SSE_SINGLE
1175 if (nbat->XFormat == nbatXYZQ)
1177 calc_bounding_box_xxxx_sse(na, nbat->x+a0*nbat->xstride,
1183 calc_bounding_box_xxxx(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1188 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1190 bb_ptr[0*STRIDE_PBB], bb_ptr[3*STRIDE_PBB],
1191 bb_ptr[1*STRIDE_PBB], bb_ptr[4*STRIDE_PBB],
1192 bb_ptr[2*STRIDE_PBB], bb_ptr[5*STRIDE_PBB]);
1198 /* Store the bounding boxes as xyz.xyz. */
1199 bb_ptr = grid->bb+((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1201 calc_bounding_box(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1207 bbo = (a0 - grid->cell0*grid->na_sc)/grid->na_c;
1208 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1210 (grid->bb+bbo*NNBSBB_B)[BBL_X],
1211 (grid->bb+bbo*NNBSBB_B)[BBU_X],
1212 (grid->bb+bbo*NNBSBB_B)[BBL_Y],
1213 (grid->bb+bbo*NNBSBB_B)[BBU_Y],
1214 (grid->bb+bbo*NNBSBB_B)[BBL_Z],
1215 (grid->bb+bbo*NNBSBB_B)[BBU_Z]);
1220 /* Spatially sort the atoms within one grid column */
1221 static void sort_columns_simple(const nbnxn_search_t nbs,
1227 nbnxn_atomdata_t *nbat,
1228 int cxy_start, int cxy_end,
1232 int cx, cy, cz, ncz, cfilled, c;
1233 int na, ash, ind, a;
1238 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1239 grid->cell0, cxy_start, cxy_end, a0, a1);
1242 /* Sort the atoms within each x,y column in 3 dimensions */
1243 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1246 cy = cxy - cx*grid->ncy;
1248 na = grid->cxy_na[cxy];
1249 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1250 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1252 /* Sort the atoms within each x,y column on z coordinate */
1253 sort_atoms(ZZ, FALSE,
1256 1.0/nbs->box[ZZ][ZZ], ncz*grid->na_sc,
1259 /* Fill the ncz cells in this column */
1260 cfilled = grid->cxy_ind[cxy];
1261 for (cz = 0; cz < ncz; cz++)
1263 c = grid->cxy_ind[cxy] + cz;
1265 ash_c = ash + cz*grid->na_sc;
1266 na_c = min(grid->na_sc, na-(ash_c-ash));
1268 fill_cell(nbs, grid, nbat,
1269 ash_c, ash_c+na_c, atinfo, x,
1270 grid->na_sc*cx + (dd_zone >> 2),
1271 grid->na_sc*cy + (dd_zone & 3),
1275 /* This copy to bbcz is not really necessary.
1276 * But it allows to use the same grid search code
1277 * for the simple and supersub cell setups.
1283 grid->bbcz[c*NNBSBB_D ] = grid->bb[cfilled*NNBSBB_B+2];
1284 grid->bbcz[c*NNBSBB_D+1] = grid->bb[cfilled*NNBSBB_B+6];
1287 /* Set the unused atom indices to -1 */
1288 for (ind = na; ind < ncz*grid->na_sc; ind++)
1290 nbs->a[ash+ind] = -1;
1295 /* Spatially sort the atoms within one grid column */
1296 static void sort_columns_supersub(const nbnxn_search_t nbs,
1302 nbnxn_atomdata_t *nbat,
1303 int cxy_start, int cxy_end,
1307 int cx, cy, cz = -1, c = -1, ncz;
1308 int na, ash, na_c, ind, a;
1309 int subdiv_z, sub_z, na_z, ash_z;
1310 int subdiv_y, sub_y, na_y, ash_y;
1311 int subdiv_x, sub_x, na_x, ash_x;
1313 /* cppcheck-suppress unassignedVariable */
1314 float bb_work_array[NNBSBB_B+3], *bb_work_align;
1316 bb_work_align = (float *)(((size_t)(bb_work_array+3)) & (~((size_t)15)));
1320 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1321 grid->cell0, cxy_start, cxy_end, a0, a1);
1324 subdiv_x = grid->na_c;
1325 subdiv_y = GPU_NSUBCELL_X*subdiv_x;
1326 subdiv_z = GPU_NSUBCELL_Y*subdiv_y;
1328 /* Sort the atoms within each x,y column in 3 dimensions */
1329 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1332 cy = cxy - cx*grid->ncy;
1334 na = grid->cxy_na[cxy];
1335 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1336 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1338 /* Sort the atoms within each x,y column on z coordinate */
1339 sort_atoms(ZZ, FALSE,
1342 1.0/nbs->box[ZZ][ZZ], ncz*grid->na_sc,
1345 /* This loop goes over the supercells and subcells along z at once */
1346 for (sub_z = 0; sub_z < ncz*GPU_NSUBCELL_Z; sub_z++)
1348 ash_z = ash + sub_z*subdiv_z;
1349 na_z = min(subdiv_z, na-(ash_z-ash));
1351 /* We have already sorted on z */
1353 if (sub_z % GPU_NSUBCELL_Z == 0)
1355 cz = sub_z/GPU_NSUBCELL_Z;
1356 c = grid->cxy_ind[cxy] + cz;
1358 /* The number of atoms in this supercell */
1359 na_c = min(grid->na_sc, na-(ash_z-ash));
1361 grid->nsubc[c] = min(GPU_NSUBCELL, (na_c+grid->na_c-1)/grid->na_c);
1363 /* Store the z-boundaries of the super cell */
1364 grid->bbcz[c*NNBSBB_D ] = x[nbs->a[ash_z]][ZZ];
1365 grid->bbcz[c*NNBSBB_D+1] = x[nbs->a[ash_z+na_c-1]][ZZ];
1368 #if GPU_NSUBCELL_Y > 1
1369 /* Sort the atoms along y */
1370 sort_atoms(YY, (sub_z & 1),
1371 nbs->a+ash_z, na_z, x,
1372 grid->c0[YY]+cy*grid->sy,
1373 grid->inv_sy, subdiv_z,
1377 for (sub_y = 0; sub_y < GPU_NSUBCELL_Y; sub_y++)
1379 ash_y = ash_z + sub_y*subdiv_y;
1380 na_y = min(subdiv_y, na-(ash_y-ash));
1382 #if GPU_NSUBCELL_X > 1
1383 /* Sort the atoms along x */
1384 sort_atoms(XX, ((cz*GPU_NSUBCELL_Y + sub_y) & 1),
1385 nbs->a+ash_y, na_y, x,
1386 grid->c0[XX]+cx*grid->sx,
1387 grid->inv_sx, subdiv_y,
1391 for (sub_x = 0; sub_x < GPU_NSUBCELL_X; sub_x++)
1393 ash_x = ash_y + sub_x*subdiv_x;
1394 na_x = min(subdiv_x, na-(ash_x-ash));
1396 fill_cell(nbs, grid, nbat,
1397 ash_x, ash_x+na_x, atinfo, x,
1398 grid->na_c*(cx*GPU_NSUBCELL_X+sub_x) + (dd_zone >> 2),
1399 grid->na_c*(cy*GPU_NSUBCELL_Y+sub_y) + (dd_zone & 3),
1406 /* Set the unused atom indices to -1 */
1407 for (ind = na; ind < ncz*grid->na_sc; ind++)
1409 nbs->a[ash+ind] = -1;
1414 /* Determine in which grid column atoms should go */
1415 static void calc_column_indices(nbnxn_grid_t *grid,
1418 int dd_zone, const int *move,
1419 int thread, int nthread,
1426 /* We add one extra cell for particles which moved during DD */
1427 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1432 n0 = a0 + (int)((thread+0)*(a1 - a0))/nthread;
1433 n1 = a0 + (int)((thread+1)*(a1 - a0))/nthread;
1437 for (i = n0; i < n1; i++)
1439 if (move == NULL || move[i] >= 0)
1441 /* We need to be careful with rounding,
1442 * particles might be a few bits outside the local zone.
1443 * The int cast takes care of the lower bound,
1444 * we will explicitly take care of the upper bound.
1446 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1447 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1450 if (cx < 0 || cx > grid->ncx ||
1451 cy < 0 || cy > grid->ncy)
1454 "grid cell cx %d cy %d out of range (max %d %d)\n"
1455 "atom %f %f %f, grid->c0 %f %f",
1456 cx, cy, grid->ncx, grid->ncy,
1457 x[i][XX], x[i][YY], x[i][ZZ], grid->c0[XX], grid->c0[YY]);
1460 /* Take care of potential rouding issues */
1461 cx = min(cx, grid->ncx - 1);
1462 cy = min(cy, grid->ncy - 1);
1464 /* For the moment cell will contain only the, grid local,
1465 * x and y indices, not z.
1467 cell[i] = cx*grid->ncy + cy;
1471 /* Put this moved particle after the end of the grid,
1472 * so we can process it later without using conditionals.
1474 cell[i] = grid->ncx*grid->ncy;
1483 for (i = n0; i < n1; i++)
1485 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1486 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1488 /* For non-home zones there could be particles outside
1489 * the non-bonded cut-off range, which have been communicated
1490 * for bonded interactions only. For the result it doesn't
1491 * matter where these end up on the grid. For performance
1492 * we put them in an extra row at the border.
1495 cx = min(cx, grid->ncx - 1);
1497 cy = min(cy, grid->ncy - 1);
1499 /* For the moment cell will contain only the, grid local,
1500 * x and y indices, not z.
1502 cell[i] = cx*grid->ncy + cy;
1509 /* Determine in which grid cells the atoms should go */
1510 static void calc_cell_indices(const nbnxn_search_t nbs,
1517 nbnxn_atomdata_t *nbat)
1520 int cx, cy, cxy, ncz_max, ncz;
1521 int nthread, thread;
1522 int *cxy_na, cxy_na_i;
1524 nthread = gmx_omp_nthreads_get(emntPairsearch);
1526 #pragma omp parallel for num_threads(nthread) schedule(static)
1527 for (thread = 0; thread < nthread; thread++)
1529 calc_column_indices(grid, a0, a1, x, dd_zone, move, thread, nthread,
1530 nbs->cell, nbs->work[thread].cxy_na);
1533 /* Make the cell index as a function of x and y */
1536 grid->cxy_ind[0] = 0;
1537 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1539 /* We set ncz_max at the beginning of the loop iso at the end
1540 * to skip i=grid->ncx*grid->ncy which are moved particles
1541 * that do not need to be ordered on the grid.
1547 cxy_na_i = nbs->work[0].cxy_na[i];
1548 for (thread = 1; thread < nthread; thread++)
1550 cxy_na_i += nbs->work[thread].cxy_na[i];
1552 ncz = (cxy_na_i + grid->na_sc - 1)/grid->na_sc;
1553 if (nbat->XFormat == nbatX8)
1555 /* Make the number of cell a multiple of 2 */
1556 ncz = (ncz + 1) & ~1;
1558 grid->cxy_ind[i+1] = grid->cxy_ind[i] + ncz;
1559 /* Clear cxy_na, so we can reuse the array below */
1560 grid->cxy_na[i] = 0;
1562 grid->nc = grid->cxy_ind[grid->ncx*grid->ncy] - grid->cxy_ind[0];
1564 nbat->natoms = (grid->cell0 + grid->nc)*grid->na_sc;
1568 fprintf(debug, "ns na_sc %d na_c %d super-cells: %d x %d y %d z %.1f maxz %d\n",
1569 grid->na_sc, grid->na_c, grid->nc,
1570 grid->ncx, grid->ncy, grid->nc/((double)(grid->ncx*grid->ncy)),
1575 for (cy = 0; cy < grid->ncy; cy++)
1577 for (cx = 0; cx < grid->ncx; cx++)
1579 fprintf(debug, " %2d", grid->cxy_ind[i+1]-grid->cxy_ind[i]);
1582 fprintf(debug, "\n");
1587 /* Make sure the work array for sorting is large enough */
1588 if (ncz_max*grid->na_sc*SGSF > nbs->work[0].sort_work_nalloc)
1590 for (thread = 0; thread < nbs->nthread_max; thread++)
1592 nbs->work[thread].sort_work_nalloc =
1593 over_alloc_large(ncz_max*grid->na_sc*SGSF);
1594 srenew(nbs->work[thread].sort_work,
1595 nbs->work[thread].sort_work_nalloc);
1596 /* When not in use, all elements should be -1 */
1597 for (i = 0; i < nbs->work[thread].sort_work_nalloc; i++)
1599 nbs->work[thread].sort_work[i] = -1;
1604 /* Now we know the dimensions we can fill the grid.
1605 * This is the first, unsorted fill. We sort the columns after this.
1607 for (i = a0; i < a1; i++)
1609 /* At this point nbs->cell contains the local grid x,y indices */
1611 nbs->a[(grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc + grid->cxy_na[cxy]++] = i;
1616 /* Set the cell indices for the moved particles */
1617 n0 = grid->nc*grid->na_sc;
1618 n1 = grid->nc*grid->na_sc+grid->cxy_na[grid->ncx*grid->ncy];
1621 for (i = n0; i < n1; i++)
1623 nbs->cell[nbs->a[i]] = i;
1628 /* Sort the super-cell columns along z into the sub-cells. */
1629 #pragma omp parallel for num_threads(nbs->nthread_max) schedule(static)
1630 for (thread = 0; thread < nbs->nthread_max; thread++)
1634 sort_columns_simple(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1635 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1636 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1637 nbs->work[thread].sort_work);
1641 sort_columns_supersub(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1642 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1643 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1644 nbs->work[thread].sort_work);
1648 #ifdef NBNXN_SEARCH_BB_SSE
1649 if (grid->bSimple && nbat->XFormat == nbatX8)
1651 combine_bounding_box_pairs(grid, grid->bb);
1657 grid->nsubc_tot = 0;
1658 for (i = 0; i < grid->nc; i++)
1660 grid->nsubc_tot += grid->nsubc[i];
1668 print_bbsizes_simple(debug, nbs, grid);
1672 fprintf(debug, "ns non-zero sub-cells: %d average atoms %.2f\n",
1673 grid->nsubc_tot, (a1-a0)/(double)grid->nsubc_tot);
1675 print_bbsizes_supersub(debug, nbs, grid);
1680 static void init_buffer_flags(nbnxn_buffer_flags_t *flags,
1685 flags->nflag = (natoms + NBNXN_BUFFERFLAG_SIZE - 1)/NBNXN_BUFFERFLAG_SIZE;
1686 if (flags->nflag > flags->flag_nalloc)
1688 flags->flag_nalloc = over_alloc_large(flags->nflag);
1689 srenew(flags->flag, flags->flag_nalloc);
1691 for (b = 0; b < flags->nflag; b++)
1697 /* Sets up a grid and puts the atoms on the grid.
1698 * This function only operates on one domain of the domain decompostion.
1699 * Note that without domain decomposition there is only one domain.
1701 void nbnxn_put_on_grid(nbnxn_search_t nbs,
1702 int ePBC, matrix box,
1704 rvec corner0, rvec corner1,
1709 int nmoved, int *move,
1711 nbnxn_atomdata_t *nbat)
1715 int nc_max_grid, nc_max;
1717 grid = &nbs->grid[dd_zone];
1719 nbs_cycle_start(&nbs->cc[enbsCCgrid]);
1721 grid->bSimple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
1723 grid->na_c = nbnxn_kernel_to_ci_size(nb_kernel_type);
1724 grid->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
1725 grid->na_sc = (grid->bSimple ? 1 : GPU_NSUBCELL)*grid->na_c;
1726 grid->na_c_2log = get_2log(grid->na_c);
1728 nbat->na_c = grid->na_c;
1737 (nbs->grid[dd_zone-1].cell0 + nbs->grid[dd_zone-1].nc)*
1738 nbs->grid[dd_zone-1].na_sc/grid->na_sc;
1746 copy_mat(box, nbs->box);
1748 if (atom_density >= 0)
1750 grid->atom_density = atom_density;
1754 grid->atom_density = grid_atom_density(n-nmoved, corner0, corner1);
1759 nbs->natoms_local = a1 - nmoved;
1760 /* We assume that nbnxn_put_on_grid is called first
1761 * for the local atoms (dd_zone=0).
1763 nbs->natoms_nonlocal = a1 - nmoved;
1767 nbs->natoms_nonlocal = max(nbs->natoms_nonlocal, a1);
1770 nc_max_grid = set_grid_size_xy(nbs, grid,
1771 dd_zone, n-nmoved, corner0, corner1,
1772 nbs->grid[0].atom_density);
1774 nc_max = grid->cell0 + nc_max_grid;
1776 if (a1 > nbs->cell_nalloc)
1778 nbs->cell_nalloc = over_alloc_large(a1);
1779 srenew(nbs->cell, nbs->cell_nalloc);
1782 /* To avoid conditionals we store the moved particles at the end of a,
1783 * make sure we have enough space.
1785 if (nc_max*grid->na_sc + nmoved > nbs->a_nalloc)
1787 nbs->a_nalloc = over_alloc_large(nc_max*grid->na_sc + nmoved);
1788 srenew(nbs->a, nbs->a_nalloc);
1791 /* We need padding up to a multiple of the buffer flag size: simply add */
1792 if (nc_max*grid->na_sc + NBNXN_BUFFERFLAG_SIZE > nbat->nalloc)
1794 nbnxn_atomdata_realloc(nbat, nc_max*grid->na_sc+NBNXN_BUFFERFLAG_SIZE);
1797 calc_cell_indices(nbs, dd_zone, grid, a0, a1, atinfo, x, move, nbat);
1801 nbat->natoms_local = nbat->natoms;
1804 nbs_cycle_stop(&nbs->cc[enbsCCgrid]);
1807 /* Calls nbnxn_put_on_grid for all non-local domains */
1808 void nbnxn_put_on_grid_nonlocal(nbnxn_search_t nbs,
1809 const gmx_domdec_zones_t *zones,
1813 nbnxn_atomdata_t *nbat)
1818 for (zone = 1; zone < zones->n; zone++)
1820 for (d = 0; d < DIM; d++)
1822 c0[d] = zones->size[zone].bb_x0[d];
1823 c1[d] = zones->size[zone].bb_x1[d];
1826 nbnxn_put_on_grid(nbs, nbs->ePBC, NULL,
1828 zones->cg_range[zone],
1829 zones->cg_range[zone+1],
1839 /* Add simple grid type information to the local super/sub grid */
1840 void nbnxn_grid_add_simple(nbnxn_search_t nbs,
1841 nbnxn_atomdata_t *nbat)
1847 grid = &nbs->grid[0];
1851 gmx_incons("nbnxn_grid_simple called with a simple grid");
1854 ncd = grid->na_sc/NBNXN_CPU_CLUSTER_I_SIZE;
1856 if (grid->nc*ncd > grid->nc_nalloc_simple)
1858 grid->nc_nalloc_simple = over_alloc_large(grid->nc*ncd);
1859 srenew(grid->bbcz_simple, grid->nc_nalloc_simple*NNBSBB_D);
1860 srenew(grid->bb_simple, grid->nc_nalloc_simple*NNBSBB_B);
1861 srenew(grid->flags_simple, grid->nc_nalloc_simple);
1864 sfree_aligned(grid->bbj);
1865 snew_aligned(grid->bbj, grid->nc_nalloc_simple/2, 16);
1869 bbcz = grid->bbcz_simple;
1870 bb = grid->bb_simple;
1872 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
1873 for (sc = 0; sc < grid->nc; sc++)
1877 for (c = 0; c < ncd; c++)
1881 na = NBNXN_CPU_CLUSTER_I_SIZE;
1883 nbat->type[tx*NBNXN_CPU_CLUSTER_I_SIZE+na-1] == nbat->ntype-1)
1890 switch (nbat->XFormat)
1893 /* PACK_X4==NBNXN_CPU_CLUSTER_I_SIZE, so this is simple */
1894 calc_bounding_box_x_x4(na, nbat->x+tx*STRIDE_P4,
1898 /* PACK_X8>NBNXN_CPU_CLUSTER_I_SIZE, more complicated */
1899 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(tx*NBNXN_CPU_CLUSTER_I_SIZE),
1903 calc_bounding_box(na, nbat->xstride,
1904 nbat->x+tx*NBNXN_CPU_CLUSTER_I_SIZE*nbat->xstride,
1908 bbcz[tx*NNBSBB_D+0] = bb[tx*NNBSBB_B +ZZ];
1909 bbcz[tx*NNBSBB_D+1] = bb[tx*NNBSBB_B+NNBSBB_C+ZZ];
1911 /* No interaction optimization yet here */
1912 grid->flags_simple[tx] = NBNXN_CI_DO_LJ(0) | NBNXN_CI_DO_COUL(0);
1916 grid->flags_simple[tx] = 0;
1921 #ifdef NBNXN_SEARCH_BB_SSE
1922 if (grid->bSimple && nbat->XFormat == nbatX8)
1924 combine_bounding_box_pairs(grid, grid->bb_simple);
1929 void nbnxn_get_ncells(nbnxn_search_t nbs, int *ncx, int *ncy)
1931 *ncx = nbs->grid[0].ncx;
1932 *ncy = nbs->grid[0].ncy;
1935 void nbnxn_get_atomorder(nbnxn_search_t nbs, int **a, int *n)
1937 const nbnxn_grid_t *grid;
1939 grid = &nbs->grid[0];
1941 /* Return the atom order for the home cell (index 0) */
1944 *n = grid->cxy_ind[grid->ncx*grid->ncy]*grid->na_sc;
1947 void nbnxn_set_atomorder(nbnxn_search_t nbs)
1950 int ao, cx, cy, cxy, cz, j;
1952 /* Set the atom order for the home cell (index 0) */
1953 grid = &nbs->grid[0];
1956 for (cx = 0; cx < grid->ncx; cx++)
1958 for (cy = 0; cy < grid->ncy; cy++)
1960 cxy = cx*grid->ncy + cy;
1961 j = grid->cxy_ind[cxy]*grid->na_sc;
1962 for (cz = 0; cz < grid->cxy_na[cxy]; cz++)
1973 /* Determines the cell range along one dimension that
1974 * the bounding box b0 - b1 sees.
1976 static void get_cell_range(real b0, real b1,
1977 int nc, real c0, real s, real invs,
1978 real d2, real r2, int *cf, int *cl)
1980 *cf = max((int)((b0 - c0)*invs), 0);
1982 while (*cf > 0 && d2 + sqr((b0 - c0) - (*cf-1+1)*s) < r2)
1987 *cl = min((int)((b1 - c0)*invs), nc-1);
1988 while (*cl < nc-1 && d2 + sqr((*cl+1)*s - (b1 - c0)) < r2)
1994 /* Reference code calculating the distance^2 between two bounding boxes */
1995 static float box_dist2(float bx0, float bx1, float by0,
1996 float by1, float bz0, float bz1,
2000 float dl, dh, dm, dm0;
2004 dl = bx0 - bb[BBU_X];
2005 dh = bb[BBL_X] - bx1;
2010 dl = by0 - bb[BBU_Y];
2011 dh = bb[BBL_Y] - by1;
2016 dl = bz0 - bb[BBU_Z];
2017 dh = bb[BBL_Z] - bz1;
2025 /* Plain C code calculating the distance^2 between two bounding boxes */
2026 static float subc_bb_dist2(int si, const float *bb_i_ci,
2027 int csj, const float *bb_j_all)
2029 const float *bb_i, *bb_j;
2031 float dl, dh, dm, dm0;
2033 bb_i = bb_i_ci + si*NNBSBB_B;
2034 bb_j = bb_j_all + csj*NNBSBB_B;
2038 dl = bb_i[BBL_X] - bb_j[BBU_X];
2039 dh = bb_j[BBL_X] - bb_i[BBU_X];
2044 dl = bb_i[BBL_Y] - bb_j[BBU_Y];
2045 dh = bb_j[BBL_Y] - bb_i[BBU_Y];
2050 dl = bb_i[BBL_Z] - bb_j[BBU_Z];
2051 dh = bb_j[BBL_Z] - bb_i[BBU_Z];
2059 #ifdef NBNXN_SEARCH_BB_SSE
2061 /* SSE code for bb distance for bb format xyz0 */
2062 static float subc_bb_dist2_sse(int si, const float *bb_i_ci,
2063 int csj, const float *bb_j_all)
2065 const float *bb_i, *bb_j;
2067 __m128 bb_i_SSE0, bb_i_SSE1;
2068 __m128 bb_j_SSE0, bb_j_SSE1;
2074 #ifndef GMX_X86_SSE4_1
2075 float d2_array[7], *d2_align;
2077 d2_align = (float *)(((size_t)(d2_array+3)) & (~((size_t)15)));
2082 bb_i = bb_i_ci + si*NNBSBB_B;
2083 bb_j = bb_j_all + csj*NNBSBB_B;
2085 bb_i_SSE0 = _mm_load_ps(bb_i);
2086 bb_i_SSE1 = _mm_load_ps(bb_i+NNBSBB_C);
2087 bb_j_SSE0 = _mm_load_ps(bb_j);
2088 bb_j_SSE1 = _mm_load_ps(bb_j+NNBSBB_C);
2090 dl_SSE = _mm_sub_ps(bb_i_SSE0, bb_j_SSE1);
2091 dh_SSE = _mm_sub_ps(bb_j_SSE0, bb_i_SSE1);
2093 dm_SSE = _mm_max_ps(dl_SSE, dh_SSE);
2094 dm0_SSE = _mm_max_ps(dm_SSE, _mm_setzero_ps());
2095 #ifndef GMX_X86_SSE4_1
2096 d2_SSE = _mm_mul_ps(dm0_SSE, dm0_SSE);
2098 _mm_store_ps(d2_align, d2_SSE);
2100 return d2_align[0] + d2_align[1] + d2_align[2];
2102 /* SSE4.1 dot product of components 0,1,2 */
2103 d2_SSE = _mm_dp_ps(dm0_SSE, dm0_SSE, 0x71);
2105 _mm_store_ss(&d2, d2_SSE);
2111 /* Calculate bb bounding distances of bb_i[si,...,si+3] and store them in d2 */
2112 #define SUBC_BB_DIST2_SSE_XXXX_INNER(si, bb_i, d2) \
2116 __m128 dx_0, dy_0, dz_0; \
2117 __m128 dx_1, dy_1, dz_1; \
2119 __m128 mx, my, mz; \
2120 __m128 m0x, m0y, m0z; \
2122 __m128 d2x, d2y, d2z; \
2125 shi = si*NNBSBB_D*DIM; \
2127 xi_l = _mm_load_ps(bb_i+shi+0*STRIDE_PBB); \
2128 yi_l = _mm_load_ps(bb_i+shi+1*STRIDE_PBB); \
2129 zi_l = _mm_load_ps(bb_i+shi+2*STRIDE_PBB); \
2130 xi_h = _mm_load_ps(bb_i+shi+3*STRIDE_PBB); \
2131 yi_h = _mm_load_ps(bb_i+shi+4*STRIDE_PBB); \
2132 zi_h = _mm_load_ps(bb_i+shi+5*STRIDE_PBB); \
2134 dx_0 = _mm_sub_ps(xi_l, xj_h); \
2135 dy_0 = _mm_sub_ps(yi_l, yj_h); \
2136 dz_0 = _mm_sub_ps(zi_l, zj_h); \
2138 dx_1 = _mm_sub_ps(xj_l, xi_h); \
2139 dy_1 = _mm_sub_ps(yj_l, yi_h); \
2140 dz_1 = _mm_sub_ps(zj_l, zi_h); \
2142 mx = _mm_max_ps(dx_0, dx_1); \
2143 my = _mm_max_ps(dy_0, dy_1); \
2144 mz = _mm_max_ps(dz_0, dz_1); \
2146 m0x = _mm_max_ps(mx, zero); \
2147 m0y = _mm_max_ps(my, zero); \
2148 m0z = _mm_max_ps(mz, zero); \
2150 d2x = _mm_mul_ps(m0x, m0x); \
2151 d2y = _mm_mul_ps(m0y, m0y); \
2152 d2z = _mm_mul_ps(m0z, m0z); \
2154 d2s = _mm_add_ps(d2x, d2y); \
2155 d2t = _mm_add_ps(d2s, d2z); \
2157 _mm_store_ps(d2+si, d2t); \
2160 /* SSE code for nsi bb distances for bb format xxxxyyyyzzzz */
2161 static void subc_bb_dist2_sse_xxxx(const float *bb_j,
2162 int nsi, const float *bb_i,
2165 __m128 xj_l, yj_l, zj_l;
2166 __m128 xj_h, yj_h, zj_h;
2167 __m128 xi_l, yi_l, zi_l;
2168 __m128 xi_h, yi_h, zi_h;
2172 zero = _mm_setzero_ps();
2174 xj_l = _mm_set1_ps(bb_j[0*STRIDE_PBB]);
2175 yj_l = _mm_set1_ps(bb_j[1*STRIDE_PBB]);
2176 zj_l = _mm_set1_ps(bb_j[2*STRIDE_PBB]);
2177 xj_h = _mm_set1_ps(bb_j[3*STRIDE_PBB]);
2178 yj_h = _mm_set1_ps(bb_j[4*STRIDE_PBB]);
2179 zj_h = _mm_set1_ps(bb_j[5*STRIDE_PBB]);
2181 /* Here we "loop" over si (0,STRIDE_PBB) from 0 to nsi with step STRIDE_PBB.
2182 * But as we know the number of iterations is 1 or 2, we unroll manually.
2184 SUBC_BB_DIST2_SSE_XXXX_INNER(0, bb_i, d2);
2185 if (STRIDE_PBB < nsi)
2187 SUBC_BB_DIST2_SSE_XXXX_INNER(STRIDE_PBB, bb_i, d2);
2191 #endif /* NBNXN_SEARCH_BB_SSE */
2193 /* Plain C function which determines if any atom pair between two cells
2194 * is within distance sqrt(rl2).
2196 static gmx_bool subc_in_range_x(int na_c,
2197 int si, const real *x_i,
2198 int csj, int stride, const real *x_j,
2204 for (i = 0; i < na_c; i++)
2206 i0 = (si*na_c + i)*DIM;
2207 for (j = 0; j < na_c; j++)
2209 j0 = (csj*na_c + j)*stride;
2211 d2 = sqr(x_i[i0 ] - x_j[j0 ]) +
2212 sqr(x_i[i0+1] - x_j[j0+1]) +
2213 sqr(x_i[i0+2] - x_j[j0+2]);
2225 /* SSE function which determines if any atom pair between two cells,
2226 * both with 8 atoms, is within distance sqrt(rl2).
2228 static gmx_bool subc_in_range_sse8(int na_c,
2229 int si, const real *x_i,
2230 int csj, int stride, const real *x_j,
2233 #ifdef NBNXN_SEARCH_SSE_SINGLE
2234 __m128 ix_SSE0, iy_SSE0, iz_SSE0;
2235 __m128 ix_SSE1, iy_SSE1, iz_SSE1;
2242 rc2_SSE = _mm_set1_ps(rl2);
2244 na_c_sse = NBNXN_GPU_CLUSTER_SIZE/STRIDE_PBB;
2245 ix_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+0)*STRIDE_PBB);
2246 iy_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+1)*STRIDE_PBB);
2247 iz_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+2)*STRIDE_PBB);
2248 ix_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+3)*STRIDE_PBB);
2249 iy_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+4)*STRIDE_PBB);
2250 iz_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+5)*STRIDE_PBB);
2252 /* We loop from the outer to the inner particles to maximize
2253 * the chance that we find a pair in range quickly and return.
2259 __m128 jx0_SSE, jy0_SSE, jz0_SSE;
2260 __m128 jx1_SSE, jy1_SSE, jz1_SSE;
2262 __m128 dx_SSE0, dy_SSE0, dz_SSE0;
2263 __m128 dx_SSE1, dy_SSE1, dz_SSE1;
2264 __m128 dx_SSE2, dy_SSE2, dz_SSE2;
2265 __m128 dx_SSE3, dy_SSE3, dz_SSE3;
2276 __m128 wco_any_SSE01, wco_any_SSE23, wco_any_SSE;
2278 jx0_SSE = _mm_load1_ps(x_j+j0*stride+0);
2279 jy0_SSE = _mm_load1_ps(x_j+j0*stride+1);
2280 jz0_SSE = _mm_load1_ps(x_j+j0*stride+2);
2282 jx1_SSE = _mm_load1_ps(x_j+j1*stride+0);
2283 jy1_SSE = _mm_load1_ps(x_j+j1*stride+1);
2284 jz1_SSE = _mm_load1_ps(x_j+j1*stride+2);
2286 /* Calculate distance */
2287 dx_SSE0 = _mm_sub_ps(ix_SSE0, jx0_SSE);
2288 dy_SSE0 = _mm_sub_ps(iy_SSE0, jy0_SSE);
2289 dz_SSE0 = _mm_sub_ps(iz_SSE0, jz0_SSE);
2290 dx_SSE1 = _mm_sub_ps(ix_SSE1, jx0_SSE);
2291 dy_SSE1 = _mm_sub_ps(iy_SSE1, jy0_SSE);
2292 dz_SSE1 = _mm_sub_ps(iz_SSE1, jz0_SSE);
2293 dx_SSE2 = _mm_sub_ps(ix_SSE0, jx1_SSE);
2294 dy_SSE2 = _mm_sub_ps(iy_SSE0, jy1_SSE);
2295 dz_SSE2 = _mm_sub_ps(iz_SSE0, jz1_SSE);
2296 dx_SSE3 = _mm_sub_ps(ix_SSE1, jx1_SSE);
2297 dy_SSE3 = _mm_sub_ps(iy_SSE1, jy1_SSE);
2298 dz_SSE3 = _mm_sub_ps(iz_SSE1, jz1_SSE);
2300 /* rsq = dx*dx+dy*dy+dz*dz */
2301 rsq_SSE0 = gmx_mm_calc_rsq_ps(dx_SSE0, dy_SSE0, dz_SSE0);
2302 rsq_SSE1 = gmx_mm_calc_rsq_ps(dx_SSE1, dy_SSE1, dz_SSE1);
2303 rsq_SSE2 = gmx_mm_calc_rsq_ps(dx_SSE2, dy_SSE2, dz_SSE2);
2304 rsq_SSE3 = gmx_mm_calc_rsq_ps(dx_SSE3, dy_SSE3, dz_SSE3);
2306 wco_SSE0 = _mm_cmplt_ps(rsq_SSE0, rc2_SSE);
2307 wco_SSE1 = _mm_cmplt_ps(rsq_SSE1, rc2_SSE);
2308 wco_SSE2 = _mm_cmplt_ps(rsq_SSE2, rc2_SSE);
2309 wco_SSE3 = _mm_cmplt_ps(rsq_SSE3, rc2_SSE);
2311 wco_any_SSE01 = _mm_or_ps(wco_SSE0, wco_SSE1);
2312 wco_any_SSE23 = _mm_or_ps(wco_SSE2, wco_SSE3);
2313 wco_any_SSE = _mm_or_ps(wco_any_SSE01, wco_any_SSE23);
2315 if (_mm_movemask_ps(wco_any_SSE))
2327 gmx_incons("SSE function called without SSE support");
2333 /* Returns the j sub-cell for index cj_ind */
2334 static int nbl_cj(const nbnxn_pairlist_t *nbl, int cj_ind)
2336 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].cj[cj_ind & (NBNXN_GPU_JGROUP_SIZE - 1)];
2339 /* Returns the i-interaction mask of the j sub-cell for index cj_ind */
2340 static unsigned nbl_imask0(const nbnxn_pairlist_t *nbl, int cj_ind)
2342 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].imei[0].imask;
2345 /* Ensures there is enough space for extra extra exclusion masks */
2346 static void check_excl_space(nbnxn_pairlist_t *nbl, int extra)
2348 if (nbl->nexcl+extra > nbl->excl_nalloc)
2350 nbl->excl_nalloc = over_alloc_small(nbl->nexcl+extra);
2351 nbnxn_realloc_void((void **)&nbl->excl,
2352 nbl->nexcl*sizeof(*nbl->excl),
2353 nbl->excl_nalloc*sizeof(*nbl->excl),
2354 nbl->alloc, nbl->free);
2358 /* Ensures there is enough space for ncell extra j-cells in the list */
2359 static void check_subcell_list_space_simple(nbnxn_pairlist_t *nbl,
2364 cj_max = nbl->ncj + ncell;
2366 if (cj_max > nbl->cj_nalloc)
2368 nbl->cj_nalloc = over_alloc_small(cj_max);
2369 nbnxn_realloc_void((void **)&nbl->cj,
2370 nbl->ncj*sizeof(*nbl->cj),
2371 nbl->cj_nalloc*sizeof(*nbl->cj),
2372 nbl->alloc, nbl->free);
2376 /* Ensures there is enough space for ncell extra j-subcells in the list */
2377 static void check_subcell_list_space_supersub(nbnxn_pairlist_t *nbl,
2380 int ncj4_max, j4, j, w, t;
2383 #define WARP_SIZE 32
2385 /* We can have maximally nsupercell*GPU_NSUBCELL sj lists */
2386 /* We can store 4 j-subcell - i-supercell pairs in one struct.
2387 * since we round down, we need one extra entry.
2389 ncj4_max = ((nbl->work->cj_ind + nsupercell*GPU_NSUBCELL + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2391 if (ncj4_max > nbl->cj4_nalloc)
2393 nbl->cj4_nalloc = over_alloc_small(ncj4_max);
2394 nbnxn_realloc_void((void **)&nbl->cj4,
2395 nbl->work->cj4_init*sizeof(*nbl->cj4),
2396 nbl->cj4_nalloc*sizeof(*nbl->cj4),
2397 nbl->alloc, nbl->free);
2400 if (ncj4_max > nbl->work->cj4_init)
2402 for (j4 = nbl->work->cj4_init; j4 < ncj4_max; j4++)
2404 /* No i-subcells and no excl's in the list initially */
2405 for (w = 0; w < NWARP; w++)
2407 nbl->cj4[j4].imei[w].imask = 0U;
2408 nbl->cj4[j4].imei[w].excl_ind = 0;
2412 nbl->work->cj4_init = ncj4_max;
2416 /* Set all excl masks for one GPU warp no exclusions */
2417 static void set_no_excls(nbnxn_excl_t *excl)
2421 for (t = 0; t < WARP_SIZE; t++)
2423 /* Turn all interaction bits on */
2424 excl->pair[t] = NBNXN_INT_MASK_ALL;
2428 /* Initializes a single nbnxn_pairlist_t data structure */
2429 static void nbnxn_init_pairlist(nbnxn_pairlist_t *nbl,
2431 nbnxn_alloc_t *alloc,
2436 nbl->alloc = nbnxn_alloc_aligned;
2444 nbl->free = nbnxn_free_aligned;
2451 nbl->bSimple = bSimple;
2462 /* We need one element extra in sj, so alloc initially with 1 */
2463 nbl->cj4_nalloc = 0;
2470 nbl->excl_nalloc = 0;
2472 check_excl_space(nbl, 1);
2474 set_no_excls(&nbl->excl[0]);
2479 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX, NBNXN_MEM_ALIGN);
2481 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL*NNBSBB_B, NBNXN_MEM_ALIGN);
2483 snew_aligned(nbl->work->x_ci, NBNXN_NA_SC_MAX*DIM, NBNXN_MEM_ALIGN);
2484 #ifdef GMX_NBNXN_SIMD
2485 snew_aligned(nbl->work->x_ci_simd_4xn, 1, NBNXN_MEM_ALIGN);
2486 snew_aligned(nbl->work->x_ci_simd_2xnn, 1, NBNXN_MEM_ALIGN);
2488 snew_aligned(nbl->work->d2, GPU_NSUBCELL, NBNXN_MEM_ALIGN);
2490 nbl->work->sort = NULL;
2491 nbl->work->sort_nalloc = 0;
2492 nbl->work->sci_sort = NULL;
2493 nbl->work->sci_sort_nalloc = 0;
2496 void nbnxn_init_pairlist_set(nbnxn_pairlist_set_t *nbl_list,
2497 gmx_bool bSimple, gmx_bool bCombined,
2498 nbnxn_alloc_t *alloc,
2503 nbl_list->bSimple = bSimple;
2504 nbl_list->bCombined = bCombined;
2506 nbl_list->nnbl = gmx_omp_nthreads_get(emntNonbonded);
2508 if (!nbl_list->bCombined &&
2509 nbl_list->nnbl > NBNXN_BUFFERFLAG_MAX_THREADS)
2511 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.",
2512 nbl_list->nnbl, NBNXN_BUFFERFLAG_MAX_THREADS, NBNXN_BUFFERFLAG_MAX_THREADS);
2515 snew(nbl_list->nbl, nbl_list->nnbl);
2516 /* Execute in order to avoid memory interleaving between threads */
2517 #pragma omp parallel for num_threads(nbl_list->nnbl) schedule(static)
2518 for (i = 0; i < nbl_list->nnbl; i++)
2520 /* Allocate the nblist data structure locally on each thread
2521 * to optimize memory access for NUMA architectures.
2523 snew(nbl_list->nbl[i], 1);
2525 /* Only list 0 is used on the GPU, use normal allocation for i>0 */
2528 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, alloc, free);
2532 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, NULL, NULL);
2537 /* Print statistics of a pair list, used for debug output */
2538 static void print_nblist_statistics_simple(FILE *fp, const nbnxn_pairlist_t *nbl,
2539 const nbnxn_search_t nbs, real rl)
2541 const nbnxn_grid_t *grid;
2546 /* This code only produces correct statistics with domain decomposition */
2547 grid = &nbs->grid[0];
2549 fprintf(fp, "nbl nci %d ncj %d\n",
2550 nbl->nci, nbl->ncj);
2551 fprintf(fp, "nbl na_sc %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2552 nbl->na_sc, rl, nbl->ncj, nbl->ncj/(double)grid->nc,
2553 nbl->ncj/(double)grid->nc*grid->na_sc,
2554 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)));
2556 fprintf(fp, "nbl average j cell list length %.1f\n",
2557 0.25*nbl->ncj/(double)nbl->nci);
2559 for (s = 0; s < SHIFTS; s++)
2564 for (i = 0; i < nbl->nci; i++)
2566 cs[nbl->ci[i].shift & NBNXN_CI_SHIFT] +=
2567 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start;
2569 j = nbl->ci[i].cj_ind_start;
2570 while (j < nbl->ci[i].cj_ind_end &&
2571 nbl->cj[j].excl != NBNXN_INT_MASK_ALL)
2577 fprintf(fp, "nbl cell pairs, total: %d excl: %d %.1f%%\n",
2578 nbl->ncj, npexcl, 100*npexcl/(double)nbl->ncj);
2579 for (s = 0; s < SHIFTS; s++)
2583 fprintf(fp, "nbl shift %2d ncj %3d\n", s, cs[s]);
2588 /* Print statistics of a pair lists, used for debug output */
2589 static void print_nblist_statistics_supersub(FILE *fp, const nbnxn_pairlist_t *nbl,
2590 const nbnxn_search_t nbs, real rl)
2592 const nbnxn_grid_t *grid;
2593 int i, j4, j, si, b;
2594 int c[GPU_NSUBCELL+1];
2596 /* This code only produces correct statistics with domain decomposition */
2597 grid = &nbs->grid[0];
2599 fprintf(fp, "nbl nsci %d ncj4 %d nsi %d excl4 %d\n",
2600 nbl->nsci, nbl->ncj4, nbl->nci_tot, nbl->nexcl);
2601 fprintf(fp, "nbl na_c %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2602 nbl->na_ci, rl, nbl->nci_tot, nbl->nci_tot/(double)grid->nsubc_tot,
2603 nbl->nci_tot/(double)grid->nsubc_tot*grid->na_c,
2604 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)));
2606 fprintf(fp, "nbl average j super cell list length %.1f\n",
2607 0.25*nbl->ncj4/(double)nbl->nsci);
2608 fprintf(fp, "nbl average i sub cell list length %.1f\n",
2609 nbl->nci_tot/((double)nbl->ncj4));
2611 for (si = 0; si <= GPU_NSUBCELL; si++)
2615 for (i = 0; i < nbl->nsci; i++)
2617 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
2619 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
2622 for (si = 0; si < GPU_NSUBCELL; si++)
2624 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
2633 for (b = 0; b <= GPU_NSUBCELL; b++)
2635 fprintf(fp, "nbl j-list #i-subcell %d %7d %4.1f\n",
2636 b, c[b], 100.0*c[b]/(double)(nbl->ncj4*NBNXN_GPU_JGROUP_SIZE));
2640 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp */
2641 static void low_get_nbl_exclusions(nbnxn_pairlist_t *nbl, int cj4,
2642 int warp, nbnxn_excl_t **excl)
2644 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2646 /* No exclusions set, make a new list entry */
2647 nbl->cj4[cj4].imei[warp].excl_ind = nbl->nexcl;
2649 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2650 set_no_excls(*excl);
2654 /* We already have some exclusions, new ones can be added to the list */
2655 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2659 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp,
2660 * allocates extra memory, if necessary.
2662 static void get_nbl_exclusions_1(nbnxn_pairlist_t *nbl, int cj4,
2663 int warp, nbnxn_excl_t **excl)
2665 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2667 /* We need to make a new list entry, check if we have space */
2668 check_excl_space(nbl, 1);
2670 low_get_nbl_exclusions(nbl, cj4, warp, excl);
2673 /* Returns pointers to the exclusion mask for cj4-unit cj4 for both warps,
2674 * allocates extra memory, if necessary.
2676 static void get_nbl_exclusions_2(nbnxn_pairlist_t *nbl, int cj4,
2677 nbnxn_excl_t **excl_w0,
2678 nbnxn_excl_t **excl_w1)
2680 /* Check for space we might need */
2681 check_excl_space(nbl, 2);
2683 low_get_nbl_exclusions(nbl, cj4, 0, excl_w0);
2684 low_get_nbl_exclusions(nbl, cj4, 1, excl_w1);
2687 /* Sets the self exclusions i=j and pair exclusions i>j */
2688 static void set_self_and_newton_excls_supersub(nbnxn_pairlist_t *nbl,
2689 int cj4_ind, int sj_offset,
2692 nbnxn_excl_t *excl[2];
2695 /* Here we only set the set self and double pair exclusions */
2697 get_nbl_exclusions_2(nbl, cj4_ind, &excl[0], &excl[1]);
2699 /* Only minor < major bits set */
2700 for (ej = 0; ej < nbl->na_ci; ej++)
2703 for (ei = ej; ei < nbl->na_ci; ei++)
2705 excl[w]->pair[(ej & (NBNXN_GPU_JGROUP_SIZE-1))*nbl->na_ci + ei] &=
2706 ~(1U << (sj_offset*GPU_NSUBCELL + si));
2711 /* Returns a diagonal or off-diagonal interaction mask for plain C lists */
2712 static unsigned int get_imask(gmx_bool rdiag, int ci, int cj)
2714 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2717 /* Returns a diagonal or off-diagonal interaction mask for SIMD128 lists */
2718 static unsigned int get_imask_simd128(gmx_bool rdiag, int ci, int cj)
2720 #ifndef GMX_DOUBLE /* cj-size = 4 */
2721 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2722 #else /* cj-size = 2 */
2723 return (rdiag && ci*2 == cj ? NBNXN_INT_MASK_DIAG_J2_0 :
2724 (rdiag && ci*2+1 == cj ? NBNXN_INT_MASK_DIAG_J2_1 :
2725 NBNXN_INT_MASK_ALL));
2729 /* Returns a diagonal or off-diagonal interaction mask for SIMD256 lists */
2730 static unsigned int get_imask_simd256(gmx_bool rdiag, int ci, int cj)
2732 #ifndef GMX_DOUBLE /* cj-size = 8 */
2733 return (rdiag && ci == cj*2 ? NBNXN_INT_MASK_DIAG_J8_0 :
2734 (rdiag && ci == cj*2+1 ? NBNXN_INT_MASK_DIAG_J8_1 :
2735 NBNXN_INT_MASK_ALL));
2736 #else /* cj-size = 4 */
2737 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2741 #ifdef GMX_NBNXN_SIMD
2742 #if GMX_NBNXN_SIMD_BITWIDTH == 128
2743 #define get_imask_simd_4xn get_imask_simd128
2745 #if GMX_NBNXN_SIMD_BITWIDTH == 256
2746 #define get_imask_simd_4xn get_imask_simd256
2747 #define get_imask_simd_2xnn get_imask_simd128
2749 #error "unsupported GMX_NBNXN_SIMD_BITWIDTH"
2754 /* Plain C code for making a pair list of cell ci vs cell cjf-cjl.
2755 * Checks bounding box distances and possibly atom pair distances.
2757 static void make_cluster_list_simple(const nbnxn_grid_t *gridj,
2758 nbnxn_pairlist_t *nbl,
2759 int ci, int cjf, int cjl,
2760 gmx_bool remove_sub_diag,
2762 real rl2, float rbb2,
2765 const nbnxn_list_work_t *work;
2772 int cjf_gl, cjl_gl, cj;
2776 bb_ci = nbl->work->bb_ci;
2777 x_ci = nbl->work->x_ci;
2780 while (!InRange && cjf <= cjl)
2782 d2 = subc_bb_dist2(0, bb_ci, cjf, gridj->bb);
2785 /* Check if the distance is within the distance where
2786 * we use only the bounding box distance rbb,
2787 * or within the cut-off and there is at least one atom pair
2788 * within the cut-off.
2798 cjf_gl = gridj->cell0 + cjf;
2799 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2801 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2803 InRange = InRange ||
2804 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2805 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2806 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2809 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2822 while (!InRange && cjl > cjf)
2824 d2 = subc_bb_dist2(0, bb_ci, cjl, gridj->bb);
2827 /* Check if the distance is within the distance where
2828 * we use only the bounding box distance rbb,
2829 * or within the cut-off and there is at least one atom pair
2830 * within the cut-off.
2840 cjl_gl = gridj->cell0 + cjl;
2841 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2843 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2845 InRange = InRange ||
2846 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2847 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2848 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2851 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2861 for (cj = cjf; cj <= cjl; cj++)
2863 /* Store cj and the interaction mask */
2864 nbl->cj[nbl->ncj].cj = gridj->cell0 + cj;
2865 nbl->cj[nbl->ncj].excl = get_imask(remove_sub_diag, ci, cj);
2868 /* Increase the closing index in i super-cell list */
2869 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
2873 #ifdef GMX_NBNXN_SIMD_4XN
2874 #include "nbnxn_search_simd_4xn.h"
2876 #ifdef GMX_NBNXN_SIMD_2XNN
2877 #include "nbnxn_search_simd_2xnn.h"
2880 /* Plain C or SSE code for making a pair list of super-cell sci vs scj.
2881 * Checks bounding box distances and possibly atom pair distances.
2883 static void make_cluster_list_supersub(const nbnxn_grid_t *gridi,
2884 const nbnxn_grid_t *gridj,
2885 nbnxn_pairlist_t *nbl,
2887 gmx_bool sci_equals_scj,
2888 int stride, const real *x,
2889 real rl2, float rbb2,
2894 int cjo, ci1, ci, cj, cj_gl;
2895 int cj4_ind, cj_offset;
2902 #define PRUNE_LIST_CPU_ONE
2903 #ifdef PRUNE_LIST_CPU_ONE
2907 d2l = nbl->work->d2;
2909 bb_ci = nbl->work->bb_ci;
2910 x_ci = nbl->work->x_ci;
2914 for (cjo = 0; cjo < gridj->nsubc[scj]; cjo++)
2916 cj4_ind = (nbl->work->cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2917 cj_offset = nbl->work->cj_ind - cj4_ind*NBNXN_GPU_JGROUP_SIZE;
2918 cj4 = &nbl->cj4[cj4_ind];
2920 cj = scj*GPU_NSUBCELL + cjo;
2922 cj_gl = gridj->cell0*GPU_NSUBCELL + cj;
2924 /* Initialize this j-subcell i-subcell list */
2925 cj4->cj[cj_offset] = cj_gl;
2934 ci1 = gridi->nsubc[sci];
2938 /* Determine all ci1 bb distances in one call with SSE */
2939 subc_bb_dist2_sse_xxxx(gridj->bb+(cj>>STRIDE_PBB_2LOG)*NNBSBB_XXXX+(cj & (STRIDE_PBB-1)),
2945 /* We use a fixed upper-bound instead of ci1 to help optimization */
2946 for (ci = 0; ci < GPU_NSUBCELL; ci++)
2953 #ifndef NBNXN_BBXXXX
2954 /* Determine the bb distance between ci and cj */
2955 d2l[ci] = subc_bb_dist2(ci, bb_ci, cj, gridj->bb);
2960 #ifdef PRUNE_LIST_CPU_ALL
2961 /* Check if the distance is within the distance where
2962 * we use only the bounding box distance rbb,
2963 * or within the cut-off and there is at least one atom pair
2964 * within the cut-off. This check is very costly.
2966 *ndistc += na_c*na_c;
2969 #ifdef NBNXN_PBB_SSE
2974 (na_c, ci, x_ci, cj_gl, stride, x, rl2)))
2976 /* Check if the distance between the two bounding boxes
2977 * in within the pair-list cut-off.
2982 /* Flag this i-subcell to be taken into account */
2983 imask |= (1U << (cj_offset*GPU_NSUBCELL+ci));
2985 #ifdef PRUNE_LIST_CPU_ONE
2993 #ifdef PRUNE_LIST_CPU_ONE
2994 /* If we only found 1 pair, check if any atoms are actually
2995 * within the cut-off, so we could get rid of it.
2997 if (npair == 1 && d2l[ci_last] >= rbb2)
2999 /* Avoid using function pointers here, as it's slower */
3001 #ifdef NBNXN_PBB_SSE
3006 (na_c, ci_last, x_ci, cj_gl, stride, x, rl2))
3008 imask &= ~(1U << (cj_offset*GPU_NSUBCELL+ci_last));
3016 /* We have a useful sj entry, close it now */
3018 /* Set the exclucions for the ci== sj entry.
3019 * Here we don't bother to check if this entry is actually flagged,
3020 * as it will nearly always be in the list.
3024 set_self_and_newton_excls_supersub(nbl, cj4_ind, cj_offset, cjo);
3027 /* Copy the cluster interaction mask to the list */
3028 for (w = 0; w < NWARP; w++)
3030 cj4->imei[w].imask |= imask;
3033 nbl->work->cj_ind++;
3035 /* Keep the count */
3036 nbl->nci_tot += npair;
3038 /* Increase the closing index in i super-cell list */
3039 nbl->sci[nbl->nsci].cj4_ind_end =
3040 ((nbl->work->cj_ind+NBNXN_GPU_JGROUP_SIZE-1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3045 /* Set all atom-pair exclusions from the topology stored in excl
3046 * as masks in the pair-list for simple list i-entry nbl_ci
3048 static void set_ci_top_excls(const nbnxn_search_t nbs,
3049 nbnxn_pairlist_t *nbl,
3050 gmx_bool diagRemoved,
3053 const nbnxn_ci_t *nbl_ci,
3054 const t_blocka *excl)
3058 int cj_ind_first, cj_ind_last;
3059 int cj_first, cj_last;
3061 int i, ai, aj, si, eind, ge, se;
3062 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3066 nbnxn_excl_t *nbl_excl;
3067 int inner_i, inner_e;
3071 if (nbl_ci->cj_ind_end == nbl_ci->cj_ind_start)
3079 cj_ind_first = nbl_ci->cj_ind_start;
3080 cj_ind_last = nbl->ncj - 1;
3082 cj_first = nbl->cj[cj_ind_first].cj;
3083 cj_last = nbl->cj[cj_ind_last].cj;
3085 /* Determine how many contiguous j-cells we have starting
3086 * from the first i-cell. This number can be used to directly
3087 * calculate j-cell indices for excluded atoms.
3090 if (na_ci_2log == na_cj_2log)
3092 while (cj_ind_first + ndirect <= cj_ind_last &&
3093 nbl->cj[cj_ind_first+ndirect].cj == ci + ndirect)
3098 #ifdef NBNXN_SEARCH_BB_SSE
3101 while (cj_ind_first + ndirect <= cj_ind_last &&
3102 nbl->cj[cj_ind_first+ndirect].cj == ci_to_cj(na_cj_2log, ci) + ndirect)
3109 /* Loop over the atoms in the i super-cell */
3110 for (i = 0; i < nbl->na_sc; i++)
3112 ai = nbs->a[ci*nbl->na_sc+i];
3115 si = (i>>na_ci_2log);
3117 /* Loop over the topology-based exclusions for this i-atom */
3118 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3124 /* The self exclusion are already set, save some time */
3130 /* Without shifts we only calculate interactions j>i
3131 * for one-way pair-lists.
3133 if (diagRemoved && ge <= ci*nbl->na_sc + i)
3138 se = (ge >> na_cj_2log);
3140 /* Could the cluster se be in our list? */
3141 if (se >= cj_first && se <= cj_last)
3143 if (se < cj_first + ndirect)
3145 /* We can calculate cj_ind directly from se */
3146 found = cj_ind_first + se - cj_first;
3150 /* Search for se using bisection */
3152 cj_ind_0 = cj_ind_first + ndirect;
3153 cj_ind_1 = cj_ind_last + 1;
3154 while (found == -1 && cj_ind_0 < cj_ind_1)
3156 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3158 cj_m = nbl->cj[cj_ind_m].cj;
3166 cj_ind_1 = cj_ind_m;
3170 cj_ind_0 = cj_ind_m + 1;
3177 inner_i = i - (si << na_ci_2log);
3178 inner_e = ge - (se << na_cj_2log);
3180 nbl->cj[found].excl &= ~(1U<<((inner_i<<na_cj_2log) + inner_e));
3188 /* Set all atom-pair exclusions from the topology stored in excl
3189 * as masks in the pair-list for i-super-cell entry nbl_sci
3191 static void set_sci_top_excls(const nbnxn_search_t nbs,
3192 nbnxn_pairlist_t *nbl,
3193 gmx_bool diagRemoved,
3195 const nbnxn_sci_t *nbl_sci,
3196 const t_blocka *excl)
3201 int cj_ind_first, cj_ind_last;
3202 int cj_first, cj_last;
3204 int i, ai, aj, si, eind, ge, se;
3205 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3209 nbnxn_excl_t *nbl_excl;
3210 int inner_i, inner_e, w;
3216 if (nbl_sci->cj4_ind_end == nbl_sci->cj4_ind_start)
3224 cj_ind_first = nbl_sci->cj4_ind_start*NBNXN_GPU_JGROUP_SIZE;
3225 cj_ind_last = nbl->work->cj_ind - 1;
3227 cj_first = nbl->cj4[nbl_sci->cj4_ind_start].cj[0];
3228 cj_last = nbl_cj(nbl, cj_ind_last);
3230 /* Determine how many contiguous j-clusters we have starting
3231 * from the first i-cluster. This number can be used to directly
3232 * calculate j-cluster indices for excluded atoms.
3235 while (cj_ind_first + ndirect <= cj_ind_last &&
3236 nbl_cj(nbl, cj_ind_first+ndirect) == sci*GPU_NSUBCELL + ndirect)
3241 /* Loop over the atoms in the i super-cell */
3242 for (i = 0; i < nbl->na_sc; i++)
3244 ai = nbs->a[sci*nbl->na_sc+i];
3247 si = (i>>na_c_2log);
3249 /* Loop over the topology-based exclusions for this i-atom */
3250 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3256 /* The self exclusion are already set, save some time */
3262 /* Without shifts we only calculate interactions j>i
3263 * for one-way pair-lists.
3265 if (diagRemoved && ge <= sci*nbl->na_sc + i)
3271 /* Could the cluster se be in our list? */
3272 if (se >= cj_first && se <= cj_last)
3274 if (se < cj_first + ndirect)
3276 /* We can calculate cj_ind directly from se */
3277 found = cj_ind_first + se - cj_first;
3281 /* Search for se using bisection */
3283 cj_ind_0 = cj_ind_first + ndirect;
3284 cj_ind_1 = cj_ind_last + 1;
3285 while (found == -1 && cj_ind_0 < cj_ind_1)
3287 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3289 cj_m = nbl_cj(nbl, cj_ind_m);
3297 cj_ind_1 = cj_ind_m;
3301 cj_ind_0 = cj_ind_m + 1;
3308 inner_i = i - si*na_c;
3309 inner_e = ge - se*na_c;
3311 /* Macro for getting the index of atom a within a cluster */
3312 #define AMODCJ4(a) ((a) & (NBNXN_GPU_JGROUP_SIZE - 1))
3313 /* Macro for converting an atom number to a cluster number */
3314 #define A2CJ4(a) ((a) >> NBNXN_GPU_JGROUP_SIZE_2LOG)
3315 /* Macro for getting the index of an i-atom within a warp */
3316 #define AMODWI(a) ((a) & (NBNXN_GPU_CLUSTER_SIZE/2 - 1))
3318 if (nbl_imask0(nbl, found) & (1U << (AMODCJ4(found)*GPU_NSUBCELL + si)))
3322 get_nbl_exclusions_1(nbl, A2CJ4(found), w, &nbl_excl);
3324 nbl_excl->pair[AMODWI(inner_e)*nbl->na_ci+inner_i] &=
3325 ~(1U << (AMODCJ4(found)*GPU_NSUBCELL + si));
3338 /* Reallocate the simple ci list for at least n entries */
3339 static void nb_realloc_ci(nbnxn_pairlist_t *nbl, int n)
3341 nbl->ci_nalloc = over_alloc_small(n);
3342 nbnxn_realloc_void((void **)&nbl->ci,
3343 nbl->nci*sizeof(*nbl->ci),
3344 nbl->ci_nalloc*sizeof(*nbl->ci),
3345 nbl->alloc, nbl->free);
3348 /* Reallocate the super-cell sci list for at least n entries */
3349 static void nb_realloc_sci(nbnxn_pairlist_t *nbl, int n)
3351 nbl->sci_nalloc = over_alloc_small(n);
3352 nbnxn_realloc_void((void **)&nbl->sci,
3353 nbl->nsci*sizeof(*nbl->sci),
3354 nbl->sci_nalloc*sizeof(*nbl->sci),
3355 nbl->alloc, nbl->free);
3358 /* Make a new ci entry at index nbl->nci */
3359 static void new_ci_entry(nbnxn_pairlist_t *nbl, int ci, int shift, int flags)
3361 if (nbl->nci + 1 > nbl->ci_nalloc)
3363 nb_realloc_ci(nbl, nbl->nci+1);
3365 nbl->ci[nbl->nci].ci = ci;
3366 nbl->ci[nbl->nci].shift = shift;
3367 /* Store the interaction flags along with the shift */
3368 nbl->ci[nbl->nci].shift |= flags;
3369 nbl->ci[nbl->nci].cj_ind_start = nbl->ncj;
3370 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
3373 /* Make a new sci entry at index nbl->nsci */
3374 static void new_sci_entry(nbnxn_pairlist_t *nbl, int sci, int shift)
3376 if (nbl->nsci + 1 > nbl->sci_nalloc)
3378 nb_realloc_sci(nbl, nbl->nsci+1);
3380 nbl->sci[nbl->nsci].sci = sci;
3381 nbl->sci[nbl->nsci].shift = shift;
3382 nbl->sci[nbl->nsci].cj4_ind_start = nbl->ncj4;
3383 nbl->sci[nbl->nsci].cj4_ind_end = nbl->ncj4;
3386 /* Sort the simple j-list cj on exclusions.
3387 * Entries with exclusions will all be sorted to the beginning of the list.
3389 static void sort_cj_excl(nbnxn_cj_t *cj, int ncj,
3390 nbnxn_list_work_t *work)
3394 if (ncj > work->cj_nalloc)
3396 work->cj_nalloc = over_alloc_large(ncj);
3397 srenew(work->cj, work->cj_nalloc);
3400 /* Make a list of the j-cells involving exclusions */
3402 for (j = 0; j < ncj; j++)
3404 if (cj[j].excl != NBNXN_INT_MASK_ALL)
3406 work->cj[jnew++] = cj[j];
3409 /* Check if there are exclusions at all or not just the first entry */
3410 if (!((jnew == 0) ||
3411 (jnew == 1 && cj[0].excl != NBNXN_INT_MASK_ALL)))
3413 for (j = 0; j < ncj; j++)
3415 if (cj[j].excl == NBNXN_INT_MASK_ALL)
3417 work->cj[jnew++] = cj[j];
3420 for (j = 0; j < ncj; j++)
3422 cj[j] = work->cj[j];
3427 /* Close this simple list i entry */
3428 static void close_ci_entry_simple(nbnxn_pairlist_t *nbl)
3432 /* All content of the new ci entry have already been filled correctly,
3433 * we only need to increase the count here (for non empty lists).
3435 jlen = nbl->ci[nbl->nci].cj_ind_end - nbl->ci[nbl->nci].cj_ind_start;
3438 sort_cj_excl(nbl->cj+nbl->ci[nbl->nci].cj_ind_start, jlen, nbl->work);
3440 /* The counts below are used for non-bonded pair/flop counts
3441 * and should therefore match the available kernel setups.
3443 if (!(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_COUL(0)))
3445 nbl->work->ncj_noq += jlen;
3447 else if ((nbl->ci[nbl->nci].shift & NBNXN_CI_HALF_LJ(0)) ||
3448 !(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_LJ(0)))
3450 nbl->work->ncj_hlj += jlen;
3457 /* Split sci entry for load balancing on the GPU.
3458 * Splitting ensures we have enough lists to fully utilize the whole GPU.
3459 * With progBal we generate progressively smaller lists, which improves
3460 * load balancing. As we only know the current count on our own thread,
3461 * we will need to estimate the current total amount of i-entries.
3462 * As the lists get concatenated later, this estimate depends
3463 * both on nthread and our own thread index.
3465 static void split_sci_entry(nbnxn_pairlist_t *nbl,
3466 int nsp_max_av, gmx_bool progBal, int nc_bal,
3467 int thread, int nthread)
3471 int cj4_start, cj4_end, j4len, cj4;
3473 int nsp, nsp_sci, nsp_cj4, nsp_cj4_e, nsp_cj4_p;
3478 /* Estimate the total numbers of ci's of the nblist combined
3479 * over all threads using the target number of ci's.
3481 nsci_est = nc_bal*thread/nthread + nbl->nsci;
3483 /* The first ci blocks should be larger, to avoid overhead.
3484 * The last ci blocks should be smaller, to improve load balancing.
3487 nsp_max_av*nc_bal*3/(2*(nsci_est - 1 + nc_bal)));
3491 nsp_max = nsp_max_av;
3494 cj4_start = nbl->sci[nbl->nsci-1].cj4_ind_start;
3495 cj4_end = nbl->sci[nbl->nsci-1].cj4_ind_end;
3496 j4len = cj4_end - cj4_start;
3498 if (j4len > 1 && j4len*GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE > nsp_max)
3500 /* Remove the last ci entry and process the cj4's again */
3508 for (cj4 = cj4_start; cj4 < cj4_end; cj4++)
3510 nsp_cj4_p = nsp_cj4;
3511 /* Count the number of cluster pairs in this cj4 group */
3513 for (p = 0; p < GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE; p++)
3515 nsp_cj4 += (nbl->cj4[cj4].imei[0].imask >> p) & 1;
3518 if (nsp_cj4 > 0 && nsp + nsp_cj4 > nsp_max)
3520 /* Split the list at cj4 */
3521 nbl->sci[sci].cj4_ind_end = cj4;
3522 /* Create a new sci entry */
3525 if (nbl->nsci+1 > nbl->sci_nalloc)
3527 nb_realloc_sci(nbl, nbl->nsci+1);
3529 nbl->sci[sci].sci = nbl->sci[nbl->nsci-1].sci;
3530 nbl->sci[sci].shift = nbl->sci[nbl->nsci-1].shift;
3531 nbl->sci[sci].cj4_ind_start = cj4;
3533 nsp_cj4_e = nsp_cj4_p;
3539 /* Put the remaining cj4's in the last sci entry */
3540 nbl->sci[sci].cj4_ind_end = cj4_end;
3542 /* Possibly balance out the last two sci's
3543 * by moving the last cj4 of the second last sci.
3545 if (nsp_sci - nsp_cj4_e >= nsp + nsp_cj4_e)
3547 nbl->sci[sci-1].cj4_ind_end--;
3548 nbl->sci[sci].cj4_ind_start--;
3555 /* Clost this super/sub list i entry */
3556 static void close_ci_entry_supersub(nbnxn_pairlist_t *nbl,
3558 gmx_bool progBal, int nc_bal,
3559 int thread, int nthread)
3564 /* All content of the new ci entry have already been filled correctly,
3565 * we only need to increase the count here (for non empty lists).
3567 j4len = nbl->sci[nbl->nsci].cj4_ind_end - nbl->sci[nbl->nsci].cj4_ind_start;
3570 /* We can only have complete blocks of 4 j-entries in a list,
3571 * so round the count up before closing.
3573 nbl->ncj4 = ((nbl->work->cj_ind + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3574 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3580 /* Measure the size of the new entry and potentially split it */
3581 split_sci_entry(nbl, nsp_max_av, progBal, nc_bal, thread, nthread);
3586 /* Syncs the working array before adding another grid pair to the list */
3587 static void sync_work(nbnxn_pairlist_t *nbl)
3591 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3592 nbl->work->cj4_init = nbl->ncj4;
3596 /* Clears an nbnxn_pairlist_t data structure */
3597 static void clear_pairlist(nbnxn_pairlist_t *nbl)
3606 nbl->work->ncj_noq = 0;
3607 nbl->work->ncj_hlj = 0;
3610 /* Sets a simple list i-cell bounding box, including PBC shift */
3611 static void set_icell_bb_simple(const float *bb, int ci,
3612 real shx, real shy, real shz,
3618 bb_ci[BBL_X] = bb[ia+BBL_X] + shx;
3619 bb_ci[BBL_Y] = bb[ia+BBL_Y] + shy;
3620 bb_ci[BBL_Z] = bb[ia+BBL_Z] + shz;
3621 bb_ci[BBU_X] = bb[ia+BBU_X] + shx;
3622 bb_ci[BBU_Y] = bb[ia+BBU_Y] + shy;
3623 bb_ci[BBU_Z] = bb[ia+BBU_Z] + shz;
3626 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
3627 static void set_icell_bb_supersub(const float *bb, int ci,
3628 real shx, real shy, real shz,
3634 ia = ci*(GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX;
3635 for (m = 0; m < (GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX; m += NNBSBB_XXXX)
3637 for (i = 0; i < STRIDE_PBB; i++)
3639 bb_ci[m+0*STRIDE_PBB+i] = bb[ia+m+0*STRIDE_PBB+i] + shx;
3640 bb_ci[m+1*STRIDE_PBB+i] = bb[ia+m+1*STRIDE_PBB+i] + shy;
3641 bb_ci[m+2*STRIDE_PBB+i] = bb[ia+m+2*STRIDE_PBB+i] + shz;
3642 bb_ci[m+3*STRIDE_PBB+i] = bb[ia+m+3*STRIDE_PBB+i] + shx;
3643 bb_ci[m+4*STRIDE_PBB+i] = bb[ia+m+4*STRIDE_PBB+i] + shy;
3644 bb_ci[m+5*STRIDE_PBB+i] = bb[ia+m+5*STRIDE_PBB+i] + shz;
3648 ia = ci*GPU_NSUBCELL*NNBSBB_B;
3649 for (i = 0; i < GPU_NSUBCELL*NNBSBB_B; i += NNBSBB_B)
3651 bb_ci[i+BBL_X] = bb[ia+i+BBL_X] + shx;
3652 bb_ci[i+BBL_Y] = bb[ia+i+BBL_Y] + shy;
3653 bb_ci[i+BBL_Z] = bb[ia+i+BBL_Z] + shz;
3654 bb_ci[i+BBU_X] = bb[ia+i+BBU_X] + shx;
3655 bb_ci[i+BBU_Y] = bb[ia+i+BBU_Y] + shy;
3656 bb_ci[i+BBU_Z] = bb[ia+i+BBU_Z] + shz;
3661 /* Copies PBC shifted i-cell atom coordinates x,y,z to working array */
3662 static void icell_set_x_simple(int ci,
3663 real shx, real shy, real shz,
3664 int gmx_unused na_c,
3665 int stride, const real *x,
3666 nbnxn_list_work_t *work)
3670 ia = ci*NBNXN_CPU_CLUSTER_I_SIZE;
3672 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE; i++)
3674 work->x_ci[i*STRIDE_XYZ+XX] = x[(ia+i)*stride+XX] + shx;
3675 work->x_ci[i*STRIDE_XYZ+YY] = x[(ia+i)*stride+YY] + shy;
3676 work->x_ci[i*STRIDE_XYZ+ZZ] = x[(ia+i)*stride+ZZ] + shz;
3680 /* Copies PBC shifted super-cell atom coordinates x,y,z to working array */
3681 static void icell_set_x_supersub(int ci,
3682 real shx, real shy, real shz,
3684 int stride, const real *x,
3685 nbnxn_list_work_t *work)
3692 ia = ci*GPU_NSUBCELL*na_c;
3693 for (i = 0; i < GPU_NSUBCELL*na_c; i++)
3695 x_ci[i*DIM + XX] = x[(ia+i)*stride + XX] + shx;
3696 x_ci[i*DIM + YY] = x[(ia+i)*stride + YY] + shy;
3697 x_ci[i*DIM + ZZ] = x[(ia+i)*stride + ZZ] + shz;
3701 #ifdef NBNXN_SEARCH_BB_SSE
3702 /* Copies PBC shifted super-cell packed atom coordinates to working array */
3703 static void icell_set_x_supersub_sse8(int ci,
3704 real shx, real shy, real shz,
3706 int stride, const real *x,
3707 nbnxn_list_work_t *work)
3709 int si, io, ia, i, j;
3714 for (si = 0; si < GPU_NSUBCELL; si++)
3716 for (i = 0; i < na_c; i += STRIDE_PBB)
3719 ia = ci*GPU_NSUBCELL*na_c + io;
3720 for (j = 0; j < STRIDE_PBB; j++)
3722 x_ci[io*DIM + j + XX*STRIDE_PBB] = x[(ia+j)*stride+XX] + shx;
3723 x_ci[io*DIM + j + YY*STRIDE_PBB] = x[(ia+j)*stride+YY] + shy;
3724 x_ci[io*DIM + j + ZZ*STRIDE_PBB] = x[(ia+j)*stride+ZZ] + shz;
3731 static real nbnxn_rlist_inc_nonloc_fac = 0.6;
3733 /* Due to the cluster size the effective pair-list is longer than
3734 * that of a simple atom pair-list. This function gives the extra distance.
3736 real nbnxn_get_rlist_effective_inc(int cluster_size, real atom_density)
3738 return ((0.5 + nbnxn_rlist_inc_nonloc_fac)*sqr(((cluster_size) - 1.0)/(cluster_size))*pow((cluster_size)/(atom_density), 1.0/3.0));
3741 /* Estimates the interaction volume^2 for non-local interactions */
3742 static real nonlocal_vol2(const gmx_domdec_zones_t *zones, rvec ls, real r)
3751 /* Here we simply add up the volumes of 1, 2 or 3 1D decomposition
3752 * not home interaction volume^2. As these volumes are not additive,
3753 * this is an overestimate, but it would only be significant in the limit
3754 * of small cells, where we anyhow need to split the lists into
3755 * as small parts as possible.
3758 for (z = 0; z < zones->n; z++)
3760 if (zones->shift[z][XX] + zones->shift[z][YY] + zones->shift[z][ZZ] == 1)
3765 for (d = 0; d < DIM; d++)
3767 if (zones->shift[z][d] == 0)
3771 za *= zones->size[z].x1[d] - zones->size[z].x0[d];
3775 /* 4 octants of a sphere */
3776 vold_est = 0.25*M_PI*r*r*r*r;
3777 /* 4 quarter pie slices on the edges */
3778 vold_est += 4*cl*M_PI/6.0*r*r*r;
3779 /* One rectangular volume on a face */
3780 vold_est += ca*0.5*r*r;
3782 vol2_est_tot += vold_est*za;
3786 return vol2_est_tot;
3789 /* Estimates the average size of a full j-list for super/sub setup */
3790 static int get_nsubpair_max(const nbnxn_search_t nbs,
3793 int min_ci_balanced)
3795 const nbnxn_grid_t *grid;
3797 real xy_diag2, r_eff_sup, vol_est, nsp_est, nsp_est_nl;
3800 grid = &nbs->grid[0];
3802 ls[XX] = (grid->c1[XX] - grid->c0[XX])/(grid->ncx*GPU_NSUBCELL_X);
3803 ls[YY] = (grid->c1[YY] - grid->c0[YY])/(grid->ncy*GPU_NSUBCELL_Y);
3804 ls[ZZ] = (grid->c1[ZZ] - grid->c0[ZZ])*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z);
3806 /* The average squared length of the diagonal of a sub cell */
3807 xy_diag2 = ls[XX]*ls[XX] + ls[YY]*ls[YY] + ls[ZZ]*ls[ZZ];
3809 /* The formulas below are a heuristic estimate of the average nsj per si*/
3810 r_eff_sup = rlist + nbnxn_rlist_inc_nonloc_fac*sqr((grid->na_c - 1.0)/grid->na_c)*sqrt(xy_diag2/3);
3812 if (!nbs->DomDec || nbs->zones->n == 1)
3819 sqr(grid->atom_density/grid->na_c)*
3820 nonlocal_vol2(nbs->zones, ls, r_eff_sup);
3825 /* Sub-cell interacts with itself */
3826 vol_est = ls[XX]*ls[YY]*ls[ZZ];
3827 /* 6/2 rectangular volume on the faces */
3828 vol_est += (ls[XX]*ls[YY] + ls[XX]*ls[ZZ] + ls[YY]*ls[ZZ])*r_eff_sup;
3829 /* 12/2 quarter pie slices on the edges */
3830 vol_est += 2*(ls[XX] + ls[YY] + ls[ZZ])*0.25*M_PI*sqr(r_eff_sup);
3831 /* 4 octants of a sphere */
3832 vol_est += 0.5*4.0/3.0*M_PI*pow(r_eff_sup, 3);
3834 nsp_est = grid->nsubc_tot*vol_est*grid->atom_density/grid->na_c;
3836 /* Subtract the non-local pair count */
3837 nsp_est -= nsp_est_nl;
3841 fprintf(debug, "nsp_est local %5.1f non-local %5.1f\n",
3842 nsp_est, nsp_est_nl);
3847 nsp_est = nsp_est_nl;
3850 if (min_ci_balanced <= 0 || grid->nc >= min_ci_balanced || grid->nc == 0)
3852 /* We don't need to worry */
3857 /* Thus the (average) maximum j-list size should be as follows */
3858 nsubpair_max = max(1, (int)(nsp_est/min_ci_balanced+0.5));
3860 /* Since the target value is a maximum (this avoids high outliers,
3861 * which lead to load imbalance), not average, we add half the
3862 * number of pairs in a cj4 block to get the average about right.
3864 nsubpair_max += GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE/2;
3869 fprintf(debug, "nbl nsp estimate %.1f, nsubpair_max %d\n",
3870 nsp_est, nsubpair_max);
3873 return nsubpair_max;
3876 /* Debug list print function */
3877 static void print_nblist_ci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3881 for (i = 0; i < nbl->nci; i++)
3883 fprintf(fp, "ci %4d shift %2d ncj %3d\n",
3884 nbl->ci[i].ci, nbl->ci[i].shift,
3885 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start);
3887 for (j = nbl->ci[i].cj_ind_start; j < nbl->ci[i].cj_ind_end; j++)
3889 fprintf(fp, " cj %5d imask %x\n",
3896 /* Debug list print function */
3897 static void print_nblist_sci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3899 int i, j4, j, ncp, si;
3901 for (i = 0; i < nbl->nsci; i++)
3903 fprintf(fp, "ci %4d shift %2d ncj4 %2d\n",
3904 nbl->sci[i].sci, nbl->sci[i].shift,
3905 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start);
3908 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
3910 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
3912 fprintf(fp, " sj %5d imask %x\n",
3914 nbl->cj4[j4].imei[0].imask);
3915 for (si = 0; si < GPU_NSUBCELL; si++)
3917 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
3924 fprintf(fp, "ci %4d shift %2d ncj4 %2d ncp %3d\n",
3925 nbl->sci[i].sci, nbl->sci[i].shift,
3926 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start,
3931 /* Combine pair lists *nbl generated on multiple threads nblc */
3932 static void combine_nblists(int nnbl, nbnxn_pairlist_t **nbl,
3933 nbnxn_pairlist_t *nblc)
3935 int nsci, ncj4, nexcl;
3940 gmx_incons("combine_nblists does not support simple lists");
3945 nexcl = nblc->nexcl;
3946 for (i = 0; i < nnbl; i++)
3948 nsci += nbl[i]->nsci;
3949 ncj4 += nbl[i]->ncj4;
3950 nexcl += nbl[i]->nexcl;
3953 if (nsci > nblc->sci_nalloc)
3955 nb_realloc_sci(nblc, nsci);
3957 if (ncj4 > nblc->cj4_nalloc)
3959 nblc->cj4_nalloc = over_alloc_small(ncj4);
3960 nbnxn_realloc_void((void **)&nblc->cj4,
3961 nblc->ncj4*sizeof(*nblc->cj4),
3962 nblc->cj4_nalloc*sizeof(*nblc->cj4),
3963 nblc->alloc, nblc->free);
3965 if (nexcl > nblc->excl_nalloc)
3967 nblc->excl_nalloc = over_alloc_small(nexcl);
3968 nbnxn_realloc_void((void **)&nblc->excl,
3969 nblc->nexcl*sizeof(*nblc->excl),
3970 nblc->excl_nalloc*sizeof(*nblc->excl),
3971 nblc->alloc, nblc->free);
3974 /* Each thread should copy its own data to the combined arrays,
3975 * as otherwise data will go back and forth between different caches.
3977 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
3978 for (n = 0; n < nnbl; n++)
3985 const nbnxn_pairlist_t *nbli;
3987 /* Determine the offset in the combined data for our thread */
3988 sci_offset = nblc->nsci;
3989 cj4_offset = nblc->ncj4;
3990 ci_offset = nblc->nci_tot;
3991 excl_offset = nblc->nexcl;
3993 for (i = 0; i < n; i++)
3995 sci_offset += nbl[i]->nsci;
3996 cj4_offset += nbl[i]->ncj4;
3997 ci_offset += nbl[i]->nci_tot;
3998 excl_offset += nbl[i]->nexcl;
4003 for (i = 0; i < nbli->nsci; i++)
4005 nblc->sci[sci_offset+i] = nbli->sci[i];
4006 nblc->sci[sci_offset+i].cj4_ind_start += cj4_offset;
4007 nblc->sci[sci_offset+i].cj4_ind_end += cj4_offset;
4010 for (j4 = 0; j4 < nbli->ncj4; j4++)
4012 nblc->cj4[cj4_offset+j4] = nbli->cj4[j4];
4013 nblc->cj4[cj4_offset+j4].imei[0].excl_ind += excl_offset;
4014 nblc->cj4[cj4_offset+j4].imei[1].excl_ind += excl_offset;
4017 for (j4 = 0; j4 < nbli->nexcl; j4++)
4019 nblc->excl[excl_offset+j4] = nbli->excl[j4];
4023 for (n = 0; n < nnbl; n++)
4025 nblc->nsci += nbl[n]->nsci;
4026 nblc->ncj4 += nbl[n]->ncj4;
4027 nblc->nci_tot += nbl[n]->nci_tot;
4028 nblc->nexcl += nbl[n]->nexcl;
4032 /* Returns the next ci to be processes by our thread */
4033 static gmx_bool next_ci(const nbnxn_grid_t *grid,
4035 int nth, int ci_block,
4036 int *ci_x, int *ci_y,
4042 if (*ci_b == ci_block)
4044 /* Jump to the next block assigned to this task */
4045 *ci += (nth - 1)*ci_block;
4049 if (*ci >= grid->nc*conv)
4054 while (*ci >= grid->cxy_ind[*ci_x*grid->ncy + *ci_y + 1]*conv)
4057 if (*ci_y == grid->ncy)
4067 /* Returns the distance^2 for which we put cell pairs in the list
4068 * without checking atom pair distances. This is usually < rlist^2.
4070 static float boundingbox_only_distance2(const nbnxn_grid_t *gridi,
4071 const nbnxn_grid_t *gridj,
4075 /* If the distance between two sub-cell bounding boxes is less
4076 * than this distance, do not check the distance between
4077 * all particle pairs in the sub-cell, since then it is likely
4078 * that the box pair has atom pairs within the cut-off.
4079 * We use the nblist cut-off minus 0.5 times the average x/y diagonal
4080 * spacing of the sub-cells. Around 40% of the checked pairs are pruned.
4081 * Using more than 0.5 gains at most 0.5%.
4082 * If forces are calculated more than twice, the performance gain
4083 * in the force calculation outweighs the cost of checking.
4084 * Note that with subcell lists, the atom-pair distance check
4085 * is only performed when only 1 out of 8 sub-cells in within range,
4086 * this is because the GPU is much faster than the cpu.
4091 bbx = 0.5*(gridi->sx + gridj->sx);
4092 bby = 0.5*(gridi->sy + gridj->sy);
4095 bbx /= GPU_NSUBCELL_X;
4096 bby /= GPU_NSUBCELL_Y;
4099 rbb2 = sqr(max(0, rlist - 0.5*sqrt(bbx*bbx + bby*bby)));
4104 return (float)((1+GMX_FLOAT_EPS)*rbb2);
4108 static int get_ci_block_size(const nbnxn_grid_t *gridi,
4109 gmx_bool bDomDec, int nth)
4111 const int ci_block_enum = 5;
4112 const int ci_block_denom = 11;
4113 const int ci_block_min_atoms = 16;
4116 /* Here we decide how to distribute the blocks over the threads.
4117 * We use prime numbers to try to avoid that the grid size becomes
4118 * a multiple of the number of threads, which would lead to some
4119 * threads getting "inner" pairs and others getting boundary pairs,
4120 * which in turns will lead to load imbalance between threads.
4121 * Set the block size as 5/11/ntask times the average number of cells
4122 * in a y,z slab. This should ensure a quite uniform distribution
4123 * of the grid parts of the different thread along all three grid
4124 * zone boundaries with 3D domain decomposition. At the same time
4125 * the blocks will not become too small.
4127 ci_block = (gridi->nc*ci_block_enum)/(ci_block_denom*gridi->ncx*nth);
4129 /* Ensure the blocks are not too small: avoids cache invalidation */
4130 if (ci_block*gridi->na_sc < ci_block_min_atoms)
4132 ci_block = (ci_block_min_atoms + gridi->na_sc - 1)/gridi->na_sc;
4135 /* Without domain decomposition
4136 * or with less than 3 blocks per task, divide in nth blocks.
4138 if (!bDomDec || ci_block*3*nth > gridi->nc)
4140 ci_block = (gridi->nc + nth - 1)/nth;
4146 /* Generates the part of pair-list nbl assigned to our thread */
4147 static void nbnxn_make_pairlist_part(const nbnxn_search_t nbs,
4148 const nbnxn_grid_t *gridi,
4149 const nbnxn_grid_t *gridj,
4150 nbnxn_search_work_t *work,
4151 const nbnxn_atomdata_t *nbat,
4152 const t_blocka *excl,
4156 gmx_bool bFBufferFlag,
4159 int min_ci_balanced,
4161 nbnxn_pairlist_t *nbl)
4168 int ci_b, ci, ci_x, ci_y, ci_xy, cj;
4174 int conv_i, cell0_i;
4175 const float *bb_i, *bbcz_i, *bbcz_j;
4177 real bx0, bx1, by0, by1, bz0, bz1;
4179 real d2cx, d2z, d2z_cx, d2z_cy, d2zx, d2zxy, d2xy;
4180 int cxf, cxl, cyf, cyf_x, cyl;
4182 int c0, c1, cs, cf, cl;
4185 int gridi_flag_shift = 0, gridj_flag_shift = 0;
4186 unsigned *gridj_flag = NULL;
4187 int ncj_old_i, ncj_old_j;
4189 nbs_cycle_start(&work->cc[enbsCCsearch]);
4191 if (gridj->bSimple != nbl->bSimple)
4193 gmx_incons("Grid incompatible with pair-list");
4197 nbl->na_sc = gridj->na_sc;
4198 nbl->na_ci = gridj->na_c;
4199 nbl->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
4200 na_cj_2log = get_2log(nbl->na_cj);
4206 /* Determine conversion of clusters to flag blocks */
4207 gridi_flag_shift = 0;
4208 while ((nbl->na_ci<<gridi_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4212 gridj_flag_shift = 0;
4213 while ((nbl->na_cj<<gridj_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4218 gridj_flag = work->buffer_flags.flag;
4221 copy_mat(nbs->box, box);
4223 rl2 = nbl->rlist*nbl->rlist;
4225 rbb2 = boundingbox_only_distance2(gridi, gridj, nbl->rlist, nbl->bSimple);
4229 fprintf(debug, "nbl bounding box only distance %f\n", sqrt(rbb2));
4232 /* Set the shift range */
4233 for (d = 0; d < DIM; d++)
4235 /* Check if we need periodicity shifts.
4236 * Without PBC or with domain decomposition we don't need them.
4238 if (d >= ePBC2npbcdim(nbs->ePBC) || nbs->dd_dim[d])
4245 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
4256 if (nbl->bSimple && !gridi->bSimple)
4258 conv_i = gridi->na_sc/gridj->na_sc;
4259 bb_i = gridi->bb_simple;
4260 bbcz_i = gridi->bbcz_simple;
4261 flags_i = gridi->flags_simple;
4267 bbcz_i = gridi->bbcz;
4268 flags_i = gridi->flags;
4270 cell0_i = gridi->cell0*conv_i;
4272 bbcz_j = gridj->bbcz;
4276 /* Blocks of the conversion factor - 1 give a large repeat count
4277 * combined with a small block size. This should result in good
4278 * load balancing for both small and large domains.
4280 ci_block = conv_i - 1;
4284 fprintf(debug, "nbl nc_i %d col.av. %.1f ci_block %d\n",
4285 gridi->nc, gridi->nc/(double)(gridi->ncx*gridi->ncy), ci_block);
4291 /* Initially ci_b and ci to 1 before where we want them to start,
4292 * as they will both be incremented in next_ci.
4295 ci = th*ci_block - 1;
4298 while (next_ci(gridi, conv_i, nth, ci_block, &ci_x, &ci_y, &ci_b, &ci))
4300 if (nbl->bSimple && flags_i[ci] == 0)
4305 ncj_old_i = nbl->ncj;
4308 if (gridj != gridi && shp[XX] == 0)
4312 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX];
4316 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx;
4318 if (bx1 < gridj->c0[XX])
4320 d2cx = sqr(gridj->c0[XX] - bx1);
4329 ci_xy = ci_x*gridi->ncy + ci_y;
4331 /* Loop over shift vectors in three dimensions */
4332 for (tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
4334 shz = tz*box[ZZ][ZZ];
4336 bz0 = bbcz_i[ci*NNBSBB_D ] + shz;
4337 bz1 = bbcz_i[ci*NNBSBB_D+1] + shz;
4349 d2z = sqr(bz0 - box[ZZ][ZZ]);
4352 d2z_cx = d2z + d2cx;
4360 bz1/((real)(gridi->cxy_ind[ci_xy+1] - gridi->cxy_ind[ci_xy]));
4365 /* The check with bz1_frac close to or larger than 1 comes later */
4367 for (ty = -shp[YY]; ty <= shp[YY]; ty++)
4369 shy = ty*box[YY][YY] + tz*box[ZZ][YY];
4373 by0 = bb_i[ci*NNBSBB_B +YY] + shy;
4374 by1 = bb_i[ci*NNBSBB_B+NNBSBB_C+YY] + shy;
4378 by0 = gridi->c0[YY] + (ci_y )*gridi->sy + shy;
4379 by1 = gridi->c0[YY] + (ci_y+1)*gridi->sy + shy;
4382 get_cell_range(by0, by1,
4383 gridj->ncy, gridj->c0[YY], gridj->sy, gridj->inv_sy,
4393 if (by1 < gridj->c0[YY])
4395 d2z_cy += sqr(gridj->c0[YY] - by1);
4397 else if (by0 > gridj->c1[YY])
4399 d2z_cy += sqr(by0 - gridj->c1[YY]);
4402 for (tx = -shp[XX]; tx <= shp[XX]; tx++)
4404 shift = XYZ2IS(tx, ty, tz);
4406 #ifdef NBNXN_SHIFT_BACKWARD
4407 if (gridi == gridj && shift > CENTRAL)
4413 shx = tx*box[XX][XX] + ty*box[YY][XX] + tz*box[ZZ][XX];
4417 bx0 = bb_i[ci*NNBSBB_B +XX] + shx;
4418 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX] + shx;
4422 bx0 = gridi->c0[XX] + (ci_x )*gridi->sx + shx;
4423 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx + shx;
4426 get_cell_range(bx0, bx1,
4427 gridj->ncx, gridj->c0[XX], gridj->sx, gridj->inv_sx,
4438 new_ci_entry(nbl, cell0_i+ci, shift, flags_i[ci]);
4442 new_sci_entry(nbl, cell0_i+ci, shift);
4445 #ifndef NBNXN_SHIFT_BACKWARD
4448 if (shift == CENTRAL && gridi == gridj &&
4452 /* Leave the pairs with i > j.
4453 * x is the major index, so skip half of it.
4460 set_icell_bb_simple(bb_i, ci, shx, shy, shz,
4465 set_icell_bb_supersub(bb_i, ci, shx, shy, shz,
4469 nbs->icell_set_x(cell0_i+ci, shx, shy, shz,
4470 gridi->na_c, nbat->xstride, nbat->x,
4473 for (cx = cxf; cx <= cxl; cx++)
4476 if (gridj->c0[XX] + cx*gridj->sx > bx1)
4478 d2zx += sqr(gridj->c0[XX] + cx*gridj->sx - bx1);
4480 else if (gridj->c0[XX] + (cx+1)*gridj->sx < bx0)
4482 d2zx += sqr(gridj->c0[XX] + (cx+1)*gridj->sx - bx0);
4485 #ifndef NBNXN_SHIFT_BACKWARD
4486 if (gridi == gridj &&
4487 cx == 0 && cyf < ci_y)
4489 if (gridi == gridj &&
4490 cx == 0 && shift == CENTRAL && cyf < ci_y)
4493 /* Leave the pairs with i > j.
4494 * Skip half of y when i and j have the same x.
4503 for (cy = cyf_x; cy <= cyl; cy++)
4505 c0 = gridj->cxy_ind[cx*gridj->ncy+cy];
4506 c1 = gridj->cxy_ind[cx*gridj->ncy+cy+1];
4507 #ifdef NBNXN_SHIFT_BACKWARD
4508 if (gridi == gridj &&
4509 shift == CENTRAL && c0 < ci)
4516 if (gridj->c0[YY] + cy*gridj->sy > by1)
4518 d2zxy += sqr(gridj->c0[YY] + cy*gridj->sy - by1);
4520 else if (gridj->c0[YY] + (cy+1)*gridj->sy < by0)
4522 d2zxy += sqr(gridj->c0[YY] + (cy+1)*gridj->sy - by0);
4524 if (c1 > c0 && d2zxy < rl2)
4526 cs = c0 + (int)(bz1_frac*(c1 - c0));
4534 /* Find the lowest cell that can possibly
4539 (bbcz_j[cf*NNBSBB_D+1] >= bz0 ||
4540 d2xy + sqr(bbcz_j[cf*NNBSBB_D+1] - bz0) < rl2))
4545 /* Find the highest cell that can possibly
4550 (bbcz_j[cl*NNBSBB_D] <= bz1 ||
4551 d2xy + sqr(bbcz_j[cl*NNBSBB_D] - bz1) < rl2))
4556 #ifdef NBNXN_REFCODE
4558 /* Simple reference code, for debugging,
4559 * overrides the more complex code above.
4564 for (k = c0; k < c1; k++)
4566 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1,
4567 bb+k*NNBSBB_B) < rl2 &&
4572 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1,
4573 bb+k*NNBSBB_B) < rl2 &&
4584 /* We want each atom/cell pair only once,
4585 * only use cj >= ci.
4587 #ifndef NBNXN_SHIFT_BACKWARD
4590 if (shift == CENTRAL)
4599 /* For f buffer flags with simple lists */
4600 ncj_old_j = nbl->ncj;
4602 switch (nb_kernel_type)
4604 case nbnxnk4x4_PlainC:
4605 check_subcell_list_space_simple(nbl, cl-cf+1);
4607 make_cluster_list_simple(gridj,
4609 (gridi == gridj && shift == CENTRAL),
4614 #ifdef GMX_NBNXN_SIMD_4XN
4615 case nbnxnk4xN_SIMD_4xN:
4616 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4617 make_cluster_list_simd_4xn(gridj,
4619 (gridi == gridj && shift == CENTRAL),
4625 #ifdef GMX_NBNXN_SIMD_2XNN
4626 case nbnxnk4xN_SIMD_2xNN:
4627 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4628 make_cluster_list_simd_2xnn(gridj,
4630 (gridi == gridj && shift == CENTRAL),
4636 case nbnxnk8x8x8_PlainC:
4637 case nbnxnk8x8x8_CUDA:
4638 check_subcell_list_space_supersub(nbl, cl-cf+1);
4639 for (cj = cf; cj <= cl; cj++)
4641 make_cluster_list_supersub(gridi, gridj,
4643 (gridi == gridj && shift == CENTRAL && ci == cj),
4644 nbat->xstride, nbat->x,
4650 ncpcheck += cl - cf + 1;
4652 if (bFBufferFlag && nbl->ncj > ncj_old_j)
4656 cbf = nbl->cj[ncj_old_j].cj >> gridj_flag_shift;
4657 cbl = nbl->cj[nbl->ncj-1].cj >> gridj_flag_shift;
4658 for (cb = cbf; cb <= cbl; cb++)
4660 gridj_flag[cb] = 1U<<th;
4668 /* Set the exclusions for this ci list */
4671 set_ci_top_excls(nbs,
4673 shift == CENTRAL && gridi == gridj,
4676 &(nbl->ci[nbl->nci]),
4681 set_sci_top_excls(nbs,
4683 shift == CENTRAL && gridi == gridj,
4685 &(nbl->sci[nbl->nsci]),
4689 /* Close this ci list */
4692 close_ci_entry_simple(nbl);
4696 close_ci_entry_supersub(nbl,
4698 progBal, min_ci_balanced,
4705 if (bFBufferFlag && nbl->ncj > ncj_old_i)
4707 work->buffer_flags.flag[(gridi->cell0+ci)>>gridi_flag_shift] = 1U<<th;
4711 work->ndistc = ndistc;
4713 nbs_cycle_stop(&work->cc[enbsCCsearch]);
4717 fprintf(debug, "number of distance checks %d\n", ndistc);
4718 fprintf(debug, "ncpcheck %s %d\n", gridi == gridj ? "local" : "non-local",
4723 print_nblist_statistics_simple(debug, nbl, nbs, rlist);
4727 print_nblist_statistics_supersub(debug, nbl, nbs, rlist);
4733 static void reduce_buffer_flags(const nbnxn_search_t nbs,
4735 const nbnxn_buffer_flags_t *dest)
4738 const unsigned *flag;
4740 for (s = 0; s < nsrc; s++)
4742 flag = nbs->work[s].buffer_flags.flag;
4744 for (b = 0; b < dest->nflag; b++)
4746 dest->flag[b] |= flag[b];
4751 static void print_reduction_cost(const nbnxn_buffer_flags_t *flags, int nout)
4753 int nelem, nkeep, ncopy, nred, b, c, out;
4759 for (b = 0; b < flags->nflag; b++)
4761 if (flags->flag[b] == 1)
4763 /* Only flag 0 is set, no copy of reduction required */
4767 else if (flags->flag[b] > 0)
4770 for (out = 0; out < nout; out++)
4772 if (flags->flag[b] & (1U<<out))
4789 fprintf(debug, "nbnxn reduction: #flag %d #list %d elem %4.2f, keep %4.2f copy %4.2f red %4.2f\n",
4791 nelem/(double)(flags->nflag),
4792 nkeep/(double)(flags->nflag),
4793 ncopy/(double)(flags->nflag),
4794 nred/(double)(flags->nflag));
4797 /* Perform a count (linear) sort to sort the smaller lists to the end.
4798 * This avoids load imbalance on the GPU, as large lists will be
4799 * scheduled and executed first and the smaller lists later.
4800 * Load balancing between multi-processors only happens at the end
4801 * and there smaller lists lead to more effective load balancing.
4802 * The sorting is done on the cj4 count, not on the actual pair counts.
4803 * Not only does this make the sort faster, but it also results in
4804 * better load balancing than using a list sorted on exact load.
4805 * This function swaps the pointer in the pair list to avoid a copy operation.
4807 static void sort_sci(nbnxn_pairlist_t *nbl)
4809 nbnxn_list_work_t *work;
4810 int m, i, s, s0, s1;
4811 nbnxn_sci_t *sci_sort;
4813 if (nbl->ncj4 <= nbl->nsci)
4815 /* nsci = 0 or all sci have size 1, sorting won't change the order */
4821 /* We will distinguish differences up to double the average */
4822 m = (2*nbl->ncj4)/nbl->nsci;
4824 if (m + 1 > work->sort_nalloc)
4826 work->sort_nalloc = over_alloc_large(m + 1);
4827 srenew(work->sort, work->sort_nalloc);
4830 if (work->sci_sort_nalloc != nbl->sci_nalloc)
4832 work->sci_sort_nalloc = nbl->sci_nalloc;
4833 nbnxn_realloc_void((void **)&work->sci_sort,
4835 work->sci_sort_nalloc*sizeof(*work->sci_sort),
4836 nbl->alloc, nbl->free);
4839 /* Count the entries of each size */
4840 for (i = 0; i <= m; i++)
4844 for (s = 0; s < nbl->nsci; s++)
4846 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4849 /* Calculate the offset for each count */
4852 for (i = m - 1; i >= 0; i--)
4855 work->sort[i] = work->sort[i + 1] + s0;
4859 /* Sort entries directly into place */
4860 sci_sort = work->sci_sort;
4861 for (s = 0; s < nbl->nsci; s++)
4863 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4864 sci_sort[work->sort[i]++] = nbl->sci[s];
4867 /* Swap the sci pointers so we use the new, sorted list */
4868 work->sci_sort = nbl->sci;
4869 nbl->sci = sci_sort;
4872 /* Make a local or non-local pair-list, depending on iloc */
4873 void nbnxn_make_pairlist(const nbnxn_search_t nbs,
4874 nbnxn_atomdata_t *nbat,
4875 const t_blocka *excl,
4877 int min_ci_balanced,
4878 nbnxn_pairlist_set_t *nbl_list,
4883 nbnxn_grid_t *gridi, *gridj;
4885 int nzi, zi, zj0, zj1, zj;
4889 nbnxn_pairlist_t **nbl;
4891 gmx_bool CombineNBLists;
4893 int np_tot, np_noq, np_hlj, nap;
4895 /* Check if we are running hybrid GPU + CPU nbnxn mode */
4896 bGPUCPU = (!nbs->grid[0].bSimple && nbl_list->bSimple);
4898 nnbl = nbl_list->nnbl;
4899 nbl = nbl_list->nbl;
4900 CombineNBLists = nbl_list->bCombined;
4904 fprintf(debug, "ns making %d nblists\n", nnbl);
4907 nbat->bUseBufferFlags = (nbat->nout > 1);
4908 /* We should re-init the flags before making the first list */
4909 if (nbat->bUseBufferFlags && (LOCAL_I(iloc) || bGPUCPU))
4911 init_buffer_flags(&nbat->buffer_flags, nbat->natoms);
4914 if (nbl_list->bSimple)
4916 switch (nb_kernel_type)
4918 #ifdef GMX_NBNXN_SIMD_4XN
4919 case nbnxnk4xN_SIMD_4xN:
4920 nbs->icell_set_x = icell_set_x_simd_4xn;
4923 #ifdef GMX_NBNXN_SIMD_2XNN
4924 case nbnxnk4xN_SIMD_2xNN:
4925 nbs->icell_set_x = icell_set_x_simd_2xnn;
4929 nbs->icell_set_x = icell_set_x_simple;
4935 #ifdef NBNXN_SEARCH_BB_SSE
4936 nbs->icell_set_x = icell_set_x_supersub_sse8;
4938 nbs->icell_set_x = icell_set_x_supersub;
4944 /* Only zone (grid) 0 vs 0 */
4951 nzi = nbs->zones->nizone;
4954 if (!nbl_list->bSimple && min_ci_balanced > 0)
4956 nsubpair_max = get_nsubpair_max(nbs, iloc, rlist, min_ci_balanced);
4963 /* Clear all pair-lists */
4964 for (th = 0; th < nnbl; th++)
4966 clear_pairlist(nbl[th]);
4969 for (zi = 0; zi < nzi; zi++)
4971 gridi = &nbs->grid[zi];
4973 if (NONLOCAL_I(iloc))
4975 zj0 = nbs->zones->izone[zi].j0;
4976 zj1 = nbs->zones->izone[zi].j1;
4982 for (zj = zj0; zj < zj1; zj++)
4984 gridj = &nbs->grid[zj];
4988 fprintf(debug, "ns search grid %d vs %d\n", zi, zj);
4991 nbs_cycle_start(&nbs->cc[enbsCCsearch]);
4993 if (nbl[0]->bSimple && !gridi->bSimple)
4995 /* Hybrid list, determine blocking later */
5000 ci_block = get_ci_block_size(gridi, nbs->DomDec, nnbl);
5003 #pragma omp parallel for num_threads(nnbl) schedule(static)
5004 for (th = 0; th < nnbl; th++)
5006 /* Re-init the thread-local work flag data before making
5007 * the first list (not an elegant conditional).
5009 if (nbat->bUseBufferFlags && ((zi == 0 && zj == 0) ||
5010 (bGPUCPU && zi == 0 && zj == 1)))
5012 init_buffer_flags(&nbs->work[th].buffer_flags, nbat->natoms);
5015 if (CombineNBLists && th > 0)
5017 clear_pairlist(nbl[th]);
5020 /* With GPU: generate progressively smaller lists for
5021 * load balancing for local only or non-local with 2 zones.
5023 progBal = (LOCAL_I(iloc) || nbs->zones->n <= 2);
5025 /* Divide the i super cell equally over the nblists */
5026 nbnxn_make_pairlist_part(nbs, gridi, gridj,
5027 &nbs->work[th], nbat, excl,
5031 nbat->bUseBufferFlags,
5033 progBal, min_ci_balanced,
5037 nbs_cycle_stop(&nbs->cc[enbsCCsearch]);
5042 for (th = 0; th < nnbl; th++)
5044 inc_nrnb(nrnb, eNR_NBNXN_DIST2, nbs->work[th].ndistc);
5046 if (nbl_list->bSimple)
5048 np_tot += nbl[th]->ncj;
5049 np_noq += nbl[th]->work->ncj_noq;
5050 np_hlj += nbl[th]->work->ncj_hlj;
5054 /* This count ignores potential subsequent pair pruning */
5055 np_tot += nbl[th]->nci_tot;
5058 nap = nbl[0]->na_ci*nbl[0]->na_cj;
5059 nbl_list->natpair_ljq = (np_tot - np_noq)*nap - np_hlj*nap/2;
5060 nbl_list->natpair_lj = np_noq*nap;
5061 nbl_list->natpair_q = np_hlj*nap/2;
5063 if (CombineNBLists && nnbl > 1)
5065 nbs_cycle_start(&nbs->cc[enbsCCcombine]);
5067 combine_nblists(nnbl-1, nbl+1, nbl[0]);
5069 nbs_cycle_stop(&nbs->cc[enbsCCcombine]);
5074 if (!nbl_list->bSimple)
5076 /* Sort the entries on size, large ones first */
5077 if (CombineNBLists || nnbl == 1)
5083 #pragma omp parallel for num_threads(nnbl) schedule(static)
5084 for (th = 0; th < nnbl; th++)
5091 if (nbat->bUseBufferFlags)
5093 reduce_buffer_flags(nbs, nnbl, &nbat->buffer_flags);
5096 /* Special performance logging stuff (env.var. GMX_NBNXN_CYCLE) */
5099 nbs->search_count++;
5101 if (nbs->print_cycles &&
5102 (!nbs->DomDec || (nbs->DomDec && !LOCAL_I(iloc))) &&
5103 nbs->search_count % 100 == 0)
5105 nbs_cycle_print(stderr, nbs);
5108 if (debug && (CombineNBLists && nnbl > 1))
5110 if (nbl[0]->bSimple)
5112 print_nblist_statistics_simple(debug, nbl[0], nbs, rlist);
5116 print_nblist_statistics_supersub(debug, nbl[0], nbs, rlist);
5124 if (nbl[0]->bSimple)
5126 print_nblist_ci_cj(debug, nbl[0]);
5130 print_nblist_sci_cj(debug, nbl[0]);
5134 if (nbat->bUseBufferFlags)
5136 print_reduction_cost(&nbat->buffer_flags, nnbl);