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 /* nbnxn_internal.h included gmx_simd_macros.h */
47 #include "nbnxn_internal.h"
49 #include "gmx_simd_vec.h"
51 #include "nbnxn_atomdata.h"
52 #include "nbnxn_search.h"
53 #include "gmx_cyclecounter.h"
55 #include "gmx_omp_nthreads.h"
59 /* Pair search box lower and upper corner in x,y,z.
60 * Store this in 4 iso 3 reals, which is useful with SSE.
61 * To avoid complicating the code we also use 4 without SSE.
64 #define NNBSBB_B (2*NNBSBB_C)
65 /* Pair search box lower and upper bound in z only. */
67 /* Pair search box lower and upper corner x,y,z indices */
76 #ifdef NBNXN_SEARCH_BB_SSE
77 /* We use SSE or AVX-128bit for bounding box calculations */
80 /* Single precision BBs + coordinates, we can also load coordinates using SSE */
81 #define NBNXN_SEARCH_SSE_SINGLE
84 /* Include basic SSE2 stuff */
85 #include <emmintrin.h>
87 #if defined NBNXN_SEARCH_SSE_SINGLE && (GPU_NSUBCELL == 4 || GPU_NSUBCELL == 8)
88 /* Store bounding boxes with x, y and z coordinates in packs of 4 */
92 /* The width of SSE/AVX128 with single precision for bounding boxes with GPU.
93 * Here AVX-256 turns out to be slightly slower than AVX-128.
96 #define STRIDE_PBB_2LOG 2
98 #endif /* NBNXN_SEARCH_BB_SSE */
100 #ifdef GMX_NBNXN_SIMD
102 /* The functions below are macros as they are performance sensitive */
104 /* 4x4 list, pack=4: no complex conversion required */
105 /* i-cluster to j-cluster conversion */
106 #define CI_TO_CJ_J4(ci) (ci)
107 /* cluster index to coordinate array index conversion */
108 #define X_IND_CI_J4(ci) ((ci)*STRIDE_P4)
109 #define X_IND_CJ_J4(cj) ((cj)*STRIDE_P4)
111 /* 4x2 list, pack=4: j-cluster size is half the packing width */
112 /* i-cluster to j-cluster conversion */
113 #define CI_TO_CJ_J2(ci) ((ci)<<1)
114 /* cluster index to coordinate array index conversion */
115 #define X_IND_CI_J2(ci) ((ci)*STRIDE_P4)
116 #define X_IND_CJ_J2(cj) (((cj)>>1)*STRIDE_P4 + ((cj) & 1)*(PACK_X4>>1))
118 /* 4x8 list, pack=8: i-cluster size is half the packing width */
119 /* i-cluster to j-cluster conversion */
120 #define CI_TO_CJ_J8(ci) ((ci)>>1)
121 /* cluster index to coordinate array index conversion */
122 #define X_IND_CI_J8(ci) (((ci)>>1)*STRIDE_P8 + ((ci) & 1)*(PACK_X8>>1))
123 #define X_IND_CJ_J8(cj) ((cj)*STRIDE_P8)
125 /* The j-cluster size is matched to the SIMD width */
126 #if GMX_SIMD_WIDTH_HERE == 2
127 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J2(ci)
128 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J2(ci)
129 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J2(cj)
131 #if GMX_SIMD_WIDTH_HERE == 4
132 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J4(ci)
133 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J4(ci)
134 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J4(cj)
136 #if GMX_SIMD_WIDTH_HERE == 8
137 #define CI_TO_CJ_SIMD_4XN(ci) CI_TO_CJ_J8(ci)
138 #define X_IND_CI_SIMD_4XN(ci) X_IND_CI_J8(ci)
139 #define X_IND_CJ_SIMD_4XN(cj) X_IND_CJ_J8(cj)
140 /* Half SIMD with j-cluster size */
141 #define CI_TO_CJ_SIMD_2XNN(ci) CI_TO_CJ_J4(ci)
142 #define X_IND_CI_SIMD_2XNN(ci) X_IND_CI_J4(ci)
143 #define X_IND_CJ_SIMD_2XNN(cj) X_IND_CJ_J4(cj)
145 #if GMX_SIMD_WIDTH_HERE == 16
146 #define CI_TO_CJ_SIMD_2XNN(ci) CI_TO_CJ_J8(ci)
147 #define X_IND_CI_SIMD_2XNN(ci) X_IND_CI_J8(ci)
148 #define X_IND_CJ_SIMD_2XNN(cj) X_IND_CJ_J8(cj)
150 #error "unsupported GMX_NBNXN_SIMD_WIDTH"
156 #endif /* GMX_NBNXN_SIMD */
159 #ifdef NBNXN_SEARCH_BB_SSE
160 /* Store bounding boxes corners as quadruplets: xxxxyyyyzzzz */
162 /* Size of bounding box corners quadruplet */
163 #define NNBSBB_XXXX (NNBSBB_D*DIM*STRIDE_PBB)
166 /* We shift the i-particles backward for PBC.
167 * This leads to more conditionals than shifting forward.
168 * We do this to get more balanced pair lists.
170 #define NBNXN_SHIFT_BACKWARD
173 /* This define is a lazy way to avoid interdependence of the grid
174 * and searching data structures.
176 #define NBNXN_NA_SC_MAX (GPU_NSUBCELL*NBNXN_GPU_CLUSTER_SIZE)
179 static void nbs_cycle_clear(nbnxn_cycle_t *cc)
183 for (i = 0; i < enbsCCnr; i++)
190 static double Mcyc_av(const nbnxn_cycle_t *cc)
192 return (double)cc->c*1e-6/cc->count;
195 static void nbs_cycle_print(FILE *fp, const nbnxn_search_t nbs)
201 fprintf(fp, "ns %4d grid %4.1f search %4.1f red.f %5.3f",
202 nbs->cc[enbsCCgrid].count,
203 Mcyc_av(&nbs->cc[enbsCCgrid]),
204 Mcyc_av(&nbs->cc[enbsCCsearch]),
205 Mcyc_av(&nbs->cc[enbsCCreducef]));
207 if (nbs->nthread_max > 1)
209 if (nbs->cc[enbsCCcombine].count > 0)
211 fprintf(fp, " comb %5.2f",
212 Mcyc_av(&nbs->cc[enbsCCcombine]));
214 fprintf(fp, " s. th");
215 for (t = 0; t < nbs->nthread_max; t++)
217 fprintf(fp, " %4.1f",
218 Mcyc_av(&nbs->work[t].cc[enbsCCsearch]));
224 static void nbnxn_grid_init(nbnxn_grid_t * grid)
227 grid->cxy_ind = NULL;
228 grid->cxy_nalloc = 0;
234 static int get_2log(int n)
239 while ((1<<log2) < n)
245 gmx_fatal(FARGS, "nbnxn na_c (%d) is not a power of 2", n);
251 static int nbnxn_kernel_to_ci_size(int nb_kernel_type)
253 switch (nb_kernel_type)
255 case nbnxnk4x4_PlainC:
256 case nbnxnk4xN_SIMD_4xN:
257 case nbnxnk4xN_SIMD_2xNN:
258 return NBNXN_CPU_CLUSTER_I_SIZE;
259 case nbnxnk8x8x8_CUDA:
260 case nbnxnk8x8x8_PlainC:
261 /* The cluster size for super/sub lists is only set here.
262 * Any value should work for the pair-search and atomdata code.
263 * The kernels, of course, might require a particular value.
265 return NBNXN_GPU_CLUSTER_SIZE;
267 gmx_incons("unknown kernel type");
273 int nbnxn_kernel_to_cj_size(int nb_kernel_type)
275 int nbnxn_simd_width = 0;
278 #ifdef GMX_NBNXN_SIMD
279 nbnxn_simd_width = GMX_SIMD_WIDTH_HERE;
282 switch (nb_kernel_type)
284 case nbnxnk4x4_PlainC:
285 cj_size = NBNXN_CPU_CLUSTER_I_SIZE;
287 case nbnxnk4xN_SIMD_4xN:
288 cj_size = nbnxn_simd_width;
290 case nbnxnk4xN_SIMD_2xNN:
291 cj_size = nbnxn_simd_width/2;
293 case nbnxnk8x8x8_CUDA:
294 case nbnxnk8x8x8_PlainC:
295 cj_size = nbnxn_kernel_to_ci_size(nb_kernel_type);
298 gmx_incons("unknown kernel type");
304 static int ci_to_cj(int na_cj_2log, int ci)
308 case 2: return ci; break;
309 case 1: return (ci<<1); break;
310 case 3: return (ci>>1); break;
316 gmx_bool nbnxn_kernel_pairlist_simple(int nb_kernel_type)
318 if (nb_kernel_type == nbnxnkNotSet)
320 gmx_fatal(FARGS, "Non-bonded kernel type not set for Verlet-style pair-list.");
323 switch (nb_kernel_type)
325 case nbnxnk8x8x8_CUDA:
326 case nbnxnk8x8x8_PlainC:
329 case nbnxnk4x4_PlainC:
330 case nbnxnk4xN_SIMD_4xN:
331 case nbnxnk4xN_SIMD_2xNN:
335 gmx_incons("Invalid nonbonded kernel type passed!");
340 void nbnxn_init_search(nbnxn_search_t * nbs_ptr,
342 gmx_domdec_zones_t *zones,
351 nbs->DomDec = (n_dd_cells != NULL);
353 clear_ivec(nbs->dd_dim);
359 for (d = 0; d < DIM; d++)
361 if ((*n_dd_cells)[d] > 1)
364 /* Each grid matches a DD zone */
370 snew(nbs->grid, nbs->ngrid);
371 for (g = 0; g < nbs->ngrid; g++)
373 nbnxn_grid_init(&nbs->grid[g]);
376 nbs->cell_nalloc = 0;
380 nbs->nthread_max = nthread_max;
382 /* Initialize the work data structures for each thread */
383 snew(nbs->work, nbs->nthread_max);
384 for (t = 0; t < nbs->nthread_max; t++)
386 nbs->work[t].cxy_na = NULL;
387 nbs->work[t].cxy_na_nalloc = 0;
388 nbs->work[t].sort_work = NULL;
389 nbs->work[t].sort_work_nalloc = 0;
392 /* Initialize detailed nbsearch cycle counting */
393 nbs->print_cycles = (getenv("GMX_NBNXN_CYCLE") != 0);
394 nbs->search_count = 0;
395 nbs_cycle_clear(nbs->cc);
396 for (t = 0; t < nbs->nthread_max; t++)
398 nbs_cycle_clear(nbs->work[t].cc);
402 static real grid_atom_density(int n, rvec corner0, rvec corner1)
406 rvec_sub(corner1, corner0, size);
408 return n/(size[XX]*size[YY]*size[ZZ]);
411 static int set_grid_size_xy(const nbnxn_search_t nbs,
414 int n, rvec corner0, rvec corner1,
419 real adens, tlen, tlen_x, tlen_y, nc_max;
422 rvec_sub(corner1, corner0, size);
426 /* target cell length */
429 /* To minimize the zero interactions, we should make
430 * the largest of the i/j cell cubic.
432 na_c = max(grid->na_c, grid->na_cj);
434 /* Approximately cubic cells */
435 tlen = pow(na_c/atom_density, 1.0/3.0);
441 /* Approximately cubic sub cells */
442 tlen = pow(grid->na_c/atom_density, 1.0/3.0);
443 tlen_x = tlen*GPU_NSUBCELL_X;
444 tlen_y = tlen*GPU_NSUBCELL_Y;
446 /* We round ncx and ncy down, because we get less cell pairs
447 * in the nbsist when the fixed cell dimensions (x,y) are
448 * larger than the variable one (z) than the other way around.
450 grid->ncx = max(1, (int)(size[XX]/tlen_x));
451 grid->ncy = max(1, (int)(size[YY]/tlen_y));
459 grid->sx = size[XX]/grid->ncx;
460 grid->sy = size[YY]/grid->ncy;
461 grid->inv_sx = 1/grid->sx;
462 grid->inv_sy = 1/grid->sy;
466 /* This is a non-home zone, add an extra row of cells
467 * for particles communicated for bonded interactions.
468 * These can be beyond the cut-off. It doesn't matter where
469 * they end up on the grid, but for performance it's better
470 * if they don't end up in cells that can be within cut-off range.
476 /* We need one additional cell entry for particles moved by DD */
477 if (grid->ncx*grid->ncy+1 > grid->cxy_nalloc)
479 grid->cxy_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
480 srenew(grid->cxy_na, grid->cxy_nalloc);
481 srenew(grid->cxy_ind, grid->cxy_nalloc+1);
483 for (t = 0; t < nbs->nthread_max; t++)
485 if (grid->ncx*grid->ncy+1 > nbs->work[t].cxy_na_nalloc)
487 nbs->work[t].cxy_na_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
488 srenew(nbs->work[t].cxy_na, nbs->work[t].cxy_na_nalloc);
492 /* Worst case scenario of 1 atom in each last cell */
493 if (grid->na_cj <= grid->na_c)
495 nc_max = n/grid->na_sc + grid->ncx*grid->ncy;
499 nc_max = n/grid->na_sc + grid->ncx*grid->ncy*grid->na_cj/grid->na_c;
502 if (nc_max > grid->nc_nalloc)
506 grid->nc_nalloc = over_alloc_large(nc_max);
507 srenew(grid->nsubc, grid->nc_nalloc);
508 srenew(grid->bbcz, grid->nc_nalloc*NNBSBB_D);
510 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX;
512 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL*NNBSBB_B;
514 sfree_aligned(grid->bb);
515 /* This snew also zeros the contents, this avoid possible
516 * floating exceptions in SSE with the unused bb elements.
518 snew_aligned(grid->bb, bb_nalloc, 16);
522 if (grid->na_cj == grid->na_c)
524 grid->bbj = grid->bb;
528 sfree_aligned(grid->bbj);
529 snew_aligned(grid->bbj, bb_nalloc*grid->na_c/grid->na_cj, 16);
533 srenew(grid->flags, grid->nc_nalloc);
536 copy_rvec(corner0, grid->c0);
537 copy_rvec(corner1, grid->c1);
542 /* We need to sort paricles in grid columns on z-coordinate.
543 * As particle are very often distributed homogeneously, we a sorting
544 * algorithm similar to pigeonhole sort. We multiply the z-coordinate
545 * by a factor, cast to an int and try to store in that hole. If the hole
546 * is full, we move this or another particle. A second pass is needed to make
547 * contiguous elements. SORT_GRID_OVERSIZE is the ratio of holes to particles.
548 * 4 is the optimal value for homogeneous particle distribution and allows
549 * for an O(#particles) sort up till distributions were all particles are
550 * concentrated in 1/4 of the space. No NlogN fallback is implemented,
551 * as it can be expensive to detect imhomogeneous particle distributions.
552 * SGSF is the maximum ratio of holes used, in the worst case all particles
553 * end up in the last hole and we need #particles extra holes at the end.
555 #define SORT_GRID_OVERSIZE 4
556 #define SGSF (SORT_GRID_OVERSIZE + 1)
558 /* Sort particle index a on coordinates x along dim.
559 * Backwards tells if we want decreasing iso increasing coordinates.
560 * h0 is the minimum of the coordinate range.
561 * invh is the 1/length of the sorting range.
562 * n_per_h (>=n) is the expected average number of particles per 1/invh
563 * sort is the sorting work array.
564 * sort should have a size of at least n_per_h*SORT_GRID_OVERSIZE + n,
565 * or easier, allocate at least n*SGSF elements.
567 static void sort_atoms(int dim, gmx_bool Backwards,
568 int *a, int n, rvec *x,
569 real h0, real invh, int n_per_h,
573 int zi, zim, zi_min, zi_max;
585 gmx_incons("n > n_per_h");
589 /* Transform the inverse range height into the inverse hole height */
590 invh *= n_per_h*SORT_GRID_OVERSIZE;
592 /* Set nsort to the maximum possible number of holes used.
593 * In worst case all n elements end up in the last bin.
595 nsort = n_per_h*SORT_GRID_OVERSIZE + n;
597 /* Determine the index range used, so we can limit it for the second pass */
601 /* Sort the particles using a simple index sort */
602 for (i = 0; i < n; i++)
604 /* The cast takes care of float-point rounding effects below zero.
605 * This code assumes particles are less than 1/SORT_GRID_OVERSIZE
606 * times the box height out of the box.
608 zi = (int)((x[a[i]][dim] - h0)*invh);
611 /* As we can have rounding effect, we use > iso >= here */
612 if (zi < 0 || zi > n_per_h*SORT_GRID_OVERSIZE)
614 gmx_fatal(FARGS, "(int)((x[%d][%c]=%f - %f)*%f) = %d, not in 0 - %d*%d\n",
615 a[i], 'x'+dim, x[a[i]][dim], h0, invh, zi,
616 n_per_h, SORT_GRID_OVERSIZE);
620 /* Ideally this particle should go in sort cell zi,
621 * but that might already be in use,
622 * in that case find the first empty cell higher up
627 zi_min = min(zi_min, zi);
628 zi_max = max(zi_max, zi);
632 /* We have multiple atoms in the same sorting slot.
633 * Sort on real z for minimal bounding box size.
634 * There is an extra check for identical z to ensure
635 * well-defined output order, independent of input order
636 * to ensure binary reproducibility after restarts.
638 while (sort[zi] >= 0 && ( x[a[i]][dim] > x[sort[zi]][dim] ||
639 (x[a[i]][dim] == x[sort[zi]][dim] &&
647 /* Shift all elements by one slot until we find an empty slot */
650 while (sort[zim] >= 0)
658 zi_max = max(zi_max, zim);
661 zi_max = max(zi_max, zi);
668 for (zi = 0; zi < nsort; zi++)
679 for (zi = zi_max; zi >= zi_min; zi--)
690 gmx_incons("Lost particles while sorting");
695 #define R2F_D(x) ((float)((x) >= 0 ? ((1-GMX_FLOAT_EPS)*(x)) : ((1+GMX_FLOAT_EPS)*(x))))
696 #define R2F_U(x) ((float)((x) >= 0 ? ((1+GMX_FLOAT_EPS)*(x)) : ((1-GMX_FLOAT_EPS)*(x))))
702 /* Coordinate order x,y,z, bb order xyz0 */
703 static void calc_bounding_box(int na, int stride, const real *x, float *bb)
706 real xl, xh, yl, yh, zl, zh;
716 for (j = 1; j < na; j++)
718 xl = min(xl, x[i+XX]);
719 xh = max(xh, x[i+XX]);
720 yl = min(yl, x[i+YY]);
721 yh = max(yh, x[i+YY]);
722 zl = min(zl, x[i+ZZ]);
723 zh = max(zh, x[i+ZZ]);
726 /* Note: possible double to float conversion here */
727 bb[BBL_X] = R2F_D(xl);
728 bb[BBL_Y] = R2F_D(yl);
729 bb[BBL_Z] = R2F_D(zl);
730 bb[BBU_X] = R2F_U(xh);
731 bb[BBU_Y] = R2F_U(yh);
732 bb[BBU_Z] = R2F_U(zh);
735 /* Packed coordinates, bb order xyz0 */
736 static void calc_bounding_box_x_x4(int na, const real *x, float *bb)
739 real xl, xh, yl, yh, zl, zh;
747 for (j = 1; j < na; j++)
749 xl = min(xl, x[j+XX*PACK_X4]);
750 xh = max(xh, x[j+XX*PACK_X4]);
751 yl = min(yl, x[j+YY*PACK_X4]);
752 yh = max(yh, x[j+YY*PACK_X4]);
753 zl = min(zl, x[j+ZZ*PACK_X4]);
754 zh = max(zh, x[j+ZZ*PACK_X4]);
756 /* Note: possible double to float conversion here */
757 bb[BBL_X] = R2F_D(xl);
758 bb[BBL_Y] = R2F_D(yl);
759 bb[BBL_Z] = R2F_D(zl);
760 bb[BBU_X] = R2F_U(xh);
761 bb[BBU_Y] = R2F_U(yh);
762 bb[BBU_Z] = R2F_U(zh);
765 /* Packed coordinates, bb order xyz0 */
766 static void calc_bounding_box_x_x8(int na, const real *x, float *bb)
769 real xl, xh, yl, yh, zl, zh;
777 for (j = 1; j < na; j++)
779 xl = min(xl, x[j+XX*PACK_X8]);
780 xh = max(xh, x[j+XX*PACK_X8]);
781 yl = min(yl, x[j+YY*PACK_X8]);
782 yh = max(yh, x[j+YY*PACK_X8]);
783 zl = min(zl, x[j+ZZ*PACK_X8]);
784 zh = max(zh, x[j+ZZ*PACK_X8]);
786 /* Note: possible double to float conversion here */
787 bb[BBL_X] = R2F_D(xl);
788 bb[BBL_Y] = R2F_D(yl);
789 bb[BBL_Z] = R2F_D(zl);
790 bb[BBU_X] = R2F_U(xh);
791 bb[BBU_Y] = R2F_U(yh);
792 bb[BBU_Z] = R2F_U(zh);
795 /* Packed coordinates, bb order xyz0 */
796 static void calc_bounding_box_x_x4_halves(int na, const real *x,
797 float *bb, float *bbj)
799 #ifndef NBNXN_SEARCH_BB_SSE
803 calc_bounding_box_x_x4(min(na, 2), x, bbj);
807 calc_bounding_box_x_x4(min(na-2, 2), x+(PACK_X4>>1), bbj+NNBSBB_B);
811 /* Set the "empty" bounding box to the same as the first one,
812 * so we don't need to treat special cases in the rest of the code.
814 #ifdef NBNXN_SEARCH_BB_SSE
815 _mm_store_ps(bbj+NNBSBB_B, _mm_load_ps(bbj));
816 _mm_store_ps(bbj+NNBSBB_B+NNBSBB_C, _mm_load_ps(bbj+NNBSBB_C));
818 for (i = 0; i < NNBSBB_B; i++)
820 bbj[NNBSBB_B + i] = bbj[i];
825 #ifdef NBNXN_SEARCH_BB_SSE
826 _mm_store_ps(bb, _mm_min_ps(_mm_load_ps(bbj),
827 _mm_load_ps(bbj+NNBSBB_B)));
828 _mm_store_ps(bb+NNBSBB_C, _mm_max_ps(_mm_load_ps(bbj+NNBSBB_C),
829 _mm_load_ps(bbj+NNBSBB_B+NNBSBB_C)));
831 for (i = 0; i < NNBSBB_C; i++)
833 bb[ i] = min(bbj[ i], bbj[NNBSBB_B + i]);
834 bb[NNBSBB_C + i] = max(bbj[NNBSBB_C + i], bbj[NNBSBB_B + NNBSBB_C + i]);
839 #ifdef NBNXN_SEARCH_BB_SSE
841 /* Coordinate order xyz, bb order xxxxyyyyzzzz */
842 static void calc_bounding_box_xxxx(int na, int stride, const real *x, float *bb)
845 real xl, xh, yl, yh, zl, zh;
855 for (j = 1; j < na; j++)
857 xl = min(xl, x[i+XX]);
858 xh = max(xh, x[i+XX]);
859 yl = min(yl, x[i+YY]);
860 yh = max(yh, x[i+YY]);
861 zl = min(zl, x[i+ZZ]);
862 zh = max(zh, x[i+ZZ]);
865 /* Note: possible double to float conversion here */
866 bb[0*STRIDE_PBB] = R2F_D(xl);
867 bb[1*STRIDE_PBB] = R2F_D(yl);
868 bb[2*STRIDE_PBB] = R2F_D(zl);
869 bb[3*STRIDE_PBB] = R2F_U(xh);
870 bb[4*STRIDE_PBB] = R2F_U(yh);
871 bb[5*STRIDE_PBB] = R2F_U(zh);
874 #endif /* NBNXN_SEARCH_BB_SSE */
876 #ifdef NBNXN_SEARCH_SSE_SINGLE
878 /* Coordinate order xyz?, bb order xyz0 */
879 static void calc_bounding_box_sse(int na, const float *x, float *bb)
881 __m128 bb_0_SSE, bb_1_SSE;
886 bb_0_SSE = _mm_load_ps(x);
889 for (i = 1; i < na; i++)
891 x_SSE = _mm_load_ps(x+i*NNBSBB_C);
892 bb_0_SSE = _mm_min_ps(bb_0_SSE, x_SSE);
893 bb_1_SSE = _mm_max_ps(bb_1_SSE, x_SSE);
896 _mm_store_ps(bb, bb_0_SSE);
897 _mm_store_ps(bb+4, bb_1_SSE);
900 /* Coordinate order xyz?, bb order xxxxyyyyzzzz */
901 static void calc_bounding_box_xxxx_sse(int na, const float *x,
905 calc_bounding_box_sse(na, x, bb_work);
907 bb[0*STRIDE_PBB] = bb_work[BBL_X];
908 bb[1*STRIDE_PBB] = bb_work[BBL_Y];
909 bb[2*STRIDE_PBB] = bb_work[BBL_Z];
910 bb[3*STRIDE_PBB] = bb_work[BBU_X];
911 bb[4*STRIDE_PBB] = bb_work[BBU_Y];
912 bb[5*STRIDE_PBB] = bb_work[BBU_Z];
915 #endif /* NBNXN_SEARCH_SSE_SINGLE */
918 /* Combines pairs of consecutive bounding boxes */
919 static void combine_bounding_box_pairs(nbnxn_grid_t *grid, const float *bb)
921 int i, j, sc2, nc2, c2;
923 for (i = 0; i < grid->ncx*grid->ncy; i++)
925 /* Starting bb in a column is expected to be 2-aligned */
926 sc2 = grid->cxy_ind[i]>>1;
927 /* For odd numbers skip the last bb here */
928 nc2 = (grid->cxy_na[i]+3)>>(2+1);
929 for (c2 = sc2; c2 < sc2+nc2; c2++)
931 #ifdef NBNXN_SEARCH_BB_SSE
932 __m128 min_SSE, max_SSE;
934 min_SSE = _mm_min_ps(_mm_load_ps(bb+(c2*4+0)*NNBSBB_C),
935 _mm_load_ps(bb+(c2*4+2)*NNBSBB_C));
936 max_SSE = _mm_max_ps(_mm_load_ps(bb+(c2*4+1)*NNBSBB_C),
937 _mm_load_ps(bb+(c2*4+3)*NNBSBB_C));
938 _mm_store_ps(grid->bbj+(c2*2+0)*NNBSBB_C, min_SSE);
939 _mm_store_ps(grid->bbj+(c2*2+1)*NNBSBB_C, max_SSE);
941 for (j = 0; j < NNBSBB_C; j++)
943 grid->bbj[(c2*2+0)*NNBSBB_C+j] = min(bb[(c2*4+0)*NNBSBB_C+j],
944 bb[(c2*4+2)*NNBSBB_C+j]);
945 grid->bbj[(c2*2+1)*NNBSBB_C+j] = max(bb[(c2*4+1)*NNBSBB_C+j],
946 bb[(c2*4+3)*NNBSBB_C+j]);
950 if (((grid->cxy_na[i]+3)>>2) & 1)
952 /* Copy the last bb for odd bb count in this column */
953 for (j = 0; j < NNBSBB_C; j++)
955 grid->bbj[(c2*2+0)*NNBSBB_C+j] = bb[(c2*4+0)*NNBSBB_C+j];
956 grid->bbj[(c2*2+1)*NNBSBB_C+j] = bb[(c2*4+1)*NNBSBB_C+j];
963 /* Prints the average bb size, used for debug output */
964 static void print_bbsizes_simple(FILE *fp,
965 const nbnxn_search_t nbs,
966 const nbnxn_grid_t *grid)
972 for (c = 0; c < grid->nc; c++)
974 for (d = 0; d < DIM; d++)
976 ba[d] += grid->bb[c*NNBSBB_B+NNBSBB_C+d] - grid->bb[c*NNBSBB_B+d];
979 dsvmul(1.0/grid->nc, ba, ba);
981 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
982 nbs->box[XX][XX]/grid->ncx,
983 nbs->box[YY][YY]/grid->ncy,
984 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/grid->nc,
985 ba[XX], ba[YY], ba[ZZ],
986 ba[XX]*grid->ncx/nbs->box[XX][XX],
987 ba[YY]*grid->ncy/nbs->box[YY][YY],
988 ba[ZZ]*grid->nc/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
991 /* Prints the average bb size, used for debug output */
992 static void print_bbsizes_supersub(FILE *fp,
993 const nbnxn_search_t nbs,
994 const nbnxn_grid_t *grid)
1001 for (c = 0; c < grid->nc; c++)
1004 for (s = 0; s < grid->nsubc[c]; s += STRIDE_PBB)
1008 cs_w = (c*GPU_NSUBCELL + s)/STRIDE_PBB;
1009 for (i = 0; i < STRIDE_PBB; i++)
1011 for (d = 0; d < DIM; d++)
1014 grid->bb[cs_w*NNBSBB_XXXX+(DIM+d)*STRIDE_PBB+i] -
1015 grid->bb[cs_w*NNBSBB_XXXX+ d *STRIDE_PBB+i];
1020 for (s = 0; s < grid->nsubc[c]; s++)
1024 cs = c*GPU_NSUBCELL + s;
1025 for (d = 0; d < DIM; d++)
1028 grid->bb[cs*NNBSBB_B+NNBSBB_C+d] -
1029 grid->bb[cs*NNBSBB_B +d];
1033 ns += grid->nsubc[c];
1035 dsvmul(1.0/ns, ba, ba);
1037 fprintf(fp, "ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
1038 nbs->box[XX][XX]/(grid->ncx*GPU_NSUBCELL_X),
1039 nbs->box[YY][YY]/(grid->ncy*GPU_NSUBCELL_Y),
1040 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z),
1041 ba[XX], ba[YY], ba[ZZ],
1042 ba[XX]*grid->ncx*GPU_NSUBCELL_X/nbs->box[XX][XX],
1043 ba[YY]*grid->ncy*GPU_NSUBCELL_Y/nbs->box[YY][YY],
1044 ba[ZZ]*grid->nc*GPU_NSUBCELL_Z/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
1047 /* Potentially sorts atoms on LJ coefficients !=0 and ==0.
1048 * Also sets interaction flags.
1050 void sort_on_lj(int na_c,
1051 int a0, int a1, const int *atinfo,
1055 int subc, s, a, n1, n2, a_lj_max, i, j;
1056 int sort1[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1057 int sort2[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
1063 for (s = a0; s < a1; s += na_c)
1065 /* Make lists for this (sub-)cell on atoms with and without LJ */
1070 for (a = s; a < min(s+na_c, a1); a++)
1072 haveQ = haveQ || GET_CGINFO_HAS_Q(atinfo[order[a]]);
1074 if (GET_CGINFO_HAS_VDW(atinfo[order[a]]))
1076 sort1[n1++] = order[a];
1081 sort2[n2++] = order[a];
1085 /* If we don't have atom with LJ, there's nothing to sort */
1088 *flags |= NBNXN_CI_DO_LJ(subc);
1092 /* Only sort when strictly necessary. Ordering particles
1093 * Ordering particles can lead to less accurate summation
1094 * due to rounding, both for LJ and Coulomb interactions.
1096 if (2*(a_lj_max - s) >= na_c)
1098 for (i = 0; i < n1; i++)
1100 order[a0+i] = sort1[i];
1102 for (j = 0; j < n2; j++)
1104 order[a0+n1+j] = sort2[j];
1108 *flags |= NBNXN_CI_HALF_LJ(subc);
1113 *flags |= NBNXN_CI_DO_COUL(subc);
1119 /* Fill a pair search cell with atoms.
1120 * Potentially sorts atoms and sets the interaction flags.
1122 void fill_cell(const nbnxn_search_t nbs,
1124 nbnxn_atomdata_t *nbat,
1128 int sx, int sy, int sz,
1139 sort_on_lj(grid->na_c, a0, a1, atinfo, nbs->a,
1140 grid->flags+(a0>>grid->na_c_2log)-grid->cell0);
1143 /* Now we have sorted the atoms, set the cell indices */
1144 for (a = a0; a < a1; a++)
1146 nbs->cell[nbs->a[a]] = a;
1149 copy_rvec_to_nbat_real(nbs->a+a0, a1-a0, grid->na_c, x,
1150 nbat->XFormat, nbat->x, a0,
1153 if (nbat->XFormat == nbatX4)
1155 /* Store the bounding boxes as xyz.xyz. */
1156 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1157 bb_ptr = grid->bb + offset;
1159 #if defined GMX_NBNXN_SIMD && GMX_SIMD_WIDTH_HERE == 2
1160 if (2*grid->na_cj == grid->na_c)
1162 calc_bounding_box_x_x4_halves(na, nbat->x+X4_IND_A(a0), bb_ptr,
1163 grid->bbj+offset*2);
1168 calc_bounding_box_x_x4(na, nbat->x+X4_IND_A(a0), bb_ptr);
1171 else if (nbat->XFormat == nbatX8)
1173 /* Store the bounding boxes as xyz.xyz. */
1174 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1175 bb_ptr = grid->bb + offset;
1177 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(a0), bb_ptr);
1180 else if (!grid->bSimple)
1182 /* Store the bounding boxes in a format convenient
1183 * for SSE calculations: xxxxyyyyzzzz...
1187 ((a0-grid->cell0*grid->na_sc)>>(grid->na_c_2log+STRIDE_PBB_2LOG))*NNBSBB_XXXX +
1188 (((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log) & (STRIDE_PBB-1));
1190 #ifdef NBNXN_SEARCH_SSE_SINGLE
1191 if (nbat->XFormat == nbatXYZQ)
1193 calc_bounding_box_xxxx_sse(na, nbat->x+a0*nbat->xstride,
1199 calc_bounding_box_xxxx(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1204 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1206 bb_ptr[0*STRIDE_PBB], bb_ptr[3*STRIDE_PBB],
1207 bb_ptr[1*STRIDE_PBB], bb_ptr[4*STRIDE_PBB],
1208 bb_ptr[2*STRIDE_PBB], bb_ptr[5*STRIDE_PBB]);
1214 /* Store the bounding boxes as xyz.xyz. */
1215 bb_ptr = grid->bb+((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1217 calc_bounding_box(na, nbat->xstride, nbat->x+a0*nbat->xstride,
1223 bbo = (a0 - grid->cell0*grid->na_sc)/grid->na_c;
1224 fprintf(debug, "%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1226 (grid->bb+bbo*NNBSBB_B)[BBL_X],
1227 (grid->bb+bbo*NNBSBB_B)[BBU_X],
1228 (grid->bb+bbo*NNBSBB_B)[BBL_Y],
1229 (grid->bb+bbo*NNBSBB_B)[BBU_Y],
1230 (grid->bb+bbo*NNBSBB_B)[BBL_Z],
1231 (grid->bb+bbo*NNBSBB_B)[BBU_Z]);
1236 /* Spatially sort the atoms within one grid column */
1237 static void sort_columns_simple(const nbnxn_search_t nbs,
1243 nbnxn_atomdata_t *nbat,
1244 int cxy_start, int cxy_end,
1248 int cx, cy, cz, ncz, cfilled, c;
1249 int na, ash, ind, a;
1254 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1255 grid->cell0, cxy_start, cxy_end, a0, a1);
1258 /* Sort the atoms within each x,y column in 3 dimensions */
1259 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1262 cy = cxy - cx*grid->ncy;
1264 na = grid->cxy_na[cxy];
1265 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1266 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1268 /* Sort the atoms within each x,y column on z coordinate */
1269 sort_atoms(ZZ, FALSE,
1272 1.0/nbs->box[ZZ][ZZ], ncz*grid->na_sc,
1275 /* Fill the ncz cells in this column */
1276 cfilled = grid->cxy_ind[cxy];
1277 for (cz = 0; cz < ncz; cz++)
1279 c = grid->cxy_ind[cxy] + cz;
1281 ash_c = ash + cz*grid->na_sc;
1282 na_c = min(grid->na_sc, na-(ash_c-ash));
1284 fill_cell(nbs, grid, nbat,
1285 ash_c, ash_c+na_c, atinfo, x,
1286 grid->na_sc*cx + (dd_zone >> 2),
1287 grid->na_sc*cy + (dd_zone & 3),
1291 /* This copy to bbcz is not really necessary.
1292 * But it allows to use the same grid search code
1293 * for the simple and supersub cell setups.
1299 grid->bbcz[c*NNBSBB_D ] = grid->bb[cfilled*NNBSBB_B+2];
1300 grid->bbcz[c*NNBSBB_D+1] = grid->bb[cfilled*NNBSBB_B+6];
1303 /* Set the unused atom indices to -1 */
1304 for (ind = na; ind < ncz*grid->na_sc; ind++)
1306 nbs->a[ash+ind] = -1;
1311 /* Spatially sort the atoms within one grid column */
1312 static void sort_columns_supersub(const nbnxn_search_t nbs,
1318 nbnxn_atomdata_t *nbat,
1319 int cxy_start, int cxy_end,
1323 int cx, cy, cz = -1, c = -1, ncz;
1324 int na, ash, na_c, ind, a;
1325 int subdiv_z, sub_z, na_z, ash_z;
1326 int subdiv_y, sub_y, na_y, ash_y;
1327 int subdiv_x, sub_x, na_x, ash_x;
1329 /* cppcheck-suppress unassignedVariable */
1330 float bb_work_array[NNBSBB_B+3], *bb_work_align;
1332 bb_work_align = (float *)(((size_t)(bb_work_array+3)) & (~((size_t)15)));
1336 fprintf(debug, "cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1337 grid->cell0, cxy_start, cxy_end, a0, a1);
1340 subdiv_x = grid->na_c;
1341 subdiv_y = GPU_NSUBCELL_X*subdiv_x;
1342 subdiv_z = GPU_NSUBCELL_Y*subdiv_y;
1344 /* Sort the atoms within each x,y column in 3 dimensions */
1345 for (cxy = cxy_start; cxy < cxy_end; cxy++)
1348 cy = cxy - cx*grid->ncy;
1350 na = grid->cxy_na[cxy];
1351 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1352 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1354 /* Sort the atoms within each x,y column on z coordinate */
1355 sort_atoms(ZZ, FALSE,
1358 1.0/nbs->box[ZZ][ZZ], ncz*grid->na_sc,
1361 /* This loop goes over the supercells and subcells along z at once */
1362 for (sub_z = 0; sub_z < ncz*GPU_NSUBCELL_Z; sub_z++)
1364 ash_z = ash + sub_z*subdiv_z;
1365 na_z = min(subdiv_z, na-(ash_z-ash));
1367 /* We have already sorted on z */
1369 if (sub_z % GPU_NSUBCELL_Z == 0)
1371 cz = sub_z/GPU_NSUBCELL_Z;
1372 c = grid->cxy_ind[cxy] + cz;
1374 /* The number of atoms in this supercell */
1375 na_c = min(grid->na_sc, na-(ash_z-ash));
1377 grid->nsubc[c] = min(GPU_NSUBCELL, (na_c+grid->na_c-1)/grid->na_c);
1379 /* Store the z-boundaries of the super cell */
1380 grid->bbcz[c*NNBSBB_D ] = x[nbs->a[ash_z]][ZZ];
1381 grid->bbcz[c*NNBSBB_D+1] = x[nbs->a[ash_z+na_c-1]][ZZ];
1384 #if GPU_NSUBCELL_Y > 1
1385 /* Sort the atoms along y */
1386 sort_atoms(YY, (sub_z & 1),
1387 nbs->a+ash_z, na_z, x,
1388 grid->c0[YY]+cy*grid->sy,
1389 grid->inv_sy, subdiv_z,
1393 for (sub_y = 0; sub_y < GPU_NSUBCELL_Y; sub_y++)
1395 ash_y = ash_z + sub_y*subdiv_y;
1396 na_y = min(subdiv_y, na-(ash_y-ash));
1398 #if GPU_NSUBCELL_X > 1
1399 /* Sort the atoms along x */
1400 sort_atoms(XX, ((cz*GPU_NSUBCELL_Y + sub_y) & 1),
1401 nbs->a+ash_y, na_y, x,
1402 grid->c0[XX]+cx*grid->sx,
1403 grid->inv_sx, subdiv_y,
1407 for (sub_x = 0; sub_x < GPU_NSUBCELL_X; sub_x++)
1409 ash_x = ash_y + sub_x*subdiv_x;
1410 na_x = min(subdiv_x, na-(ash_x-ash));
1412 fill_cell(nbs, grid, nbat,
1413 ash_x, ash_x+na_x, atinfo, x,
1414 grid->na_c*(cx*GPU_NSUBCELL_X+sub_x) + (dd_zone >> 2),
1415 grid->na_c*(cy*GPU_NSUBCELL_Y+sub_y) + (dd_zone & 3),
1422 /* Set the unused atom indices to -1 */
1423 for (ind = na; ind < ncz*grid->na_sc; ind++)
1425 nbs->a[ash+ind] = -1;
1430 /* Determine in which grid column atoms should go */
1431 static void calc_column_indices(nbnxn_grid_t *grid,
1434 int dd_zone, const int *move,
1435 int thread, int nthread,
1442 /* We add one extra cell for particles which moved during DD */
1443 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1448 n0 = a0 + (int)((thread+0)*(a1 - a0))/nthread;
1449 n1 = a0 + (int)((thread+1)*(a1 - a0))/nthread;
1453 for (i = n0; i < n1; i++)
1455 if (move == NULL || move[i] >= 0)
1457 /* We need to be careful with rounding,
1458 * particles might be a few bits outside the local zone.
1459 * The int cast takes care of the lower bound,
1460 * we will explicitly take care of the upper bound.
1462 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1463 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1466 if (cx < 0 || cx > grid->ncx ||
1467 cy < 0 || cy > grid->ncy)
1470 "grid cell cx %d cy %d out of range (max %d %d)\n"
1471 "atom %f %f %f, grid->c0 %f %f",
1472 cx, cy, grid->ncx, grid->ncy,
1473 x[i][XX], x[i][YY], x[i][ZZ], grid->c0[XX], grid->c0[YY]);
1476 /* Take care of potential rouding issues */
1477 cx = min(cx, grid->ncx - 1);
1478 cy = min(cy, grid->ncy - 1);
1480 /* For the moment cell will contain only the, grid local,
1481 * x and y indices, not z.
1483 cell[i] = cx*grid->ncy + cy;
1487 /* Put this moved particle after the end of the grid,
1488 * so we can process it later without using conditionals.
1490 cell[i] = grid->ncx*grid->ncy;
1499 for (i = n0; i < n1; i++)
1501 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1502 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1504 /* For non-home zones there could be particles outside
1505 * the non-bonded cut-off range, which have been communicated
1506 * for bonded interactions only. For the result it doesn't
1507 * matter where these end up on the grid. For performance
1508 * we put them in an extra row at the border.
1511 cx = min(cx, grid->ncx - 1);
1513 cy = min(cy, grid->ncy - 1);
1515 /* For the moment cell will contain only the, grid local,
1516 * x and y indices, not z.
1518 cell[i] = cx*grid->ncy + cy;
1525 /* Determine in which grid cells the atoms should go */
1526 static void calc_cell_indices(const nbnxn_search_t nbs,
1533 nbnxn_atomdata_t *nbat)
1536 int cx, cy, cxy, ncz_max, ncz;
1537 int nthread, thread;
1538 int *cxy_na, cxy_na_i;
1540 nthread = gmx_omp_nthreads_get(emntPairsearch);
1542 #pragma omp parallel for num_threads(nthread) schedule(static)
1543 for (thread = 0; thread < nthread; thread++)
1545 calc_column_indices(grid, a0, a1, x, dd_zone, move, thread, nthread,
1546 nbs->cell, nbs->work[thread].cxy_na);
1549 /* Make the cell index as a function of x and y */
1552 grid->cxy_ind[0] = 0;
1553 for (i = 0; i < grid->ncx*grid->ncy+1; i++)
1555 /* We set ncz_max at the beginning of the loop iso at the end
1556 * to skip i=grid->ncx*grid->ncy which are moved particles
1557 * that do not need to be ordered on the grid.
1563 cxy_na_i = nbs->work[0].cxy_na[i];
1564 for (thread = 1; thread < nthread; thread++)
1566 cxy_na_i += nbs->work[thread].cxy_na[i];
1568 ncz = (cxy_na_i + grid->na_sc - 1)/grid->na_sc;
1569 if (nbat->XFormat == nbatX8)
1571 /* Make the number of cell a multiple of 2 */
1572 ncz = (ncz + 1) & ~1;
1574 grid->cxy_ind[i+1] = grid->cxy_ind[i] + ncz;
1575 /* Clear cxy_na, so we can reuse the array below */
1576 grid->cxy_na[i] = 0;
1578 grid->nc = grid->cxy_ind[grid->ncx*grid->ncy] - grid->cxy_ind[0];
1580 nbat->natoms = (grid->cell0 + grid->nc)*grid->na_sc;
1584 fprintf(debug, "ns na_sc %d na_c %d super-cells: %d x %d y %d z %.1f maxz %d\n",
1585 grid->na_sc, grid->na_c, grid->nc,
1586 grid->ncx, grid->ncy, grid->nc/((double)(grid->ncx*grid->ncy)),
1591 for (cy = 0; cy < grid->ncy; cy++)
1593 for (cx = 0; cx < grid->ncx; cx++)
1595 fprintf(debug, " %2d", grid->cxy_ind[i+1]-grid->cxy_ind[i]);
1598 fprintf(debug, "\n");
1603 /* Make sure the work array for sorting is large enough */
1604 if (ncz_max*grid->na_sc*SGSF > nbs->work[0].sort_work_nalloc)
1606 for (thread = 0; thread < nbs->nthread_max; thread++)
1608 nbs->work[thread].sort_work_nalloc =
1609 over_alloc_large(ncz_max*grid->na_sc*SGSF);
1610 srenew(nbs->work[thread].sort_work,
1611 nbs->work[thread].sort_work_nalloc);
1612 /* When not in use, all elements should be -1 */
1613 for (i = 0; i < nbs->work[thread].sort_work_nalloc; i++)
1615 nbs->work[thread].sort_work[i] = -1;
1620 /* Now we know the dimensions we can fill the grid.
1621 * This is the first, unsorted fill. We sort the columns after this.
1623 for (i = a0; i < a1; i++)
1625 /* At this point nbs->cell contains the local grid x,y indices */
1627 nbs->a[(grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc + grid->cxy_na[cxy]++] = i;
1632 /* Set the cell indices for the moved particles */
1633 n0 = grid->nc*grid->na_sc;
1634 n1 = grid->nc*grid->na_sc+grid->cxy_na[grid->ncx*grid->ncy];
1637 for (i = n0; i < n1; i++)
1639 nbs->cell[nbs->a[i]] = i;
1644 /* Sort the super-cell columns along z into the sub-cells. */
1645 #pragma omp parallel for num_threads(nbs->nthread_max) schedule(static)
1646 for (thread = 0; thread < nbs->nthread_max; thread++)
1650 sort_columns_simple(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1651 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1652 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1653 nbs->work[thread].sort_work);
1657 sort_columns_supersub(nbs, dd_zone, grid, a0, a1, atinfo, x, nbat,
1658 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1659 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1660 nbs->work[thread].sort_work);
1664 if (grid->bSimple && nbat->XFormat == nbatX8)
1666 combine_bounding_box_pairs(grid, grid->bb);
1671 grid->nsubc_tot = 0;
1672 for (i = 0; i < grid->nc; i++)
1674 grid->nsubc_tot += grid->nsubc[i];
1682 print_bbsizes_simple(debug, nbs, grid);
1686 fprintf(debug, "ns non-zero sub-cells: %d average atoms %.2f\n",
1687 grid->nsubc_tot, (a1-a0)/(double)grid->nsubc_tot);
1689 print_bbsizes_supersub(debug, nbs, grid);
1694 static void init_buffer_flags(nbnxn_buffer_flags_t *flags,
1699 flags->nflag = (natoms + NBNXN_BUFFERFLAG_SIZE - 1)/NBNXN_BUFFERFLAG_SIZE;
1700 if (flags->nflag > flags->flag_nalloc)
1702 flags->flag_nalloc = over_alloc_large(flags->nflag);
1703 srenew(flags->flag, flags->flag_nalloc);
1705 for (b = 0; b < flags->nflag; b++)
1711 /* Sets up a grid and puts the atoms on the grid.
1712 * This function only operates on one domain of the domain decompostion.
1713 * Note that without domain decomposition there is only one domain.
1715 void nbnxn_put_on_grid(nbnxn_search_t nbs,
1716 int ePBC, matrix box,
1718 rvec corner0, rvec corner1,
1723 int nmoved, int *move,
1725 nbnxn_atomdata_t *nbat)
1729 int nc_max_grid, nc_max;
1731 grid = &nbs->grid[dd_zone];
1733 nbs_cycle_start(&nbs->cc[enbsCCgrid]);
1735 grid->bSimple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
1737 grid->na_c = nbnxn_kernel_to_ci_size(nb_kernel_type);
1738 grid->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
1739 grid->na_sc = (grid->bSimple ? 1 : GPU_NSUBCELL)*grid->na_c;
1740 grid->na_c_2log = get_2log(grid->na_c);
1742 nbat->na_c = grid->na_c;
1751 (nbs->grid[dd_zone-1].cell0 + nbs->grid[dd_zone-1].nc)*
1752 nbs->grid[dd_zone-1].na_sc/grid->na_sc;
1760 copy_mat(box, nbs->box);
1762 if (atom_density >= 0)
1764 grid->atom_density = atom_density;
1768 grid->atom_density = grid_atom_density(n-nmoved, corner0, corner1);
1773 nbs->natoms_local = a1 - nmoved;
1774 /* We assume that nbnxn_put_on_grid is called first
1775 * for the local atoms (dd_zone=0).
1777 nbs->natoms_nonlocal = a1 - nmoved;
1781 nbs->natoms_nonlocal = max(nbs->natoms_nonlocal, a1);
1784 nc_max_grid = set_grid_size_xy(nbs, grid,
1785 dd_zone, n-nmoved, corner0, corner1,
1786 nbs->grid[0].atom_density);
1788 nc_max = grid->cell0 + nc_max_grid;
1790 if (a1 > nbs->cell_nalloc)
1792 nbs->cell_nalloc = over_alloc_large(a1);
1793 srenew(nbs->cell, nbs->cell_nalloc);
1796 /* To avoid conditionals we store the moved particles at the end of a,
1797 * make sure we have enough space.
1799 if (nc_max*grid->na_sc + nmoved > nbs->a_nalloc)
1801 nbs->a_nalloc = over_alloc_large(nc_max*grid->na_sc + nmoved);
1802 srenew(nbs->a, nbs->a_nalloc);
1805 /* We need padding up to a multiple of the buffer flag size: simply add */
1806 if (nc_max*grid->na_sc + NBNXN_BUFFERFLAG_SIZE > nbat->nalloc)
1808 nbnxn_atomdata_realloc(nbat, nc_max*grid->na_sc+NBNXN_BUFFERFLAG_SIZE);
1811 calc_cell_indices(nbs, dd_zone, grid, a0, a1, atinfo, x, move, nbat);
1815 nbat->natoms_local = nbat->natoms;
1818 nbs_cycle_stop(&nbs->cc[enbsCCgrid]);
1821 /* Calls nbnxn_put_on_grid for all non-local domains */
1822 void nbnxn_put_on_grid_nonlocal(nbnxn_search_t nbs,
1823 const gmx_domdec_zones_t *zones,
1827 nbnxn_atomdata_t *nbat)
1832 for (zone = 1; zone < zones->n; zone++)
1834 for (d = 0; d < DIM; d++)
1836 c0[d] = zones->size[zone].bb_x0[d];
1837 c1[d] = zones->size[zone].bb_x1[d];
1840 nbnxn_put_on_grid(nbs, nbs->ePBC, NULL,
1842 zones->cg_range[zone],
1843 zones->cg_range[zone+1],
1853 /* Add simple grid type information to the local super/sub grid */
1854 void nbnxn_grid_add_simple(nbnxn_search_t nbs,
1855 nbnxn_atomdata_t *nbat)
1861 grid = &nbs->grid[0];
1865 gmx_incons("nbnxn_grid_simple called with a simple grid");
1868 ncd = grid->na_sc/NBNXN_CPU_CLUSTER_I_SIZE;
1870 if (grid->nc*ncd > grid->nc_nalloc_simple)
1872 grid->nc_nalloc_simple = over_alloc_large(grid->nc*ncd);
1873 srenew(grid->bbcz_simple, grid->nc_nalloc_simple*NNBSBB_D);
1874 srenew(grid->bb_simple, grid->nc_nalloc_simple*NNBSBB_B);
1875 srenew(grid->flags_simple, grid->nc_nalloc_simple);
1878 sfree_aligned(grid->bbj);
1879 snew_aligned(grid->bbj, grid->nc_nalloc_simple/2, 16);
1883 bbcz = grid->bbcz_simple;
1884 bb = grid->bb_simple;
1886 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
1887 for (sc = 0; sc < grid->nc; sc++)
1891 for (c = 0; c < ncd; c++)
1895 na = NBNXN_CPU_CLUSTER_I_SIZE;
1897 nbat->type[tx*NBNXN_CPU_CLUSTER_I_SIZE+na-1] == nbat->ntype-1)
1904 switch (nbat->XFormat)
1907 /* PACK_X4==NBNXN_CPU_CLUSTER_I_SIZE, so this is simple */
1908 calc_bounding_box_x_x4(na, nbat->x+tx*STRIDE_P4,
1912 /* PACK_X8>NBNXN_CPU_CLUSTER_I_SIZE, more complicated */
1913 calc_bounding_box_x_x8(na, nbat->x+X8_IND_A(tx*NBNXN_CPU_CLUSTER_I_SIZE),
1917 calc_bounding_box(na, nbat->xstride,
1918 nbat->x+tx*NBNXN_CPU_CLUSTER_I_SIZE*nbat->xstride,
1922 bbcz[tx*NNBSBB_D+0] = bb[tx*NNBSBB_B +ZZ];
1923 bbcz[tx*NNBSBB_D+1] = bb[tx*NNBSBB_B+NNBSBB_C+ZZ];
1925 /* No interaction optimization yet here */
1926 grid->flags_simple[tx] = NBNXN_CI_DO_LJ(0) | NBNXN_CI_DO_COUL(0);
1930 grid->flags_simple[tx] = 0;
1935 if (grid->bSimple && nbat->XFormat == nbatX8)
1937 combine_bounding_box_pairs(grid, grid->bb_simple);
1941 void nbnxn_get_ncells(nbnxn_search_t nbs, int *ncx, int *ncy)
1943 *ncx = nbs->grid[0].ncx;
1944 *ncy = nbs->grid[0].ncy;
1947 void nbnxn_get_atomorder(nbnxn_search_t nbs, int **a, int *n)
1949 const nbnxn_grid_t *grid;
1951 grid = &nbs->grid[0];
1953 /* Return the atom order for the home cell (index 0) */
1956 *n = grid->cxy_ind[grid->ncx*grid->ncy]*grid->na_sc;
1959 void nbnxn_set_atomorder(nbnxn_search_t nbs)
1962 int ao, cx, cy, cxy, cz, j;
1964 /* Set the atom order for the home cell (index 0) */
1965 grid = &nbs->grid[0];
1968 for (cx = 0; cx < grid->ncx; cx++)
1970 for (cy = 0; cy < grid->ncy; cy++)
1972 cxy = cx*grid->ncy + cy;
1973 j = grid->cxy_ind[cxy]*grid->na_sc;
1974 for (cz = 0; cz < grid->cxy_na[cxy]; cz++)
1985 /* Determines the cell range along one dimension that
1986 * the bounding box b0 - b1 sees.
1988 static void get_cell_range(real b0, real b1,
1989 int nc, real c0, real s, real invs,
1990 real d2, real r2, int *cf, int *cl)
1992 *cf = max((int)((b0 - c0)*invs), 0);
1994 while (*cf > 0 && d2 + sqr((b0 - c0) - (*cf-1+1)*s) < r2)
1999 *cl = min((int)((b1 - c0)*invs), nc-1);
2000 while (*cl < nc-1 && d2 + sqr((*cl+1)*s - (b1 - c0)) < r2)
2006 /* Reference code calculating the distance^2 between two bounding boxes */
2007 static float box_dist2(float bx0, float bx1, float by0,
2008 float by1, float bz0, float bz1,
2012 float dl, dh, dm, dm0;
2016 dl = bx0 - bb[BBU_X];
2017 dh = bb[BBL_X] - bx1;
2022 dl = by0 - bb[BBU_Y];
2023 dh = bb[BBL_Y] - by1;
2028 dl = bz0 - bb[BBU_Z];
2029 dh = bb[BBL_Z] - bz1;
2037 /* Plain C code calculating the distance^2 between two bounding boxes */
2038 static float subc_bb_dist2(int si, const float *bb_i_ci,
2039 int csj, const float *bb_j_all)
2041 const float *bb_i, *bb_j;
2043 float dl, dh, dm, dm0;
2045 bb_i = bb_i_ci + si*NNBSBB_B;
2046 bb_j = bb_j_all + csj*NNBSBB_B;
2050 dl = bb_i[BBL_X] - bb_j[BBU_X];
2051 dh = bb_j[BBL_X] - bb_i[BBU_X];
2056 dl = bb_i[BBL_Y] - bb_j[BBU_Y];
2057 dh = bb_j[BBL_Y] - bb_i[BBU_Y];
2062 dl = bb_i[BBL_Z] - bb_j[BBU_Z];
2063 dh = bb_j[BBL_Z] - bb_i[BBU_Z];
2071 #ifdef NBNXN_SEARCH_BB_SSE
2073 /* SSE code for bb distance for bb format xyz0 */
2074 static float subc_bb_dist2_sse(int si, const float *bb_i_ci,
2075 int csj, const float *bb_j_all)
2077 const float *bb_i, *bb_j;
2079 __m128 bb_i_SSE0, bb_i_SSE1;
2080 __m128 bb_j_SSE0, bb_j_SSE1;
2086 #ifndef GMX_X86_SSE4_1
2087 float d2_array[7], *d2_align;
2089 d2_align = (float *)(((size_t)(d2_array+3)) & (~((size_t)15)));
2094 bb_i = bb_i_ci + si*NNBSBB_B;
2095 bb_j = bb_j_all + csj*NNBSBB_B;
2097 bb_i_SSE0 = _mm_load_ps(bb_i);
2098 bb_i_SSE1 = _mm_load_ps(bb_i+NNBSBB_C);
2099 bb_j_SSE0 = _mm_load_ps(bb_j);
2100 bb_j_SSE1 = _mm_load_ps(bb_j+NNBSBB_C);
2102 dl_SSE = _mm_sub_ps(bb_i_SSE0, bb_j_SSE1);
2103 dh_SSE = _mm_sub_ps(bb_j_SSE0, bb_i_SSE1);
2105 dm_SSE = _mm_max_ps(dl_SSE, dh_SSE);
2106 dm0_SSE = _mm_max_ps(dm_SSE, _mm_setzero_ps());
2107 #ifndef GMX_X86_SSE4_1
2108 d2_SSE = _mm_mul_ps(dm0_SSE, dm0_SSE);
2110 _mm_store_ps(d2_align, d2_SSE);
2112 return d2_align[0] + d2_align[1] + d2_align[2];
2114 /* SSE4.1 dot product of components 0,1,2 */
2115 d2_SSE = _mm_dp_ps(dm0_SSE, dm0_SSE, 0x71);
2117 _mm_store_ss(&d2, d2_SSE);
2123 /* Calculate bb bounding distances of bb_i[si,...,si+3] and store them in d2 */
2124 #define SUBC_BB_DIST2_SSE_XXXX_INNER(si, bb_i, d2) \
2128 __m128 dx_0, dy_0, dz_0; \
2129 __m128 dx_1, dy_1, dz_1; \
2131 __m128 mx, my, mz; \
2132 __m128 m0x, m0y, m0z; \
2134 __m128 d2x, d2y, d2z; \
2137 shi = si*NNBSBB_D*DIM; \
2139 xi_l = _mm_load_ps(bb_i+shi+0*STRIDE_PBB); \
2140 yi_l = _mm_load_ps(bb_i+shi+1*STRIDE_PBB); \
2141 zi_l = _mm_load_ps(bb_i+shi+2*STRIDE_PBB); \
2142 xi_h = _mm_load_ps(bb_i+shi+3*STRIDE_PBB); \
2143 yi_h = _mm_load_ps(bb_i+shi+4*STRIDE_PBB); \
2144 zi_h = _mm_load_ps(bb_i+shi+5*STRIDE_PBB); \
2146 dx_0 = _mm_sub_ps(xi_l, xj_h); \
2147 dy_0 = _mm_sub_ps(yi_l, yj_h); \
2148 dz_0 = _mm_sub_ps(zi_l, zj_h); \
2150 dx_1 = _mm_sub_ps(xj_l, xi_h); \
2151 dy_1 = _mm_sub_ps(yj_l, yi_h); \
2152 dz_1 = _mm_sub_ps(zj_l, zi_h); \
2154 mx = _mm_max_ps(dx_0, dx_1); \
2155 my = _mm_max_ps(dy_0, dy_1); \
2156 mz = _mm_max_ps(dz_0, dz_1); \
2158 m0x = _mm_max_ps(mx, zero); \
2159 m0y = _mm_max_ps(my, zero); \
2160 m0z = _mm_max_ps(mz, zero); \
2162 d2x = _mm_mul_ps(m0x, m0x); \
2163 d2y = _mm_mul_ps(m0y, m0y); \
2164 d2z = _mm_mul_ps(m0z, m0z); \
2166 d2s = _mm_add_ps(d2x, d2y); \
2167 d2t = _mm_add_ps(d2s, d2z); \
2169 _mm_store_ps(d2+si, d2t); \
2172 /* SSE code for nsi bb distances for bb format xxxxyyyyzzzz */
2173 static void subc_bb_dist2_sse_xxxx(const float *bb_j,
2174 int nsi, const float *bb_i,
2177 __m128 xj_l, yj_l, zj_l;
2178 __m128 xj_h, yj_h, zj_h;
2179 __m128 xi_l, yi_l, zi_l;
2180 __m128 xi_h, yi_h, zi_h;
2184 zero = _mm_setzero_ps();
2186 xj_l = _mm_set1_ps(bb_j[0*STRIDE_PBB]);
2187 yj_l = _mm_set1_ps(bb_j[1*STRIDE_PBB]);
2188 zj_l = _mm_set1_ps(bb_j[2*STRIDE_PBB]);
2189 xj_h = _mm_set1_ps(bb_j[3*STRIDE_PBB]);
2190 yj_h = _mm_set1_ps(bb_j[4*STRIDE_PBB]);
2191 zj_h = _mm_set1_ps(bb_j[5*STRIDE_PBB]);
2193 /* Here we "loop" over si (0,STRIDE_PBB) from 0 to nsi with step STRIDE_PBB.
2194 * But as we know the number of iterations is 1 or 2, we unroll manually.
2196 SUBC_BB_DIST2_SSE_XXXX_INNER(0, bb_i, d2);
2197 if (STRIDE_PBB < nsi)
2199 SUBC_BB_DIST2_SSE_XXXX_INNER(STRIDE_PBB, bb_i, d2);
2203 #endif /* NBNXN_SEARCH_BB_SSE */
2205 /* Plain C function which determines if any atom pair between two cells
2206 * is within distance sqrt(rl2).
2208 static gmx_bool subc_in_range_x(int na_c,
2209 int si, const real *x_i,
2210 int csj, int stride, const real *x_j,
2216 for (i = 0; i < na_c; i++)
2218 i0 = (si*na_c + i)*DIM;
2219 for (j = 0; j < na_c; j++)
2221 j0 = (csj*na_c + j)*stride;
2223 d2 = sqr(x_i[i0 ] - x_j[j0 ]) +
2224 sqr(x_i[i0+1] - x_j[j0+1]) +
2225 sqr(x_i[i0+2] - x_j[j0+2]);
2237 #ifdef NBNXN_SEARCH_SSE_SINGLE
2238 /* When we make seperate single/double precision SIMD vector operation
2239 * include files, this function should be moved there (also using FMA).
2241 static inline __m128
2242 gmx_mm_calc_rsq_ps(__m128 x, __m128 y, __m128 z)
2244 return _mm_add_ps( _mm_add_ps( _mm_mul_ps(x, x), _mm_mul_ps(y, y) ), _mm_mul_ps(z, z) );
2248 /* SSE function which determines if any atom pair between two cells,
2249 * both with 8 atoms, is within distance sqrt(rl2).
2250 * Not performance critical, so only uses plain SSE.
2252 static gmx_bool subc_in_range_sse8(int na_c,
2253 int si, const real *x_i,
2254 int csj, int stride, const real *x_j,
2257 #ifdef NBNXN_SEARCH_SSE_SINGLE
2258 __m128 ix_SSE0, iy_SSE0, iz_SSE0;
2259 __m128 ix_SSE1, iy_SSE1, iz_SSE1;
2266 rc2_SSE = _mm_set1_ps(rl2);
2268 na_c_sse = NBNXN_GPU_CLUSTER_SIZE/STRIDE_PBB;
2269 ix_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+0)*STRIDE_PBB);
2270 iy_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+1)*STRIDE_PBB);
2271 iz_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+2)*STRIDE_PBB);
2272 ix_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+3)*STRIDE_PBB);
2273 iy_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+4)*STRIDE_PBB);
2274 iz_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+5)*STRIDE_PBB);
2276 /* We loop from the outer to the inner particles to maximize
2277 * the chance that we find a pair in range quickly and return.
2283 __m128 jx0_SSE, jy0_SSE, jz0_SSE;
2284 __m128 jx1_SSE, jy1_SSE, jz1_SSE;
2286 __m128 dx_SSE0, dy_SSE0, dz_SSE0;
2287 __m128 dx_SSE1, dy_SSE1, dz_SSE1;
2288 __m128 dx_SSE2, dy_SSE2, dz_SSE2;
2289 __m128 dx_SSE3, dy_SSE3, dz_SSE3;
2300 __m128 wco_any_SSE01, wco_any_SSE23, wco_any_SSE;
2302 jx0_SSE = _mm_load1_ps(x_j+j0*stride+0);
2303 jy0_SSE = _mm_load1_ps(x_j+j0*stride+1);
2304 jz0_SSE = _mm_load1_ps(x_j+j0*stride+2);
2306 jx1_SSE = _mm_load1_ps(x_j+j1*stride+0);
2307 jy1_SSE = _mm_load1_ps(x_j+j1*stride+1);
2308 jz1_SSE = _mm_load1_ps(x_j+j1*stride+2);
2310 /* Calculate distance */
2311 dx_SSE0 = _mm_sub_ps(ix_SSE0, jx0_SSE);
2312 dy_SSE0 = _mm_sub_ps(iy_SSE0, jy0_SSE);
2313 dz_SSE0 = _mm_sub_ps(iz_SSE0, jz0_SSE);
2314 dx_SSE1 = _mm_sub_ps(ix_SSE1, jx0_SSE);
2315 dy_SSE1 = _mm_sub_ps(iy_SSE1, jy0_SSE);
2316 dz_SSE1 = _mm_sub_ps(iz_SSE1, jz0_SSE);
2317 dx_SSE2 = _mm_sub_ps(ix_SSE0, jx1_SSE);
2318 dy_SSE2 = _mm_sub_ps(iy_SSE0, jy1_SSE);
2319 dz_SSE2 = _mm_sub_ps(iz_SSE0, jz1_SSE);
2320 dx_SSE3 = _mm_sub_ps(ix_SSE1, jx1_SSE);
2321 dy_SSE3 = _mm_sub_ps(iy_SSE1, jy1_SSE);
2322 dz_SSE3 = _mm_sub_ps(iz_SSE1, jz1_SSE);
2324 /* rsq = dx*dx+dy*dy+dz*dz */
2325 rsq_SSE0 = gmx_mm_calc_rsq_ps(dx_SSE0, dy_SSE0, dz_SSE0);
2326 rsq_SSE1 = gmx_mm_calc_rsq_ps(dx_SSE1, dy_SSE1, dz_SSE1);
2327 rsq_SSE2 = gmx_mm_calc_rsq_ps(dx_SSE2, dy_SSE2, dz_SSE2);
2328 rsq_SSE3 = gmx_mm_calc_rsq_ps(dx_SSE3, dy_SSE3, dz_SSE3);
2330 wco_SSE0 = _mm_cmplt_ps(rsq_SSE0, rc2_SSE);
2331 wco_SSE1 = _mm_cmplt_ps(rsq_SSE1, rc2_SSE);
2332 wco_SSE2 = _mm_cmplt_ps(rsq_SSE2, rc2_SSE);
2333 wco_SSE3 = _mm_cmplt_ps(rsq_SSE3, rc2_SSE);
2335 wco_any_SSE01 = _mm_or_ps(wco_SSE0, wco_SSE1);
2336 wco_any_SSE23 = _mm_or_ps(wco_SSE2, wco_SSE3);
2337 wco_any_SSE = _mm_or_ps(wco_any_SSE01, wco_any_SSE23);
2339 if (_mm_movemask_ps(wco_any_SSE))
2351 gmx_incons("SSE function called without SSE support");
2357 /* Returns the j sub-cell for index cj_ind */
2358 static int nbl_cj(const nbnxn_pairlist_t *nbl, int cj_ind)
2360 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].cj[cj_ind & (NBNXN_GPU_JGROUP_SIZE - 1)];
2363 /* Returns the i-interaction mask of the j sub-cell for index cj_ind */
2364 static unsigned nbl_imask0(const nbnxn_pairlist_t *nbl, int cj_ind)
2366 return nbl->cj4[cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG].imei[0].imask;
2369 /* Ensures there is enough space for extra extra exclusion masks */
2370 static void check_excl_space(nbnxn_pairlist_t *nbl, int extra)
2372 if (nbl->nexcl+extra > nbl->excl_nalloc)
2374 nbl->excl_nalloc = over_alloc_small(nbl->nexcl+extra);
2375 nbnxn_realloc_void((void **)&nbl->excl,
2376 nbl->nexcl*sizeof(*nbl->excl),
2377 nbl->excl_nalloc*sizeof(*nbl->excl),
2378 nbl->alloc, nbl->free);
2382 /* Ensures there is enough space for ncell extra j-cells in the list */
2383 static void check_subcell_list_space_simple(nbnxn_pairlist_t *nbl,
2388 cj_max = nbl->ncj + ncell;
2390 if (cj_max > nbl->cj_nalloc)
2392 nbl->cj_nalloc = over_alloc_small(cj_max);
2393 nbnxn_realloc_void((void **)&nbl->cj,
2394 nbl->ncj*sizeof(*nbl->cj),
2395 nbl->cj_nalloc*sizeof(*nbl->cj),
2396 nbl->alloc, nbl->free);
2400 /* Ensures there is enough space for ncell extra j-subcells in the list */
2401 static void check_subcell_list_space_supersub(nbnxn_pairlist_t *nbl,
2404 int ncj4_max, j4, j, w, t;
2407 #define WARP_SIZE 32
2409 /* We can have maximally nsupercell*GPU_NSUBCELL sj lists */
2410 /* We can store 4 j-subcell - i-supercell pairs in one struct.
2411 * since we round down, we need one extra entry.
2413 ncj4_max = ((nbl->work->cj_ind + nsupercell*GPU_NSUBCELL + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2415 if (ncj4_max > nbl->cj4_nalloc)
2417 nbl->cj4_nalloc = over_alloc_small(ncj4_max);
2418 nbnxn_realloc_void((void **)&nbl->cj4,
2419 nbl->work->cj4_init*sizeof(*nbl->cj4),
2420 nbl->cj4_nalloc*sizeof(*nbl->cj4),
2421 nbl->alloc, nbl->free);
2424 if (ncj4_max > nbl->work->cj4_init)
2426 for (j4 = nbl->work->cj4_init; j4 < ncj4_max; j4++)
2428 /* No i-subcells and no excl's in the list initially */
2429 for (w = 0; w < NWARP; w++)
2431 nbl->cj4[j4].imei[w].imask = 0U;
2432 nbl->cj4[j4].imei[w].excl_ind = 0;
2436 nbl->work->cj4_init = ncj4_max;
2440 /* Set all excl masks for one GPU warp no exclusions */
2441 static void set_no_excls(nbnxn_excl_t *excl)
2445 for (t = 0; t < WARP_SIZE; t++)
2447 /* Turn all interaction bits on */
2448 excl->pair[t] = NBNXN_INTERACTION_MASK_ALL;
2452 /* Initializes a single nbnxn_pairlist_t data structure */
2453 static void nbnxn_init_pairlist(nbnxn_pairlist_t *nbl,
2455 nbnxn_alloc_t *alloc,
2460 nbl->alloc = nbnxn_alloc_aligned;
2468 nbl->free = nbnxn_free_aligned;
2475 nbl->bSimple = bSimple;
2486 /* We need one element extra in sj, so alloc initially with 1 */
2487 nbl->cj4_nalloc = 0;
2494 nbl->excl_nalloc = 0;
2496 check_excl_space(nbl, 1);
2498 set_no_excls(&nbl->excl[0]);
2503 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL/STRIDE_PBB*NNBSBB_XXXX, NBNXN_MEM_ALIGN);
2505 snew_aligned(nbl->work->bb_ci, GPU_NSUBCELL*NNBSBB_B, NBNXN_MEM_ALIGN);
2507 snew_aligned(nbl->work->x_ci, NBNXN_NA_SC_MAX*DIM, NBNXN_MEM_ALIGN);
2508 #ifdef GMX_NBNXN_SIMD
2509 snew_aligned(nbl->work->x_ci_simd_4xn, 1, NBNXN_MEM_ALIGN);
2510 snew_aligned(nbl->work->x_ci_simd_2xnn, 1, NBNXN_MEM_ALIGN);
2512 snew_aligned(nbl->work->d2, GPU_NSUBCELL, NBNXN_MEM_ALIGN);
2514 nbl->work->sort = NULL;
2515 nbl->work->sort_nalloc = 0;
2516 nbl->work->sci_sort = NULL;
2517 nbl->work->sci_sort_nalloc = 0;
2520 void nbnxn_init_pairlist_set(nbnxn_pairlist_set_t *nbl_list,
2521 gmx_bool bSimple, gmx_bool bCombined,
2522 nbnxn_alloc_t *alloc,
2527 nbl_list->bSimple = bSimple;
2528 nbl_list->bCombined = bCombined;
2530 nbl_list->nnbl = gmx_omp_nthreads_get(emntNonbonded);
2532 if (!nbl_list->bCombined &&
2533 nbl_list->nnbl > NBNXN_BUFFERFLAG_MAX_THREADS)
2535 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.",
2536 nbl_list->nnbl, NBNXN_BUFFERFLAG_MAX_THREADS, NBNXN_BUFFERFLAG_MAX_THREADS);
2539 snew(nbl_list->nbl, nbl_list->nnbl);
2540 /* Execute in order to avoid memory interleaving between threads */
2541 #pragma omp parallel for num_threads(nbl_list->nnbl) schedule(static)
2542 for (i = 0; i < nbl_list->nnbl; i++)
2544 /* Allocate the nblist data structure locally on each thread
2545 * to optimize memory access for NUMA architectures.
2547 snew(nbl_list->nbl[i], 1);
2549 /* Only list 0 is used on the GPU, use normal allocation for i>0 */
2552 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, alloc, free);
2556 nbnxn_init_pairlist(nbl_list->nbl[i], nbl_list->bSimple, NULL, NULL);
2561 /* Print statistics of a pair list, used for debug output */
2562 static void print_nblist_statistics_simple(FILE *fp, const nbnxn_pairlist_t *nbl,
2563 const nbnxn_search_t nbs, real rl)
2565 const nbnxn_grid_t *grid;
2570 /* This code only produces correct statistics with domain decomposition */
2571 grid = &nbs->grid[0];
2573 fprintf(fp, "nbl nci %d ncj %d\n",
2574 nbl->nci, nbl->ncj);
2575 fprintf(fp, "nbl na_sc %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2576 nbl->na_sc, rl, nbl->ncj, nbl->ncj/(double)grid->nc,
2577 nbl->ncj/(double)grid->nc*grid->na_sc,
2578 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)));
2580 fprintf(fp, "nbl average j cell list length %.1f\n",
2581 0.25*nbl->ncj/(double)nbl->nci);
2583 for (s = 0; s < SHIFTS; s++)
2588 for (i = 0; i < nbl->nci; i++)
2590 cs[nbl->ci[i].shift & NBNXN_CI_SHIFT] +=
2591 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start;
2593 j = nbl->ci[i].cj_ind_start;
2594 while (j < nbl->ci[i].cj_ind_end &&
2595 nbl->cj[j].excl != NBNXN_INTERACTION_MASK_ALL)
2601 fprintf(fp, "nbl cell pairs, total: %d excl: %d %.1f%%\n",
2602 nbl->ncj, npexcl, 100*npexcl/(double)nbl->ncj);
2603 for (s = 0; s < SHIFTS; s++)
2607 fprintf(fp, "nbl shift %2d ncj %3d\n", s, cs[s]);
2612 /* Print statistics of a pair lists, used for debug output */
2613 static void print_nblist_statistics_supersub(FILE *fp, const nbnxn_pairlist_t *nbl,
2614 const nbnxn_search_t nbs, real rl)
2616 const nbnxn_grid_t *grid;
2617 int i, j4, j, si, b;
2618 int c[GPU_NSUBCELL+1];
2620 /* This code only produces correct statistics with domain decomposition */
2621 grid = &nbs->grid[0];
2623 fprintf(fp, "nbl nsci %d ncj4 %d nsi %d excl4 %d\n",
2624 nbl->nsci, nbl->ncj4, nbl->nci_tot, nbl->nexcl);
2625 fprintf(fp, "nbl na_c %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2626 nbl->na_ci, rl, nbl->nci_tot, nbl->nci_tot/(double)grid->nsubc_tot,
2627 nbl->nci_tot/(double)grid->nsubc_tot*grid->na_c,
2628 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)));
2630 fprintf(fp, "nbl average j super cell list length %.1f\n",
2631 0.25*nbl->ncj4/(double)nbl->nsci);
2632 fprintf(fp, "nbl average i sub cell list length %.1f\n",
2633 nbl->nci_tot/((double)nbl->ncj4));
2635 for (si = 0; si <= GPU_NSUBCELL; si++)
2639 for (i = 0; i < nbl->nsci; i++)
2641 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
2643 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
2646 for (si = 0; si < GPU_NSUBCELL; si++)
2648 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
2657 for (b = 0; b <= GPU_NSUBCELL; b++)
2659 fprintf(fp, "nbl j-list #i-subcell %d %7d %4.1f\n",
2660 b, c[b], 100.0*c[b]/(double)(nbl->ncj4*NBNXN_GPU_JGROUP_SIZE));
2664 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp */
2665 static void low_get_nbl_exclusions(nbnxn_pairlist_t *nbl, int cj4,
2666 int warp, nbnxn_excl_t **excl)
2668 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2670 /* No exclusions set, make a new list entry */
2671 nbl->cj4[cj4].imei[warp].excl_ind = nbl->nexcl;
2673 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2674 set_no_excls(*excl);
2678 /* We already have some exclusions, new ones can be added to the list */
2679 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2683 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp,
2684 * allocates extra memory, if necessary.
2686 static void get_nbl_exclusions_1(nbnxn_pairlist_t *nbl, int cj4,
2687 int warp, nbnxn_excl_t **excl)
2689 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2691 /* We need to make a new list entry, check if we have space */
2692 check_excl_space(nbl, 1);
2694 low_get_nbl_exclusions(nbl, cj4, warp, excl);
2697 /* Returns pointers to the exclusion mask for cj4-unit cj4 for both warps,
2698 * allocates extra memory, if necessary.
2700 static void get_nbl_exclusions_2(nbnxn_pairlist_t *nbl, int cj4,
2701 nbnxn_excl_t **excl_w0,
2702 nbnxn_excl_t **excl_w1)
2704 /* Check for space we might need */
2705 check_excl_space(nbl, 2);
2707 low_get_nbl_exclusions(nbl, cj4, 0, excl_w0);
2708 low_get_nbl_exclusions(nbl, cj4, 1, excl_w1);
2711 /* Sets the self exclusions i=j and pair exclusions i>j */
2712 static void set_self_and_newton_excls_supersub(nbnxn_pairlist_t *nbl,
2713 int cj4_ind, int sj_offset,
2716 nbnxn_excl_t *excl[2];
2719 /* Here we only set the set self and double pair exclusions */
2721 get_nbl_exclusions_2(nbl, cj4_ind, &excl[0], &excl[1]);
2723 /* Only minor < major bits set */
2724 for (ej = 0; ej < nbl->na_ci; ej++)
2727 for (ei = ej; ei < nbl->na_ci; ei++)
2729 excl[w]->pair[(ej & (NBNXN_GPU_JGROUP_SIZE-1))*nbl->na_ci + ei] &=
2730 ~(1U << (sj_offset*GPU_NSUBCELL + si));
2735 /* Returns a diagonal or off-diagonal interaction mask for plain C lists */
2736 static unsigned int get_imask(gmx_bool rdiag, int ci, int cj)
2738 return (rdiag && ci == cj ? NBNXN_INTERACTION_MASK_DIAG : NBNXN_INTERACTION_MASK_ALL);
2741 /* Returns a diagonal or off-diagonal interaction mask for cj-size=2 */
2742 static unsigned int get_imask_simd_j2(gmx_bool rdiag, int ci, int cj)
2744 return (rdiag && ci*2 == cj ? NBNXN_INTERACTION_MASK_DIAG_J2_0 :
2745 (rdiag && ci*2+1 == cj ? NBNXN_INTERACTION_MASK_DIAG_J2_1 :
2746 NBNXN_INTERACTION_MASK_ALL));
2749 /* Returns a diagonal or off-diagonal interaction mask for cj-size=4 */
2750 static unsigned int get_imask_simd_j4(gmx_bool rdiag, int ci, int cj)
2752 return (rdiag && ci == cj ? NBNXN_INTERACTION_MASK_DIAG : NBNXN_INTERACTION_MASK_ALL);
2755 /* Returns a diagonal or off-diagonal interaction mask for cj-size=8 */
2756 static unsigned int get_imask_simd_j8(gmx_bool rdiag, int ci, int cj)
2758 return (rdiag && ci == cj*2 ? NBNXN_INTERACTION_MASK_DIAG_J8_0 :
2759 (rdiag && ci == cj*2+1 ? NBNXN_INTERACTION_MASK_DIAG_J8_1 :
2760 NBNXN_INTERACTION_MASK_ALL));
2763 #ifdef GMX_NBNXN_SIMD
2764 #if GMX_SIMD_WIDTH_HERE == 2
2765 #define get_imask_simd_4xn get_imask_simd_j2
2767 #if GMX_SIMD_WIDTH_HERE == 4
2768 #define get_imask_simd_4xn get_imask_simd_j4
2770 #if GMX_SIMD_WIDTH_HERE == 8
2771 #define get_imask_simd_4xn get_imask_simd_j8
2772 #define get_imask_simd_2xnn get_imask_simd_j4
2774 #if GMX_SIMD_WIDTH_HERE == 16
2775 #define get_imask_simd_2xnn get_imask_simd_j8
2779 /* Plain C code for making a pair list of cell ci vs cell cjf-cjl.
2780 * Checks bounding box distances and possibly atom pair distances.
2782 static void make_cluster_list_simple(const nbnxn_grid_t *gridj,
2783 nbnxn_pairlist_t *nbl,
2784 int ci, int cjf, int cjl,
2785 gmx_bool remove_sub_diag,
2787 real rl2, float rbb2,
2790 const nbnxn_list_work_t *work;
2797 int cjf_gl, cjl_gl, cj;
2801 bb_ci = nbl->work->bb_ci;
2802 x_ci = nbl->work->x_ci;
2805 while (!InRange && cjf <= cjl)
2807 d2 = subc_bb_dist2(0, bb_ci, cjf, gridj->bb);
2810 /* Check if the distance is within the distance where
2811 * we use only the bounding box distance rbb,
2812 * or within the cut-off and there is at least one atom pair
2813 * within the cut-off.
2823 cjf_gl = gridj->cell0 + cjf;
2824 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2826 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2828 InRange = InRange ||
2829 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2830 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2831 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2834 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2847 while (!InRange && cjl > cjf)
2849 d2 = subc_bb_dist2(0, bb_ci, cjl, gridj->bb);
2852 /* Check if the distance is within the distance where
2853 * we use only the bounding box distance rbb,
2854 * or within the cut-off and there is at least one atom pair
2855 * within the cut-off.
2865 cjl_gl = gridj->cell0 + cjl;
2866 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2868 for (j = 0; j < NBNXN_CPU_CLUSTER_I_SIZE; j++)
2870 InRange = InRange ||
2871 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2872 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2873 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2876 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2886 for (cj = cjf; cj <= cjl; cj++)
2888 /* Store cj and the interaction mask */
2889 nbl->cj[nbl->ncj].cj = gridj->cell0 + cj;
2890 nbl->cj[nbl->ncj].excl = get_imask(remove_sub_diag, ci, cj);
2893 /* Increase the closing index in i super-cell list */
2894 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
2898 #ifdef GMX_NBNXN_SIMD_4XN
2899 #include "nbnxn_search_simd_4xn.h"
2901 #ifdef GMX_NBNXN_SIMD_2XNN
2902 #include "nbnxn_search_simd_2xnn.h"
2905 /* Plain C or SSE code for making a pair list of super-cell sci vs scj.
2906 * Checks bounding box distances and possibly atom pair distances.
2908 static void make_cluster_list_supersub(const nbnxn_grid_t *gridi,
2909 const nbnxn_grid_t *gridj,
2910 nbnxn_pairlist_t *nbl,
2912 gmx_bool sci_equals_scj,
2913 int stride, const real *x,
2914 real rl2, float rbb2,
2919 int cjo, ci1, ci, cj, cj_gl;
2920 int cj4_ind, cj_offset;
2927 #define PRUNE_LIST_CPU_ONE
2928 #ifdef PRUNE_LIST_CPU_ONE
2932 d2l = nbl->work->d2;
2934 bb_ci = nbl->work->bb_ci;
2935 x_ci = nbl->work->x_ci;
2939 for (cjo = 0; cjo < gridj->nsubc[scj]; cjo++)
2941 cj4_ind = (nbl->work->cj_ind >> NBNXN_GPU_JGROUP_SIZE_2LOG);
2942 cj_offset = nbl->work->cj_ind - cj4_ind*NBNXN_GPU_JGROUP_SIZE;
2943 cj4 = &nbl->cj4[cj4_ind];
2945 cj = scj*GPU_NSUBCELL + cjo;
2947 cj_gl = gridj->cell0*GPU_NSUBCELL + cj;
2949 /* Initialize this j-subcell i-subcell list */
2950 cj4->cj[cj_offset] = cj_gl;
2959 ci1 = gridi->nsubc[sci];
2963 /* Determine all ci1 bb distances in one call with SSE */
2964 subc_bb_dist2_sse_xxxx(gridj->bb+(cj>>STRIDE_PBB_2LOG)*NNBSBB_XXXX+(cj & (STRIDE_PBB-1)),
2970 /* We use a fixed upper-bound instead of ci1 to help optimization */
2971 for (ci = 0; ci < GPU_NSUBCELL; ci++)
2978 #ifndef NBNXN_BBXXXX
2979 /* Determine the bb distance between ci and cj */
2980 d2l[ci] = subc_bb_dist2(ci, bb_ci, cj, gridj->bb);
2985 #ifdef PRUNE_LIST_CPU_ALL
2986 /* Check if the distance is within the distance where
2987 * we use only the bounding box distance rbb,
2988 * or within the cut-off and there is at least one atom pair
2989 * within the cut-off. This check is very costly.
2991 *ndistc += na_c*na_c;
2994 #ifdef NBNXN_PBB_SSE
2999 (na_c, ci, x_ci, cj_gl, stride, x, rl2)))
3001 /* Check if the distance between the two bounding boxes
3002 * in within the pair-list cut-off.
3007 /* Flag this i-subcell to be taken into account */
3008 imask |= (1U << (cj_offset*GPU_NSUBCELL+ci));
3010 #ifdef PRUNE_LIST_CPU_ONE
3018 #ifdef PRUNE_LIST_CPU_ONE
3019 /* If we only found 1 pair, check if any atoms are actually
3020 * within the cut-off, so we could get rid of it.
3022 if (npair == 1 && d2l[ci_last] >= rbb2)
3024 /* Avoid using function pointers here, as it's slower */
3026 #ifdef NBNXN_PBB_SSE
3031 (na_c, ci_last, x_ci, cj_gl, stride, x, rl2))
3033 imask &= ~(1U << (cj_offset*GPU_NSUBCELL+ci_last));
3041 /* We have a useful sj entry, close it now */
3043 /* Set the exclucions for the ci== sj entry.
3044 * Here we don't bother to check if this entry is actually flagged,
3045 * as it will nearly always be in the list.
3049 set_self_and_newton_excls_supersub(nbl, cj4_ind, cj_offset, cjo);
3052 /* Copy the cluster interaction mask to the list */
3053 for (w = 0; w < NWARP; w++)
3055 cj4->imei[w].imask |= imask;
3058 nbl->work->cj_ind++;
3060 /* Keep the count */
3061 nbl->nci_tot += npair;
3063 /* Increase the closing index in i super-cell list */
3064 nbl->sci[nbl->nsci].cj4_ind_end =
3065 ((nbl->work->cj_ind+NBNXN_GPU_JGROUP_SIZE-1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3070 /* Set all atom-pair exclusions from the topology stored in excl
3071 * as masks in the pair-list for simple list i-entry nbl_ci
3073 static void set_ci_top_excls(const nbnxn_search_t nbs,
3074 nbnxn_pairlist_t *nbl,
3075 gmx_bool diagRemoved,
3078 const nbnxn_ci_t *nbl_ci,
3079 const t_blocka *excl)
3083 int cj_ind_first, cj_ind_last;
3084 int cj_first, cj_last;
3086 int i, ai, aj, si, eind, ge, se;
3087 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3091 nbnxn_excl_t *nbl_excl;
3092 int inner_i, inner_e;
3096 if (nbl_ci->cj_ind_end == nbl_ci->cj_ind_start)
3104 cj_ind_first = nbl_ci->cj_ind_start;
3105 cj_ind_last = nbl->ncj - 1;
3107 cj_first = nbl->cj[cj_ind_first].cj;
3108 cj_last = nbl->cj[cj_ind_last].cj;
3110 /* Determine how many contiguous j-cells we have starting
3111 * from the first i-cell. This number can be used to directly
3112 * calculate j-cell indices for excluded atoms.
3115 if (na_ci_2log == na_cj_2log)
3117 while (cj_ind_first + ndirect <= cj_ind_last &&
3118 nbl->cj[cj_ind_first+ndirect].cj == ci + ndirect)
3123 #ifdef NBNXN_SEARCH_BB_SSE
3126 while (cj_ind_first + ndirect <= cj_ind_last &&
3127 nbl->cj[cj_ind_first+ndirect].cj == ci_to_cj(na_cj_2log, ci) + ndirect)
3134 /* Loop over the atoms in the i super-cell */
3135 for (i = 0; i < nbl->na_sc; i++)
3137 ai = nbs->a[ci*nbl->na_sc+i];
3140 si = (i>>na_ci_2log);
3142 /* Loop over the topology-based exclusions for this i-atom */
3143 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3149 /* The self exclusion are already set, save some time */
3155 /* Without shifts we only calculate interactions j>i
3156 * for one-way pair-lists.
3158 if (diagRemoved && ge <= ci*nbl->na_sc + i)
3163 se = (ge >> na_cj_2log);
3165 /* Could the cluster se be in our list? */
3166 if (se >= cj_first && se <= cj_last)
3168 if (se < cj_first + ndirect)
3170 /* We can calculate cj_ind directly from se */
3171 found = cj_ind_first + se - cj_first;
3175 /* Search for se using bisection */
3177 cj_ind_0 = cj_ind_first + ndirect;
3178 cj_ind_1 = cj_ind_last + 1;
3179 while (found == -1 && cj_ind_0 < cj_ind_1)
3181 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3183 cj_m = nbl->cj[cj_ind_m].cj;
3191 cj_ind_1 = cj_ind_m;
3195 cj_ind_0 = cj_ind_m + 1;
3202 inner_i = i - (si << na_ci_2log);
3203 inner_e = ge - (se << na_cj_2log);
3205 nbl->cj[found].excl &= ~(1U<<((inner_i<<na_cj_2log) + inner_e));
3213 /* Set all atom-pair exclusions from the topology stored in excl
3214 * as masks in the pair-list for i-super-cell entry nbl_sci
3216 static void set_sci_top_excls(const nbnxn_search_t nbs,
3217 nbnxn_pairlist_t *nbl,
3218 gmx_bool diagRemoved,
3220 const nbnxn_sci_t *nbl_sci,
3221 const t_blocka *excl)
3226 int cj_ind_first, cj_ind_last;
3227 int cj_first, cj_last;
3229 int i, ai, aj, si, eind, ge, se;
3230 int found, cj_ind_0, cj_ind_1, cj_ind_m;
3234 nbnxn_excl_t *nbl_excl;
3235 int inner_i, inner_e, w;
3241 if (nbl_sci->cj4_ind_end == nbl_sci->cj4_ind_start)
3249 cj_ind_first = nbl_sci->cj4_ind_start*NBNXN_GPU_JGROUP_SIZE;
3250 cj_ind_last = nbl->work->cj_ind - 1;
3252 cj_first = nbl->cj4[nbl_sci->cj4_ind_start].cj[0];
3253 cj_last = nbl_cj(nbl, cj_ind_last);
3255 /* Determine how many contiguous j-clusters we have starting
3256 * from the first i-cluster. This number can be used to directly
3257 * calculate j-cluster indices for excluded atoms.
3260 while (cj_ind_first + ndirect <= cj_ind_last &&
3261 nbl_cj(nbl, cj_ind_first+ndirect) == sci*GPU_NSUBCELL + ndirect)
3266 /* Loop over the atoms in the i super-cell */
3267 for (i = 0; i < nbl->na_sc; i++)
3269 ai = nbs->a[sci*nbl->na_sc+i];
3272 si = (i>>na_c_2log);
3274 /* Loop over the topology-based exclusions for this i-atom */
3275 for (eind = excl->index[ai]; eind < excl->index[ai+1]; eind++)
3281 /* The self exclusion are already set, save some time */
3287 /* Without shifts we only calculate interactions j>i
3288 * for one-way pair-lists.
3290 if (diagRemoved && ge <= sci*nbl->na_sc + i)
3296 /* Could the cluster se be in our list? */
3297 if (se >= cj_first && se <= cj_last)
3299 if (se < cj_first + ndirect)
3301 /* We can calculate cj_ind directly from se */
3302 found = cj_ind_first + se - cj_first;
3306 /* Search for se using bisection */
3308 cj_ind_0 = cj_ind_first + ndirect;
3309 cj_ind_1 = cj_ind_last + 1;
3310 while (found == -1 && cj_ind_0 < cj_ind_1)
3312 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3314 cj_m = nbl_cj(nbl, cj_ind_m);
3322 cj_ind_1 = cj_ind_m;
3326 cj_ind_0 = cj_ind_m + 1;
3333 inner_i = i - si*na_c;
3334 inner_e = ge - se*na_c;
3336 /* Macro for getting the index of atom a within a cluster */
3337 #define AMODCJ4(a) ((a) & (NBNXN_GPU_JGROUP_SIZE - 1))
3338 /* Macro for converting an atom number to a cluster number */
3339 #define A2CJ4(a) ((a) >> NBNXN_GPU_JGROUP_SIZE_2LOG)
3340 /* Macro for getting the index of an i-atom within a warp */
3341 #define AMODWI(a) ((a) & (NBNXN_GPU_CLUSTER_SIZE/2 - 1))
3343 if (nbl_imask0(nbl, found) & (1U << (AMODCJ4(found)*GPU_NSUBCELL + si)))
3347 get_nbl_exclusions_1(nbl, A2CJ4(found), w, &nbl_excl);
3349 nbl_excl->pair[AMODWI(inner_e)*nbl->na_ci+inner_i] &=
3350 ~(1U << (AMODCJ4(found)*GPU_NSUBCELL + si));
3363 /* Reallocate the simple ci list for at least n entries */
3364 static void nb_realloc_ci(nbnxn_pairlist_t *nbl, int n)
3366 nbl->ci_nalloc = over_alloc_small(n);
3367 nbnxn_realloc_void((void **)&nbl->ci,
3368 nbl->nci*sizeof(*nbl->ci),
3369 nbl->ci_nalloc*sizeof(*nbl->ci),
3370 nbl->alloc, nbl->free);
3373 /* Reallocate the super-cell sci list for at least n entries */
3374 static void nb_realloc_sci(nbnxn_pairlist_t *nbl, int n)
3376 nbl->sci_nalloc = over_alloc_small(n);
3377 nbnxn_realloc_void((void **)&nbl->sci,
3378 nbl->nsci*sizeof(*nbl->sci),
3379 nbl->sci_nalloc*sizeof(*nbl->sci),
3380 nbl->alloc, nbl->free);
3383 /* Make a new ci entry at index nbl->nci */
3384 static void new_ci_entry(nbnxn_pairlist_t *nbl, int ci, int shift, int flags)
3386 if (nbl->nci + 1 > nbl->ci_nalloc)
3388 nb_realloc_ci(nbl, nbl->nci+1);
3390 nbl->ci[nbl->nci].ci = ci;
3391 nbl->ci[nbl->nci].shift = shift;
3392 /* Store the interaction flags along with the shift */
3393 nbl->ci[nbl->nci].shift |= flags;
3394 nbl->ci[nbl->nci].cj_ind_start = nbl->ncj;
3395 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
3398 /* Make a new sci entry at index nbl->nsci */
3399 static void new_sci_entry(nbnxn_pairlist_t *nbl, int sci, int shift)
3401 if (nbl->nsci + 1 > nbl->sci_nalloc)
3403 nb_realloc_sci(nbl, nbl->nsci+1);
3405 nbl->sci[nbl->nsci].sci = sci;
3406 nbl->sci[nbl->nsci].shift = shift;
3407 nbl->sci[nbl->nsci].cj4_ind_start = nbl->ncj4;
3408 nbl->sci[nbl->nsci].cj4_ind_end = nbl->ncj4;
3411 /* Sort the simple j-list cj on exclusions.
3412 * Entries with exclusions will all be sorted to the beginning of the list.
3414 static void sort_cj_excl(nbnxn_cj_t *cj, int ncj,
3415 nbnxn_list_work_t *work)
3419 if (ncj > work->cj_nalloc)
3421 work->cj_nalloc = over_alloc_large(ncj);
3422 srenew(work->cj, work->cj_nalloc);
3425 /* Make a list of the j-cells involving exclusions */
3427 for (j = 0; j < ncj; j++)
3429 if (cj[j].excl != NBNXN_INTERACTION_MASK_ALL)
3431 work->cj[jnew++] = cj[j];
3434 /* Check if there are exclusions at all or not just the first entry */
3435 if (!((jnew == 0) ||
3436 (jnew == 1 && cj[0].excl != NBNXN_INTERACTION_MASK_ALL)))
3438 for (j = 0; j < ncj; j++)
3440 if (cj[j].excl == NBNXN_INTERACTION_MASK_ALL)
3442 work->cj[jnew++] = cj[j];
3445 for (j = 0; j < ncj; j++)
3447 cj[j] = work->cj[j];
3452 /* Close this simple list i entry */
3453 static void close_ci_entry_simple(nbnxn_pairlist_t *nbl)
3457 /* All content of the new ci entry have already been filled correctly,
3458 * we only need to increase the count here (for non empty lists).
3460 jlen = nbl->ci[nbl->nci].cj_ind_end - nbl->ci[nbl->nci].cj_ind_start;
3463 sort_cj_excl(nbl->cj+nbl->ci[nbl->nci].cj_ind_start, jlen, nbl->work);
3465 /* The counts below are used for non-bonded pair/flop counts
3466 * and should therefore match the available kernel setups.
3468 if (!(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_COUL(0)))
3470 nbl->work->ncj_noq += jlen;
3472 else if ((nbl->ci[nbl->nci].shift & NBNXN_CI_HALF_LJ(0)) ||
3473 !(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_LJ(0)))
3475 nbl->work->ncj_hlj += jlen;
3482 /* Split sci entry for load balancing on the GPU.
3483 * Splitting ensures we have enough lists to fully utilize the whole GPU.
3484 * With progBal we generate progressively smaller lists, which improves
3485 * load balancing. As we only know the current count on our own thread,
3486 * we will need to estimate the current total amount of i-entries.
3487 * As the lists get concatenated later, this estimate depends
3488 * both on nthread and our own thread index.
3490 static void split_sci_entry(nbnxn_pairlist_t *nbl,
3491 int nsp_max_av, gmx_bool progBal, int nc_bal,
3492 int thread, int nthread)
3496 int cj4_start, cj4_end, j4len, cj4;
3498 int nsp, nsp_sci, nsp_cj4, nsp_cj4_e, nsp_cj4_p;
3503 /* Estimate the total numbers of ci's of the nblist combined
3504 * over all threads using the target number of ci's.
3506 nsci_est = nc_bal*thread/nthread + nbl->nsci;
3508 /* The first ci blocks should be larger, to avoid overhead.
3509 * The last ci blocks should be smaller, to improve load balancing.
3512 nsp_max_av*nc_bal*3/(2*(nsci_est - 1 + nc_bal)));
3516 nsp_max = nsp_max_av;
3519 cj4_start = nbl->sci[nbl->nsci-1].cj4_ind_start;
3520 cj4_end = nbl->sci[nbl->nsci-1].cj4_ind_end;
3521 j4len = cj4_end - cj4_start;
3523 if (j4len > 1 && j4len*GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE > nsp_max)
3525 /* Remove the last ci entry and process the cj4's again */
3533 for (cj4 = cj4_start; cj4 < cj4_end; cj4++)
3535 nsp_cj4_p = nsp_cj4;
3536 /* Count the number of cluster pairs in this cj4 group */
3538 for (p = 0; p < GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE; p++)
3540 nsp_cj4 += (nbl->cj4[cj4].imei[0].imask >> p) & 1;
3543 if (nsp_cj4 > 0 && nsp + nsp_cj4 > nsp_max)
3545 /* Split the list at cj4 */
3546 nbl->sci[sci].cj4_ind_end = cj4;
3547 /* Create a new sci entry */
3550 if (nbl->nsci+1 > nbl->sci_nalloc)
3552 nb_realloc_sci(nbl, nbl->nsci+1);
3554 nbl->sci[sci].sci = nbl->sci[nbl->nsci-1].sci;
3555 nbl->sci[sci].shift = nbl->sci[nbl->nsci-1].shift;
3556 nbl->sci[sci].cj4_ind_start = cj4;
3558 nsp_cj4_e = nsp_cj4_p;
3564 /* Put the remaining cj4's in the last sci entry */
3565 nbl->sci[sci].cj4_ind_end = cj4_end;
3567 /* Possibly balance out the last two sci's
3568 * by moving the last cj4 of the second last sci.
3570 if (nsp_sci - nsp_cj4_e >= nsp + nsp_cj4_e)
3572 nbl->sci[sci-1].cj4_ind_end--;
3573 nbl->sci[sci].cj4_ind_start--;
3580 /* Clost this super/sub list i entry */
3581 static void close_ci_entry_supersub(nbnxn_pairlist_t *nbl,
3583 gmx_bool progBal, int nc_bal,
3584 int thread, int nthread)
3589 /* All content of the new ci entry have already been filled correctly,
3590 * we only need to increase the count here (for non empty lists).
3592 j4len = nbl->sci[nbl->nsci].cj4_ind_end - nbl->sci[nbl->nsci].cj4_ind_start;
3595 /* We can only have complete blocks of 4 j-entries in a list,
3596 * so round the count up before closing.
3598 nbl->ncj4 = ((nbl->work->cj_ind + NBNXN_GPU_JGROUP_SIZE - 1) >> NBNXN_GPU_JGROUP_SIZE_2LOG);
3599 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3605 /* Measure the size of the new entry and potentially split it */
3606 split_sci_entry(nbl, nsp_max_av, progBal, nc_bal, thread, nthread);
3611 /* Syncs the working array before adding another grid pair to the list */
3612 static void sync_work(nbnxn_pairlist_t *nbl)
3616 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3617 nbl->work->cj4_init = nbl->ncj4;
3621 /* Clears an nbnxn_pairlist_t data structure */
3622 static void clear_pairlist(nbnxn_pairlist_t *nbl)
3631 nbl->work->ncj_noq = 0;
3632 nbl->work->ncj_hlj = 0;
3635 /* Sets a simple list i-cell bounding box, including PBC shift */
3636 static void set_icell_bb_simple(const float *bb, int ci,
3637 real shx, real shy, real shz,
3643 bb_ci[BBL_X] = bb[ia+BBL_X] + shx;
3644 bb_ci[BBL_Y] = bb[ia+BBL_Y] + shy;
3645 bb_ci[BBL_Z] = bb[ia+BBL_Z] + shz;
3646 bb_ci[BBU_X] = bb[ia+BBU_X] + shx;
3647 bb_ci[BBU_Y] = bb[ia+BBU_Y] + shy;
3648 bb_ci[BBU_Z] = bb[ia+BBU_Z] + shz;
3651 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
3652 static void set_icell_bb_supersub(const float *bb, int ci,
3653 real shx, real shy, real shz,
3659 ia = ci*(GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX;
3660 for (m = 0; m < (GPU_NSUBCELL>>STRIDE_PBB_2LOG)*NNBSBB_XXXX; m += NNBSBB_XXXX)
3662 for (i = 0; i < STRIDE_PBB; i++)
3664 bb_ci[m+0*STRIDE_PBB+i] = bb[ia+m+0*STRIDE_PBB+i] + shx;
3665 bb_ci[m+1*STRIDE_PBB+i] = bb[ia+m+1*STRIDE_PBB+i] + shy;
3666 bb_ci[m+2*STRIDE_PBB+i] = bb[ia+m+2*STRIDE_PBB+i] + shz;
3667 bb_ci[m+3*STRIDE_PBB+i] = bb[ia+m+3*STRIDE_PBB+i] + shx;
3668 bb_ci[m+4*STRIDE_PBB+i] = bb[ia+m+4*STRIDE_PBB+i] + shy;
3669 bb_ci[m+5*STRIDE_PBB+i] = bb[ia+m+5*STRIDE_PBB+i] + shz;
3673 ia = ci*GPU_NSUBCELL*NNBSBB_B;
3674 for (i = 0; i < GPU_NSUBCELL*NNBSBB_B; i += NNBSBB_B)
3676 bb_ci[i+BBL_X] = bb[ia+i+BBL_X] + shx;
3677 bb_ci[i+BBL_Y] = bb[ia+i+BBL_Y] + shy;
3678 bb_ci[i+BBL_Z] = bb[ia+i+BBL_Z] + shz;
3679 bb_ci[i+BBU_X] = bb[ia+i+BBU_X] + shx;
3680 bb_ci[i+BBU_Y] = bb[ia+i+BBU_Y] + shy;
3681 bb_ci[i+BBU_Z] = bb[ia+i+BBU_Z] + shz;
3686 /* Copies PBC shifted i-cell atom coordinates x,y,z to working array */
3687 static void icell_set_x_simple(int ci,
3688 real shx, real shy, real shz,
3689 int gmx_unused na_c,
3690 int stride, const real *x,
3691 nbnxn_list_work_t *work)
3695 ia = ci*NBNXN_CPU_CLUSTER_I_SIZE;
3697 for (i = 0; i < NBNXN_CPU_CLUSTER_I_SIZE; i++)
3699 work->x_ci[i*STRIDE_XYZ+XX] = x[(ia+i)*stride+XX] + shx;
3700 work->x_ci[i*STRIDE_XYZ+YY] = x[(ia+i)*stride+YY] + shy;
3701 work->x_ci[i*STRIDE_XYZ+ZZ] = x[(ia+i)*stride+ZZ] + shz;
3705 /* Copies PBC shifted super-cell atom coordinates x,y,z to working array */
3706 static void icell_set_x_supersub(int ci,
3707 real shx, real shy, real shz,
3709 int stride, const real *x,
3710 nbnxn_list_work_t *work)
3717 ia = ci*GPU_NSUBCELL*na_c;
3718 for (i = 0; i < GPU_NSUBCELL*na_c; i++)
3720 x_ci[i*DIM + XX] = x[(ia+i)*stride + XX] + shx;
3721 x_ci[i*DIM + YY] = x[(ia+i)*stride + YY] + shy;
3722 x_ci[i*DIM + ZZ] = x[(ia+i)*stride + ZZ] + shz;
3726 #ifdef NBNXN_SEARCH_BB_SSE
3727 /* Copies PBC shifted super-cell packed atom coordinates to working array */
3728 static void icell_set_x_supersub_sse8(int ci,
3729 real shx, real shy, real shz,
3731 int stride, const real *x,
3732 nbnxn_list_work_t *work)
3734 int si, io, ia, i, j;
3739 for (si = 0; si < GPU_NSUBCELL; si++)
3741 for (i = 0; i < na_c; i += STRIDE_PBB)
3744 ia = ci*GPU_NSUBCELL*na_c + io;
3745 for (j = 0; j < STRIDE_PBB; j++)
3747 x_ci[io*DIM + j + XX*STRIDE_PBB] = x[(ia+j)*stride+XX] + shx;
3748 x_ci[io*DIM + j + YY*STRIDE_PBB] = x[(ia+j)*stride+YY] + shy;
3749 x_ci[io*DIM + j + ZZ*STRIDE_PBB] = x[(ia+j)*stride+ZZ] + shz;
3756 static real nbnxn_rlist_inc_nonloc_fac = 0.6;
3758 /* Due to the cluster size the effective pair-list is longer than
3759 * that of a simple atom pair-list. This function gives the extra distance.
3761 real nbnxn_get_rlist_effective_inc(int cluster_size, real atom_density)
3763 return ((0.5 + nbnxn_rlist_inc_nonloc_fac)*sqr(((cluster_size) - 1.0)/(cluster_size))*pow((cluster_size)/(atom_density), 1.0/3.0));
3766 /* Estimates the interaction volume^2 for non-local interactions */
3767 static real nonlocal_vol2(const gmx_domdec_zones_t *zones, rvec ls, real r)
3776 /* Here we simply add up the volumes of 1, 2 or 3 1D decomposition
3777 * not home interaction volume^2. As these volumes are not additive,
3778 * this is an overestimate, but it would only be significant in the limit
3779 * of small cells, where we anyhow need to split the lists into
3780 * as small parts as possible.
3783 for (z = 0; z < zones->n; z++)
3785 if (zones->shift[z][XX] + zones->shift[z][YY] + zones->shift[z][ZZ] == 1)
3790 for (d = 0; d < DIM; d++)
3792 if (zones->shift[z][d] == 0)
3796 za *= zones->size[z].x1[d] - zones->size[z].x0[d];
3800 /* 4 octants of a sphere */
3801 vold_est = 0.25*M_PI*r*r*r*r;
3802 /* 4 quarter pie slices on the edges */
3803 vold_est += 4*cl*M_PI/6.0*r*r*r;
3804 /* One rectangular volume on a face */
3805 vold_est += ca*0.5*r*r;
3807 vol2_est_tot += vold_est*za;
3811 return vol2_est_tot;
3814 /* Estimates the average size of a full j-list for super/sub setup */
3815 static int get_nsubpair_max(const nbnxn_search_t nbs,
3818 int min_ci_balanced)
3820 const nbnxn_grid_t *grid;
3822 real xy_diag2, r_eff_sup, vol_est, nsp_est, nsp_est_nl;
3825 grid = &nbs->grid[0];
3827 ls[XX] = (grid->c1[XX] - grid->c0[XX])/(grid->ncx*GPU_NSUBCELL_X);
3828 ls[YY] = (grid->c1[YY] - grid->c0[YY])/(grid->ncy*GPU_NSUBCELL_Y);
3829 ls[ZZ] = (grid->c1[ZZ] - grid->c0[ZZ])*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z);
3831 /* The average squared length of the diagonal of a sub cell */
3832 xy_diag2 = ls[XX]*ls[XX] + ls[YY]*ls[YY] + ls[ZZ]*ls[ZZ];
3834 /* The formulas below are a heuristic estimate of the average nsj per si*/
3835 r_eff_sup = rlist + nbnxn_rlist_inc_nonloc_fac*sqr((grid->na_c - 1.0)/grid->na_c)*sqrt(xy_diag2/3);
3837 if (!nbs->DomDec || nbs->zones->n == 1)
3844 sqr(grid->atom_density/grid->na_c)*
3845 nonlocal_vol2(nbs->zones, ls, r_eff_sup);
3850 /* Sub-cell interacts with itself */
3851 vol_est = ls[XX]*ls[YY]*ls[ZZ];
3852 /* 6/2 rectangular volume on the faces */
3853 vol_est += (ls[XX]*ls[YY] + ls[XX]*ls[ZZ] + ls[YY]*ls[ZZ])*r_eff_sup;
3854 /* 12/2 quarter pie slices on the edges */
3855 vol_est += 2*(ls[XX] + ls[YY] + ls[ZZ])*0.25*M_PI*sqr(r_eff_sup);
3856 /* 4 octants of a sphere */
3857 vol_est += 0.5*4.0/3.0*M_PI*pow(r_eff_sup, 3);
3859 nsp_est = grid->nsubc_tot*vol_est*grid->atom_density/grid->na_c;
3861 /* Subtract the non-local pair count */
3862 nsp_est -= nsp_est_nl;
3866 fprintf(debug, "nsp_est local %5.1f non-local %5.1f\n",
3867 nsp_est, nsp_est_nl);
3872 nsp_est = nsp_est_nl;
3875 if (min_ci_balanced <= 0 || grid->nc >= min_ci_balanced || grid->nc == 0)
3877 /* We don't need to worry */
3882 /* Thus the (average) maximum j-list size should be as follows */
3883 nsubpair_max = max(1, (int)(nsp_est/min_ci_balanced+0.5));
3885 /* Since the target value is a maximum (this avoids high outliers,
3886 * which lead to load imbalance), not average, we add half the
3887 * number of pairs in a cj4 block to get the average about right.
3889 nsubpair_max += GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE/2;
3894 fprintf(debug, "nbl nsp estimate %.1f, nsubpair_max %d\n",
3895 nsp_est, nsubpair_max);
3898 return nsubpair_max;
3901 /* Debug list print function */
3902 static void print_nblist_ci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3906 for (i = 0; i < nbl->nci; i++)
3908 fprintf(fp, "ci %4d shift %2d ncj %3d\n",
3909 nbl->ci[i].ci, nbl->ci[i].shift,
3910 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start);
3912 for (j = nbl->ci[i].cj_ind_start; j < nbl->ci[i].cj_ind_end; j++)
3914 fprintf(fp, " cj %5d imask %x\n",
3921 /* Debug list print function */
3922 static void print_nblist_sci_cj(FILE *fp, const nbnxn_pairlist_t *nbl)
3924 int i, j4, j, ncp, si;
3926 for (i = 0; i < nbl->nsci; i++)
3928 fprintf(fp, "ci %4d shift %2d ncj4 %2d\n",
3929 nbl->sci[i].sci, nbl->sci[i].shift,
3930 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start);
3933 for (j4 = nbl->sci[i].cj4_ind_start; j4 < nbl->sci[i].cj4_ind_end; j4++)
3935 for (j = 0; j < NBNXN_GPU_JGROUP_SIZE; j++)
3937 fprintf(fp, " sj %5d imask %x\n",
3939 nbl->cj4[j4].imei[0].imask);
3940 for (si = 0; si < GPU_NSUBCELL; si++)
3942 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
3949 fprintf(fp, "ci %4d shift %2d ncj4 %2d ncp %3d\n",
3950 nbl->sci[i].sci, nbl->sci[i].shift,
3951 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start,
3956 /* Combine pair lists *nbl generated on multiple threads nblc */
3957 static void combine_nblists(int nnbl, nbnxn_pairlist_t **nbl,
3958 nbnxn_pairlist_t *nblc)
3960 int nsci, ncj4, nexcl;
3965 gmx_incons("combine_nblists does not support simple lists");
3970 nexcl = nblc->nexcl;
3971 for (i = 0; i < nnbl; i++)
3973 nsci += nbl[i]->nsci;
3974 ncj4 += nbl[i]->ncj4;
3975 nexcl += nbl[i]->nexcl;
3978 if (nsci > nblc->sci_nalloc)
3980 nb_realloc_sci(nblc, nsci);
3982 if (ncj4 > nblc->cj4_nalloc)
3984 nblc->cj4_nalloc = over_alloc_small(ncj4);
3985 nbnxn_realloc_void((void **)&nblc->cj4,
3986 nblc->ncj4*sizeof(*nblc->cj4),
3987 nblc->cj4_nalloc*sizeof(*nblc->cj4),
3988 nblc->alloc, nblc->free);
3990 if (nexcl > nblc->excl_nalloc)
3992 nblc->excl_nalloc = over_alloc_small(nexcl);
3993 nbnxn_realloc_void((void **)&nblc->excl,
3994 nblc->nexcl*sizeof(*nblc->excl),
3995 nblc->excl_nalloc*sizeof(*nblc->excl),
3996 nblc->alloc, nblc->free);
3999 /* Each thread should copy its own data to the combined arrays,
4000 * as otherwise data will go back and forth between different caches.
4002 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
4003 for (n = 0; n < nnbl; n++)
4010 const nbnxn_pairlist_t *nbli;
4012 /* Determine the offset in the combined data for our thread */
4013 sci_offset = nblc->nsci;
4014 cj4_offset = nblc->ncj4;
4015 ci_offset = nblc->nci_tot;
4016 excl_offset = nblc->nexcl;
4018 for (i = 0; i < n; i++)
4020 sci_offset += nbl[i]->nsci;
4021 cj4_offset += nbl[i]->ncj4;
4022 ci_offset += nbl[i]->nci_tot;
4023 excl_offset += nbl[i]->nexcl;
4028 for (i = 0; i < nbli->nsci; i++)
4030 nblc->sci[sci_offset+i] = nbli->sci[i];
4031 nblc->sci[sci_offset+i].cj4_ind_start += cj4_offset;
4032 nblc->sci[sci_offset+i].cj4_ind_end += cj4_offset;
4035 for (j4 = 0; j4 < nbli->ncj4; j4++)
4037 nblc->cj4[cj4_offset+j4] = nbli->cj4[j4];
4038 nblc->cj4[cj4_offset+j4].imei[0].excl_ind += excl_offset;
4039 nblc->cj4[cj4_offset+j4].imei[1].excl_ind += excl_offset;
4042 for (j4 = 0; j4 < nbli->nexcl; j4++)
4044 nblc->excl[excl_offset+j4] = nbli->excl[j4];
4048 for (n = 0; n < nnbl; n++)
4050 nblc->nsci += nbl[n]->nsci;
4051 nblc->ncj4 += nbl[n]->ncj4;
4052 nblc->nci_tot += nbl[n]->nci_tot;
4053 nblc->nexcl += nbl[n]->nexcl;
4057 /* Returns the next ci to be processes by our thread */
4058 static gmx_bool next_ci(const nbnxn_grid_t *grid,
4060 int nth, int ci_block,
4061 int *ci_x, int *ci_y,
4067 if (*ci_b == ci_block)
4069 /* Jump to the next block assigned to this task */
4070 *ci += (nth - 1)*ci_block;
4074 if (*ci >= grid->nc*conv)
4079 while (*ci >= grid->cxy_ind[*ci_x*grid->ncy + *ci_y + 1]*conv)
4082 if (*ci_y == grid->ncy)
4092 /* Returns the distance^2 for which we put cell pairs in the list
4093 * without checking atom pair distances. This is usually < rlist^2.
4095 static float boundingbox_only_distance2(const nbnxn_grid_t *gridi,
4096 const nbnxn_grid_t *gridj,
4100 /* If the distance between two sub-cell bounding boxes is less
4101 * than this distance, do not check the distance between
4102 * all particle pairs in the sub-cell, since then it is likely
4103 * that the box pair has atom pairs within the cut-off.
4104 * We use the nblist cut-off minus 0.5 times the average x/y diagonal
4105 * spacing of the sub-cells. Around 40% of the checked pairs are pruned.
4106 * Using more than 0.5 gains at most 0.5%.
4107 * If forces are calculated more than twice, the performance gain
4108 * in the force calculation outweighs the cost of checking.
4109 * Note that with subcell lists, the atom-pair distance check
4110 * is only performed when only 1 out of 8 sub-cells in within range,
4111 * this is because the GPU is much faster than the cpu.
4116 bbx = 0.5*(gridi->sx + gridj->sx);
4117 bby = 0.5*(gridi->sy + gridj->sy);
4120 bbx /= GPU_NSUBCELL_X;
4121 bby /= GPU_NSUBCELL_Y;
4124 rbb2 = sqr(max(0, rlist - 0.5*sqrt(bbx*bbx + bby*bby)));
4129 return (float)((1+GMX_FLOAT_EPS)*rbb2);
4133 static int get_ci_block_size(const nbnxn_grid_t *gridi,
4134 gmx_bool bDomDec, int nth)
4136 const int ci_block_enum = 5;
4137 const int ci_block_denom = 11;
4138 const int ci_block_min_atoms = 16;
4141 /* Here we decide how to distribute the blocks over the threads.
4142 * We use prime numbers to try to avoid that the grid size becomes
4143 * a multiple of the number of threads, which would lead to some
4144 * threads getting "inner" pairs and others getting boundary pairs,
4145 * which in turns will lead to load imbalance between threads.
4146 * Set the block size as 5/11/ntask times the average number of cells
4147 * in a y,z slab. This should ensure a quite uniform distribution
4148 * of the grid parts of the different thread along all three grid
4149 * zone boundaries with 3D domain decomposition. At the same time
4150 * the blocks will not become too small.
4152 ci_block = (gridi->nc*ci_block_enum)/(ci_block_denom*gridi->ncx*nth);
4154 /* Ensure the blocks are not too small: avoids cache invalidation */
4155 if (ci_block*gridi->na_sc < ci_block_min_atoms)
4157 ci_block = (ci_block_min_atoms + gridi->na_sc - 1)/gridi->na_sc;
4160 /* Without domain decomposition
4161 * or with less than 3 blocks per task, divide in nth blocks.
4163 if (!bDomDec || ci_block*3*nth > gridi->nc)
4165 ci_block = (gridi->nc + nth - 1)/nth;
4171 /* Generates the part of pair-list nbl assigned to our thread */
4172 static void nbnxn_make_pairlist_part(const nbnxn_search_t nbs,
4173 const nbnxn_grid_t *gridi,
4174 const nbnxn_grid_t *gridj,
4175 nbnxn_search_work_t *work,
4176 const nbnxn_atomdata_t *nbat,
4177 const t_blocka *excl,
4181 gmx_bool bFBufferFlag,
4184 int min_ci_balanced,
4186 nbnxn_pairlist_t *nbl)
4193 int ci_b, ci, ci_x, ci_y, ci_xy, cj;
4199 int conv_i, cell0_i;
4200 const float *bb_i, *bbcz_i, *bbcz_j;
4202 real bx0, bx1, by0, by1, bz0, bz1;
4204 real d2cx, d2z, d2z_cx, d2z_cy, d2zx, d2zxy, d2xy;
4205 int cxf, cxl, cyf, cyf_x, cyl;
4207 int c0, c1, cs, cf, cl;
4210 int gridi_flag_shift = 0, gridj_flag_shift = 0;
4211 unsigned *gridj_flag = NULL;
4212 int ncj_old_i, ncj_old_j;
4214 nbs_cycle_start(&work->cc[enbsCCsearch]);
4216 if (gridj->bSimple != nbl->bSimple)
4218 gmx_incons("Grid incompatible with pair-list");
4222 nbl->na_sc = gridj->na_sc;
4223 nbl->na_ci = gridj->na_c;
4224 nbl->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
4225 na_cj_2log = get_2log(nbl->na_cj);
4231 /* Determine conversion of clusters to flag blocks */
4232 gridi_flag_shift = 0;
4233 while ((nbl->na_ci<<gridi_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4237 gridj_flag_shift = 0;
4238 while ((nbl->na_cj<<gridj_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4243 gridj_flag = work->buffer_flags.flag;
4246 copy_mat(nbs->box, box);
4248 rl2 = nbl->rlist*nbl->rlist;
4250 rbb2 = boundingbox_only_distance2(gridi, gridj, nbl->rlist, nbl->bSimple);
4254 fprintf(debug, "nbl bounding box only distance %f\n", sqrt(rbb2));
4257 /* Set the shift range */
4258 for (d = 0; d < DIM; d++)
4260 /* Check if we need periodicity shifts.
4261 * Without PBC or with domain decomposition we don't need them.
4263 if (d >= ePBC2npbcdim(nbs->ePBC) || nbs->dd_dim[d])
4270 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
4281 if (nbl->bSimple && !gridi->bSimple)
4283 conv_i = gridi->na_sc/gridj->na_sc;
4284 bb_i = gridi->bb_simple;
4285 bbcz_i = gridi->bbcz_simple;
4286 flags_i = gridi->flags_simple;
4292 bbcz_i = gridi->bbcz;
4293 flags_i = gridi->flags;
4295 cell0_i = gridi->cell0*conv_i;
4297 bbcz_j = gridj->bbcz;
4301 /* Blocks of the conversion factor - 1 give a large repeat count
4302 * combined with a small block size. This should result in good
4303 * load balancing for both small and large domains.
4305 ci_block = conv_i - 1;
4309 fprintf(debug, "nbl nc_i %d col.av. %.1f ci_block %d\n",
4310 gridi->nc, gridi->nc/(double)(gridi->ncx*gridi->ncy), ci_block);
4316 /* Initially ci_b and ci to 1 before where we want them to start,
4317 * as they will both be incremented in next_ci.
4320 ci = th*ci_block - 1;
4323 while (next_ci(gridi, conv_i, nth, ci_block, &ci_x, &ci_y, &ci_b, &ci))
4325 if (nbl->bSimple && flags_i[ci] == 0)
4330 ncj_old_i = nbl->ncj;
4333 if (gridj != gridi && shp[XX] == 0)
4337 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX];
4341 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx;
4343 if (bx1 < gridj->c0[XX])
4345 d2cx = sqr(gridj->c0[XX] - bx1);
4354 ci_xy = ci_x*gridi->ncy + ci_y;
4356 /* Loop over shift vectors in three dimensions */
4357 for (tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
4359 shz = tz*box[ZZ][ZZ];
4361 bz0 = bbcz_i[ci*NNBSBB_D ] + shz;
4362 bz1 = bbcz_i[ci*NNBSBB_D+1] + shz;
4374 d2z = sqr(bz0 - box[ZZ][ZZ]);
4377 d2z_cx = d2z + d2cx;
4385 bz1/((real)(gridi->cxy_ind[ci_xy+1] - gridi->cxy_ind[ci_xy]));
4390 /* The check with bz1_frac close to or larger than 1 comes later */
4392 for (ty = -shp[YY]; ty <= shp[YY]; ty++)
4394 shy = ty*box[YY][YY] + tz*box[ZZ][YY];
4398 by0 = bb_i[ci*NNBSBB_B +YY] + shy;
4399 by1 = bb_i[ci*NNBSBB_B+NNBSBB_C+YY] + shy;
4403 by0 = gridi->c0[YY] + (ci_y )*gridi->sy + shy;
4404 by1 = gridi->c0[YY] + (ci_y+1)*gridi->sy + shy;
4407 get_cell_range(by0, by1,
4408 gridj->ncy, gridj->c0[YY], gridj->sy, gridj->inv_sy,
4418 if (by1 < gridj->c0[YY])
4420 d2z_cy += sqr(gridj->c0[YY] - by1);
4422 else if (by0 > gridj->c1[YY])
4424 d2z_cy += sqr(by0 - gridj->c1[YY]);
4427 for (tx = -shp[XX]; tx <= shp[XX]; tx++)
4429 shift = XYZ2IS(tx, ty, tz);
4431 #ifdef NBNXN_SHIFT_BACKWARD
4432 if (gridi == gridj && shift > CENTRAL)
4438 shx = tx*box[XX][XX] + ty*box[YY][XX] + tz*box[ZZ][XX];
4442 bx0 = bb_i[ci*NNBSBB_B +XX] + shx;
4443 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX] + shx;
4447 bx0 = gridi->c0[XX] + (ci_x )*gridi->sx + shx;
4448 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx + shx;
4451 get_cell_range(bx0, bx1,
4452 gridj->ncx, gridj->c0[XX], gridj->sx, gridj->inv_sx,
4463 new_ci_entry(nbl, cell0_i+ci, shift, flags_i[ci]);
4467 new_sci_entry(nbl, cell0_i+ci, shift);
4470 #ifndef NBNXN_SHIFT_BACKWARD
4473 if (shift == CENTRAL && gridi == gridj &&
4477 /* Leave the pairs with i > j.
4478 * x is the major index, so skip half of it.
4485 set_icell_bb_simple(bb_i, ci, shx, shy, shz,
4490 set_icell_bb_supersub(bb_i, ci, shx, shy, shz,
4494 nbs->icell_set_x(cell0_i+ci, shx, shy, shz,
4495 gridi->na_c, nbat->xstride, nbat->x,
4498 for (cx = cxf; cx <= cxl; cx++)
4501 if (gridj->c0[XX] + cx*gridj->sx > bx1)
4503 d2zx += sqr(gridj->c0[XX] + cx*gridj->sx - bx1);
4505 else if (gridj->c0[XX] + (cx+1)*gridj->sx < bx0)
4507 d2zx += sqr(gridj->c0[XX] + (cx+1)*gridj->sx - bx0);
4510 #ifndef NBNXN_SHIFT_BACKWARD
4511 if (gridi == gridj &&
4512 cx == 0 && cyf < ci_y)
4514 if (gridi == gridj &&
4515 cx == 0 && shift == CENTRAL && cyf < ci_y)
4518 /* Leave the pairs with i > j.
4519 * Skip half of y when i and j have the same x.
4528 for (cy = cyf_x; cy <= cyl; cy++)
4530 c0 = gridj->cxy_ind[cx*gridj->ncy+cy];
4531 c1 = gridj->cxy_ind[cx*gridj->ncy+cy+1];
4532 #ifdef NBNXN_SHIFT_BACKWARD
4533 if (gridi == gridj &&
4534 shift == CENTRAL && c0 < ci)
4541 if (gridj->c0[YY] + cy*gridj->sy > by1)
4543 d2zxy += sqr(gridj->c0[YY] + cy*gridj->sy - by1);
4545 else if (gridj->c0[YY] + (cy+1)*gridj->sy < by0)
4547 d2zxy += sqr(gridj->c0[YY] + (cy+1)*gridj->sy - by0);
4549 if (c1 > c0 && d2zxy < rl2)
4551 cs = c0 + (int)(bz1_frac*(c1 - c0));
4559 /* Find the lowest cell that can possibly
4564 (bbcz_j[cf*NNBSBB_D+1] >= bz0 ||
4565 d2xy + sqr(bbcz_j[cf*NNBSBB_D+1] - bz0) < rl2))
4570 /* Find the highest cell that can possibly
4575 (bbcz_j[cl*NNBSBB_D] <= bz1 ||
4576 d2xy + sqr(bbcz_j[cl*NNBSBB_D] - bz1) < rl2))
4581 #ifdef NBNXN_REFCODE
4583 /* Simple reference code, for debugging,
4584 * overrides the more complex code above.
4589 for (k = c0; k < c1; k++)
4591 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1,
4592 bb+k*NNBSBB_B) < rl2 &&
4597 if (box_dist2(bx0, bx1, by0, by1, bz0, bz1,
4598 bb+k*NNBSBB_B) < rl2 &&
4609 /* We want each atom/cell pair only once,
4610 * only use cj >= ci.
4612 #ifndef NBNXN_SHIFT_BACKWARD
4615 if (shift == CENTRAL)
4624 /* For f buffer flags with simple lists */
4625 ncj_old_j = nbl->ncj;
4627 switch (nb_kernel_type)
4629 case nbnxnk4x4_PlainC:
4630 check_subcell_list_space_simple(nbl, cl-cf+1);
4632 make_cluster_list_simple(gridj,
4634 (gridi == gridj && shift == CENTRAL),
4639 #ifdef GMX_NBNXN_SIMD_4XN
4640 case nbnxnk4xN_SIMD_4xN:
4641 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4642 make_cluster_list_simd_4xn(gridj,
4644 (gridi == gridj && shift == CENTRAL),
4650 #ifdef GMX_NBNXN_SIMD_2XNN
4651 case nbnxnk4xN_SIMD_2xNN:
4652 check_subcell_list_space_simple(nbl, ci_to_cj(na_cj_2log, cl-cf)+2);
4653 make_cluster_list_simd_2xnn(gridj,
4655 (gridi == gridj && shift == CENTRAL),
4661 case nbnxnk8x8x8_PlainC:
4662 case nbnxnk8x8x8_CUDA:
4663 check_subcell_list_space_supersub(nbl, cl-cf+1);
4664 for (cj = cf; cj <= cl; cj++)
4666 make_cluster_list_supersub(gridi, gridj,
4668 (gridi == gridj && shift == CENTRAL && ci == cj),
4669 nbat->xstride, nbat->x,
4675 ncpcheck += cl - cf + 1;
4677 if (bFBufferFlag && nbl->ncj > ncj_old_j)
4681 cbf = nbl->cj[ncj_old_j].cj >> gridj_flag_shift;
4682 cbl = nbl->cj[nbl->ncj-1].cj >> gridj_flag_shift;
4683 for (cb = cbf; cb <= cbl; cb++)
4685 gridj_flag[cb] = 1U<<th;
4693 /* Set the exclusions for this ci list */
4696 set_ci_top_excls(nbs,
4698 shift == CENTRAL && gridi == gridj,
4701 &(nbl->ci[nbl->nci]),
4706 set_sci_top_excls(nbs,
4708 shift == CENTRAL && gridi == gridj,
4710 &(nbl->sci[nbl->nsci]),
4714 /* Close this ci list */
4717 close_ci_entry_simple(nbl);
4721 close_ci_entry_supersub(nbl,
4723 progBal, min_ci_balanced,
4730 if (bFBufferFlag && nbl->ncj > ncj_old_i)
4732 work->buffer_flags.flag[(gridi->cell0+ci)>>gridi_flag_shift] = 1U<<th;
4736 work->ndistc = ndistc;
4738 nbs_cycle_stop(&work->cc[enbsCCsearch]);
4742 fprintf(debug, "number of distance checks %d\n", ndistc);
4743 fprintf(debug, "ncpcheck %s %d\n", gridi == gridj ? "local" : "non-local",
4748 print_nblist_statistics_simple(debug, nbl, nbs, rlist);
4752 print_nblist_statistics_supersub(debug, nbl, nbs, rlist);
4758 static void reduce_buffer_flags(const nbnxn_search_t nbs,
4760 const nbnxn_buffer_flags_t *dest)
4763 const unsigned *flag;
4765 for (s = 0; s < nsrc; s++)
4767 flag = nbs->work[s].buffer_flags.flag;
4769 for (b = 0; b < dest->nflag; b++)
4771 dest->flag[b] |= flag[b];
4776 static void print_reduction_cost(const nbnxn_buffer_flags_t *flags, int nout)
4778 int nelem, nkeep, ncopy, nred, b, c, out;
4784 for (b = 0; b < flags->nflag; b++)
4786 if (flags->flag[b] == 1)
4788 /* Only flag 0 is set, no copy of reduction required */
4792 else if (flags->flag[b] > 0)
4795 for (out = 0; out < nout; out++)
4797 if (flags->flag[b] & (1U<<out))
4814 fprintf(debug, "nbnxn reduction: #flag %d #list %d elem %4.2f, keep %4.2f copy %4.2f red %4.2f\n",
4816 nelem/(double)(flags->nflag),
4817 nkeep/(double)(flags->nflag),
4818 ncopy/(double)(flags->nflag),
4819 nred/(double)(flags->nflag));
4822 /* Perform a count (linear) sort to sort the smaller lists to the end.
4823 * This avoids load imbalance on the GPU, as large lists will be
4824 * scheduled and executed first and the smaller lists later.
4825 * Load balancing between multi-processors only happens at the end
4826 * and there smaller lists lead to more effective load balancing.
4827 * The sorting is done on the cj4 count, not on the actual pair counts.
4828 * Not only does this make the sort faster, but it also results in
4829 * better load balancing than using a list sorted on exact load.
4830 * This function swaps the pointer in the pair list to avoid a copy operation.
4832 static void sort_sci(nbnxn_pairlist_t *nbl)
4834 nbnxn_list_work_t *work;
4835 int m, i, s, s0, s1;
4836 nbnxn_sci_t *sci_sort;
4838 if (nbl->ncj4 <= nbl->nsci)
4840 /* nsci = 0 or all sci have size 1, sorting won't change the order */
4846 /* We will distinguish differences up to double the average */
4847 m = (2*nbl->ncj4)/nbl->nsci;
4849 if (m + 1 > work->sort_nalloc)
4851 work->sort_nalloc = over_alloc_large(m + 1);
4852 srenew(work->sort, work->sort_nalloc);
4855 if (work->sci_sort_nalloc != nbl->sci_nalloc)
4857 work->sci_sort_nalloc = nbl->sci_nalloc;
4858 nbnxn_realloc_void((void **)&work->sci_sort,
4860 work->sci_sort_nalloc*sizeof(*work->sci_sort),
4861 nbl->alloc, nbl->free);
4864 /* Count the entries of each size */
4865 for (i = 0; i <= m; i++)
4869 for (s = 0; s < nbl->nsci; s++)
4871 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4874 /* Calculate the offset for each count */
4877 for (i = m - 1; i >= 0; i--)
4880 work->sort[i] = work->sort[i + 1] + s0;
4884 /* Sort entries directly into place */
4885 sci_sort = work->sci_sort;
4886 for (s = 0; s < nbl->nsci; s++)
4888 i = min(m, nbl->sci[s].cj4_ind_end - nbl->sci[s].cj4_ind_start);
4889 sci_sort[work->sort[i]++] = nbl->sci[s];
4892 /* Swap the sci pointers so we use the new, sorted list */
4893 work->sci_sort = nbl->sci;
4894 nbl->sci = sci_sort;
4897 /* Make a local or non-local pair-list, depending on iloc */
4898 void nbnxn_make_pairlist(const nbnxn_search_t nbs,
4899 nbnxn_atomdata_t *nbat,
4900 const t_blocka *excl,
4902 int min_ci_balanced,
4903 nbnxn_pairlist_set_t *nbl_list,
4908 nbnxn_grid_t *gridi, *gridj;
4910 int nzi, zi, zj0, zj1, zj;
4914 nbnxn_pairlist_t **nbl;
4916 gmx_bool CombineNBLists;
4918 int np_tot, np_noq, np_hlj, nap;
4920 /* Check if we are running hybrid GPU + CPU nbnxn mode */
4921 bGPUCPU = (!nbs->grid[0].bSimple && nbl_list->bSimple);
4923 nnbl = nbl_list->nnbl;
4924 nbl = nbl_list->nbl;
4925 CombineNBLists = nbl_list->bCombined;
4929 fprintf(debug, "ns making %d nblists\n", nnbl);
4932 nbat->bUseBufferFlags = (nbat->nout > 1);
4933 /* We should re-init the flags before making the first list */
4934 if (nbat->bUseBufferFlags && (LOCAL_I(iloc) || bGPUCPU))
4936 init_buffer_flags(&nbat->buffer_flags, nbat->natoms);
4939 if (nbl_list->bSimple)
4941 switch (nb_kernel_type)
4943 #ifdef GMX_NBNXN_SIMD_4XN
4944 case nbnxnk4xN_SIMD_4xN:
4945 nbs->icell_set_x = icell_set_x_simd_4xn;
4948 #ifdef GMX_NBNXN_SIMD_2XNN
4949 case nbnxnk4xN_SIMD_2xNN:
4950 nbs->icell_set_x = icell_set_x_simd_2xnn;
4954 nbs->icell_set_x = icell_set_x_simple;
4960 #ifdef NBNXN_SEARCH_BB_SSE
4961 nbs->icell_set_x = icell_set_x_supersub_sse8;
4963 nbs->icell_set_x = icell_set_x_supersub;
4969 /* Only zone (grid) 0 vs 0 */
4976 nzi = nbs->zones->nizone;
4979 if (!nbl_list->bSimple && min_ci_balanced > 0)
4981 nsubpair_max = get_nsubpair_max(nbs, iloc, rlist, min_ci_balanced);
4988 /* Clear all pair-lists */
4989 for (th = 0; th < nnbl; th++)
4991 clear_pairlist(nbl[th]);
4994 for (zi = 0; zi < nzi; zi++)
4996 gridi = &nbs->grid[zi];
4998 if (NONLOCAL_I(iloc))
5000 zj0 = nbs->zones->izone[zi].j0;
5001 zj1 = nbs->zones->izone[zi].j1;
5007 for (zj = zj0; zj < zj1; zj++)
5009 gridj = &nbs->grid[zj];
5013 fprintf(debug, "ns search grid %d vs %d\n", zi, zj);
5016 nbs_cycle_start(&nbs->cc[enbsCCsearch]);
5018 if (nbl[0]->bSimple && !gridi->bSimple)
5020 /* Hybrid list, determine blocking later */
5025 ci_block = get_ci_block_size(gridi, nbs->DomDec, nnbl);
5028 #pragma omp parallel for num_threads(nnbl) schedule(static)
5029 for (th = 0; th < nnbl; th++)
5031 /* Re-init the thread-local work flag data before making
5032 * the first list (not an elegant conditional).
5034 if (nbat->bUseBufferFlags && ((zi == 0 && zj == 0) ||
5035 (bGPUCPU && zi == 0 && zj == 1)))
5037 init_buffer_flags(&nbs->work[th].buffer_flags, nbat->natoms);
5040 if (CombineNBLists && th > 0)
5042 clear_pairlist(nbl[th]);
5045 /* With GPU: generate progressively smaller lists for
5046 * load balancing for local only or non-local with 2 zones.
5048 progBal = (LOCAL_I(iloc) || nbs->zones->n <= 2);
5050 /* Divide the i super cell equally over the nblists */
5051 nbnxn_make_pairlist_part(nbs, gridi, gridj,
5052 &nbs->work[th], nbat, excl,
5056 nbat->bUseBufferFlags,
5058 progBal, min_ci_balanced,
5062 nbs_cycle_stop(&nbs->cc[enbsCCsearch]);
5067 for (th = 0; th < nnbl; th++)
5069 inc_nrnb(nrnb, eNR_NBNXN_DIST2, nbs->work[th].ndistc);
5071 if (nbl_list->bSimple)
5073 np_tot += nbl[th]->ncj;
5074 np_noq += nbl[th]->work->ncj_noq;
5075 np_hlj += nbl[th]->work->ncj_hlj;
5079 /* This count ignores potential subsequent pair pruning */
5080 np_tot += nbl[th]->nci_tot;
5083 nap = nbl[0]->na_ci*nbl[0]->na_cj;
5084 nbl_list->natpair_ljq = (np_tot - np_noq)*nap - np_hlj*nap/2;
5085 nbl_list->natpair_lj = np_noq*nap;
5086 nbl_list->natpair_q = np_hlj*nap/2;
5088 if (CombineNBLists && nnbl > 1)
5090 nbs_cycle_start(&nbs->cc[enbsCCcombine]);
5092 combine_nblists(nnbl-1, nbl+1, nbl[0]);
5094 nbs_cycle_stop(&nbs->cc[enbsCCcombine]);
5099 if (!nbl_list->bSimple)
5101 /* Sort the entries on size, large ones first */
5102 if (CombineNBLists || nnbl == 1)
5108 #pragma omp parallel for num_threads(nnbl) schedule(static)
5109 for (th = 0; th < nnbl; th++)
5116 if (nbat->bUseBufferFlags)
5118 reduce_buffer_flags(nbs, nnbl, &nbat->buffer_flags);
5121 /* Special performance logging stuff (env.var. GMX_NBNXN_CYCLE) */
5124 nbs->search_count++;
5126 if (nbs->print_cycles &&
5127 (!nbs->DomDec || (nbs->DomDec && !LOCAL_I(iloc))) &&
5128 nbs->search_count % 100 == 0)
5130 nbs_cycle_print(stderr, nbs);
5133 if (debug && (CombineNBLists && nnbl > 1))
5135 if (nbl[0]->bSimple)
5137 print_nblist_statistics_simple(debug, nbl[0], nbs, rlist);
5141 print_nblist_statistics_supersub(debug, nbl[0], nbs, rlist);
5149 if (nbl[0]->bSimple)
5151 print_nblist_ci_cj(debug, nbl[0]);
5155 print_nblist_sci_cj(debug, nbl[0]);
5159 if (nbat->bUseBufferFlags)
5161 print_reduction_cost(&nbat->buffer_flags, nnbl);