1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2012, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
45 #include "nbnxn_consts.h"
46 #include "nbnxn_internal.h"
47 #include "nbnxn_atomdata.h"
48 #include "nbnxn_search.h"
49 #include "gmx_cyclecounter.h"
51 #include "gmx_omp_nthreads.h"
55 /* Pair search box lower and upper corner in x,y,z.
56 * Store this in 4 iso 3 reals, which is useful with SSE.
57 * To avoid complicating the code we also use 4 without SSE.
60 #define NNBSBB_B (2*NNBSBB_C)
61 /* Pair search box lower and upper bound in z only. */
63 /* Pair search box lower and upper corner x,y,z indices */
72 #ifdef NBNXN_SEARCH_SSE
75 #define NBNXN_SEARCH_SSE_SINGLE
76 #include "gmx_x86_simd_single.h"
78 #include "gmx_x86_simd_double.h"
81 #if defined NBNXN_SEARCH_SSE_SINGLE && GPU_NSUBCELL == 8
85 /* The width of SSE/AVX128 with single precision for bounding boxes with GPU.
86 * Here AVX-256 turns out to be slightly slower than AVX-128.
89 #define STRIDE_8BB_2LOG 2
92 /* The functions below are macros as they are performance sensitive */
94 /* 4x4 list, pack=4: no complex conversion required */
95 /* i-cluster to j-cluster conversion */
96 #define CI_TO_CJ_J4(ci) (ci)
97 /* cluster index to coordinate array index conversion */
98 #define X_IND_CI_J4(ci) ((ci)*STRIDE_P4)
99 #define X_IND_CJ_J4(cj) ((cj)*STRIDE_P4)
101 /* 4x2 list, pack=4: j-cluster size is half the packing width */
102 /* i-cluster to j-cluster conversion */
103 #define CI_TO_CJ_J2(ci) ((ci)<<1)
104 /* cluster index to coordinate array index conversion */
105 #define X_IND_CI_J2(ci) ((ci)*STRIDE_P4)
106 #define X_IND_CJ_J2(cj) (((cj)>>1)*STRIDE_P4 + ((cj) & 1)*(PACK_X4>>1))
108 /* 4x8 list, pack=8: i-cluster size is half the packing width */
109 /* i-cluster to j-cluster conversion */
110 #define CI_TO_CJ_J8(ci) ((ci)>>1)
111 /* cluster index to coordinate array index conversion */
112 #define X_IND_CI_J8(ci) (((ci)>>1)*STRIDE_P8 + ((ci) & 1)*(PACK_X8>>1))
113 #define X_IND_CJ_J8(cj) ((cj)*STRIDE_P8)
115 /* The j-cluster size is matched to the SIMD width */
117 /* 128 bits can hold 4 floats */
118 #define CI_TO_CJ_S128(ci) CI_TO_CJ_J4(ci)
119 #define X_IND_CI_S128(ci) X_IND_CI_J4(ci)
120 #define X_IND_CJ_S128(cj) X_IND_CJ_J4(cj)
121 /* 256 bits can hold 8 floats */
122 #define CI_TO_CJ_S256(ci) CI_TO_CJ_J8(ci)
123 #define X_IND_CI_S256(ci) X_IND_CI_J8(ci)
124 #define X_IND_CJ_S256(cj) X_IND_CJ_J8(cj)
126 /* 128 bits can hold 2 doubles */
127 #define CI_TO_CJ_S128(ci) CI_TO_CJ_J2(ci)
128 #define X_IND_CI_S128(ci) X_IND_CI_J2(ci)
129 #define X_IND_CJ_S128(cj) X_IND_CJ_J2(cj)
130 /* 256 bits can hold 4 doubles */
131 #define CI_TO_CJ_S256(ci) CI_TO_CJ_J4(ci)
132 #define X_IND_CI_S256(ci) X_IND_CI_J4(ci)
133 #define X_IND_CJ_S256(cj) X_IND_CJ_J4(cj)
136 #endif /* NBNXN_SEARCH_SSE */
139 /* Interaction masks for 4xN atom interactions.
140 * Bit i*CJ_SIZE + j tells if atom i and j interact.
142 /* All interaction mask is the same for all kernels */
143 #define NBNXN_INT_MASK_ALL 0xffffffff
144 /* 4x4 kernel diagonal mask */
145 #define NBNXN_INT_MASK_DIAG 0x08ce
146 /* 4x2 kernel diagonal masks */
147 #define NBNXN_INT_MASK_DIAG_J2_0 0x0002
148 #define NBNXN_INT_MASK_DIAG_J2_1 0x002F
149 /* 4x8 kernel diagonal masks */
150 #define NBNXN_INT_MASK_DIAG_J8_0 0xf0f8fcfe
151 #define NBNXN_INT_MASK_DIAG_J8_1 0x0080c0e0
154 #ifdef NBNXN_SEARCH_SSE
155 /* Store bounding boxes corners as quadruplets: xxxxyyyyzzzz */
157 /* Size of bounding box corners quadruplet */
158 #define NNBSBB_XXXX (NNBSBB_D*DIM*STRIDE_8BB)
161 /* We shift the i-particles backward for PBC.
162 * This leads to more conditionals than shifting forward.
163 * We do this to get more balanced pair lists.
165 #define NBNXN_SHIFT_BACKWARD
168 /* This define is a lazy way to avoid interdependence of the grid
169 * and searching data structures.
171 #define NBNXN_NA_SC_MAX (GPU_NSUBCELL*NBNXN_GPU_CLUSTER_SIZE)
174 static void nbs_cycle_clear(nbnxn_cycle_t *cc)
178 for(i=0; i<enbsCCnr; i++)
185 static double Mcyc_av(const nbnxn_cycle_t *cc)
187 return (double)cc->c*1e-6/cc->count;
190 static void nbs_cycle_print(FILE *fp,const nbnxn_search_t nbs)
196 fprintf(fp,"ns %4d grid %4.1f search %4.1f red.f %5.3f",
197 nbs->cc[enbsCCgrid].count,
198 Mcyc_av(&nbs->cc[enbsCCgrid]),
199 Mcyc_av(&nbs->cc[enbsCCsearch]),
200 Mcyc_av(&nbs->cc[enbsCCreducef]));
202 if (nbs->nthread_max > 1)
204 if (nbs->cc[enbsCCcombine].count > 0)
206 fprintf(fp," comb %5.2f",
207 Mcyc_av(&nbs->cc[enbsCCcombine]));
209 fprintf(fp," s. th");
210 for(t=0; t<nbs->nthread_max; t++)
213 Mcyc_av(&nbs->work[t].cc[enbsCCsearch]));
219 static void nbnxn_grid_init(nbnxn_grid_t * grid)
222 grid->cxy_ind = NULL;
223 grid->cxy_nalloc = 0;
229 static int get_2log(int n)
234 while ((1<<log2) < n)
240 gmx_fatal(FARGS,"nbnxn na_c (%d) is not a power of 2",n);
246 static int nbnxn_kernel_to_ci_size(int nb_kernel_type)
248 switch (nb_kernel_type)
251 case nbk4xN_X86_SIMD128:
252 case nbk4xN_X86_SIMD256:
253 return NBNXN_CPU_CLUSTER_I_SIZE;
255 case nbk8x8x8_PlainC:
256 /* The cluster size for super/sub lists is only set here.
257 * Any value should work for the pair-search and atomdata code.
258 * The kernels, of course, might require a particular value.
260 return NBNXN_GPU_CLUSTER_SIZE;
262 gmx_incons("unknown kernel type");
268 int nbnxn_kernel_to_cj_size(int nb_kernel_type)
270 switch (nb_kernel_type)
273 return NBNXN_CPU_CLUSTER_I_SIZE;
274 case nbk4xN_X86_SIMD128:
275 /* Number of reals that fit in SIMD (128 bits = 16 bytes) */
276 return 16/sizeof(real);
277 case nbk4xN_X86_SIMD256:
278 /* Number of reals that fit in SIMD (256 bits = 32 bytes) */
279 return 32/sizeof(real);
281 case nbk8x8x8_PlainC:
282 return nbnxn_kernel_to_ci_size(nb_kernel_type);
284 gmx_incons("unknown kernel type");
290 static int ci_to_cj(int na_cj_2log,int ci)
294 case 2: return ci; break;
295 case 1: return (ci<<1); break;
296 case 3: return (ci>>1); break;
302 gmx_bool nbnxn_kernel_pairlist_simple(int nb_kernel_type)
304 if (nb_kernel_type == nbkNotSet)
306 gmx_fatal(FARGS, "Non-bonded kernel type not set for Verlet-style pair-list.");
309 switch (nb_kernel_type)
312 case nbk8x8x8_PlainC:
316 case nbk4xN_X86_SIMD128:
317 case nbk4xN_X86_SIMD256:
321 gmx_incons("Invalid nonbonded kernel type passed!");
326 void nbnxn_init_search(nbnxn_search_t * nbs_ptr,
328 gmx_domdec_zones_t *zones,
337 nbs->DomDec = (n_dd_cells != NULL);
339 clear_ivec(nbs->dd_dim);
347 if ((*n_dd_cells)[d] > 1)
350 /* Each grid matches a DD zone */
356 snew(nbs->grid,nbs->ngrid);
357 for(g=0; g<nbs->ngrid; g++)
359 nbnxn_grid_init(&nbs->grid[g]);
362 nbs->cell_nalloc = 0;
366 nbs->nthread_max = nthread_max;
368 /* Initialize the work data structures for each thread */
369 snew(nbs->work,nbs->nthread_max);
370 for(t=0; t<nbs->nthread_max; t++)
372 nbs->work[t].cxy_na = NULL;
373 nbs->work[t].cxy_na_nalloc = 0;
374 nbs->work[t].sort_work = NULL;
375 nbs->work[t].sort_work_nalloc = 0;
378 /* Initialize detailed nbsearch cycle counting */
379 nbs->print_cycles = (getenv("GMX_NBNXN_CYCLE") != 0);
380 nbs->search_count = 0;
381 nbs_cycle_clear(nbs->cc);
382 for(t=0; t<nbs->nthread_max; t++)
384 nbs_cycle_clear(nbs->work[t].cc);
388 static real grid_atom_density(int n,rvec corner0,rvec corner1)
392 rvec_sub(corner1,corner0,size);
394 return n/(size[XX]*size[YY]*size[ZZ]);
397 static int set_grid_size_xy(const nbnxn_search_t nbs,
399 int n,rvec corner0,rvec corner1,
405 real adens,tlen,tlen_x,tlen_y,nc_max;
408 rvec_sub(corner1,corner0,size);
412 /* target cell length */
415 /* To minimize the zero interactions, we should make
416 * the largest of the i/j cell cubic.
418 na_c = max(grid->na_c,grid->na_cj);
420 /* Approximately cubic cells */
421 tlen = pow(na_c/atom_density,1.0/3.0);
427 /* Approximately cubic sub cells */
428 tlen = pow(grid->na_c/atom_density,1.0/3.0);
429 tlen_x = tlen*GPU_NSUBCELL_X;
430 tlen_y = tlen*GPU_NSUBCELL_Y;
432 /* We round ncx and ncy down, because we get less cell pairs
433 * in the nbsist when the fixed cell dimensions (x,y) are
434 * larger than the variable one (z) than the other way around.
436 grid->ncx = max(1,(int)(size[XX]/tlen_x));
437 grid->ncy = max(1,(int)(size[YY]/tlen_y));
445 /* We need one additional cell entry for particles moved by DD */
446 if (grid->ncx*grid->ncy+1 > grid->cxy_nalloc)
448 grid->cxy_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
449 srenew(grid->cxy_na,grid->cxy_nalloc);
450 srenew(grid->cxy_ind,grid->cxy_nalloc+1);
452 for(t=0; t<nbs->nthread_max; t++)
454 if (grid->ncx*grid->ncy+1 > nbs->work[t].cxy_na_nalloc)
456 nbs->work[t].cxy_na_nalloc = over_alloc_large(grid->ncx*grid->ncy+1);
457 srenew(nbs->work[t].cxy_na,nbs->work[t].cxy_na_nalloc);
461 /* Worst case scenario of 1 atom in each last cell */
462 if (grid->na_cj <= grid->na_c)
464 nc_max = n/grid->na_sc + grid->ncx*grid->ncy;
468 nc_max = n/grid->na_sc + grid->ncx*grid->ncy*grid->na_cj/grid->na_c;
471 if (nc_max > grid->nc_nalloc)
475 grid->nc_nalloc = over_alloc_large(nc_max);
476 srenew(grid->nsubc,grid->nc_nalloc);
477 srenew(grid->bbcz,grid->nc_nalloc*NNBSBB_D);
479 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL/STRIDE_8BB*NNBSBB_XXXX;
481 bb_nalloc = grid->nc_nalloc*GPU_NSUBCELL*NNBSBB_B;
483 sfree_aligned(grid->bb);
484 /* This snew also zeros the contents, this avoid possible
485 * floating exceptions in SSE with the unused bb elements.
487 snew_aligned(grid->bb,bb_nalloc,16);
491 if (grid->na_cj == grid->na_c)
493 grid->bbj = grid->bb;
497 sfree_aligned(grid->bbj);
498 snew_aligned(grid->bbj,bb_nalloc*grid->na_c/grid->na_cj,16);
502 srenew(grid->flags,grid->nc_nalloc);
505 copy_rvec(corner0,grid->c0);
506 copy_rvec(corner1,grid->c1);
507 grid->sx = size[XX]/grid->ncx;
508 grid->sy = size[YY]/grid->ncy;
509 grid->inv_sx = 1/grid->sx;
510 grid->inv_sy = 1/grid->sy;
515 #define SORT_GRID_OVERSIZE 2
516 #define SGSF (SORT_GRID_OVERSIZE + 1)
518 static void sort_atoms(int dim,gmx_bool Backwards,
519 int *a,int n,rvec *x,
520 real h0,real invh,int nsort,int *sort)
532 /* For small oversize factors clearing the whole area is fastest.
533 * For large oversize we should clear the used elements after use.
535 for(i=0; i<nsort; i++)
539 /* Sort the particles using a simple index sort */
542 /* The cast takes care of float-point rounding effects below zero.
543 * This code assumes particles are less than 1/SORT_GRID_OVERSIZE
544 * times the box height out of the box.
546 zi = (int)((x[a[i]][dim] - h0)*invh);
548 #ifdef DEBUG_NBNXN_GRIDDING
549 if (zi < 0 || zi >= nsort)
551 gmx_fatal(FARGS,"(int)((x[%d][%c]=%f - %f)*%f) = %d, not in 0 - %d\n",
552 a[i],'x'+dim,x[a[i]][dim],h0,invh,zi,nsort);
556 /* Ideally this particle should go in sort cell zi,
557 * but that might already be in use,
558 * in that case find the first empty cell higher up
566 /* We have multiple atoms in the same sorting slot.
567 * Sort on real z for minimal bounding box size.
568 * There is an extra check for identical z to ensure
569 * well-defined output order, independent of input order
570 * to ensure binary reproducibility after restarts.
572 while(sort[zi] >= 0 && ( x[a[i]][dim] > x[sort[zi]][dim] ||
573 (x[a[i]][dim] == x[sort[zi]][dim] &&
581 /* Shift all elements by one slot until we find an empty slot */
584 while (sort[zim] >= 0)
600 for(zi=0; zi<nsort; zi++)
610 for(zi=nsort-1; zi>=0; zi--)
620 gmx_incons("Lost particles while sorting");
625 #define R2F_D(x) ((float)((x) >= 0 ? ((1-GMX_FLOAT_EPS)*(x)) : ((1+GMX_FLOAT_EPS)*(x))))
626 #define R2F_U(x) ((float)((x) >= 0 ? ((1+GMX_FLOAT_EPS)*(x)) : ((1-GMX_FLOAT_EPS)*(x))))
632 /* Coordinate order x,y,z, bb order xyz0 */
633 static void calc_bounding_box(int na,int stride,const real *x,float *bb)
636 real xl,xh,yl,yh,zl,zh;
648 xl = min(xl,x[i+XX]);
649 xh = max(xh,x[i+XX]);
650 yl = min(yl,x[i+YY]);
651 yh = max(yh,x[i+YY]);
652 zl = min(zl,x[i+ZZ]);
653 zh = max(zh,x[i+ZZ]);
656 /* Note: possible double to float conversion here */
657 bb[BBL_X] = R2F_D(xl);
658 bb[BBL_Y] = R2F_D(yl);
659 bb[BBL_Z] = R2F_D(zl);
660 bb[BBU_X] = R2F_U(xh);
661 bb[BBU_Y] = R2F_U(yh);
662 bb[BBU_Z] = R2F_U(zh);
665 /* Packed coordinates, bb order xyz0 */
666 static void calc_bounding_box_x_x4(int na,const real *x,float *bb)
669 real xl,xh,yl,yh,zl,zh;
679 xl = min(xl,x[j+XX*PACK_X4]);
680 xh = max(xh,x[j+XX*PACK_X4]);
681 yl = min(yl,x[j+YY*PACK_X4]);
682 yh = max(yh,x[j+YY*PACK_X4]);
683 zl = min(zl,x[j+ZZ*PACK_X4]);
684 zh = max(zh,x[j+ZZ*PACK_X4]);
686 /* Note: possible double to float conversion here */
687 bb[BBL_X] = R2F_D(xl);
688 bb[BBL_Y] = R2F_D(yl);
689 bb[BBL_Z] = R2F_D(zl);
690 bb[BBU_X] = R2F_U(xh);
691 bb[BBU_Y] = R2F_U(yh);
692 bb[BBU_Z] = R2F_U(zh);
695 /* Packed coordinates, bb order xyz0 */
696 static void calc_bounding_box_x_x8(int na,const real *x,float *bb)
699 real xl,xh,yl,yh,zl,zh;
709 xl = min(xl,x[j+XX*PACK_X8]);
710 xh = max(xh,x[j+XX*PACK_X8]);
711 yl = min(yl,x[j+YY*PACK_X8]);
712 yh = max(yh,x[j+YY*PACK_X8]);
713 zl = min(zl,x[j+ZZ*PACK_X8]);
714 zh = max(zh,x[j+ZZ*PACK_X8]);
716 /* Note: possible double to float conversion here */
717 bb[BBL_X] = R2F_D(xl);
718 bb[BBL_Y] = R2F_D(yl);
719 bb[BBL_Z] = R2F_D(zl);
720 bb[BBU_X] = R2F_U(xh);
721 bb[BBU_Y] = R2F_U(yh);
722 bb[BBU_Z] = R2F_U(zh);
725 #ifdef NBNXN_SEARCH_SSE
727 /* Packed coordinates, bb order xyz0 */
728 static void calc_bounding_box_x_x4_halves(int na,const real *x,
729 float *bb,float *bbj)
731 calc_bounding_box_x_x4(min(na,2),x,bbj);
735 calc_bounding_box_x_x4(min(na-2,2),x+(PACK_X4>>1),bbj+NNBSBB_B);
739 /* Set the "empty" bounding box to the same as the first one,
740 * so we don't need to treat special cases in the rest of the code.
742 _mm_store_ps(bbj+NNBSBB_B ,_mm_load_ps(bbj));
743 _mm_store_ps(bbj+NNBSBB_B+NNBSBB_C,_mm_load_ps(bbj+NNBSBB_C));
746 _mm_store_ps(bb ,_mm_min_ps(_mm_load_ps(bbj),
747 _mm_load_ps(bbj+NNBSBB_B)));
748 _mm_store_ps(bb+NNBSBB_C,_mm_max_ps(_mm_load_ps(bbj+NNBSBB_C),
749 _mm_load_ps(bbj+NNBSBB_B+NNBSBB_C)));
752 /* Coordinate order xyz, bb order xxxxyyyyzzzz */
753 static void calc_bounding_box_xxxx(int na,int stride,const real *x,float *bb)
756 real xl,xh,yl,yh,zl,zh;
768 xl = min(xl,x[i+XX]);
769 xh = max(xh,x[i+XX]);
770 yl = min(yl,x[i+YY]);
771 yh = max(yh,x[i+YY]);
772 zl = min(zl,x[i+ZZ]);
773 zh = max(zh,x[i+ZZ]);
776 /* Note: possible double to float conversion here */
777 bb[0*STRIDE_8BB] = R2F_D(xl);
778 bb[1*STRIDE_8BB] = R2F_D(yl);
779 bb[2*STRIDE_8BB] = R2F_D(zl);
780 bb[3*STRIDE_8BB] = R2F_U(xh);
781 bb[4*STRIDE_8BB] = R2F_U(yh);
782 bb[5*STRIDE_8BB] = R2F_U(zh);
785 #endif /* NBNXN_SEARCH_SSE */
787 #ifdef NBNXN_SEARCH_SSE_SINGLE
789 /* Coordinate order xyz?, bb order xyz0 */
790 static void calc_bounding_box_sse(int na,const float *x,float *bb)
792 __m128 bb_0_SSE,bb_1_SSE;
797 bb_0_SSE = _mm_load_ps(x);
802 x_SSE = _mm_load_ps(x+i*NNBSBB_C);
803 bb_0_SSE = _mm_min_ps(bb_0_SSE,x_SSE);
804 bb_1_SSE = _mm_max_ps(bb_1_SSE,x_SSE);
807 _mm_store_ps(bb ,bb_0_SSE);
808 _mm_store_ps(bb+4,bb_1_SSE);
811 /* Coordinate order xyz?, bb order xxxxyyyyzzzz */
812 static void calc_bounding_box_xxxx_sse(int na,const float *x,
816 calc_bounding_box_sse(na,x,bb_work);
818 bb[0*STRIDE_8BB] = bb_work[BBL_X];
819 bb[1*STRIDE_8BB] = bb_work[BBL_Y];
820 bb[2*STRIDE_8BB] = bb_work[BBL_Z];
821 bb[3*STRIDE_8BB] = bb_work[BBU_X];
822 bb[4*STRIDE_8BB] = bb_work[BBU_Y];
823 bb[5*STRIDE_8BB] = bb_work[BBU_Z];
826 #endif /* NBNXN_SEARCH_SSE_SINGLE */
828 #ifdef NBNXN_SEARCH_SSE
830 /* Combines pairs of consecutive bounding boxes */
831 static void combine_bounding_box_pairs(nbnxn_grid_t *grid,const float *bb)
834 __m128 min_SSE,max_SSE;
836 for(i=0; i<grid->ncx*grid->ncy; i++)
838 /* Starting bb in a column is expected to be 2-aligned */
839 sc2 = grid->cxy_ind[i]>>1;
840 /* For odd numbers skip the last bb here */
841 nc2 = (grid->cxy_na[i]+3)>>(2+1);
842 for(c2=sc2; c2<sc2+nc2; c2++)
844 min_SSE = _mm_min_ps(_mm_load_ps(bb+(c2*4+0)*NNBSBB_C),
845 _mm_load_ps(bb+(c2*4+2)*NNBSBB_C));
846 max_SSE = _mm_max_ps(_mm_load_ps(bb+(c2*4+1)*NNBSBB_C),
847 _mm_load_ps(bb+(c2*4+3)*NNBSBB_C));
848 _mm_store_ps(grid->bbj+(c2*2+0)*NNBSBB_C,min_SSE);
849 _mm_store_ps(grid->bbj+(c2*2+1)*NNBSBB_C,max_SSE);
851 if (((grid->cxy_na[i]+3)>>2) & 1)
853 /* Copy the last bb for odd bb count in this column */
854 for(j=0; j<NNBSBB_C; j++)
856 grid->bbj[(c2*2+0)*NNBSBB_C+j] = bb[(c2*4+0)*NNBSBB_C+j];
857 grid->bbj[(c2*2+1)*NNBSBB_C+j] = bb[(c2*4+1)*NNBSBB_C+j];
866 /* Prints the average bb size, used for debug output */
867 static void print_bbsizes_simple(FILE *fp,
868 const nbnxn_search_t nbs,
869 const nbnxn_grid_t *grid)
875 for(c=0; c<grid->nc; c++)
879 ba[d] += grid->bb[c*NNBSBB_B+NNBSBB_C+d] - grid->bb[c*NNBSBB_B+d];
882 dsvmul(1.0/grid->nc,ba,ba);
884 fprintf(fp,"ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
885 nbs->box[XX][XX]/grid->ncx,
886 nbs->box[YY][YY]/grid->ncy,
887 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/grid->nc,
888 ba[XX],ba[YY],ba[ZZ],
889 ba[XX]*grid->ncx/nbs->box[XX][XX],
890 ba[YY]*grid->ncy/nbs->box[YY][YY],
891 ba[ZZ]*grid->nc/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
894 /* Prints the average bb size, used for debug output */
895 static void print_bbsizes_supersub(FILE *fp,
896 const nbnxn_search_t nbs,
897 const nbnxn_grid_t *grid)
904 for(c=0; c<grid->nc; c++)
907 for(s=0; s<grid->nsubc[c]; s+=STRIDE_8BB)
911 cs_w = (c*GPU_NSUBCELL + s)/STRIDE_8BB;
912 for(i=0; i<STRIDE_8BB; i++)
917 grid->bb[cs_w*NNBSBB_XXXX+(DIM+d)*STRIDE_8BB+i] -
918 grid->bb[cs_w*NNBSBB_XXXX+ d *STRIDE_8BB+i];
923 for(s=0; s<grid->nsubc[c]; s++)
927 cs = c*GPU_NSUBCELL + s;
931 grid->bb[cs*NNBSBB_B+NNBSBB_C+d] -
932 grid->bb[cs*NNBSBB_B +d];
936 ns += grid->nsubc[c];
938 dsvmul(1.0/ns,ba,ba);
940 fprintf(fp,"ns bb: %4.2f %4.2f %4.2f %4.2f %4.2f %4.2f rel %4.2f %4.2f %4.2f\n",
941 nbs->box[XX][XX]/(grid->ncx*GPU_NSUBCELL_X),
942 nbs->box[YY][YY]/(grid->ncy*GPU_NSUBCELL_Y),
943 nbs->box[ZZ][ZZ]*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z),
944 ba[XX],ba[YY],ba[ZZ],
945 ba[XX]*grid->ncx*GPU_NSUBCELL_X/nbs->box[XX][XX],
946 ba[YY]*grid->ncy*GPU_NSUBCELL_Y/nbs->box[YY][YY],
947 ba[ZZ]*grid->nc*GPU_NSUBCELL_Z/(grid->ncx*grid->ncy*nbs->box[ZZ][ZZ]));
950 /* Potentially sorts atoms on LJ coefficients !=0 and ==0.
951 * Also sets interaction flags.
953 void sort_on_lj(nbnxn_atomdata_t *nbat,int na_c,
954 int a0,int a1,const int *atinfo,
958 int subc,s,a,n1,n2,a_lj_max,i,j;
959 int sort1[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
960 int sort2[NBNXN_NA_SC_MAX/GPU_NSUBCELL];
966 for(s=a0; s<a1; s+=na_c)
968 /* Make lists for this (sub-)cell on atoms with and without LJ */
973 for(a=s; a<min(s+na_c,a1); a++)
975 haveQ = haveQ || GET_CGINFO_HAS_Q(atinfo[order[a]]);
977 if (GET_CGINFO_HAS_VDW(atinfo[order[a]]))
979 sort1[n1++] = order[a];
984 sort2[n2++] = order[a];
988 /* If we don't have atom with LJ, there's nothing to sort */
991 *flags |= NBNXN_CI_DO_LJ(subc);
995 /* Only sort when strictly necessary. Ordering particles
996 * Ordering particles can lead to less accurate summation
997 * due to rounding, both for LJ and Coulomb interactions.
999 if (2*(a_lj_max - s) >= na_c)
1003 order[a0+i] = sort1[i];
1007 order[a0+n1+j] = sort2[j];
1011 *flags |= NBNXN_CI_HALF_LJ(subc);
1016 *flags |= NBNXN_CI_DO_COUL(subc);
1022 /* Fill a pair search cell with atoms.
1023 * Potentially sorts atoms and sets the interaction flags.
1025 void fill_cell(const nbnxn_search_t nbs,
1027 nbnxn_atomdata_t *nbat,
1031 int sx,int sy, int sz,
1042 sort_on_lj(nbat,grid->na_c,a0,a1,atinfo,nbs->a,
1043 grid->flags+(a0>>grid->na_c_2log)-grid->cell0);
1046 /* Now we have sorted the atoms, set the cell indices */
1047 for(a=a0; a<a1; a++)
1049 nbs->cell[nbs->a[a]] = a;
1052 copy_rvec_to_nbat_real(nbs->a+a0,a1-a0,grid->na_c,x,
1053 nbat->XFormat,nbat->x,a0,
1056 if (nbat->XFormat == nbatX4)
1058 /* Store the bounding boxes as xyz.xyz. */
1059 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1060 bb_ptr = grid->bb + offset;
1062 #if defined GMX_DOUBLE && defined NBNXN_SEARCH_SSE
1063 if (2*grid->na_cj == grid->na_c)
1065 calc_bounding_box_x_x4_halves(na,nbat->x+X4_IND_A(a0),bb_ptr,
1066 grid->bbj+offset*2);
1071 calc_bounding_box_x_x4(na,nbat->x+X4_IND_A(a0),bb_ptr);
1074 else if (nbat->XFormat == nbatX8)
1076 /* Store the bounding boxes as xyz.xyz. */
1077 offset = ((a0 - grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1078 bb_ptr = grid->bb + offset;
1080 calc_bounding_box_x_x8(na,nbat->x+X8_IND_A(a0),bb_ptr);
1083 else if (!grid->bSimple)
1085 /* Store the bounding boxes in a format convenient
1086 * for SSE calculations: xxxxyyyyzzzz...
1090 ((a0-grid->cell0*grid->na_sc)>>(grid->na_c_2log+STRIDE_8BB_2LOG))*NNBSBB_XXXX +
1091 (((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log) & (STRIDE_8BB-1));
1093 #ifdef NBNXN_SEARCH_SSE_SINGLE
1094 if (nbat->XFormat == nbatXYZQ)
1096 calc_bounding_box_xxxx_sse(na,nbat->x+a0*nbat->xstride,
1102 calc_bounding_box_xxxx(na,nbat->xstride,nbat->x+a0*nbat->xstride,
1107 fprintf(debug,"%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1109 bb_ptr[0*STRIDE_8BB],bb_ptr[3*STRIDE_8BB],
1110 bb_ptr[1*STRIDE_8BB],bb_ptr[4*STRIDE_8BB],
1111 bb_ptr[2*STRIDE_8BB],bb_ptr[5*STRIDE_8BB]);
1117 /* Store the bounding boxes as xyz.xyz. */
1118 bb_ptr = grid->bb+((a0-grid->cell0*grid->na_sc)>>grid->na_c_2log)*NNBSBB_B;
1120 calc_bounding_box(na,nbat->xstride,nbat->x+a0*nbat->xstride,
1126 bbo = (a0 - grid->cell0*grid->na_sc)/grid->na_c;
1127 fprintf(debug,"%2d %2d %2d bb %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
1129 (grid->bb+bbo*NNBSBB_B)[BBL_X],
1130 (grid->bb+bbo*NNBSBB_B)[BBU_X],
1131 (grid->bb+bbo*NNBSBB_B)[BBL_Y],
1132 (grid->bb+bbo*NNBSBB_B)[BBU_Y],
1133 (grid->bb+bbo*NNBSBB_B)[BBL_Z],
1134 (grid->bb+bbo*NNBSBB_B)[BBU_Z]);
1139 /* Spatially sort the atoms within one grid column */
1140 static void sort_columns_simple(const nbnxn_search_t nbs,
1146 nbnxn_atomdata_t *nbat,
1147 int cxy_start,int cxy_end,
1151 int cx,cy,cz,ncz,cfilled,c;
1157 fprintf(debug,"cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1158 grid->cell0,cxy_start,cxy_end,a0,a1);
1161 /* Sort the atoms within each x,y column in 3 dimensions */
1162 for(cxy=cxy_start; cxy<cxy_end; cxy++)
1165 cy = cxy - cx*grid->ncy;
1167 na = grid->cxy_na[cxy];
1168 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1169 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1171 /* Sort the atoms within each x,y column on z coordinate */
1172 sort_atoms(ZZ,FALSE,
1175 ncz*grid->na_sc*SORT_GRID_OVERSIZE/nbs->box[ZZ][ZZ],
1176 ncz*grid->na_sc*SGSF,sort_work);
1178 /* Fill the ncz cells in this column */
1179 cfilled = grid->cxy_ind[cxy];
1180 for(cz=0; cz<ncz; cz++)
1182 c = grid->cxy_ind[cxy] + cz ;
1184 ash_c = ash + cz*grid->na_sc;
1185 na_c = min(grid->na_sc,na-(ash_c-ash));
1187 fill_cell(nbs,grid,nbat,
1188 ash_c,ash_c+na_c,atinfo,x,
1189 grid->na_sc*cx + (dd_zone >> 2),
1190 grid->na_sc*cy + (dd_zone & 3),
1194 /* This copy to bbcz is not really necessary.
1195 * But it allows to use the same grid search code
1196 * for the simple and supersub cell setups.
1202 grid->bbcz[c*NNBSBB_D ] = grid->bb[cfilled*NNBSBB_B+2];
1203 grid->bbcz[c*NNBSBB_D+1] = grid->bb[cfilled*NNBSBB_B+6];
1206 /* Set the unused atom indices to -1 */
1207 for(ind=na; ind<ncz*grid->na_sc; ind++)
1209 nbs->a[ash+ind] = -1;
1214 /* Spatially sort the atoms within one grid column */
1215 static void sort_columns_supersub(const nbnxn_search_t nbs,
1221 nbnxn_atomdata_t *nbat,
1222 int cxy_start,int cxy_end,
1226 int cx,cy,cz=-1,c=-1,ncz;
1227 int na,ash,na_c,ind,a;
1228 int subdiv_z,sub_z,na_z,ash_z;
1229 int subdiv_y,sub_y,na_y,ash_y;
1230 int subdiv_x,sub_x,na_x,ash_x;
1232 /* cppcheck-suppress unassignedVariable */
1233 float bb_work_array[NNBSBB_B+3],*bb_work_align;
1235 bb_work_align = (float *)(((size_t)(bb_work_array+3)) & (~((size_t)15)));
1239 fprintf(debug,"cell0 %d sorting columns %d - %d, atoms %d - %d\n",
1240 grid->cell0,cxy_start,cxy_end,a0,a1);
1243 subdiv_x = grid->na_c;
1244 subdiv_y = GPU_NSUBCELL_X*subdiv_x;
1245 subdiv_z = GPU_NSUBCELL_Y*subdiv_y;
1247 /* Sort the atoms within each x,y column in 3 dimensions */
1248 for(cxy=cxy_start; cxy<cxy_end; cxy++)
1251 cy = cxy - cx*grid->ncy;
1253 na = grid->cxy_na[cxy];
1254 ncz = grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy];
1255 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
1257 /* Sort the atoms within each x,y column on z coordinate */
1258 sort_atoms(ZZ,FALSE,
1261 ncz*grid->na_sc*SORT_GRID_OVERSIZE/nbs->box[ZZ][ZZ],
1262 ncz*grid->na_sc*SGSF,sort_work);
1264 /* This loop goes over the supercells and subcells along z at once */
1265 for(sub_z=0; sub_z<ncz*GPU_NSUBCELL_Z; sub_z++)
1267 ash_z = ash + sub_z*subdiv_z;
1268 na_z = min(subdiv_z,na-(ash_z-ash));
1270 /* We have already sorted on z */
1272 if (sub_z % GPU_NSUBCELL_Z == 0)
1274 cz = sub_z/GPU_NSUBCELL_Z;
1275 c = grid->cxy_ind[cxy] + cz ;
1277 /* The number of atoms in this supercell */
1278 na_c = min(grid->na_sc,na-(ash_z-ash));
1280 grid->nsubc[c] = min(GPU_NSUBCELL,(na_c+grid->na_c-1)/grid->na_c);
1282 /* Store the z-boundaries of the super cell */
1283 grid->bbcz[c*NNBSBB_D ] = x[nbs->a[ash_z]][ZZ];
1284 grid->bbcz[c*NNBSBB_D+1] = x[nbs->a[ash_z+na_c-1]][ZZ];
1287 #if GPU_NSUBCELL_Y > 1
1288 /* Sort the atoms along y */
1289 sort_atoms(YY,(sub_z & 1),
1290 nbs->a+ash_z,na_z,x,
1291 grid->c0[YY]+cy*grid->sy,grid->inv_sy,
1292 subdiv_y*SGSF,sort_work);
1295 for(sub_y=0; sub_y<GPU_NSUBCELL_Y; sub_y++)
1297 ash_y = ash_z + sub_y*subdiv_y;
1298 na_y = min(subdiv_y,na-(ash_y-ash));
1300 #if GPU_NSUBCELL_X > 1
1301 /* Sort the atoms along x */
1302 sort_atoms(XX,((cz*GPU_NSUBCELL_Y + sub_y) & 1),
1303 nbs->a+ash_y,na_y,x,
1304 grid->c0[XX]+cx*grid->sx,grid->inv_sx,
1305 subdiv_x*SGSF,sort_work);
1308 for(sub_x=0; sub_x<GPU_NSUBCELL_X; sub_x++)
1310 ash_x = ash_y + sub_x*subdiv_x;
1311 na_x = min(subdiv_x,na-(ash_x-ash));
1313 fill_cell(nbs,grid,nbat,
1314 ash_x,ash_x+na_x,atinfo,x,
1315 grid->na_c*(cx*GPU_NSUBCELL_X+sub_x) + (dd_zone >> 2),
1316 grid->na_c*(cy*GPU_NSUBCELL_Y+sub_y) + (dd_zone & 3),
1323 /* Set the unused atom indices to -1 */
1324 for(ind=na; ind<ncz*grid->na_sc; ind++)
1326 nbs->a[ash+ind] = -1;
1331 /* Determine in which grid column atoms should go */
1332 static void calc_column_indices(nbnxn_grid_t *grid,
1334 rvec *x,const int *move,
1335 int thread,int nthread,
1342 /* We add one extra cell for particles which moved during DD */
1343 for(i=0; i<grid->ncx*grid->ncy+1; i++)
1348 n0 = a0 + (int)((thread+0)*(a1 - a0))/nthread;
1349 n1 = a0 + (int)((thread+1)*(a1 - a0))/nthread;
1350 for(i=n0; i<n1; i++)
1352 if (move == NULL || move[i] >= 0)
1354 /* We need to be careful with rounding,
1355 * particles might be a few bits outside the local box.
1356 * The int cast takes care of the lower bound,
1357 * we need to explicitly take care of the upper bound.
1359 cx = (int)((x[i][XX] - grid->c0[XX])*grid->inv_sx);
1360 if (cx == grid->ncx)
1364 cy = (int)((x[i][YY] - grid->c0[YY])*grid->inv_sy);
1365 if (cy == grid->ncy)
1369 /* For the moment cell contains only the, grid local,
1370 * x and y indices, not z.
1372 cell[i] = cx*grid->ncy + cy;
1374 #ifdef DEBUG_NBNXN_GRIDDING
1375 if (cell[i] < 0 || cell[i] >= grid->ncx*grid->ncy)
1378 "grid cell cx %d cy %d out of range (max %d %d)\n"
1379 "atom %f %f %f, grid->c0 %f %f",
1380 cx,cy,grid->ncx,grid->ncy,
1381 x[i][XX],x[i][YY],x[i][ZZ],grid->c0[XX],grid->c0[YY]);
1387 /* Put this moved particle after the end of the grid,
1388 * so we can process it later without using conditionals.
1390 cell[i] = grid->ncx*grid->ncy;
1397 /* Determine in which grid cells the atoms should go */
1398 static void calc_cell_indices(const nbnxn_search_t nbs,
1405 nbnxn_atomdata_t *nbat)
1408 int cx,cy,cxy,ncz_max,ncz;
1410 int *cxy_na,cxy_na_i;
1412 nthread = gmx_omp_nthreads_get(emntPairsearch);
1414 #pragma omp parallel for num_threads(nthread) schedule(static)
1415 for(thread=0; thread<nthread; thread++)
1417 calc_column_indices(grid,a0,a1,x,move,thread,nthread,
1418 nbs->cell,nbs->work[thread].cxy_na);
1421 /* Make the cell index as a function of x and y */
1424 grid->cxy_ind[0] = 0;
1425 for(i=0; i<grid->ncx*grid->ncy+1; i++)
1427 /* We set ncz_max at the beginning of the loop iso at the end
1428 * to skip i=grid->ncx*grid->ncy which are moved particles
1429 * that do not need to be ordered on the grid.
1435 cxy_na_i = nbs->work[0].cxy_na[i];
1436 for(thread=1; thread<nthread; thread++)
1438 cxy_na_i += nbs->work[thread].cxy_na[i];
1440 ncz = (cxy_na_i + grid->na_sc - 1)/grid->na_sc;
1441 if (nbat->XFormat == nbatX8)
1443 /* Make the number of cell a multiple of 2 */
1444 ncz = (ncz + 1) & ~1;
1446 grid->cxy_ind[i+1] = grid->cxy_ind[i] + ncz;
1447 /* Clear cxy_na, so we can reuse the array below */
1448 grid->cxy_na[i] = 0;
1450 grid->nc = grid->cxy_ind[grid->ncx*grid->ncy] - grid->cxy_ind[0];
1452 nbat->natoms = (grid->cell0 + grid->nc)*grid->na_sc;
1456 fprintf(debug,"ns na_sc %d na_c %d super-cells: %d x %d y %d z %.1f maxz %d\n",
1457 grid->na_sc,grid->na_c,grid->nc,
1458 grid->ncx,grid->ncy,grid->nc/((double)(grid->ncx*grid->ncy)),
1463 for(cy=0; cy<grid->ncy; cy++)
1465 for(cx=0; cx<grid->ncx; cx++)
1467 fprintf(debug," %2d",grid->cxy_ind[i+1]-grid->cxy_ind[i]);
1470 fprintf(debug,"\n");
1475 /* Make sure the work array for sorting is large enough */
1476 if (ncz_max*grid->na_sc*SGSF > nbs->work[0].sort_work_nalloc)
1478 for(thread=0; thread<nbs->nthread_max; thread++)
1480 nbs->work[thread].sort_work_nalloc =
1481 over_alloc_large(ncz_max*grid->na_sc*SGSF);
1482 srenew(nbs->work[thread].sort_work,
1483 nbs->work[thread].sort_work_nalloc);
1487 /* Now we know the dimensions we can fill the grid.
1488 * This is the first, unsorted fill. We sort the columns after this.
1490 for(i=a0; i<a1; i++)
1492 /* At this point nbs->cell contains the local grid x,y indices */
1494 nbs->a[(grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc + grid->cxy_na[cxy]++] = i;
1497 /* Set the cell indices for the moved particles */
1498 n0 = grid->nc*grid->na_sc;
1499 n1 = grid->nc*grid->na_sc+grid->cxy_na[grid->ncx*grid->ncy];
1500 for(i=n0; i<n1; i++)
1502 nbs->cell[nbs->a[i]] = i;
1505 /* Sort the super-cell columns along z into the sub-cells. */
1506 #pragma omp parallel for num_threads(nbs->nthread_max) schedule(static)
1507 for(thread=0; thread<nbs->nthread_max; thread++)
1511 sort_columns_simple(nbs,dd_zone,grid,a0,a1,atinfo,x,nbat,
1512 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1513 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1514 nbs->work[thread].sort_work);
1518 sort_columns_supersub(nbs,dd_zone,grid,a0,a1,atinfo,x,nbat,
1519 ((thread+0)*grid->ncx*grid->ncy)/nthread,
1520 ((thread+1)*grid->ncx*grid->ncy)/nthread,
1521 nbs->work[thread].sort_work);
1525 #ifdef NBNXN_SEARCH_SSE
1526 if (grid->bSimple && nbat->XFormat == nbatX8)
1528 combine_bounding_box_pairs(grid,grid->bb);
1534 grid->nsubc_tot = 0;
1535 for(i=0; i<grid->nc; i++)
1537 grid->nsubc_tot += grid->nsubc[i];
1545 print_bbsizes_simple(debug,nbs,grid);
1549 fprintf(debug,"ns non-zero sub-cells: %d average atoms %.2f\n",
1550 grid->nsubc_tot,(a1-a0)/(double)grid->nsubc_tot);
1552 print_bbsizes_supersub(debug,nbs,grid);
1557 static void init_buffer_flags(nbnxn_buffer_flags_t *flags,
1562 flags->nflag = (natoms + NBNXN_BUFFERFLAG_SIZE - 1)/NBNXN_BUFFERFLAG_SIZE;
1563 if (flags->nflag > flags->flag_nalloc)
1565 flags->flag_nalloc = over_alloc_large(flags->nflag);
1566 srenew(flags->flag,flags->flag_nalloc);
1568 for(b=0; b<flags->nflag; b++)
1574 /* Sets up a grid and puts the atoms on the grid.
1575 * This function only operates on one domain of the domain decompostion.
1576 * Note that without domain decomposition there is only one domain.
1578 void nbnxn_put_on_grid(nbnxn_search_t nbs,
1579 int ePBC,matrix box,
1581 rvec corner0,rvec corner1,
1586 int nmoved,int *move,
1588 nbnxn_atomdata_t *nbat)
1592 int nc_max_grid,nc_max;
1594 grid = &nbs->grid[dd_zone];
1596 nbs_cycle_start(&nbs->cc[enbsCCgrid]);
1598 grid->bSimple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
1600 grid->na_c = nbnxn_kernel_to_ci_size(nb_kernel_type);
1601 grid->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
1602 grid->na_sc = (grid->bSimple ? 1 : GPU_NSUBCELL)*grid->na_c;
1603 grid->na_c_2log = get_2log(grid->na_c);
1605 nbat->na_c = grid->na_c;
1614 (nbs->grid[dd_zone-1].cell0 + nbs->grid[dd_zone-1].nc)*
1615 nbs->grid[dd_zone-1].na_sc/grid->na_sc;
1623 copy_mat(box,nbs->box);
1625 if (atom_density >= 0)
1627 grid->atom_density = atom_density;
1631 grid->atom_density = grid_atom_density(n-nmoved,corner0,corner1);
1636 nbs->natoms_local = a1 - nmoved;
1637 /* We assume that nbnxn_put_on_grid is called first
1638 * for the local atoms (dd_zone=0).
1640 nbs->natoms_nonlocal = a1 - nmoved;
1644 nbs->natoms_nonlocal = max(nbs->natoms_nonlocal,a1);
1647 nc_max_grid = set_grid_size_xy(nbs,grid,n-nmoved,corner0,corner1,
1648 nbs->grid[0].atom_density,
1651 nc_max = grid->cell0 + nc_max_grid;
1653 if (a1 > nbs->cell_nalloc)
1655 nbs->cell_nalloc = over_alloc_large(a1);
1656 srenew(nbs->cell,nbs->cell_nalloc);
1659 /* To avoid conditionals we store the moved particles at the end of a,
1660 * make sure we have enough space.
1662 if (nc_max*grid->na_sc + nmoved > nbs->a_nalloc)
1664 nbs->a_nalloc = over_alloc_large(nc_max*grid->na_sc + nmoved);
1665 srenew(nbs->a,nbs->a_nalloc);
1668 /* We need padding up to a multiple of the buffer flag size: simply add */
1669 if (nc_max*grid->na_sc + NBNXN_BUFFERFLAG_SIZE > nbat->nalloc)
1671 nbnxn_atomdata_realloc(nbat,nc_max*grid->na_sc+NBNXN_BUFFERFLAG_SIZE);
1674 calc_cell_indices(nbs,dd_zone,grid,a0,a1,atinfo,x,move,nbat);
1678 nbat->natoms_local = nbat->natoms;
1681 nbs_cycle_stop(&nbs->cc[enbsCCgrid]);
1684 /* Calls nbnxn_put_on_grid for all non-local domains */
1685 void nbnxn_put_on_grid_nonlocal(nbnxn_search_t nbs,
1686 const gmx_domdec_zones_t *zones,
1690 nbnxn_atomdata_t *nbat)
1695 for(zone=1; zone<zones->n; zone++)
1697 for(d=0; d<DIM; d++)
1699 c0[d] = zones->size[zone].bb_x0[d];
1700 c1[d] = zones->size[zone].bb_x1[d];
1703 nbnxn_put_on_grid(nbs,nbs->ePBC,NULL,
1705 zones->cg_range[zone],
1706 zones->cg_range[zone+1],
1716 /* Add simple grid type information to the local super/sub grid */
1717 void nbnxn_grid_add_simple(nbnxn_search_t nbs,
1718 nbnxn_atomdata_t *nbat)
1724 grid = &nbs->grid[0];
1728 gmx_incons("nbnxn_grid_simple called with a simple grid");
1731 ncd = grid->na_sc/NBNXN_CPU_CLUSTER_I_SIZE;
1733 if (grid->nc*ncd > grid->nc_nalloc_simple)
1735 grid->nc_nalloc_simple = over_alloc_large(grid->nc*ncd);
1736 srenew(grid->bbcz_simple,grid->nc_nalloc_simple*NNBSBB_D);
1737 srenew(grid->bb_simple,grid->nc_nalloc_simple*NNBSBB_B);
1738 srenew(grid->flags_simple,grid->nc_nalloc_simple);
1741 sfree_aligned(grid->bbj);
1742 snew_aligned(grid->bbj,grid->nc_nalloc_simple/2,16);
1746 bbcz = grid->bbcz_simple;
1747 bb = grid->bb_simple;
1749 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
1750 for(sc=0; sc<grid->nc; sc++)
1754 for(c=0; c<ncd; c++)
1758 na = NBNXN_CPU_CLUSTER_I_SIZE;
1760 nbat->type[tx*NBNXN_CPU_CLUSTER_I_SIZE+na-1] == nbat->ntype-1)
1767 switch (nbat->XFormat)
1770 /* PACK_X4==NBNXN_CPU_CLUSTER_I_SIZE, so this is simple */
1771 calc_bounding_box_x_x4(na,nbat->x+tx*STRIDE_P4,
1775 /* PACK_X8>NBNXN_CPU_CLUSTER_I_SIZE, more complicated */
1776 calc_bounding_box_x_x8(na,nbat->x+X8_IND_A(tx*NBNXN_CPU_CLUSTER_I_SIZE),
1780 calc_bounding_box(na,nbat->xstride,
1781 nbat->x+tx*NBNXN_CPU_CLUSTER_I_SIZE*nbat->xstride,
1785 bbcz[tx*NNBSBB_D+0] = bb[tx*NNBSBB_B +ZZ];
1786 bbcz[tx*NNBSBB_D+1] = bb[tx*NNBSBB_B+NNBSBB_C+ZZ];
1788 /* No interaction optimization yet here */
1789 grid->flags_simple[tx] = NBNXN_CI_DO_LJ(0) | NBNXN_CI_DO_COUL(0);
1793 grid->flags_simple[tx] = 0;
1798 #ifdef NBNXN_SEARCH_SSE
1799 if (grid->bSimple && nbat->XFormat == nbatX8)
1801 combine_bounding_box_pairs(grid,grid->bb_simple);
1806 void nbnxn_get_ncells(nbnxn_search_t nbs,int *ncx,int *ncy)
1808 *ncx = nbs->grid[0].ncx;
1809 *ncy = nbs->grid[0].ncy;
1812 void nbnxn_get_atomorder(nbnxn_search_t nbs,int **a,int *n)
1814 const nbnxn_grid_t *grid;
1816 grid = &nbs->grid[0];
1818 /* Return the atom order for the home cell (index 0) */
1821 *n = grid->cxy_ind[grid->ncx*grid->ncy]*grid->na_sc;
1824 void nbnxn_set_atomorder(nbnxn_search_t nbs)
1827 int ao,cx,cy,cxy,cz,j;
1829 /* Set the atom order for the home cell (index 0) */
1830 grid = &nbs->grid[0];
1833 for(cx=0; cx<grid->ncx; cx++)
1835 for(cy=0; cy<grid->ncy; cy++)
1837 cxy = cx*grid->ncy + cy;
1838 j = grid->cxy_ind[cxy]*grid->na_sc;
1839 for(cz=0; cz<grid->cxy_na[cxy]; cz++)
1850 /* Determines the cell range along one dimension that
1851 * the bounding box b0 - b1 sees.
1853 static void get_cell_range(real b0,real b1,
1854 int nc,real c0,real s,real invs,
1855 real d2,real r2,int *cf,int *cl)
1857 *cf = max((int)((b0 - c0)*invs),0);
1859 while (*cf > 0 && d2 + sqr((b0 - c0) - (*cf-1+1)*s) < r2)
1864 *cl = min((int)((b1 - c0)*invs),nc-1);
1865 while (*cl < nc-1 && d2 + sqr((*cl+1)*s - (b1 - c0)) < r2)
1871 /* Reference code calculating the distance^2 between two bounding boxes */
1872 static float box_dist2(float bx0,float bx1,float by0,
1873 float by1,float bz0,float bz1,
1881 dl = bx0 - bb[BBU_X];
1882 dh = bb[BBL_X] - bx1;
1887 dl = by0 - bb[BBU_Y];
1888 dh = bb[BBL_Y] - by1;
1893 dl = bz0 - bb[BBU_Z];
1894 dh = bb[BBL_Z] - bz1;
1902 /* Plain C code calculating the distance^2 between two bounding boxes */
1903 static float subc_bb_dist2(int si,const float *bb_i_ci,
1904 int csj,const float *bb_j_all)
1906 const float *bb_i,*bb_j;
1910 bb_i = bb_i_ci + si*NNBSBB_B;
1911 bb_j = bb_j_all + csj*NNBSBB_B;
1915 dl = bb_i[BBL_X] - bb_j[BBU_X];
1916 dh = bb_j[BBL_X] - bb_i[BBU_X];
1921 dl = bb_i[BBL_Y] - bb_j[BBU_Y];
1922 dh = bb_j[BBL_Y] - bb_i[BBU_Y];
1927 dl = bb_i[BBL_Z] - bb_j[BBU_Z];
1928 dh = bb_j[BBL_Z] - bb_i[BBU_Z];
1936 #ifdef NBNXN_SEARCH_SSE
1938 /* SSE code for bb distance for bb format xyz0 */
1939 static float subc_bb_dist2_sse(int na_c,
1940 int si,const float *bb_i_ci,
1941 int csj,const float *bb_j_all)
1943 const float *bb_i,*bb_j;
1945 __m128 bb_i_SSE0,bb_i_SSE1;
1946 __m128 bb_j_SSE0,bb_j_SSE1;
1952 #ifndef GMX_X86_SSE4_1
1953 float d2_array[7],*d2_align;
1955 d2_align = (float *)(((size_t)(d2_array+3)) & (~((size_t)15)));
1960 bb_i = bb_i_ci + si*NNBSBB_B;
1961 bb_j = bb_j_all + csj*NNBSBB_B;
1963 bb_i_SSE0 = _mm_load_ps(bb_i);
1964 bb_i_SSE1 = _mm_load_ps(bb_i+NNBSBB_C);
1965 bb_j_SSE0 = _mm_load_ps(bb_j);
1966 bb_j_SSE1 = _mm_load_ps(bb_j+NNBSBB_C);
1968 dl_SSE = _mm_sub_ps(bb_i_SSE0,bb_j_SSE1);
1969 dh_SSE = _mm_sub_ps(bb_j_SSE0,bb_i_SSE1);
1971 dm_SSE = _mm_max_ps(dl_SSE,dh_SSE);
1972 dm0_SSE = _mm_max_ps(dm_SSE,_mm_setzero_ps());
1973 #ifndef GMX_X86_SSE4_1
1974 d2_SSE = _mm_mul_ps(dm0_SSE,dm0_SSE);
1976 _mm_store_ps(d2_align,d2_SSE);
1978 return d2_align[0] + d2_align[1] + d2_align[2];
1980 /* SSE4.1 dot product of components 0,1,2 */
1981 d2_SSE = _mm_dp_ps(dm0_SSE,dm0_SSE,0x71);
1983 _mm_store_ss(&d2,d2_SSE);
1989 /* Calculate bb bounding distances of bb_i[si,...,si+3] and store them in d2 */
1990 #define SUBC_BB_DIST2_SSE_XXXX_INNER(si,bb_i,d2) \
1994 __m128 dx_0,dy_0,dz_0; \
1995 __m128 dx_1,dy_1,dz_1; \
1998 __m128 m0x,m0y,m0z; \
2000 __m128 d2x,d2y,d2z; \
2003 shi = si*NNBSBB_D*DIM; \
2005 xi_l = _mm_load_ps(bb_i+shi+0*STRIDE_8BB); \
2006 yi_l = _mm_load_ps(bb_i+shi+1*STRIDE_8BB); \
2007 zi_l = _mm_load_ps(bb_i+shi+2*STRIDE_8BB); \
2008 xi_h = _mm_load_ps(bb_i+shi+3*STRIDE_8BB); \
2009 yi_h = _mm_load_ps(bb_i+shi+4*STRIDE_8BB); \
2010 zi_h = _mm_load_ps(bb_i+shi+5*STRIDE_8BB); \
2012 dx_0 = _mm_sub_ps(xi_l,xj_h); \
2013 dy_0 = _mm_sub_ps(yi_l,yj_h); \
2014 dz_0 = _mm_sub_ps(zi_l,zj_h); \
2016 dx_1 = _mm_sub_ps(xj_l,xi_h); \
2017 dy_1 = _mm_sub_ps(yj_l,yi_h); \
2018 dz_1 = _mm_sub_ps(zj_l,zi_h); \
2020 mx = _mm_max_ps(dx_0,dx_1); \
2021 my = _mm_max_ps(dy_0,dy_1); \
2022 mz = _mm_max_ps(dz_0,dz_1); \
2024 m0x = _mm_max_ps(mx,zero); \
2025 m0y = _mm_max_ps(my,zero); \
2026 m0z = _mm_max_ps(mz,zero); \
2028 d2x = _mm_mul_ps(m0x,m0x); \
2029 d2y = _mm_mul_ps(m0y,m0y); \
2030 d2z = _mm_mul_ps(m0z,m0z); \
2032 d2s = _mm_add_ps(d2x,d2y); \
2033 d2t = _mm_add_ps(d2s,d2z); \
2035 _mm_store_ps(d2+si,d2t); \
2038 /* SSE code for nsi bb distances for bb format xxxxyyyyzzzz */
2039 static void subc_bb_dist2_sse_xxxx(const float *bb_j,
2040 int nsi,const float *bb_i,
2043 __m128 xj_l,yj_l,zj_l;
2044 __m128 xj_h,yj_h,zj_h;
2045 __m128 xi_l,yi_l,zi_l;
2046 __m128 xi_h,yi_h,zi_h;
2050 zero = _mm_setzero_ps();
2052 xj_l = _mm_set1_ps(bb_j[0*STRIDE_8BB]);
2053 yj_l = _mm_set1_ps(bb_j[1*STRIDE_8BB]);
2054 zj_l = _mm_set1_ps(bb_j[2*STRIDE_8BB]);
2055 xj_h = _mm_set1_ps(bb_j[3*STRIDE_8BB]);
2056 yj_h = _mm_set1_ps(bb_j[4*STRIDE_8BB]);
2057 zj_h = _mm_set1_ps(bb_j[5*STRIDE_8BB]);
2059 /* Here we "loop" over si (0,STRIDE_8BB) from 0 to nsi with step STRIDE_8BB.
2060 * But as we know the number of iterations is 1 or 2, we unroll manually.
2062 SUBC_BB_DIST2_SSE_XXXX_INNER(0,bb_i,d2);
2063 if (STRIDE_8BB < nsi)
2065 SUBC_BB_DIST2_SSE_XXXX_INNER(STRIDE_8BB,bb_i,d2);
2069 #endif /* NBNXN_SEARCH_SSE */
2071 /* Plain C function which determines if any atom pair between two cells
2072 * is within distance sqrt(rl2).
2074 static gmx_bool subc_in_range_x(int na_c,
2075 int si,const real *x_i,
2076 int csj,int stride,const real *x_j,
2082 for(i=0; i<na_c; i++)
2084 i0 = (si*na_c + i)*DIM;
2085 for(j=0; j<na_c; j++)
2087 j0 = (csj*na_c + j)*stride;
2089 d2 = sqr(x_i[i0 ] - x_j[j0 ]) +
2090 sqr(x_i[i0+1] - x_j[j0+1]) +
2091 sqr(x_i[i0+2] - x_j[j0+2]);
2103 /* SSE function which determines if any atom pair between two cells,
2104 * both with 8 atoms, is within distance sqrt(rl2).
2106 static gmx_bool subc_in_range_sse8(int na_c,
2107 int si,const real *x_i,
2108 int csj,int stride,const real *x_j,
2111 #ifdef NBNXN_SEARCH_SSE_SINGLE
2112 __m128 ix_SSE0,iy_SSE0,iz_SSE0;
2113 __m128 ix_SSE1,iy_SSE1,iz_SSE1;
2120 rc2_SSE = _mm_set1_ps(rl2);
2122 na_c_sse = NBNXN_GPU_CLUSTER_SIZE/STRIDE_8BB;
2123 ix_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+0)*STRIDE_8BB);
2124 iy_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+1)*STRIDE_8BB);
2125 iz_SSE0 = _mm_load_ps(x_i+(si*na_c_sse*DIM+2)*STRIDE_8BB);
2126 ix_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+3)*STRIDE_8BB);
2127 iy_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+4)*STRIDE_8BB);
2128 iz_SSE1 = _mm_load_ps(x_i+(si*na_c_sse*DIM+5)*STRIDE_8BB);
2130 /* We loop from the outer to the inner particles to maximize
2131 * the chance that we find a pair in range quickly and return.
2137 __m128 jx0_SSE,jy0_SSE,jz0_SSE;
2138 __m128 jx1_SSE,jy1_SSE,jz1_SSE;
2140 __m128 dx_SSE0,dy_SSE0,dz_SSE0;
2141 __m128 dx_SSE1,dy_SSE1,dz_SSE1;
2142 __m128 dx_SSE2,dy_SSE2,dz_SSE2;
2143 __m128 dx_SSE3,dy_SSE3,dz_SSE3;
2154 __m128 wco_any_SSE01,wco_any_SSE23,wco_any_SSE;
2156 jx0_SSE = _mm_load1_ps(x_j+j0*stride+0);
2157 jy0_SSE = _mm_load1_ps(x_j+j0*stride+1);
2158 jz0_SSE = _mm_load1_ps(x_j+j0*stride+2);
2160 jx1_SSE = _mm_load1_ps(x_j+j1*stride+0);
2161 jy1_SSE = _mm_load1_ps(x_j+j1*stride+1);
2162 jz1_SSE = _mm_load1_ps(x_j+j1*stride+2);
2164 /* Calculate distance */
2165 dx_SSE0 = _mm_sub_ps(ix_SSE0,jx0_SSE);
2166 dy_SSE0 = _mm_sub_ps(iy_SSE0,jy0_SSE);
2167 dz_SSE0 = _mm_sub_ps(iz_SSE0,jz0_SSE);
2168 dx_SSE1 = _mm_sub_ps(ix_SSE1,jx0_SSE);
2169 dy_SSE1 = _mm_sub_ps(iy_SSE1,jy0_SSE);
2170 dz_SSE1 = _mm_sub_ps(iz_SSE1,jz0_SSE);
2171 dx_SSE2 = _mm_sub_ps(ix_SSE0,jx1_SSE);
2172 dy_SSE2 = _mm_sub_ps(iy_SSE0,jy1_SSE);
2173 dz_SSE2 = _mm_sub_ps(iz_SSE0,jz1_SSE);
2174 dx_SSE3 = _mm_sub_ps(ix_SSE1,jx1_SSE);
2175 dy_SSE3 = _mm_sub_ps(iy_SSE1,jy1_SSE);
2176 dz_SSE3 = _mm_sub_ps(iz_SSE1,jz1_SSE);
2178 /* rsq = dx*dx+dy*dy+dz*dz */
2179 rsq_SSE0 = gmx_mm_calc_rsq_ps(dx_SSE0,dy_SSE0,dz_SSE0);
2180 rsq_SSE1 = gmx_mm_calc_rsq_ps(dx_SSE1,dy_SSE1,dz_SSE1);
2181 rsq_SSE2 = gmx_mm_calc_rsq_ps(dx_SSE2,dy_SSE2,dz_SSE2);
2182 rsq_SSE3 = gmx_mm_calc_rsq_ps(dx_SSE3,dy_SSE3,dz_SSE3);
2184 wco_SSE0 = _mm_cmplt_ps(rsq_SSE0,rc2_SSE);
2185 wco_SSE1 = _mm_cmplt_ps(rsq_SSE1,rc2_SSE);
2186 wco_SSE2 = _mm_cmplt_ps(rsq_SSE2,rc2_SSE);
2187 wco_SSE3 = _mm_cmplt_ps(rsq_SSE3,rc2_SSE);
2189 wco_any_SSE01 = _mm_or_ps(wco_SSE0,wco_SSE1);
2190 wco_any_SSE23 = _mm_or_ps(wco_SSE2,wco_SSE3);
2191 wco_any_SSE = _mm_or_ps(wco_any_SSE01,wco_any_SSE23);
2193 if (_mm_movemask_ps(wco_any_SSE))
2205 gmx_incons("SSE function called without SSE support");
2211 /* Returns the j sub-cell for index cj_ind */
2212 static int nbl_cj(const nbnxn_pairlist_t *nbl,int cj_ind)
2214 return nbl->cj4[cj_ind>>2].cj[cj_ind & 3];
2217 /* Returns the i-interaction mask of the j sub-cell for index cj_ind */
2218 static unsigned nbl_imask0(const nbnxn_pairlist_t *nbl,int cj_ind)
2220 return nbl->cj4[cj_ind>>2].imei[0].imask;
2223 /* Ensures there is enough space for extra extra exclusion masks */
2224 static void check_excl_space(nbnxn_pairlist_t *nbl,int extra)
2226 if (nbl->nexcl+extra > nbl->excl_nalloc)
2228 nbl->excl_nalloc = over_alloc_small(nbl->nexcl+extra);
2229 nbnxn_realloc_void((void **)&nbl->excl,
2230 nbl->nexcl*sizeof(*nbl->excl),
2231 nbl->excl_nalloc*sizeof(*nbl->excl),
2232 nbl->alloc,nbl->free);
2236 /* Ensures there is enough space for ncell extra j-cells in the list */
2237 static void check_subcell_list_space_simple(nbnxn_pairlist_t *nbl,
2242 cj_max = nbl->ncj + ncell;
2244 if (cj_max > nbl->cj_nalloc)
2246 nbl->cj_nalloc = over_alloc_small(cj_max);
2247 nbnxn_realloc_void((void **)&nbl->cj,
2248 nbl->ncj*sizeof(*nbl->cj),
2249 nbl->cj_nalloc*sizeof(*nbl->cj),
2250 nbl->alloc,nbl->free);
2254 /* Ensures there is enough space for ncell extra j-subcells in the list */
2255 static void check_subcell_list_space_supersub(nbnxn_pairlist_t *nbl,
2258 int ncj4_max,j4,j,w,t;
2261 #define WARP_SIZE 32
2263 /* We can have maximally nsupercell*GPU_NSUBCELL sj lists */
2264 /* We can store 4 j-subcell - i-supercell pairs in one struct.
2265 * since we round down, we need one extra entry.
2267 ncj4_max = ((nbl->work->cj_ind + nsupercell*GPU_NSUBCELL + 4-1) >> 2);
2269 if (ncj4_max > nbl->cj4_nalloc)
2271 nbl->cj4_nalloc = over_alloc_small(ncj4_max);
2272 nbnxn_realloc_void((void **)&nbl->cj4,
2273 nbl->work->cj4_init*sizeof(*nbl->cj4),
2274 nbl->cj4_nalloc*sizeof(*nbl->cj4),
2275 nbl->alloc,nbl->free);
2278 if (ncj4_max > nbl->work->cj4_init)
2280 for(j4=nbl->work->cj4_init; j4<ncj4_max; j4++)
2282 /* No i-subcells and no excl's in the list initially */
2283 for(w=0; w<NWARP; w++)
2285 nbl->cj4[j4].imei[w].imask = 0U;
2286 nbl->cj4[j4].imei[w].excl_ind = 0;
2290 nbl->work->cj4_init = ncj4_max;
2294 /* Set all excl masks for one GPU warp no exclusions */
2295 static void set_no_excls(nbnxn_excl_t *excl)
2299 for(t=0; t<WARP_SIZE; t++)
2301 /* Turn all interaction bits on */
2302 excl->pair[t] = NBNXN_INT_MASK_ALL;
2306 /* Initializes a single nbnxn_pairlist_t data structure */
2307 static void nbnxn_init_pairlist(nbnxn_pairlist_t *nbl,
2309 nbnxn_alloc_t *alloc,
2314 nbl->alloc = nbnxn_alloc_aligned;
2322 nbl->free = nbnxn_free_aligned;
2329 nbl->bSimple = bSimple;
2340 /* We need one element extra in sj, so alloc initially with 1 */
2341 nbl->cj4_nalloc = 0;
2348 nbl->excl_nalloc = 0;
2350 check_excl_space(nbl,1);
2352 set_no_excls(&nbl->excl[0]);
2357 snew_aligned(nbl->work->bb_ci,GPU_NSUBCELL/STRIDE_8BB*NNBSBB_XXXX,16);
2359 snew_aligned(nbl->work->bb_ci,GPU_NSUBCELL*NNBSBB_B,16);
2361 snew_aligned(nbl->work->x_ci,NBNXN_NA_SC_MAX*DIM,16);
2362 #ifdef NBNXN_SEARCH_SSE
2363 snew_aligned(nbl->work->x_ci_x86_simd128,1,16);
2364 #ifdef GMX_X86_AVX_256
2365 snew_aligned(nbl->work->x_ci_x86_simd256,1,32);
2368 snew_aligned(nbl->work->d2,GPU_NSUBCELL,16);
2371 void nbnxn_init_pairlist_set(nbnxn_pairlist_set_t *nbl_list,
2372 gmx_bool bSimple, gmx_bool bCombined,
2373 nbnxn_alloc_t *alloc,
2378 nbl_list->bSimple = bSimple;
2379 nbl_list->bCombined = bCombined;
2381 nbl_list->nnbl = gmx_omp_nthreads_get(emntNonbonded);
2383 if (!nbl_list->bCombined &&
2384 nbl_list->nnbl > NBNXN_BUFFERFLAG_MAX_THREADS)
2386 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.",
2387 nbl_list->nnbl,NBNXN_BUFFERFLAG_MAX_THREADS,NBNXN_BUFFERFLAG_MAX_THREADS);
2390 snew(nbl_list->nbl,nbl_list->nnbl);
2391 /* Execute in order to avoid memory interleaving between threads */
2392 #pragma omp parallel for num_threads(nbl_list->nnbl) schedule(static)
2393 for(i=0; i<nbl_list->nnbl; i++)
2395 /* Allocate the nblist data structure locally on each thread
2396 * to optimize memory access for NUMA architectures.
2398 snew(nbl_list->nbl[i],1);
2400 /* Only list 0 is used on the GPU, use normal allocation for i>0 */
2403 nbnxn_init_pairlist(nbl_list->nbl[i],nbl_list->bSimple,alloc,free);
2407 nbnxn_init_pairlist(nbl_list->nbl[i],nbl_list->bSimple,NULL,NULL);
2412 /* Print statistics of a pair list, used for debug output */
2413 static void print_nblist_statistics_simple(FILE *fp,const nbnxn_pairlist_t *nbl,
2414 const nbnxn_search_t nbs,real rl)
2416 const nbnxn_grid_t *grid;
2421 /* This code only produces correct statistics with domain decomposition */
2422 grid = &nbs->grid[0];
2424 fprintf(fp,"nbl nci %d ncj %d\n",
2426 fprintf(fp,"nbl na_sc %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2427 nbl->na_sc,rl,nbl->ncj,nbl->ncj/(double)grid->nc,
2428 nbl->ncj/(double)grid->nc*grid->na_sc,
2429 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)));
2431 fprintf(fp,"nbl average j cell list length %.1f\n",
2432 0.25*nbl->ncj/(double)nbl->nci);
2434 for(s=0; s<SHIFTS; s++)
2439 for(i=0; i<nbl->nci; i++)
2441 cs[nbl->ci[i].shift & NBNXN_CI_SHIFT] +=
2442 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start;
2444 j = nbl->ci[i].cj_ind_start;
2445 while (j < nbl->ci[i].cj_ind_end &&
2446 nbl->cj[j].excl != NBNXN_INT_MASK_ALL)
2452 fprintf(fp,"nbl cell pairs, total: %d excl: %d %.1f%%\n",
2453 nbl->ncj,npexcl,100*npexcl/(double)nbl->ncj);
2454 for(s=0; s<SHIFTS; s++)
2458 fprintf(fp,"nbl shift %2d ncj %3d\n",s,cs[s]);
2463 /* Print statistics of a pair lists, used for debug output */
2464 static void print_nblist_statistics_supersub(FILE *fp,const nbnxn_pairlist_t *nbl,
2465 const nbnxn_search_t nbs,real rl)
2467 const nbnxn_grid_t *grid;
2469 int c[GPU_NSUBCELL+1];
2471 /* This code only produces correct statistics with domain decomposition */
2472 grid = &nbs->grid[0];
2474 fprintf(fp,"nbl nsci %d ncj4 %d nsi %d excl4 %d\n",
2475 nbl->nsci,nbl->ncj4,nbl->nci_tot,nbl->nexcl);
2476 fprintf(fp,"nbl na_c %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
2477 nbl->na_ci,rl,nbl->nci_tot,nbl->nci_tot/(double)grid->nsubc_tot,
2478 nbl->nci_tot/(double)grid->nsubc_tot*grid->na_c,
2479 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)));
2481 fprintf(fp,"nbl average j super cell list length %.1f\n",
2482 0.25*nbl->ncj4/(double)nbl->nsci);
2483 fprintf(fp,"nbl average i sub cell list length %.1f\n",
2484 nbl->nci_tot/(0.25*nbl->ncj4));
2486 for(si=0; si<=GPU_NSUBCELL; si++)
2490 for(i=0; i<nbl->nsci; i++)
2492 for(j4=nbl->sci[i].cj4_ind_start; j4<nbl->sci[i].cj4_ind_end; j4++)
2497 for(si=0; si<GPU_NSUBCELL; si++)
2499 if (nbl->cj4[j4].imei[0].imask & (1U << (j*GPU_NSUBCELL + si)))
2508 for(b=0; b<=GPU_NSUBCELL; b++)
2510 fprintf(fp,"nbl j-list #i-subcell %d %7d %4.1f\n",
2511 b,c[b],100.0*c[b]/(double)(nbl->ncj4*NBNXN_GPU_JGROUP_SIZE));
2515 /* Print the full pair list, used for debug output */
2516 static void print_supersub_nsp(const char *fn,
2517 const nbnxn_pairlist_t *nbl,
2524 sprintf(buf,"%s_%s.xvg",fn,NONLOCAL_I(iloc) ? "nl" : "l");
2525 fp = ffopen(buf,"w");
2527 for(i=0; i<nbl->nci; i++)
2530 for(j4=nbl->sci[i].cj4_ind_start; j4<nbl->sci[i].cj4_ind_end; j4++)
2532 for(p=0; p<NBNXN_GPU_JGROUP_SIZE*GPU_NSUBCELL; p++)
2534 nsp += (nbl->cj4[j4].imei[0].imask >> p) & 1;
2537 fprintf(fp,"%4d %3d %3d\n",
2540 nbl->sci[i].cj4_ind_end-nbl->sci[i].cj4_ind_start);
2546 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp */
2547 static void low_get_nbl_exclusions(nbnxn_pairlist_t *nbl,int cj4,
2548 int warp,nbnxn_excl_t **excl)
2550 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2552 /* No exclusions set, make a new list entry */
2553 nbl->cj4[cj4].imei[warp].excl_ind = nbl->nexcl;
2555 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2556 set_no_excls(*excl);
2560 /* We already have some exclusions, new ones can be added to the list */
2561 *excl = &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
2565 /* Returns a pointer to the exclusion mask for cj4-unit cj4, warp warp,
2566 * allocates extra memory, if necessary.
2568 static void get_nbl_exclusions_1(nbnxn_pairlist_t *nbl,int cj4,
2569 int warp,nbnxn_excl_t **excl)
2571 if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
2573 /* We need to make a new list entry, check if we have space */
2574 check_excl_space(nbl,1);
2576 low_get_nbl_exclusions(nbl,cj4,warp,excl);
2579 /* Returns pointers to the exclusion mask for cj4-unit cj4 for both warps,
2580 * allocates extra memory, if necessary.
2582 static void get_nbl_exclusions_2(nbnxn_pairlist_t *nbl,int cj4,
2583 nbnxn_excl_t **excl_w0,
2584 nbnxn_excl_t **excl_w1)
2586 /* Check for space we might need */
2587 check_excl_space(nbl,2);
2589 low_get_nbl_exclusions(nbl,cj4,0,excl_w0);
2590 low_get_nbl_exclusions(nbl,cj4,1,excl_w1);
2593 /* Sets the self exclusions i=j and pair exclusions i>j */
2594 static void set_self_and_newton_excls_supersub(nbnxn_pairlist_t *nbl,
2595 int cj4_ind,int sj_offset,
2598 nbnxn_excl_t *excl[2];
2601 /* Here we only set the set self and double pair exclusions */
2603 get_nbl_exclusions_2(nbl,cj4_ind,&excl[0],&excl[1]);
2605 /* Only minor < major bits set */
2606 for(ej=0; ej<nbl->na_ci; ej++)
2609 for(ei=ej; ei<nbl->na_ci; ei++)
2611 excl[w]->pair[(ej&(4-1))*nbl->na_ci+ei] &=
2612 ~(1U << (sj_offset*GPU_NSUBCELL+si));
2617 /* Returns a diagonal or off-diagonal interaction mask for plain C lists */
2618 static unsigned int get_imask(gmx_bool rdiag,int ci,int cj)
2620 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2623 #ifdef NBNXN_SEARCH_SSE
2624 /* Returns a diagonal or off-diagonal interaction mask for SIMD128 lists */
2625 static unsigned int get_imask_x86_simd128(gmx_bool rdiag,int ci,int cj)
2627 #ifndef GMX_DOUBLE /* cj-size = 4 */
2628 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2629 #else /* cj-size = 2 */
2630 return (rdiag && ci*2 == cj ? NBNXN_INT_MASK_DIAG_J2_0 :
2631 (rdiag && ci*2+1 == cj ? NBNXN_INT_MASK_DIAG_J2_1 :
2632 NBNXN_INT_MASK_ALL));
2636 #ifdef GMX_X86_AVX_256
2637 /* Returns a diagonal or off-diagonal interaction mask for SIMD256 lists */
2638 static unsigned int get_imask_x86_simd256(gmx_bool rdiag,int ci,int cj)
2640 #ifndef GMX_DOUBLE /* cj-size = 8 */
2641 return (rdiag && ci == cj*2 ? NBNXN_INT_MASK_DIAG_J8_0 :
2642 (rdiag && ci == cj*2+1 ? NBNXN_INT_MASK_DIAG_J8_1 :
2643 NBNXN_INT_MASK_ALL));
2644 #else /* cj-size = 2 */
2645 return (rdiag && ci == cj ? NBNXN_INT_MASK_DIAG : NBNXN_INT_MASK_ALL);
2649 #endif /* NBNXN_SEARCH_SSE */
2651 /* Plain C code for making a pair list of cell ci vs cell cjf-cjl.
2652 * Checks bounding box distances and possibly atom pair distances.
2654 static void make_cluster_list_simple(const nbnxn_grid_t *gridj,
2655 nbnxn_pairlist_t *nbl,
2656 int ci,int cjf,int cjl,
2657 gmx_bool remove_sub_diag,
2659 real rl2,float rbb2,
2662 const nbnxn_list_work_t *work;
2669 int cjf_gl,cjl_gl,cj;
2673 bb_ci = nbl->work->bb_ci;
2674 x_ci = nbl->work->x_ci;
2677 while (!InRange && cjf <= cjl)
2679 d2 = subc_bb_dist2(0,bb_ci,cjf,gridj->bb);
2682 /* Check if the distance is within the distance where
2683 * we use only the bounding box distance rbb,
2684 * or within the cut-off and there is at least one atom pair
2685 * within the cut-off.
2695 cjf_gl = gridj->cell0 + cjf;
2696 for(i=0; i<NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2698 for(j=0; j<NBNXN_CPU_CLUSTER_I_SIZE; j++)
2700 InRange = InRange ||
2701 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2702 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2703 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjf_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2706 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2719 while (!InRange && cjl > cjf)
2721 d2 = subc_bb_dist2(0,bb_ci,cjl,gridj->bb);
2724 /* Check if the distance is within the distance where
2725 * we use only the bounding box distance rbb,
2726 * or within the cut-off and there is at least one atom pair
2727 * within the cut-off.
2737 cjl_gl = gridj->cell0 + cjl;
2738 for(i=0; i<NBNXN_CPU_CLUSTER_I_SIZE && !InRange; i++)
2740 for(j=0; j<NBNXN_CPU_CLUSTER_I_SIZE; j++)
2742 InRange = InRange ||
2743 (sqr(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+XX]) +
2744 sqr(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+YY]) +
2745 sqr(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjl_gl*NBNXN_CPU_CLUSTER_I_SIZE+j)*STRIDE_XYZ+ZZ]) < rl2);
2748 *ndistc += NBNXN_CPU_CLUSTER_I_SIZE*NBNXN_CPU_CLUSTER_I_SIZE;
2758 for(cj=cjf; cj<=cjl; cj++)
2760 /* Store cj and the interaction mask */
2761 nbl->cj[nbl->ncj].cj = gridj->cell0 + cj;
2762 nbl->cj[nbl->ncj].excl = get_imask(remove_sub_diag,ci,cj);
2765 /* Increase the closing index in i super-cell list */
2766 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
2770 #ifdef NBNXN_SEARCH_SSE
2771 /* Include make_cluster_list_x86_simd128/256 */
2772 #define GMX_MM128_HERE
2773 #include "gmx_x86_simd_macros.h"
2774 #define STRIDE_S PACK_X4
2775 #include "nbnxn_search_x86_simd.h"
2777 #undef GMX_MM128_HERE
2778 #ifdef GMX_X86_AVX_256
2779 /* Include make_cluster_list_x86_simd128/256 */
2780 #define GMX_MM256_HERE
2781 #include "gmx_x86_simd_macros.h"
2782 #define STRIDE_S GMX_X86_SIMD_WIDTH_HERE
2783 #include "nbnxn_search_x86_simd.h"
2785 #undef GMX_MM256_HERE
2789 /* Plain C or SSE code for making a pair list of super-cell sci vs scj.
2790 * Checks bounding box distances and possibly atom pair distances.
2792 static void make_cluster_list_supersub(const nbnxn_search_t nbs,
2793 const nbnxn_grid_t *gridi,
2794 const nbnxn_grid_t *gridj,
2795 nbnxn_pairlist_t *nbl,
2797 gmx_bool sci_equals_scj,
2798 int stride,const real *x,
2799 real rl2,float rbb2,
2804 int cjo,ci1,ci,cj,cj_gl;
2805 int cj4_ind,cj_offset;
2812 #define PRUNE_LIST_CPU_ONE
2813 #ifdef PRUNE_LIST_CPU_ONE
2817 d2l = nbl->work->d2;
2819 bb_ci = nbl->work->bb_ci;
2820 x_ci = nbl->work->x_ci;
2824 for(cjo=0; cjo<gridj->nsubc[scj]; cjo++)
2826 cj4_ind = (nbl->work->cj_ind >> 2);
2827 cj_offset = nbl->work->cj_ind - cj4_ind*NBNXN_GPU_JGROUP_SIZE;
2828 cj4 = &nbl->cj4[cj4_ind];
2830 cj = scj*GPU_NSUBCELL + cjo;
2832 cj_gl = gridj->cell0*GPU_NSUBCELL + cj;
2834 /* Initialize this j-subcell i-subcell list */
2835 cj4->cj[cj_offset] = cj_gl;
2844 ci1 = gridi->nsubc[sci];
2848 /* Determine all ci1 bb distances in one call with SSE */
2849 subc_bb_dist2_sse_xxxx(gridj->bb+(cj>>STRIDE_8BB_2LOG)*NNBSBB_XXXX+(cj & (STRIDE_8BB-1)),
2855 /* We use a fixed upper-bound instead of ci1 to help optimization */
2856 for(ci=0; ci<GPU_NSUBCELL; ci++)
2863 #ifndef NBNXN_BBXXXX
2864 /* Determine the bb distance between ci and cj */
2865 d2l[ci] = subc_bb_dist2(ci,bb_ci,cj,gridj->bb);
2870 #ifdef PRUNE_LIST_CPU_ALL
2871 /* Check if the distance is within the distance where
2872 * we use only the bounding box distance rbb,
2873 * or within the cut-off and there is at least one atom pair
2874 * within the cut-off. This check is very costly.
2876 *ndistc += na_c*na_c;
2878 (d2 < rl2 && subc_in_range_x(na_c,ci,x_ci,cj_gl,stride,x,rl2)))
2880 /* Check if the distance between the two bounding boxes
2881 * in within the pair-list cut-off.
2886 /* Flag this i-subcell to be taken into account */
2887 imask |= (1U << (cj_offset*GPU_NSUBCELL+ci));
2889 #ifdef PRUNE_LIST_CPU_ONE
2897 #ifdef PRUNE_LIST_CPU_ONE
2898 /* If we only found 1 pair, check if any atoms are actually
2899 * within the cut-off, so we could get rid of it.
2901 if (npair == 1 && d2l[ci_last] >= rbb2)
2903 /* Avoid using function pointers here, as it's slower */
2905 #ifdef NBNXN_8BB_SSE
2910 (na_c,ci_last,x_ci,cj_gl,stride,x,rl2))
2912 imask &= ~(1U << (cj_offset*GPU_NSUBCELL+ci_last));
2920 /* We have a useful sj entry, close it now */
2922 /* Set the exclucions for the ci== sj entry.
2923 * Here we don't bother to check if this entry is actually flagged,
2924 * as it will nearly always be in the list.
2928 set_self_and_newton_excls_supersub(nbl,cj4_ind,cj_offset,cjo);
2931 /* Copy the cluster interaction mask to the list */
2932 for(w=0; w<NWARP; w++)
2934 cj4->imei[w].imask |= imask;
2937 nbl->work->cj_ind++;
2939 /* Keep the count */
2940 nbl->nci_tot += npair;
2942 /* Increase the closing index in i super-cell list */
2943 nbl->sci[nbl->nsci].cj4_ind_end = ((nbl->work->cj_ind+4-1)>>2);
2948 /* Set all atom-pair exclusions from the topology stored in excl
2949 * as masks in the pair-list for simple list i-entry nbl_ci
2951 static void set_ci_top_excls(const nbnxn_search_t nbs,
2952 nbnxn_pairlist_t *nbl,
2953 gmx_bool diagRemoved,
2956 const nbnxn_ci_t *nbl_ci,
2957 const t_blocka *excl)
2961 int cj_ind_first,cj_ind_last;
2962 int cj_first,cj_last;
2964 int i,ai,aj,si,eind,ge,se;
2965 int found,cj_ind_0,cj_ind_1,cj_ind_m;
2969 nbnxn_excl_t *nbl_excl;
2970 int inner_i,inner_e;
2974 if (nbl_ci->cj_ind_end == nbl_ci->cj_ind_start)
2982 cj_ind_first = nbl_ci->cj_ind_start;
2983 cj_ind_last = nbl->ncj - 1;
2985 cj_first = nbl->cj[cj_ind_first].cj;
2986 cj_last = nbl->cj[cj_ind_last].cj;
2988 /* Determine how many contiguous j-cells we have starting
2989 * from the first i-cell. This number can be used to directly
2990 * calculate j-cell indices for excluded atoms.
2993 if (na_ci_2log == na_cj_2log)
2995 while (cj_ind_first + ndirect <= cj_ind_last &&
2996 nbl->cj[cj_ind_first+ndirect].cj == ci + ndirect)
3001 #ifdef NBNXN_SEARCH_SSE
3004 while (cj_ind_first + ndirect <= cj_ind_last &&
3005 nbl->cj[cj_ind_first+ndirect].cj == ci_to_cj(na_cj_2log,ci) + ndirect)
3012 /* Loop over the atoms in the i super-cell */
3013 for(i=0; i<nbl->na_sc; i++)
3015 ai = nbs->a[ci*nbl->na_sc+i];
3018 si = (i>>na_ci_2log);
3020 /* Loop over the topology-based exclusions for this i-atom */
3021 for(eind=excl->index[ai]; eind<excl->index[ai+1]; eind++)
3027 /* The self exclusion are already set, save some time */
3033 /* Without shifts we only calculate interactions j>i
3034 * for one-way pair-lists.
3036 if (diagRemoved && ge <= ci*nbl->na_sc + i)
3041 se = (ge >> na_cj_2log);
3043 /* Could the cluster se be in our list? */
3044 if (se >= cj_first && se <= cj_last)
3046 if (se < cj_first + ndirect)
3048 /* We can calculate cj_ind directly from se */
3049 found = cj_ind_first + se - cj_first;
3053 /* Search for se using bisection */
3055 cj_ind_0 = cj_ind_first + ndirect;
3056 cj_ind_1 = cj_ind_last + 1;
3057 while (found == -1 && cj_ind_0 < cj_ind_1)
3059 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3061 cj_m = nbl->cj[cj_ind_m].cj;
3069 cj_ind_1 = cj_ind_m;
3073 cj_ind_0 = cj_ind_m + 1;
3080 inner_i = i - (si << na_ci_2log);
3081 inner_e = ge - (se << na_cj_2log);
3083 nbl->cj[found].excl &= ~(1U<<((inner_i<<na_cj_2log) + inner_e));
3091 /* Set all atom-pair exclusions from the topology stored in excl
3092 * as masks in the pair-list for i-super-cell entry nbl_sci
3094 static void set_sci_top_excls(const nbnxn_search_t nbs,
3095 nbnxn_pairlist_t *nbl,
3096 gmx_bool diagRemoved,
3098 const nbnxn_sci_t *nbl_sci,
3099 const t_blocka *excl)
3104 int cj_ind_first,cj_ind_last;
3105 int cj_first,cj_last;
3107 int i,ai,aj,si,eind,ge,se;
3108 int found,cj_ind_0,cj_ind_1,cj_ind_m;
3112 nbnxn_excl_t *nbl_excl;
3113 int inner_i,inner_e,w;
3119 if (nbl_sci->cj4_ind_end == nbl_sci->cj4_ind_start)
3127 cj_ind_first = nbl_sci->cj4_ind_start*NBNXN_GPU_JGROUP_SIZE;
3128 cj_ind_last = nbl->work->cj_ind - 1;
3130 cj_first = nbl->cj4[nbl_sci->cj4_ind_start].cj[0];
3131 cj_last = nbl_cj(nbl,cj_ind_last);
3133 /* Determine how many contiguous j-clusters we have starting
3134 * from the first i-cluster. This number can be used to directly
3135 * calculate j-cluster indices for excluded atoms.
3138 while (cj_ind_first + ndirect <= cj_ind_last &&
3139 nbl_cj(nbl,cj_ind_first+ndirect) == sci*GPU_NSUBCELL + ndirect)
3144 /* Loop over the atoms in the i super-cell */
3145 for(i=0; i<nbl->na_sc; i++)
3147 ai = nbs->a[sci*nbl->na_sc+i];
3150 si = (i>>na_c_2log);
3152 /* Loop over the topology-based exclusions for this i-atom */
3153 for(eind=excl->index[ai]; eind<excl->index[ai+1]; eind++)
3159 /* The self exclusion are already set, save some time */
3165 /* Without shifts we only calculate interactions j>i
3166 * for one-way pair-lists.
3168 if (diagRemoved && ge <= sci*nbl->na_sc + i)
3174 /* Could the cluster se be in our list? */
3175 if (se >= cj_first && se <= cj_last)
3177 if (se < cj_first + ndirect)
3179 /* We can calculate cj_ind directly from se */
3180 found = cj_ind_first + se - cj_first;
3184 /* Search for se using bisection */
3186 cj_ind_0 = cj_ind_first + ndirect;
3187 cj_ind_1 = cj_ind_last + 1;
3188 while (found == -1 && cj_ind_0 < cj_ind_1)
3190 cj_ind_m = (cj_ind_0 + cj_ind_1)>>1;
3192 cj_m = nbl_cj(nbl,cj_ind_m);
3200 cj_ind_1 = cj_ind_m;
3204 cj_ind_0 = cj_ind_m + 1;
3211 inner_i = i - si*na_c;
3212 inner_e = ge - se*na_c;
3214 /* Macro for getting the index of atom a within a cluster */
3215 #define AMODI(a) ((a) & (NBNXN_CPU_CLUSTER_I_SIZE - 1))
3216 /* Macro for converting an atom number to a cluster number */
3217 #define A2CI(a) ((a) >> NBNXN_CPU_CLUSTER_I_SIZE_2LOG)
3219 if (nbl_imask0(nbl,found) & (1U << (AMODI(found)*GPU_NSUBCELL + si)))
3223 get_nbl_exclusions_1(nbl,A2CI(found),w,&nbl_excl);
3225 nbl_excl->pair[AMODI(inner_e)*nbl->na_ci+inner_i] &=
3226 ~(1U << (AMODI(found)*GPU_NSUBCELL + si));
3238 /* Reallocate the simple ci list for at least n entries */
3239 static void nb_realloc_ci(nbnxn_pairlist_t *nbl,int n)
3241 nbl->ci_nalloc = over_alloc_small(n);
3242 nbnxn_realloc_void((void **)&nbl->ci,
3243 nbl->nci*sizeof(*nbl->ci),
3244 nbl->ci_nalloc*sizeof(*nbl->ci),
3245 nbl->alloc,nbl->free);
3248 /* Reallocate the super-cell sci list for at least n entries */
3249 static void nb_realloc_sci(nbnxn_pairlist_t *nbl,int n)
3251 nbl->sci_nalloc = over_alloc_small(n);
3252 nbnxn_realloc_void((void **)&nbl->sci,
3253 nbl->nsci*sizeof(*nbl->sci),
3254 nbl->sci_nalloc*sizeof(*nbl->sci),
3255 nbl->alloc,nbl->free);
3258 /* Make a new ci entry at index nbl->nci */
3259 static void new_ci_entry(nbnxn_pairlist_t *nbl,int ci,int shift,int flags,
3260 nbnxn_list_work_t *work)
3262 if (nbl->nci + 1 > nbl->ci_nalloc)
3264 nb_realloc_ci(nbl,nbl->nci+1);
3266 nbl->ci[nbl->nci].ci = ci;
3267 nbl->ci[nbl->nci].shift = shift;
3268 /* Store the interaction flags along with the shift */
3269 nbl->ci[nbl->nci].shift |= flags;
3270 nbl->ci[nbl->nci].cj_ind_start = nbl->ncj;
3271 nbl->ci[nbl->nci].cj_ind_end = nbl->ncj;
3274 /* Make a new sci entry at index nbl->nsci */
3275 static void new_sci_entry(nbnxn_pairlist_t *nbl,int sci,int shift,int flags,
3276 nbnxn_list_work_t *work)
3278 if (nbl->nsci + 1 > nbl->sci_nalloc)
3280 nb_realloc_sci(nbl,nbl->nsci+1);
3282 nbl->sci[nbl->nsci].sci = sci;
3283 nbl->sci[nbl->nsci].shift = shift;
3284 nbl->sci[nbl->nsci].cj4_ind_start = nbl->ncj4;
3285 nbl->sci[nbl->nsci].cj4_ind_end = nbl->ncj4;
3288 /* Sort the simple j-list cj on exclusions.
3289 * Entries with exclusions will all be sorted to the beginning of the list.
3291 static void sort_cj_excl(nbnxn_cj_t *cj,int ncj,
3292 nbnxn_list_work_t *work)
3296 if (ncj > work->cj_nalloc)
3298 work->cj_nalloc = over_alloc_large(ncj);
3299 srenew(work->cj,work->cj_nalloc);
3302 /* Make a list of the j-cells involving exclusions */
3304 for(j=0; j<ncj; j++)
3306 if (cj[j].excl != NBNXN_INT_MASK_ALL)
3308 work->cj[jnew++] = cj[j];
3311 /* Check if there are exclusions at all or not just the first entry */
3312 if (!((jnew == 0) ||
3313 (jnew == 1 && cj[0].excl != NBNXN_INT_MASK_ALL)))
3315 for(j=0; j<ncj; j++)
3317 if (cj[j].excl == NBNXN_INT_MASK_ALL)
3319 work->cj[jnew++] = cj[j];
3322 for(j=0; j<ncj; j++)
3324 cj[j] = work->cj[j];
3329 /* Close this simple list i entry */
3330 static void close_ci_entry_simple(nbnxn_pairlist_t *nbl)
3334 /* All content of the new ci entry have already been filled correctly,
3335 * we only need to increase the count here (for non empty lists).
3337 jlen = nbl->ci[nbl->nci].cj_ind_end - nbl->ci[nbl->nci].cj_ind_start;
3340 sort_cj_excl(nbl->cj+nbl->ci[nbl->nci].cj_ind_start,jlen,nbl->work);
3342 if (nbl->ci[nbl->nci].shift & NBNXN_CI_HALF_LJ(0))
3344 nbl->work->ncj_hlj += jlen;
3346 else if (!(nbl->ci[nbl->nci].shift & NBNXN_CI_DO_COUL(0)))
3348 nbl->work->ncj_noq += jlen;
3355 /* Split sci entry for load balancing on the GPU.
3356 * As we only now the current count on our own thread,
3357 * we will need to estimate the current total amount of i-entries.
3358 * As the lists get concatenated later, this estimate depends
3359 * both on nthread and our own thread index thread.
3361 static void split_sci_entry(nbnxn_pairlist_t *nbl,
3362 int nsp_max_av,gmx_bool progBal,int nc_bal,
3363 int thread,int nthread)
3367 int cj4_start,cj4_end,j4len,cj4;
3369 int nsp,nsp_sci,nsp_cj4,nsp_cj4_e,nsp_cj4_p;
3372 /* Estimate the total numbers of ci's of the nblist combined
3373 * over all threads using the target number of ci's.
3375 nsci_est = nc_bal*thread/nthread + nbl->nsci;
3378 /* The first ci blocks should be larger, to avoid overhead.
3379 * The last ci blocks should be smaller, to improve load balancing.
3382 nsp_max_av*nc_bal*3/(2*(nsci_est - 1 + nc_bal)));
3386 nsp_max = nsp_max_av;
3389 cj4_start = nbl->sci[nbl->nsci-1].cj4_ind_start;
3390 cj4_end = nbl->sci[nbl->nsci-1].cj4_ind_end;
3391 j4len = cj4_end - cj4_start;
3393 if (j4len > 1 && j4len*GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE > nsp_max)
3395 /* Remove the last ci entry and process the cj4's again */
3404 while (cj4 < cj4_end)
3406 nsp_cj4_p = nsp_cj4;
3408 for(p=0; p<GPU_NSUBCELL*NBNXN_GPU_JGROUP_SIZE; p++)
3410 nsp_cj4 += (nbl->cj4[cj4].imei[0].imask >> p) & 1;
3414 if (nsp > nsp_max && nsp > nsp_cj4)
3416 nbl->sci[sci].cj4_ind_end = cj4;
3419 if (nbl->nsci+1 > nbl->sci_nalloc)
3421 nb_realloc_sci(nbl,nbl->nsci+1);
3423 nbl->sci[sci].sci = nbl->sci[nbl->nsci-1].sci;
3424 nbl->sci[sci].shift = nbl->sci[nbl->nsci-1].shift;
3425 nbl->sci[sci].cj4_ind_start = cj4;
3426 nsp_sci = nsp - nsp_cj4;
3427 nsp_cj4_e = nsp_cj4_p;
3434 /* Put the remaining cj4's in a new ci entry */
3435 nbl->sci[sci].cj4_ind_end = cj4_end;
3437 /* Possibly balance out the last two ci's
3438 * by moving the last cj4 of the second last ci.
3440 if (nsp_sci - nsp_cj4_e >= nsp + nsp_cj4_e)
3442 nbl->sci[sci-1].cj4_ind_end--;
3443 nbl->sci[sci].cj4_ind_start--;
3451 /* Clost this super/sub list i entry */
3452 static void close_ci_entry_supersub(nbnxn_pairlist_t *nbl,
3454 gmx_bool progBal,int nc_bal,
3455 int thread,int nthread)
3460 /* All content of the new ci entry have already been filled correctly,
3461 * we only need to increase the count here (for non empty lists).
3463 j4len = nbl->sci[nbl->nsci].cj4_ind_end - nbl->sci[nbl->nsci].cj4_ind_start;
3466 /* We can only have complete blocks of 4 j-entries in a list,
3467 * so round the count up before closing.
3469 nbl->ncj4 = ((nbl->work->cj_ind + 4-1) >> 2);
3470 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3476 split_sci_entry(nbl,nsp_max_av,progBal,nc_bal,thread,nthread);
3481 /* Syncs the working array before adding another grid pair to the list */
3482 static void sync_work(nbnxn_pairlist_t *nbl)
3486 nbl->work->cj_ind = nbl->ncj4*NBNXN_GPU_JGROUP_SIZE;
3487 nbl->work->cj4_init = nbl->ncj4;
3491 /* Clears an nbnxn_pairlist_t data structure */
3492 static void clear_pairlist(nbnxn_pairlist_t *nbl)
3501 nbl->work->ncj_noq = 0;
3502 nbl->work->ncj_hlj = 0;
3505 /* Sets a simple list i-cell bounding box, including PBC shift */
3506 static void set_icell_bb_simple(const float *bb,int ci,
3507 real shx,real shy,real shz,
3513 bb_ci[BBL_X] = bb[ia+BBL_X] + shx;
3514 bb_ci[BBL_Y] = bb[ia+BBL_Y] + shy;
3515 bb_ci[BBL_Z] = bb[ia+BBL_Z] + shz;
3516 bb_ci[BBU_X] = bb[ia+BBU_X] + shx;
3517 bb_ci[BBU_Y] = bb[ia+BBU_Y] + shy;
3518 bb_ci[BBU_Z] = bb[ia+BBU_Z] + shz;
3521 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
3522 static void set_icell_bb_supersub(const float *bb,int ci,
3523 real shx,real shy,real shz,
3529 ia = ci*(GPU_NSUBCELL>>STRIDE_8BB_2LOG)*NNBSBB_XXXX;
3530 for(m=0; m<(GPU_NSUBCELL>>STRIDE_8BB_2LOG)*NNBSBB_XXXX; m+=NNBSBB_XXXX)
3532 for(i=0; i<STRIDE_8BB; i++)
3534 bb_ci[m+0*STRIDE_8BB+i] = bb[ia+m+0*STRIDE_8BB+i] + shx;
3535 bb_ci[m+1*STRIDE_8BB+i] = bb[ia+m+1*STRIDE_8BB+i] + shy;
3536 bb_ci[m+2*STRIDE_8BB+i] = bb[ia+m+2*STRIDE_8BB+i] + shz;
3537 bb_ci[m+3*STRIDE_8BB+i] = bb[ia+m+3*STRIDE_8BB+i] + shx;
3538 bb_ci[m+4*STRIDE_8BB+i] = bb[ia+m+4*STRIDE_8BB+i] + shy;
3539 bb_ci[m+5*STRIDE_8BB+i] = bb[ia+m+5*STRIDE_8BB+i] + shz;
3543 ia = ci*GPU_NSUBCELL*NNBSBB_B;
3544 for(i=0; i<GPU_NSUBCELL*NNBSBB_B; i+=NNBSBB_B)
3546 bb_ci[i+BBL_X] = bb[ia+i+BBL_X] + shx;
3547 bb_ci[i+BBL_Y] = bb[ia+i+BBL_Y] + shy;
3548 bb_ci[i+BBL_Z] = bb[ia+i+BBL_Z] + shz;
3549 bb_ci[i+BBU_X] = bb[ia+i+BBU_X] + shx;
3550 bb_ci[i+BBU_Y] = bb[ia+i+BBU_Y] + shy;
3551 bb_ci[i+BBU_Z] = bb[ia+i+BBU_Z] + shz;
3556 /* Copies PBC shifted i-cell atom coordinates x,y,z to working array */
3557 static void icell_set_x_simple(int ci,
3558 real shx,real shy,real shz,
3560 int stride,const real *x,
3561 nbnxn_list_work_t *work)
3565 ia = ci*NBNXN_CPU_CLUSTER_I_SIZE;
3567 for(i=0; i<NBNXN_CPU_CLUSTER_I_SIZE; i++)
3569 work->x_ci[i*STRIDE_XYZ+XX] = x[(ia+i)*stride+XX] + shx;
3570 work->x_ci[i*STRIDE_XYZ+YY] = x[(ia+i)*stride+YY] + shy;
3571 work->x_ci[i*STRIDE_XYZ+ZZ] = x[(ia+i)*stride+ZZ] + shz;
3575 /* Copies PBC shifted super-cell atom coordinates x,y,z to working array */
3576 static void icell_set_x_supersub(int ci,
3577 real shx,real shy,real shz,
3579 int stride,const real *x,
3580 nbnxn_list_work_t *work)
3587 ia = ci*GPU_NSUBCELL*na_c;
3588 for(i=0; i<GPU_NSUBCELL*na_c; i++)
3590 x_ci[i*DIM + XX] = x[(ia+i)*stride + XX] + shx;
3591 x_ci[i*DIM + YY] = x[(ia+i)*stride + YY] + shy;
3592 x_ci[i*DIM + ZZ] = x[(ia+i)*stride + ZZ] + shz;
3596 #ifdef NBNXN_SEARCH_SSE
3597 /* Copies PBC shifted super-cell packed atom coordinates to working array */
3598 static void icell_set_x_supersub_sse8(int ci,
3599 real shx,real shy,real shz,
3601 int stride,const real *x,
3602 nbnxn_list_work_t *work)
3609 for(si=0; si<GPU_NSUBCELL; si++)
3611 for(i=0; i<na_c; i+=STRIDE_8BB)
3614 ia = ci*GPU_NSUBCELL*na_c + io;
3615 for(j=0; j<STRIDE_8BB; j++)
3617 x_ci[io*DIM + j + XX*STRIDE_8BB] = x[(ia+j)*stride+XX] + shx;
3618 x_ci[io*DIM + j + YY*STRIDE_8BB] = x[(ia+j)*stride+YY] + shy;
3619 x_ci[io*DIM + j + ZZ*STRIDE_8BB] = x[(ia+j)*stride+ZZ] + shz;
3626 static real nbnxn_rlist_inc_nonloc_fac = 0.6;
3628 /* Due to the cluster size the effective pair-list is longer than
3629 * that of a simple atom pair-list. This function gives the extra distance.
3631 real nbnxn_get_rlist_effective_inc(int cluster_size,real atom_density)
3633 return ((0.5 + nbnxn_rlist_inc_nonloc_fac)*sqr(((cluster_size) - 1.0)/(cluster_size))*pow((cluster_size)/(atom_density),1.0/3.0));
3636 /* Estimates the interaction volume^2 for non-local interactions */
3637 static real nonlocal_vol2(const gmx_domdec_zones_t *zones,rvec ls,real r)
3646 /* Here we simply add up the volumes of 1, 2 or 3 1D decomposition
3647 * not home interaction volume^2. As these volumes are not additive,
3648 * this is an overestimate, but it would only be significant in the limit
3649 * of small cells, where we anyhow need to split the lists into
3650 * as small parts as possible.
3653 for(z=0; z<zones->n; z++)
3655 if (zones->shift[z][XX] + zones->shift[z][YY] + zones->shift[z][ZZ] == 1)
3660 for(d=0; d<DIM; d++)
3662 if (zones->shift[z][d] == 0)
3666 za *= zones->size[z].x1[d] - zones->size[z].x0[d];
3670 /* 4 octants of a sphere */
3671 vold_est = 0.25*M_PI*r*r*r*r;
3672 /* 4 quarter pie slices on the edges */
3673 vold_est += 4*cl*M_PI/6.0*r*r*r;
3674 /* One rectangular volume on a face */
3675 vold_est += ca*0.5*r*r;
3677 vol2_est_tot += vold_est*za;
3681 return vol2_est_tot;
3684 /* Estimates the average size of a full j-list for super/sub setup */
3685 static int get_nsubpair_max(const nbnxn_search_t nbs,
3688 int min_ci_balanced)
3690 const nbnxn_grid_t *grid;
3692 real xy_diag2,r_eff_sup,vol_est,nsp_est,nsp_est_nl;
3695 grid = &nbs->grid[0];
3697 ls[XX] = (grid->c1[XX] - grid->c0[XX])/(grid->ncx*GPU_NSUBCELL_X);
3698 ls[YY] = (grid->c1[YY] - grid->c0[YY])/(grid->ncy*GPU_NSUBCELL_Y);
3699 ls[ZZ] = (grid->c1[ZZ] - grid->c0[ZZ])*grid->ncx*grid->ncy/(grid->nc*GPU_NSUBCELL_Z);
3701 /* The average squared length of the diagonal of a sub cell */
3702 xy_diag2 = ls[XX]*ls[XX] + ls[YY]*ls[YY] + ls[ZZ]*ls[ZZ];
3704 /* The formulas below are a heuristic estimate of the average nsj per si*/
3705 r_eff_sup = rlist + nbnxn_rlist_inc_nonloc_fac*sqr((grid->na_c - 1.0)/grid->na_c)*sqrt(xy_diag2/3);
3707 if (!nbs->DomDec || nbs->zones->n == 1)
3714 sqr(grid->atom_density/grid->na_c)*
3715 nonlocal_vol2(nbs->zones,ls,r_eff_sup);
3720 /* Sub-cell interacts with itself */
3721 vol_est = ls[XX]*ls[YY]*ls[ZZ];
3722 /* 6/2 rectangular volume on the faces */
3723 vol_est += (ls[XX]*ls[YY] + ls[XX]*ls[ZZ] + ls[YY]*ls[ZZ])*r_eff_sup;
3724 /* 12/2 quarter pie slices on the edges */
3725 vol_est += 2*(ls[XX] + ls[YY] + ls[ZZ])*0.25*M_PI*sqr(r_eff_sup);
3726 /* 4 octants of a sphere */
3727 vol_est += 0.5*4.0/3.0*M_PI*pow(r_eff_sup,3);
3729 nsp_est = grid->nsubc_tot*vol_est*grid->atom_density/grid->na_c;
3731 /* Subtract the non-local pair count */
3732 nsp_est -= nsp_est_nl;
3736 fprintf(debug,"nsp_est local %5.1f non-local %5.1f\n",
3737 nsp_est,nsp_est_nl);
3742 nsp_est = nsp_est_nl;
3745 if (min_ci_balanced <= 0 || grid->nc >= min_ci_balanced || grid->nc == 0)
3747 /* We don't need to worry */
3752 /* Thus the (average) maximum j-list size should be as follows */
3753 nsubpair_max = max(1,(int)(nsp_est/min_ci_balanced+0.5));
3755 /* Since the target value is a maximum (this avoid high outliers,
3756 * which lead to load imbalance), not average, we get more lists
3757 * than we ask for (to compensate we need to add GPU_NSUBCELL*4/4).
3758 * But more importantly, the optimal GPU performance moves
3759 * to lower number of block for very small blocks.
3760 * To compensate we add the maximum pair count per cj4.
3762 nsubpair_max += GPU_NSUBCELL*NBNXN_CPU_CLUSTER_I_SIZE;
3767 fprintf(debug,"nbl nsp estimate %.1f, nsubpair_max %d\n",
3768 nsp_est,nsubpair_max);
3771 return nsubpair_max;
3774 /* Debug list print function */
3775 static void print_nblist_ci_cj(FILE *fp,const nbnxn_pairlist_t *nbl)
3779 for(i=0; i<nbl->nci; i++)
3781 fprintf(fp,"ci %4d shift %2d ncj %3d\n",
3782 nbl->ci[i].ci,nbl->ci[i].shift,
3783 nbl->ci[i].cj_ind_end - nbl->ci[i].cj_ind_start);
3785 for(j=nbl->ci[i].cj_ind_start; j<nbl->ci[i].cj_ind_end; j++)
3787 fprintf(fp," cj %5d imask %x\n",
3794 /* Debug list print function */
3795 static void print_nblist_sci_cj(FILE *fp,const nbnxn_pairlist_t *nbl)
3799 for(i=0; i<nbl->nsci; i++)
3801 fprintf(fp,"ci %4d shift %2d ncj4 %2d\n",
3802 nbl->sci[i].sci,nbl->sci[i].shift,
3803 nbl->sci[i].cj4_ind_end - nbl->sci[i].cj4_ind_start);
3805 for(j4=nbl->sci[i].cj4_ind_start; j4<nbl->sci[i].cj4_ind_end; j4++)
3809 fprintf(fp," sj %5d imask %x\n",
3811 nbl->cj4[j4].imei[0].imask);
3817 /* Combine pair lists *nbl generated on multiple threads nblc */
3818 static void combine_nblists(int nnbl,nbnxn_pairlist_t **nbl,
3819 nbnxn_pairlist_t *nblc)
3821 int nsci,ncj4,nexcl;
3826 gmx_incons("combine_nblists does not support simple lists");
3831 nexcl = nblc->nexcl;
3832 for(i=0; i<nnbl; i++)
3834 nsci += nbl[i]->nsci;
3835 ncj4 += nbl[i]->ncj4;
3836 nexcl += nbl[i]->nexcl;
3839 if (nsci > nblc->sci_nalloc)
3841 nb_realloc_sci(nblc,nsci);
3843 if (ncj4 > nblc->cj4_nalloc)
3845 nblc->cj4_nalloc = over_alloc_small(ncj4);
3846 nbnxn_realloc_void((void **)&nblc->cj4,
3847 nblc->ncj4*sizeof(*nblc->cj4),
3848 nblc->cj4_nalloc*sizeof(*nblc->cj4),
3849 nblc->alloc,nblc->free);
3851 if (nexcl > nblc->excl_nalloc)
3853 nblc->excl_nalloc = over_alloc_small(nexcl);
3854 nbnxn_realloc_void((void **)&nblc->excl,
3855 nblc->nexcl*sizeof(*nblc->excl),
3856 nblc->excl_nalloc*sizeof(*nblc->excl),
3857 nblc->alloc,nblc->free);
3860 /* Each thread should copy its own data to the combined arrays,
3861 * as otherwise data will go back and forth between different caches.
3863 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntPairsearch)) schedule(static)
3864 for(n=0; n<nnbl; n++)
3871 const nbnxn_pairlist_t *nbli;
3873 /* Determine the offset in the combined data for our thread */
3874 sci_offset = nblc->nsci;
3875 cj4_offset = nblc->ncj4;
3876 ci_offset = nblc->nci_tot;
3877 excl_offset = nblc->nexcl;
3881 sci_offset += nbl[i]->nsci;
3882 cj4_offset += nbl[i]->ncj4;
3883 ci_offset += nbl[i]->nci_tot;
3884 excl_offset += nbl[i]->nexcl;
3889 for(i=0; i<nbli->nsci; i++)
3891 nblc->sci[sci_offset+i] = nbli->sci[i];
3892 nblc->sci[sci_offset+i].cj4_ind_start += cj4_offset;
3893 nblc->sci[sci_offset+i].cj4_ind_end += cj4_offset;
3896 for(j4=0; j4<nbli->ncj4; j4++)
3898 nblc->cj4[cj4_offset+j4] = nbli->cj4[j4];
3899 nblc->cj4[cj4_offset+j4].imei[0].excl_ind += excl_offset;
3900 nblc->cj4[cj4_offset+j4].imei[1].excl_ind += excl_offset;
3903 for(j4=0; j4<nbli->nexcl; j4++)
3905 nblc->excl[excl_offset+j4] = nbli->excl[j4];
3909 for(n=0; n<nnbl; n++)
3911 nblc->nsci += nbl[n]->nsci;
3912 nblc->ncj4 += nbl[n]->ncj4;
3913 nblc->nci_tot += nbl[n]->nci_tot;
3914 nblc->nexcl += nbl[n]->nexcl;
3918 /* Returns the next ci to be processes by our thread */
3919 static gmx_bool next_ci(const nbnxn_grid_t *grid,
3921 int nth,int ci_block,
3922 int *ci_x,int *ci_y,
3928 if (*ci_b == ci_block)
3930 /* Jump to the next block assigned to this task */
3931 *ci += (nth - 1)*ci_block;
3935 if (*ci >= grid->nc*conv)
3940 while (*ci >= grid->cxy_ind[*ci_x*grid->ncy + *ci_y + 1]*conv)
3943 if (*ci_y == grid->ncy)
3953 /* Returns the distance^2 for which we put cell pairs in the list
3954 * without checking atom pair distances. This is usually < rlist^2.
3956 static float boundingbox_only_distance2(const nbnxn_grid_t *gridi,
3957 const nbnxn_grid_t *gridj,
3961 /* If the distance between two sub-cell bounding boxes is less
3962 * than this distance, do not check the distance between
3963 * all particle pairs in the sub-cell, since then it is likely
3964 * that the box pair has atom pairs within the cut-off.
3965 * We use the nblist cut-off minus 0.5 times the average x/y diagonal
3966 * spacing of the sub-cells. Around 40% of the checked pairs are pruned.
3967 * Using more than 0.5 gains at most 0.5%.
3968 * If forces are calculated more than twice, the performance gain
3969 * in the force calculation outweighs the cost of checking.
3970 * Note that with subcell lists, the atom-pair distance check
3971 * is only performed when only 1 out of 8 sub-cells in within range,
3972 * this is because the GPU is much faster than the cpu.
3977 bbx = 0.5*(gridi->sx + gridj->sx);
3978 bby = 0.5*(gridi->sy + gridj->sy);
3981 bbx /= GPU_NSUBCELL_X;
3982 bby /= GPU_NSUBCELL_Y;
3985 rbb2 = sqr(max(0,rlist - 0.5*sqrt(bbx*bbx + bby*bby)));
3990 return (float)((1+GMX_FLOAT_EPS)*rbb2);
3994 static int get_ci_block_size(const nbnxn_grid_t *gridi,
3995 gmx_bool bDomDec, int nth)
3997 const int ci_block_enum = 5;
3998 const int ci_block_denom = 11;
3999 const int ci_block_min_atoms = 16;
4002 /* Here we decide how to distribute the blocks over the threads.
4003 * We use prime numbers to try to avoid that the grid size becomes
4004 * a multiple of the number of threads, which would lead to some
4005 * threads getting "inner" pairs and others getting boundary pairs,
4006 * which in turns will lead to load imbalance between threads.
4007 * Set the block size as 5/11/ntask times the average number of cells
4008 * in a y,z slab. This should ensure a quite uniform distribution
4009 * of the grid parts of the different thread along all three grid
4010 * zone boundaries with 3D domain decomposition. At the same time
4011 * the blocks will not become too small.
4013 ci_block = (gridi->nc*ci_block_enum)/(ci_block_denom*gridi->ncx*nth);
4015 /* Ensure the blocks are not too small: avoids cache invalidation */
4016 if (ci_block*gridi->na_sc < ci_block_min_atoms)
4018 ci_block = (ci_block_min_atoms + gridi->na_sc - 1)/gridi->na_sc;
4021 /* Without domain decomposition
4022 * or with less than 3 blocks per task, divide in nth blocks.
4024 if (!bDomDec || ci_block*3*nth > gridi->nc)
4026 ci_block = (gridi->nc + nth - 1)/nth;
4032 /* Generates the part of pair-list nbl assigned to our thread */
4033 static void nbnxn_make_pairlist_part(const nbnxn_search_t nbs,
4034 const nbnxn_grid_t *gridi,
4035 const nbnxn_grid_t *gridj,
4036 nbnxn_search_work_t *work,
4037 const nbnxn_atomdata_t *nbat,
4038 const t_blocka *excl,
4042 gmx_bool bFBufferFlag,
4045 int min_ci_balanced,
4047 nbnxn_pairlist_t *nbl)
4054 int ci_b,ci,ci_x,ci_y,ci_xy,cj;
4061 const float *bb_i,*bbcz_i,*bbcz_j;
4063 real bx0,bx1,by0,by1,bz0,bz1;
4065 real d2cx,d2z,d2z_cx,d2z_cy,d2zx,d2zxy,d2xy;
4066 int cxf,cxl,cyf,cyf_x,cyl;
4071 int gridi_flag_shift=0,gridj_flag_shift=0;
4072 unsigned *gridj_flag=NULL;
4073 int ncj_old_i,ncj_old_j;
4075 nbs_cycle_start(&work->cc[enbsCCsearch]);
4077 if (gridj->bSimple != nbl->bSimple)
4079 gmx_incons("Grid incompatible with pair-list");
4083 nbl->na_sc = gridj->na_sc;
4084 nbl->na_ci = gridj->na_c;
4085 nbl->na_cj = nbnxn_kernel_to_cj_size(nb_kernel_type);
4086 na_cj_2log = get_2log(nbl->na_cj);
4092 /* Determine conversion of clusters to flag blocks */
4093 gridi_flag_shift = 0;
4094 while ((nbl->na_ci<<gridi_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4098 gridj_flag_shift = 0;
4099 while ((nbl->na_cj<<gridj_flag_shift) < NBNXN_BUFFERFLAG_SIZE)
4104 gridj_flag = work->buffer_flags.flag;
4107 copy_mat(nbs->box,box);
4109 rl2 = nbl->rlist*nbl->rlist;
4111 rbb2 = boundingbox_only_distance2(gridi,gridj,nbl->rlist,nbl->bSimple);
4115 fprintf(debug,"nbl bounding box only distance %f\n",sqrt(rbb2));
4118 /* Set the shift range */
4119 for(d=0; d<DIM; d++)
4121 /* Check if we need periodicity shifts.
4122 * Without PBC or with domain decomposition we don't need them.
4124 if (d >= ePBC2npbcdim(nbs->ePBC) || nbs->dd_dim[d])
4131 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < sqrt(rl2))
4142 if (nbl->bSimple && !gridi->bSimple)
4144 conv_i = gridi->na_sc/gridj->na_sc;
4145 bb_i = gridi->bb_simple;
4146 bbcz_i = gridi->bbcz_simple;
4147 flags_i = gridi->flags_simple;
4153 bbcz_i = gridi->bbcz;
4154 flags_i = gridi->flags;
4156 cell0_i = gridi->cell0*conv_i;
4158 bbcz_j = gridj->bbcz;
4162 /* Blocks of the conversion factor - 1 give a large repeat count
4163 * combined with a small block size. This should result in good
4164 * load balancing for both small and large domains.
4166 ci_block = conv_i - 1;
4170 fprintf(debug,"nbl nc_i %d col.av. %.1f ci_block %d\n",
4171 gridi->nc,gridi->nc/(double)(gridi->ncx*gridi->ncy),ci_block);
4177 /* Initially ci_b and ci to 1 before where we want them to start,
4178 * as they will both be incremented in next_ci.
4181 ci = th*ci_block - 1;
4184 while (next_ci(gridi,conv_i,nth,ci_block,&ci_x,&ci_y,&ci_b,&ci))
4186 if (nbl->bSimple && flags_i[ci] == 0)
4191 ncj_old_i = nbl->ncj;
4194 if (gridj != gridi && shp[XX] == 0)
4198 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX];
4202 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx;
4204 if (bx1 < gridj->c0[XX])
4206 d2cx = sqr(gridj->c0[XX] - bx1);
4215 ci_xy = ci_x*gridi->ncy + ci_y;
4217 /* Loop over shift vectors in three dimensions */
4218 for (tz=-shp[ZZ]; tz<=shp[ZZ]; tz++)
4220 shz = tz*box[ZZ][ZZ];
4222 bz0 = bbcz_i[ci*NNBSBB_D ] + shz;
4223 bz1 = bbcz_i[ci*NNBSBB_D+1] + shz;
4235 d2z = sqr(bz0 - box[ZZ][ZZ]);
4238 d2z_cx = d2z + d2cx;
4246 bz1/((real)(gridi->cxy_ind[ci_xy+1] - gridi->cxy_ind[ci_xy]));
4251 /* The check with bz1_frac close to or larger than 1 comes later */
4253 for (ty=-shp[YY]; ty<=shp[YY]; ty++)
4255 shy = ty*box[YY][YY] + tz*box[ZZ][YY];
4259 by0 = bb_i[ci*NNBSBB_B +YY] + shy;
4260 by1 = bb_i[ci*NNBSBB_B+NNBSBB_C+YY] + shy;
4264 by0 = gridi->c0[YY] + (ci_y )*gridi->sy + shy;
4265 by1 = gridi->c0[YY] + (ci_y+1)*gridi->sy + shy;
4268 get_cell_range(by0,by1,
4269 gridj->ncy,gridj->c0[YY],gridj->sy,gridj->inv_sy,
4279 if (by1 < gridj->c0[YY])
4281 d2z_cy += sqr(gridj->c0[YY] - by1);
4283 else if (by0 > gridj->c1[YY])
4285 d2z_cy += sqr(by0 - gridj->c1[YY]);
4288 for (tx=-shp[XX]; tx<=shp[XX]; tx++)
4290 shift = XYZ2IS(tx,ty,tz);
4292 #ifdef NBNXN_SHIFT_BACKWARD
4293 if (gridi == gridj && shift > CENTRAL)
4299 shx = tx*box[XX][XX] + ty*box[YY][XX] + tz*box[ZZ][XX];
4303 bx0 = bb_i[ci*NNBSBB_B +XX] + shx;
4304 bx1 = bb_i[ci*NNBSBB_B+NNBSBB_C+XX] + shx;
4308 bx0 = gridi->c0[XX] + (ci_x )*gridi->sx + shx;
4309 bx1 = gridi->c0[XX] + (ci_x+1)*gridi->sx + shx;
4312 get_cell_range(bx0,bx1,
4313 gridj->ncx,gridj->c0[XX],gridj->sx,gridj->inv_sx,
4324 new_ci_entry(nbl,cell0_i+ci,shift,flags_i[ci],
4329 new_sci_entry(nbl,cell0_i+ci,shift,flags_i[ci],
4333 #ifndef NBNXN_SHIFT_BACKWARD
4336 if (shift == CENTRAL && gridi == gridj &&
4340 /* Leave the pairs with i > j.
4341 * x is the major index, so skip half of it.
4348 set_icell_bb_simple(bb_i,ci,shx,shy,shz,
4353 set_icell_bb_supersub(bb_i,ci,shx,shy,shz,
4357 nbs->icell_set_x(cell0_i+ci,shx,shy,shz,
4358 gridi->na_c,nbat->xstride,nbat->x,
4361 for(cx=cxf; cx<=cxl; cx++)
4364 if (gridj->c0[XX] + cx*gridj->sx > bx1)
4366 d2zx += sqr(gridj->c0[XX] + cx*gridj->sx - bx1);
4368 else if (gridj->c0[XX] + (cx+1)*gridj->sx < bx0)
4370 d2zx += sqr(gridj->c0[XX] + (cx+1)*gridj->sx - bx0);
4373 #ifndef NBNXN_SHIFT_BACKWARD
4374 if (gridi == gridj &&
4375 cx == 0 && cyf < ci_y)
4377 if (gridi == gridj &&
4378 cx == 0 && shift == CENTRAL && cyf < ci_y)
4381 /* Leave the pairs with i > j.
4382 * Skip half of y when i and j have the same x.
4391 for(cy=cyf_x; cy<=cyl; cy++)
4393 c0 = gridj->cxy_ind[cx*gridj->ncy+cy];
4394 c1 = gridj->cxy_ind[cx*gridj->ncy+cy+1];
4395 #ifdef NBNXN_SHIFT_BACKWARD
4396 if (gridi == gridj &&
4397 shift == CENTRAL && c0 < ci)
4404 if (gridj->c0[YY] + cy*gridj->sy > by1)
4406 d2zxy += sqr(gridj->c0[YY] + cy*gridj->sy - by1);
4408 else if (gridj->c0[YY] + (cy+1)*gridj->sy < by0)
4410 d2zxy += sqr(gridj->c0[YY] + (cy+1)*gridj->sy - by0);
4412 if (c1 > c0 && d2zxy < rl2)
4414 cs = c0 + (int)(bz1_frac*(c1 - c0));
4422 /* Find the lowest cell that can possibly
4427 (bbcz_j[cf*NNBSBB_D+1] >= bz0 ||
4428 d2xy + sqr(bbcz_j[cf*NNBSBB_D+1] - bz0) < rl2))
4433 /* Find the highest cell that can possibly
4438 (bbcz_j[cl*NNBSBB_D] <= bz1 ||
4439 d2xy + sqr(bbcz_j[cl*NNBSBB_D] - bz1) < rl2))
4444 #ifdef NBNXN_REFCODE
4446 /* Simple reference code, for debugging,
4447 * overrides the more complex code above.
4452 for(k=c0; k<c1; k++)
4454 if (box_dist2(bx0,bx1,by0,by1,bz0,bz1,
4455 bb+k*NNBSBB_B) < rl2 &&
4460 if (box_dist2(bx0,bx1,by0,by1,bz0,bz1,
4461 bb+k*NNBSBB_B) < rl2 &&
4472 /* We want each atom/cell pair only once,
4473 * only use cj >= ci.
4475 #ifndef NBNXN_SHIFT_BACKWARD
4478 if (shift == CENTRAL)
4487 /* For f buffer flags with simple lists */
4488 ncj_old_j = nbl->ncj;
4490 switch (nb_kernel_type)
4493 check_subcell_list_space_simple(nbl,cl-cf+1);
4495 make_cluster_list_simple(gridj,
4497 (gridi == gridj && shift == CENTRAL),
4502 #ifdef NBNXN_SEARCH_SSE
4503 case nbk4xN_X86_SIMD128:
4504 check_subcell_list_space_simple(nbl,ci_to_cj(na_cj_2log,cl-cf)+2);
4505 make_cluster_list_x86_simd128(gridj,
4507 (gridi == gridj && shift == CENTRAL),
4512 #ifdef GMX_X86_AVX_256
4513 case nbk4xN_X86_SIMD256:
4514 check_subcell_list_space_simple(nbl,ci_to_cj(na_cj_2log,cl-cf)+2);
4515 make_cluster_list_x86_simd256(gridj,
4517 (gridi == gridj && shift == CENTRAL),
4524 case nbk8x8x8_PlainC:
4526 check_subcell_list_space_supersub(nbl,cl-cf+1);
4527 for(cj=cf; cj<=cl; cj++)
4529 make_cluster_list_supersub(nbs,gridi,gridj,
4531 (gridi == gridj && shift == CENTRAL && ci == cj),
4532 nbat->xstride,nbat->x,
4538 ncpcheck += cl - cf + 1;
4540 if (bFBufferFlag && nbl->ncj > ncj_old_j)
4544 cbf = nbl->cj[ncj_old_j].cj >> gridj_flag_shift;
4545 cbl = nbl->cj[nbl->ncj-1].cj >> gridj_flag_shift;
4546 for(cb=cbf; cb<=cbl; cb++)
4548 gridj_flag[cb] = 1U<<th;
4556 /* Set the exclusions for this ci list */
4559 set_ci_top_excls(nbs,
4561 shift == CENTRAL && gridi == gridj,
4564 &(nbl->ci[nbl->nci]),
4569 set_sci_top_excls(nbs,
4571 shift == CENTRAL && gridi == gridj,
4573 &(nbl->sci[nbl->nsci]),
4577 /* Close this ci list */
4580 close_ci_entry_simple(nbl);
4584 close_ci_entry_supersub(nbl,
4586 progBal,min_ci_balanced,
4593 if (bFBufferFlag && nbl->ncj > ncj_old_i)
4595 work->buffer_flags.flag[(gridi->cell0+ci)>>gridi_flag_shift] = 1U<<th;
4599 work->ndistc = ndistc;
4601 nbs_cycle_stop(&work->cc[enbsCCsearch]);
4605 fprintf(debug,"number of distance checks %d\n",ndistc);
4606 fprintf(debug,"ncpcheck %s %d\n",gridi==gridj ? "local" : "non-local",
4611 print_nblist_statistics_simple(debug,nbl,nbs,rlist);
4615 print_nblist_statistics_supersub(debug,nbl,nbs,rlist);
4621 static void reduce_buffer_flags(const nbnxn_search_t nbs,
4623 const nbnxn_buffer_flags_t *dest)
4626 const unsigned *flag;
4628 for(s=0; s<nsrc; s++)
4630 flag = nbs->work[s].buffer_flags.flag;
4632 for(b=0; b<dest->nflag; b++)
4634 dest->flag[b] |= flag[b];
4639 static void print_reduction_cost(const nbnxn_buffer_flags_t *flags,int nout)
4641 int nelem,nkeep,ncopy,nred,b,c,out;
4647 for(b=0; b<flags->nflag; b++)
4649 if (flags->flag[b] == 1)
4651 /* Only flag 0 is set, no copy of reduction required */
4655 else if (flags->flag[b] > 0)
4658 for(out=0; out<nout; out++)
4660 if (flags->flag[b] & (1U<<out))
4677 fprintf(debug,"nbnxn reduction: #flag %d #list %d elem %4.2f, keep %4.2f copy %4.2f red %4.2f\n",
4679 nelem/(double)(flags->nflag),
4680 nkeep/(double)(flags->nflag),
4681 ncopy/(double)(flags->nflag),
4682 nred/(double)(flags->nflag));
4685 /* Make a local or non-local pair-list, depending on iloc */
4686 void nbnxn_make_pairlist(const nbnxn_search_t nbs,
4687 nbnxn_atomdata_t *nbat,
4688 const t_blocka *excl,
4690 int min_ci_balanced,
4691 nbnxn_pairlist_set_t *nbl_list,
4696 nbnxn_grid_t *gridi,*gridj;
4697 int nzi,zi,zj0,zj1,zj;
4701 nbnxn_pairlist_t **nbl;
4703 gmx_bool CombineNBLists;
4704 int np_tot,np_noq,np_hlj,nap;
4706 nnbl = nbl_list->nnbl;
4707 nbl = nbl_list->nbl;
4708 CombineNBLists = nbl_list->bCombined;
4712 fprintf(debug,"ns making %d nblists\n", nnbl);
4715 nbat->bUseBufferFlags = (nbat->nout > 1);
4716 if (nbat->bUseBufferFlags && LOCAL_I(iloc))
4718 init_buffer_flags(&nbat->buffer_flags,nbat->natoms);
4721 if (nbl_list->bSimple)
4723 switch (nb_kernel_type)
4725 #ifdef NBNXN_SEARCH_SSE
4726 case nbk4xN_X86_SIMD128:
4727 nbs->icell_set_x = icell_set_x_x86_simd128;
4729 #ifdef GMX_X86_AVX_256
4730 case nbk4xN_X86_SIMD256:
4731 nbs->icell_set_x = icell_set_x_x86_simd256;
4736 nbs->icell_set_x = icell_set_x_simple;
4742 #ifdef NBNXN_SEARCH_SSE
4743 nbs->icell_set_x = icell_set_x_supersub_sse8;
4745 nbs->icell_set_x = icell_set_x_supersub;
4751 /* Only zone (grid) 0 vs 0 */
4758 nzi = nbs->zones->nizone;
4761 if (!nbl_list->bSimple && min_ci_balanced > 0)
4763 nsubpair_max = get_nsubpair_max(nbs,iloc,rlist,min_ci_balanced);
4770 /* Clear all pair-lists */
4771 for(th=0; th<nnbl; th++)
4773 clear_pairlist(nbl[th]);
4776 for(zi=0; zi<nzi; zi++)
4778 gridi = &nbs->grid[zi];
4780 if (NONLOCAL_I(iloc))
4782 zj0 = nbs->zones->izone[zi].j0;
4783 zj1 = nbs->zones->izone[zi].j1;
4789 for(zj=zj0; zj<zj1; zj++)
4791 gridj = &nbs->grid[zj];
4795 fprintf(debug,"ns search grid %d vs %d\n",zi,zj);
4798 nbs_cycle_start(&nbs->cc[enbsCCsearch]);
4800 if (nbl[0]->bSimple && !gridi->bSimple)
4802 /* Hybrid list, determine blocking later */
4807 ci_block = get_ci_block_size(gridi,nbs->DomDec,nnbl);
4810 #pragma omp parallel for num_threads(nnbl) schedule(static)
4811 for(th=0; th<nnbl; th++)
4813 if (nbat->bUseBufferFlags && zi == 0 && zj == 0)
4815 init_buffer_flags(&nbs->work[th].buffer_flags,nbat->natoms);
4818 if (CombineNBLists && th > 0)
4820 clear_pairlist(nbl[th]);
4823 /* Divide the i super cell equally over the nblists */
4824 nbnxn_make_pairlist_part(nbs,gridi,gridj,
4825 &nbs->work[th],nbat,excl,
4829 nbat->bUseBufferFlags,
4831 (LOCAL_I(iloc) || nbs->zones->n <= 2),
4836 nbs_cycle_stop(&nbs->cc[enbsCCsearch]);
4841 for(th=0; th<nnbl; th++)
4843 inc_nrnb(nrnb,eNR_NBNXN_DIST2,nbs->work[th].ndistc);
4845 if (nbl_list->bSimple)
4847 np_tot += nbl[th]->ncj;
4848 np_noq += nbl[th]->work->ncj_noq;
4849 np_hlj += nbl[th]->work->ncj_hlj;
4853 /* This count ignores potential subsequent pair pruning */
4854 np_tot += nbl[th]->nci_tot;
4857 nap = nbl[0]->na_ci*nbl[0]->na_cj;
4858 nbl_list->natpair_ljq = (np_tot - np_noq)*nap - np_hlj*nap/2;
4859 nbl_list->natpair_lj = np_noq*nap;
4860 nbl_list->natpair_q = np_hlj*nap/2;
4862 if (CombineNBLists && nnbl > 1)
4864 nbs_cycle_start(&nbs->cc[enbsCCcombine]);
4866 combine_nblists(nnbl-1,nbl+1,nbl[0]);
4868 nbs_cycle_stop(&nbs->cc[enbsCCcombine]);
4873 if (nbat->bUseBufferFlags)
4875 reduce_buffer_flags(nbs,nnbl,&nbat->buffer_flags);
4879 print_supersub_nsp("nsubpair",nbl[0],iloc);
4882 /* Special performance logging stuff (env.var. GMX_NBNXN_CYCLE) */
4885 nbs->search_count++;
4887 if (nbs->print_cycles &&
4888 (!nbs->DomDec || (nbs->DomDec && !LOCAL_I(iloc))) &&
4889 nbs->search_count % 100 == 0)
4891 nbs_cycle_print(stderr,nbs);
4894 if (debug && (CombineNBLists && nnbl > 1))
4896 if (nbl[0]->bSimple)
4898 print_nblist_statistics_simple(debug,nbl[0],nbs,rlist);
4902 print_nblist_statistics_supersub(debug,nbl[0],nbs,rlist);
4910 if (nbl[0]->bSimple)
4912 print_nblist_ci_cj(debug,nbl[0]);
4916 print_nblist_sci_cj(debug,nbl[0]);
4920 if (nbat->bUseBufferFlags)
4922 print_reduction_cost(&nbat->buffer_flags,nnbl);