8365163e24c38c983da38ad89a0195c7310de175
[alexxy/gromacs.git] / src / gromacs / nbnxm / pairlist.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 #include "gmxpre.h"
37
38 #include "pairlist.h"
39
40 #include "config.h"
41
42 #include <cassert>
43 #include <cmath>
44 #include <cstring>
45
46 #include <algorithm>
47
48 #include "gromacs/domdec/domdec_struct.h"
49 #include "gromacs/gmxlib/nrnb.h"
50 #include "gromacs/math/functions.h"
51 #include "gromacs/math/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/mdlib/gmx_omp_nthreads.h"
54 #include "gromacs/mdlib/ns.h"
55 #include "gromacs/mdtypes/group.h"
56 #include "gromacs/mdtypes/md_enums.h"
57 #include "gromacs/nbnxm/atomdata.h"
58 #include "gromacs/nbnxm/gpu_data_mgmt.h"
59 #include "gromacs/nbnxm/nbnxm.h"
60 #include "gromacs/nbnxm/nbnxm_geometry.h"
61 #include "gromacs/nbnxm/nbnxm_simd.h"
62 #include "gromacs/nbnxm/pairlistset.h"
63 #include "gromacs/pbcutil/ishift.h"
64 #include "gromacs/pbcutil/pbc.h"
65 #include "gromacs/simd/simd.h"
66 #include "gromacs/simd/vector_operations.h"
67 #include "gromacs/topology/block.h"
68 #include "gromacs/utility/exceptions.h"
69 #include "gromacs/utility/fatalerror.h"
70 #include "gromacs/utility/gmxomp.h"
71 #include "gromacs/utility/smalloc.h"
72
73 #include "grid.h"
74 #include "internal.h"
75 #include "pairlistwork.h"
76
77 using namespace gmx; // TODO: Remove when this file is moved into gmx namespace
78
79 // Convience alias for partial Nbnxn namespace usage
80 using InteractionLocality = Nbnxm::InteractionLocality;
81
82 /* We shift the i-particles backward for PBC.
83  * This leads to more conditionals than shifting forward.
84  * We do this to get more balanced pair lists.
85  */
86 constexpr bool c_pbcShiftBackward = true;
87
88
89 static void nbs_cycle_clear(nbnxn_cycle_t *cc)
90 {
91     for (int i = 0; i < enbsCCnr; i++)
92     {
93         cc[i].count = 0;
94         cc[i].c     = 0;
95     }
96 }
97
98 static double Mcyc_av(const nbnxn_cycle_t *cc)
99 {
100     return static_cast<double>(cc->c)*1e-6/cc->count;
101 }
102
103 static void nbs_cycle_print(FILE *fp, const nbnxn_search *nbs)
104 {
105     fprintf(fp, "\n");
106     fprintf(fp, "ns %4d grid %4.1f search %4.1f red.f %5.3f",
107             nbs->cc[enbsCCgrid].count,
108             Mcyc_av(&nbs->cc[enbsCCgrid]),
109             Mcyc_av(&nbs->cc[enbsCCsearch]),
110             Mcyc_av(&nbs->cc[enbsCCreducef]));
111
112     if (nbs->work.size() > 1)
113     {
114         if (nbs->cc[enbsCCcombine].count > 0)
115         {
116             fprintf(fp, " comb %5.2f",
117                     Mcyc_av(&nbs->cc[enbsCCcombine]));
118         }
119         fprintf(fp, " s. th");
120         for (const nbnxn_search_work_t &work : nbs->work)
121         {
122             fprintf(fp, " %4.1f",
123                     Mcyc_av(&work.cc[enbsCCsearch]));
124         }
125     }
126     fprintf(fp, "\n");
127 }
128
129 /* Layout for the nonbonded NxN pair lists */
130 enum class NbnxnLayout
131 {
132     NoSimd4x4, // i-cluster size 4, j-cluster size 4
133     Simd4xN,   // i-cluster size 4, j-cluster size SIMD width
134     Simd2xNN,  // i-cluster size 4, j-cluster size half SIMD width
135     Gpu8x8x8   // i-cluster size 8, j-cluster size 8 + super-clustering
136 };
137
138 #if GMX_SIMD
139 /* Returns the j-cluster size */
140 template <NbnxnLayout layout>
141 static constexpr int jClusterSize()
142 {
143     static_assert(layout == NbnxnLayout::NoSimd4x4 || layout == NbnxnLayout::Simd4xN || layout == NbnxnLayout::Simd2xNN, "Currently jClusterSize only supports CPU layouts");
144
145     return layout == NbnxnLayout::Simd4xN ? GMX_SIMD_REAL_WIDTH : (layout == NbnxnLayout::Simd2xNN ? GMX_SIMD_REAL_WIDTH/2 : c_nbnxnCpuIClusterSize);
146 }
147
148 /*! \brief Returns the j-cluster index given the i-cluster index.
149  *
150  * \tparam    jClusterSize      The number of atoms in a j-cluster
151  * \tparam    jSubClusterIndex  The j-sub-cluster index (0/1), used when size(j-cluster) < size(i-cluster)
152  * \param[in] ci                The i-cluster index
153  */
154 template <int jClusterSize, int jSubClusterIndex>
155 static inline int cjFromCi(int ci)
156 {
157     static_assert(jClusterSize == c_nbnxnCpuIClusterSize/2 || jClusterSize == c_nbnxnCpuIClusterSize || jClusterSize == c_nbnxnCpuIClusterSize*2, "Only j-cluster sizes 2, 4 and 8 are currently implemented");
158
159     static_assert(jSubClusterIndex == 0 || jSubClusterIndex == 1,
160                   "Only sub-cluster indices 0 and 1 are supported");
161
162     if (jClusterSize == c_nbnxnCpuIClusterSize/2)
163     {
164         if (jSubClusterIndex == 0)
165         {
166             return ci << 1;
167         }
168         else
169         {
170             return ((ci + 1) << 1) - 1;
171         }
172     }
173     else if (jClusterSize == c_nbnxnCpuIClusterSize)
174     {
175         return ci;
176     }
177     else
178     {
179         return ci >> 1;
180     }
181 }
182
183 /*! \brief Returns the j-cluster index given the i-cluster index.
184  *
185  * \tparam    layout            The pair-list layout
186  * \tparam    jSubClusterIndex  The j-sub-cluster index (0/1), used when size(j-cluster) < size(i-cluster)
187  * \param[in] ci                The i-cluster index
188  */
189 template <NbnxnLayout layout, int jSubClusterIndex>
190 static inline int cjFromCi(int ci)
191 {
192     constexpr int clusterSize = jClusterSize<layout>();
193
194     return cjFromCi<clusterSize, jSubClusterIndex>(ci);
195 }
196
197 /* Returns the nbnxn coordinate data index given the i-cluster index */
198 template <NbnxnLayout layout>
199 static inline int xIndexFromCi(int ci)
200 {
201     constexpr int clusterSize = jClusterSize<layout>();
202
203     static_assert(clusterSize == c_nbnxnCpuIClusterSize/2 || clusterSize == c_nbnxnCpuIClusterSize || clusterSize == c_nbnxnCpuIClusterSize*2, "Only j-cluster sizes 2, 4 and 8 are currently implemented");
204
205     if (clusterSize <= c_nbnxnCpuIClusterSize)
206     {
207         /* Coordinates are stored packed in groups of 4 */
208         return ci*STRIDE_P4;
209     }
210     else
211     {
212         /* Coordinates packed in 8, i-cluster size is half the packing width */
213         return (ci >> 1)*STRIDE_P8 + (ci & 1)*(c_packX8 >> 1);
214     }
215 }
216
217 /* Returns the nbnxn coordinate data index given the j-cluster index */
218 template <NbnxnLayout layout>
219 static inline int xIndexFromCj(int cj)
220 {
221     constexpr int clusterSize = jClusterSize<layout>();
222
223     static_assert(clusterSize == c_nbnxnCpuIClusterSize/2 || clusterSize == c_nbnxnCpuIClusterSize || clusterSize == c_nbnxnCpuIClusterSize*2, "Only j-cluster sizes 2, 4 and 8 are currently implemented");
224
225     if (clusterSize == c_nbnxnCpuIClusterSize/2)
226     {
227         /* Coordinates are stored packed in groups of 4 */
228         return (cj >> 1)*STRIDE_P4 + (cj & 1)*(c_packX4 >> 1);
229     }
230     else if (clusterSize == c_nbnxnCpuIClusterSize)
231     {
232         /* Coordinates are stored packed in groups of 4 */
233         return cj*STRIDE_P4;
234     }
235     else
236     {
237         /* Coordinates are stored packed in groups of 8 */
238         return cj*STRIDE_P8;
239     }
240 }
241 #endif //GMX_SIMD
242
243 /* Initializes a single nbnxn_pairlist_t data structure */
244 static void nbnxn_init_pairlist_fep(t_nblist *nl)
245 {
246     nl->type        = GMX_NBLIST_INTERACTION_FREE_ENERGY;
247     nl->igeometry   = GMX_NBLIST_GEOMETRY_PARTICLE_PARTICLE;
248     /* The interaction functions are set in the free energy kernel fuction */
249     nl->ivdw        = -1;
250     nl->ivdwmod     = -1;
251     nl->ielec       = -1;
252     nl->ielecmod    = -1;
253
254     nl->maxnri      = 0;
255     nl->maxnrj      = 0;
256     nl->nri         = 0;
257     nl->nrj         = 0;
258     nl->iinr        = nullptr;
259     nl->gid         = nullptr;
260     nl->shift       = nullptr;
261     nl->jindex      = nullptr;
262     nl->jjnr        = nullptr;
263     nl->excl_fep    = nullptr;
264
265 }
266
267 static void free_nblist(t_nblist *nl)
268 {
269     sfree(nl->iinr);
270     sfree(nl->gid);
271     sfree(nl->shift);
272     sfree(nl->jindex);
273     sfree(nl->jjnr);
274     sfree(nl->excl_fep);
275 }
276
277 nbnxn_search_work_t::nbnxn_search_work_t() :
278     cp0({{0}}
279         ),
280     buffer_flags({0, nullptr, 0}),
281     ndistc(0),
282     nbl_fep(new t_nblist),
283     cp1({{0}})
284 {
285     nbnxn_init_pairlist_fep(nbl_fep.get());
286
287     nbs_cycle_clear(cc);
288 }
289
290 nbnxn_search_work_t::~nbnxn_search_work_t()
291 {
292     sfree(buffer_flags.flag);
293
294     free_nblist(nbl_fep.get());
295 }
296
297 nbnxn_search::nbnxn_search(int                       ePBC,
298                            const ivec               *n_dd_cells,
299                            const gmx_domdec_zones_t *zones,
300                            gmx_bool                  bFEP,
301                            int                       nthread_max) :
302     bFEP(bFEP),
303     ePBC(ePBC),
304     zones(zones),
305     natoms_local(0),
306     natoms_nonlocal(0),
307     search_count(0),
308     work(nthread_max)
309 {
310     // The correct value will be set during the gridding
311     clear_mat(box);
312     clear_ivec(dd_dim);
313     int numGrids = 1;
314     DomDec = n_dd_cells != nullptr;
315     if (DomDec)
316     {
317         for (int d = 0; d < DIM; d++)
318         {
319             if ((*n_dd_cells)[d] > 1)
320             {
321                 dd_dim[d] = 1;
322                 /* Each grid matches a DD zone */
323                 numGrids *= 2;
324             }
325         }
326     }
327
328     grid.resize(numGrids);
329
330     /* Initialize detailed nbsearch cycle counting */
331     print_cycles = (getenv("GMX_NBNXN_CYCLE") != nullptr);
332     nbs_cycle_clear(cc);
333 }
334
335 static void init_buffer_flags(nbnxn_buffer_flags_t *flags,
336                               int                   natoms)
337 {
338     flags->nflag = (natoms + NBNXN_BUFFERFLAG_SIZE - 1)/NBNXN_BUFFERFLAG_SIZE;
339     if (flags->nflag > flags->flag_nalloc)
340     {
341         flags->flag_nalloc = over_alloc_large(flags->nflag);
342         srenew(flags->flag, flags->flag_nalloc);
343     }
344     for (int b = 0; b < flags->nflag; b++)
345     {
346         bitmask_clear(&(flags->flag[b]));
347     }
348 }
349
350 /* Returns the pair-list cutoff between a bounding box and a grid cell given an atom-to-atom pair-list cutoff
351  *
352  * Given a cutoff distance between atoms, this functions returns the cutoff
353  * distance2 between a bounding box of a group of atoms and a grid cell.
354  * Since atoms can be geometrically outside of the cell they have been
355  * assigned to (when atom groups instead of individual atoms are assigned
356  * to cells), this distance returned can be larger than the input.
357  */
358 static real listRangeForBoundingBoxToGridCell(real                rlist,
359                                               const nbnxn_grid_t &grid)
360 {
361     return rlist + grid.maxAtomGroupRadius;
362
363 }
364 /* Returns the pair-list cutoff between a grid cells given an atom-to-atom pair-list cutoff
365  *
366  * Given a cutoff distance between atoms, this functions returns the cutoff
367  * distance2 between two grid cells.
368  * Since atoms can be geometrically outside of the cell they have been
369  * assigned to (when atom groups instead of individual atoms are assigned
370  * to cells), this distance returned can be larger than the input.
371  */
372 static real listRangeForGridCellToGridCell(real                rlist,
373                                            const nbnxn_grid_t &iGrid,
374                                            const nbnxn_grid_t &jGrid)
375 {
376     return rlist + iGrid.maxAtomGroupRadius + jGrid.maxAtomGroupRadius;
377 }
378
379 /* Determines the cell range along one dimension that
380  * the bounding box b0 - b1 sees.
381  */
382 template<int dim>
383 static void get_cell_range(real b0, real b1,
384                            const nbnxn_grid_t &jGrid,
385                            real d2, real rlist, int *cf, int *cl)
386 {
387     real listRangeBBToCell2 = gmx::square(listRangeForBoundingBoxToGridCell(rlist, jGrid));
388     real distanceInCells    = (b0 - jGrid.c0[dim])*jGrid.invCellSize[dim];
389     *cf                     = std::max(static_cast<int>(distanceInCells), 0);
390
391     while (*cf > 0 &&
392            d2 + gmx::square((b0 - jGrid.c0[dim]) - (*cf - 1 + 1)*jGrid.cellSize[dim]) < listRangeBBToCell2)
393     {
394         (*cf)--;
395     }
396
397     *cl = std::min(static_cast<int>((b1 - jGrid.c0[dim])*jGrid.invCellSize[dim]), jGrid.numCells[dim] - 1);
398     while (*cl < jGrid.numCells[dim] - 1 &&
399            d2 + gmx::square((*cl + 1)*jGrid.cellSize[dim] - (b1 - jGrid.c0[dim])) < listRangeBBToCell2)
400     {
401         (*cl)++;
402     }
403 }
404
405 /* Reference code calculating the distance^2 between two bounding boxes */
406 /*
407    static float box_dist2(float bx0, float bx1, float by0,
408                        float by1, float bz0, float bz1,
409                        const nbnxn_bb_t *bb)
410    {
411     float d2;
412     float dl, dh, dm, dm0;
413
414     d2 = 0;
415
416     dl  = bx0 - bb->upper[BB_X];
417     dh  = bb->lower[BB_X] - bx1;
418     dm  = std::max(dl, dh);
419     dm0 = std::max(dm, 0.0f);
420     d2 += dm0*dm0;
421
422     dl  = by0 - bb->upper[BB_Y];
423     dh  = bb->lower[BB_Y] - by1;
424     dm  = std::max(dl, dh);
425     dm0 = std::max(dm, 0.0f);
426     d2 += dm0*dm0;
427
428     dl  = bz0 - bb->upper[BB_Z];
429     dh  = bb->lower[BB_Z] - bz1;
430     dm  = std::max(dl, dh);
431     dm0 = std::max(dm, 0.0f);
432     d2 += dm0*dm0;
433
434     return d2;
435    }
436  */
437
438 /* Plain C code calculating the distance^2 between two bounding boxes */
439 static float subc_bb_dist2(int                              si,
440                            const nbnxn_bb_t                *bb_i_ci,
441                            int                              csj,
442                            gmx::ArrayRef<const nbnxn_bb_t>  bb_j_all)
443 {
444     const nbnxn_bb_t *bb_i = bb_i_ci         +  si;
445     const nbnxn_bb_t *bb_j = bb_j_all.data() + csj;
446
447     float             d2 = 0;
448     float             dl, dh, dm, dm0;
449
450     dl  = bb_i->lower[BB_X] - bb_j->upper[BB_X];
451     dh  = bb_j->lower[BB_X] - bb_i->upper[BB_X];
452     dm  = std::max(dl, dh);
453     dm0 = std::max(dm, 0.0f);
454     d2 += dm0*dm0;
455
456     dl  = bb_i->lower[BB_Y] - bb_j->upper[BB_Y];
457     dh  = bb_j->lower[BB_Y] - bb_i->upper[BB_Y];
458     dm  = std::max(dl, dh);
459     dm0 = std::max(dm, 0.0f);
460     d2 += dm0*dm0;
461
462     dl  = bb_i->lower[BB_Z] - bb_j->upper[BB_Z];
463     dh  = bb_j->lower[BB_Z] - bb_i->upper[BB_Z];
464     dm  = std::max(dl, dh);
465     dm0 = std::max(dm, 0.0f);
466     d2 += dm0*dm0;
467
468     return d2;
469 }
470
471 #if NBNXN_SEARCH_BB_SIMD4
472
473 /* 4-wide SIMD code for bb distance for bb format xyz0 */
474 static float subc_bb_dist2_simd4(int                              si,
475                                  const nbnxn_bb_t                *bb_i_ci,
476                                  int                              csj,
477                                  gmx::ArrayRef<const nbnxn_bb_t>  bb_j_all)
478 {
479     // TODO: During SIMDv2 transition only some archs use namespace (remove when done)
480     using namespace gmx;
481
482     Simd4Float bb_i_S0, bb_i_S1;
483     Simd4Float bb_j_S0, bb_j_S1;
484     Simd4Float dl_S;
485     Simd4Float dh_S;
486     Simd4Float dm_S;
487     Simd4Float dm0_S;
488
489     bb_i_S0 = load4(&bb_i_ci[si].lower[0]);
490     bb_i_S1 = load4(&bb_i_ci[si].upper[0]);
491     bb_j_S0 = load4(&bb_j_all[csj].lower[0]);
492     bb_j_S1 = load4(&bb_j_all[csj].upper[0]);
493
494     dl_S    = bb_i_S0 - bb_j_S1;
495     dh_S    = bb_j_S0 - bb_i_S1;
496
497     dm_S    = max(dl_S, dh_S);
498     dm0_S   = max(dm_S, simd4SetZeroF());
499
500     return dotProduct(dm0_S, dm0_S);
501 }
502
503 /* Calculate bb bounding distances of bb_i[si,...,si+3] and store them in d2 */
504 #define SUBC_BB_DIST2_SIMD4_XXXX_INNER(si, bb_i, d2) \
505     {                                                \
506         int               shi;                                  \
507                                                  \
508         Simd4Float        dx_0, dy_0, dz_0;                    \
509         Simd4Float        dx_1, dy_1, dz_1;                    \
510                                                  \
511         Simd4Float        mx, my, mz;                          \
512         Simd4Float        m0x, m0y, m0z;                       \
513                                                  \
514         Simd4Float        d2x, d2y, d2z;                       \
515         Simd4Float        d2s, d2t;                            \
516                                                  \
517         shi = (si)*NNBSBB_D*DIM;                       \
518                                                  \
519         xi_l = load4((bb_i)+shi+0*STRIDE_PBB);   \
520         yi_l = load4((bb_i)+shi+1*STRIDE_PBB);   \
521         zi_l = load4((bb_i)+shi+2*STRIDE_PBB);   \
522         xi_h = load4((bb_i)+shi+3*STRIDE_PBB);   \
523         yi_h = load4((bb_i)+shi+4*STRIDE_PBB);   \
524         zi_h = load4((bb_i)+shi+5*STRIDE_PBB);   \
525                                                  \
526         dx_0 = xi_l - xj_h;                 \
527         dy_0 = yi_l - yj_h;                 \
528         dz_0 = zi_l - zj_h;                 \
529                                                  \
530         dx_1 = xj_l - xi_h;                 \
531         dy_1 = yj_l - yi_h;                 \
532         dz_1 = zj_l - zi_h;                 \
533                                                  \
534         mx   = max(dx_0, dx_1);                 \
535         my   = max(dy_0, dy_1);                 \
536         mz   = max(dz_0, dz_1);                 \
537                                                  \
538         m0x  = max(mx, zero);                   \
539         m0y  = max(my, zero);                   \
540         m0z  = max(mz, zero);                   \
541                                                  \
542         d2x  = m0x * m0x;                   \
543         d2y  = m0y * m0y;                   \
544         d2z  = m0z * m0z;                   \
545                                                  \
546         d2s  = d2x + d2y;                   \
547         d2t  = d2s + d2z;                   \
548                                                  \
549         store4((d2)+(si), d2t);                      \
550     }
551
552 /* 4-wide SIMD code for nsi bb distances for bb format xxxxyyyyzzzz */
553 static void subc_bb_dist2_simd4_xxxx(const float *bb_j,
554                                      int nsi, const float *bb_i,
555                                      float *d2)
556 {
557     // TODO: During SIMDv2 transition only some archs use namespace (remove when done)
558     using namespace gmx;
559
560     Simd4Float xj_l, yj_l, zj_l;
561     Simd4Float xj_h, yj_h, zj_h;
562     Simd4Float xi_l, yi_l, zi_l;
563     Simd4Float xi_h, yi_h, zi_h;
564
565     Simd4Float zero;
566
567     zero = setZero();
568
569     xj_l = Simd4Float(bb_j[0*STRIDE_PBB]);
570     yj_l = Simd4Float(bb_j[1*STRIDE_PBB]);
571     zj_l = Simd4Float(bb_j[2*STRIDE_PBB]);
572     xj_h = Simd4Float(bb_j[3*STRIDE_PBB]);
573     yj_h = Simd4Float(bb_j[4*STRIDE_PBB]);
574     zj_h = Simd4Float(bb_j[5*STRIDE_PBB]);
575
576     /* Here we "loop" over si (0,STRIDE_PBB) from 0 to nsi with step STRIDE_PBB.
577      * But as we know the number of iterations is 1 or 2, we unroll manually.
578      */
579     SUBC_BB_DIST2_SIMD4_XXXX_INNER(0, bb_i, d2);
580     if (STRIDE_PBB < nsi)
581     {
582         SUBC_BB_DIST2_SIMD4_XXXX_INNER(STRIDE_PBB, bb_i, d2);
583     }
584 }
585
586 #endif /* NBNXN_SEARCH_BB_SIMD4 */
587
588
589 /* Returns if any atom pair from two clusters is within distance sqrt(rlist2) */
590 static inline gmx_bool
591 clusterpair_in_range(const NbnxnPairlistGpuWork &work,
592                      int si,
593                      int csj, int stride, const real *x_j,
594                      real rlist2)
595 {
596 #if !GMX_SIMD4_HAVE_REAL
597
598     /* Plain C version.
599      * All coordinates are stored as xyzxyz...
600      */
601
602     const real *x_i = work.iSuperClusterData.x.data();
603
604     for (int i = 0; i < c_nbnxnGpuClusterSize; i++)
605     {
606         int i0 = (si*c_nbnxnGpuClusterSize + i)*DIM;
607         for (int j = 0; j < c_nbnxnGpuClusterSize; j++)
608         {
609             int  j0 = (csj*c_nbnxnGpuClusterSize + j)*stride;
610
611             real d2 = gmx::square(x_i[i0  ] - x_j[j0  ]) + gmx::square(x_i[i0+1] - x_j[j0+1]) + gmx::square(x_i[i0+2] - x_j[j0+2]);
612
613             if (d2 < rlist2)
614             {
615                 return TRUE;
616             }
617         }
618     }
619
620     return FALSE;
621
622 #else /* !GMX_SIMD4_HAVE_REAL */
623
624     /* 4-wide SIMD version.
625      * The coordinates x_i are stored as xxxxyyyy..., x_j is stored xyzxyz...
626      * Using 8-wide AVX(2) is not faster on Intel Sandy Bridge and Haswell.
627      */
628     static_assert(c_nbnxnGpuClusterSize == 8 || c_nbnxnGpuClusterSize == 4,
629                   "A cluster is hard-coded to 4/8 atoms.");
630
631     Simd4Real   rc2_S      = Simd4Real(rlist2);
632
633     const real *x_i        = work.iSuperClusterData.xSimd.data();
634
635     int         dim_stride = c_nbnxnGpuClusterSize*DIM;
636     Simd4Real   ix_S0      = load4(x_i + si*dim_stride + 0*GMX_SIMD4_WIDTH);
637     Simd4Real   iy_S0      = load4(x_i + si*dim_stride + 1*GMX_SIMD4_WIDTH);
638     Simd4Real   iz_S0      = load4(x_i + si*dim_stride + 2*GMX_SIMD4_WIDTH);
639
640     Simd4Real   ix_S1, iy_S1, iz_S1;
641     if (c_nbnxnGpuClusterSize == 8)
642     {
643         ix_S1      = load4(x_i + si*dim_stride + 3*GMX_SIMD4_WIDTH);
644         iy_S1      = load4(x_i + si*dim_stride + 4*GMX_SIMD4_WIDTH);
645         iz_S1      = load4(x_i + si*dim_stride + 5*GMX_SIMD4_WIDTH);
646     }
647     /* We loop from the outer to the inner particles to maximize
648      * the chance that we find a pair in range quickly and return.
649      */
650     int j0 = csj*c_nbnxnGpuClusterSize;
651     int j1 = j0 + c_nbnxnGpuClusterSize - 1;
652     while (j0 < j1)
653     {
654         Simd4Real jx0_S, jy0_S, jz0_S;
655         Simd4Real jx1_S, jy1_S, jz1_S;
656
657         Simd4Real dx_S0, dy_S0, dz_S0;
658         Simd4Real dx_S1, dy_S1, dz_S1;
659         Simd4Real dx_S2, dy_S2, dz_S2;
660         Simd4Real dx_S3, dy_S3, dz_S3;
661
662         Simd4Real rsq_S0;
663         Simd4Real rsq_S1;
664         Simd4Real rsq_S2;
665         Simd4Real rsq_S3;
666
667         Simd4Bool wco_S0;
668         Simd4Bool wco_S1;
669         Simd4Bool wco_S2;
670         Simd4Bool wco_S3;
671         Simd4Bool wco_any_S01, wco_any_S23, wco_any_S;
672
673         jx0_S = Simd4Real(x_j[j0*stride+0]);
674         jy0_S = Simd4Real(x_j[j0*stride+1]);
675         jz0_S = Simd4Real(x_j[j0*stride+2]);
676
677         jx1_S = Simd4Real(x_j[j1*stride+0]);
678         jy1_S = Simd4Real(x_j[j1*stride+1]);
679         jz1_S = Simd4Real(x_j[j1*stride+2]);
680
681         /* Calculate distance */
682         dx_S0            = ix_S0 - jx0_S;
683         dy_S0            = iy_S0 - jy0_S;
684         dz_S0            = iz_S0 - jz0_S;
685         dx_S2            = ix_S0 - jx1_S;
686         dy_S2            = iy_S0 - jy1_S;
687         dz_S2            = iz_S0 - jz1_S;
688         if (c_nbnxnGpuClusterSize == 8)
689         {
690             dx_S1            = ix_S1 - jx0_S;
691             dy_S1            = iy_S1 - jy0_S;
692             dz_S1            = iz_S1 - jz0_S;
693             dx_S3            = ix_S1 - jx1_S;
694             dy_S3            = iy_S1 - jy1_S;
695             dz_S3            = iz_S1 - jz1_S;
696         }
697
698         /* rsq = dx*dx+dy*dy+dz*dz */
699         rsq_S0           = norm2(dx_S0, dy_S0, dz_S0);
700         rsq_S2           = norm2(dx_S2, dy_S2, dz_S2);
701         if (c_nbnxnGpuClusterSize == 8)
702         {
703             rsq_S1           = norm2(dx_S1, dy_S1, dz_S1);
704             rsq_S3           = norm2(dx_S3, dy_S3, dz_S3);
705         }
706
707         wco_S0           = (rsq_S0 < rc2_S);
708         wco_S2           = (rsq_S2 < rc2_S);
709         if (c_nbnxnGpuClusterSize == 8)
710         {
711             wco_S1           = (rsq_S1 < rc2_S);
712             wco_S3           = (rsq_S3 < rc2_S);
713         }
714         if (c_nbnxnGpuClusterSize == 8)
715         {
716             wco_any_S01      = wco_S0 || wco_S1;
717             wco_any_S23      = wco_S2 || wco_S3;
718             wco_any_S        = wco_any_S01 || wco_any_S23;
719         }
720         else
721         {
722             wco_any_S = wco_S0 || wco_S2;
723         }
724
725         if (anyTrue(wco_any_S))
726         {
727             return TRUE;
728         }
729
730         j0++;
731         j1--;
732     }
733
734     return FALSE;
735
736 #endif /* !GMX_SIMD4_HAVE_REAL */
737 }
738
739 /* Returns the j-cluster index for index cjIndex in a cj list */
740 static inline int nblCj(gmx::ArrayRef<const nbnxn_cj_t> cjList,
741                         int                             cjIndex)
742 {
743     return cjList[cjIndex].cj;
744 }
745
746 /* Returns the j-cluster index for index cjIndex in a cj4 list */
747 static inline int nblCj(gmx::ArrayRef<const nbnxn_cj4_t> cj4List,
748                         int                              cjIndex)
749 {
750     return cj4List[cjIndex/c_nbnxnGpuJgroupSize].cj[cjIndex & (c_nbnxnGpuJgroupSize - 1)];
751 }
752
753 /* Returns the i-interaction mask of the j sub-cell for index cj_ind */
754 static unsigned int nbl_imask0(const NbnxnPairlistGpu *nbl, int cj_ind)
755 {
756     return nbl->cj4[cj_ind/c_nbnxnGpuJgroupSize].imei[0].imask;
757 }
758
759 /* Initializes a single NbnxnPairlistCpu data structure */
760 static void nbnxn_init_pairlist(NbnxnPairlistCpu *nbl)
761 {
762     nbl->na_ci       = c_nbnxnCpuIClusterSize;
763     nbl->na_cj       = 0;
764     nbl->ci.clear();
765     nbl->ciOuter.clear();
766     nbl->ncjInUse    = 0;
767     nbl->cj.clear();
768     nbl->cjOuter.clear();
769     nbl->nci_tot     = 0;
770
771     nbl->work        = new NbnxnPairlistCpuWork();
772 }
773
774 NbnxnPairlistGpu::NbnxnPairlistGpu(gmx::PinningPolicy pinningPolicy) :
775     na_ci(c_nbnxnGpuClusterSize),
776     na_cj(c_nbnxnGpuClusterSize),
777     na_sc(c_gpuNumClusterPerCell*c_nbnxnGpuClusterSize),
778     rlist(0),
779     sci({}, {pinningPolicy}),
780     cj4({}, {pinningPolicy}),
781     excl({}, {pinningPolicy}),
782     nci_tot(0)
783 {
784     static_assert(c_nbnxnGpuNumClusterPerSupercluster == c_gpuNumClusterPerCell,
785                   "The search code assumes that the a super-cluster matches a search grid cell");
786
787     static_assert(sizeof(cj4[0].imei[0].imask)*8 >= c_nbnxnGpuJgroupSize*c_gpuNumClusterPerCell,
788                   "The i super-cluster cluster interaction mask does not contain a sufficient number of bits");
789
790     static_assert(sizeof(excl[0])*8 >= c_nbnxnGpuJgroupSize*c_gpuNumClusterPerCell, "The GPU exclusion mask does not contain a sufficient number of bits");
791
792     // We always want a first entry without any exclusions
793     excl.resize(1);
794
795     work = new NbnxnPairlistGpuWork();
796 }
797
798 void nbnxn_init_pairlist_set(nbnxn_pairlist_set_t *nbl_list)
799 {
800     nbl_list->bSimple   =
801         (nbl_list->params.pairlistType == PairlistType::Simple4x2 ||
802          nbl_list->params.pairlistType == PairlistType::Simple4x4 ||
803          nbl_list->params.pairlistType == PairlistType::Simple4x8);
804     // Currently GPU lists are always combined
805     nbl_list->bCombined = !nbl_list->bSimple;
806
807     nbl_list->nnbl = gmx_omp_nthreads_get(emntNonbonded);
808
809     if (!nbl_list->bCombined &&
810         nbl_list->nnbl > NBNXN_BUFFERFLAG_MAX_THREADS)
811     {
812         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.",
813                   nbl_list->nnbl, NBNXN_BUFFERFLAG_MAX_THREADS, NBNXN_BUFFERFLAG_MAX_THREADS);
814     }
815
816     if (nbl_list->bSimple)
817     {
818         snew(nbl_list->nbl, nbl_list->nnbl);
819         if (nbl_list->nnbl > 1)
820         {
821             snew(nbl_list->nbl_work, nbl_list->nnbl);
822         }
823     }
824     else
825     {
826         snew(nbl_list->nblGpu, nbl_list->nnbl);
827     }
828     nbl_list->nbl_fep.resize(nbl_list->nnbl);
829     /* Execute in order to avoid memory interleaving between threads */
830 #pragma omp parallel for num_threads(nbl_list->nnbl) schedule(static)
831     for (int i = 0; i < nbl_list->nnbl; i++)
832     {
833         try
834         {
835             /* Allocate the nblist data structure locally on each thread
836              * to optimize memory access for NUMA architectures.
837              */
838             if (nbl_list->bSimple)
839             {
840                 nbl_list->nbl[i] = new NbnxnPairlistCpu();
841
842                 nbnxn_init_pairlist(nbl_list->nbl[i]);
843                 if (nbl_list->nnbl > 1)
844                 {
845                     nbl_list->nbl_work[i] = new NbnxnPairlistCpu();
846                     nbnxn_init_pairlist(nbl_list->nbl_work[i]);
847                 }
848             }
849             else
850             {
851                 /* Only list 0 is used on the GPU, use normal allocation for i>0 */
852                 auto pinningPolicy = (i == 0 ? gmx::PinningPolicy::PinnedIfSupported : gmx::PinningPolicy::CannotBePinned);
853
854                 nbl_list->nblGpu[i] = new NbnxnPairlistGpu(pinningPolicy);
855             }
856
857             snew(nbl_list->nbl_fep[i], 1);
858             nbnxn_init_pairlist_fep(nbl_list->nbl_fep[i]);
859         }
860         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
861     }
862 }
863
864 /* Print statistics of a pair list, used for debug output */
865 static void print_nblist_statistics(FILE *fp, const NbnxnPairlistCpu *nbl,
866                                     const nbnxn_search *nbs, real rl)
867 {
868     const nbnxn_grid_t *grid;
869     int                 cs[SHIFTS];
870     int                 npexcl;
871
872     grid = &nbs->grid[0];
873
874     fprintf(fp, "nbl nci %zu ncj %d\n",
875             nbl->ci.size(), nbl->ncjInUse);
876     fprintf(fp, "nbl na_cj %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
877             nbl->na_cj, rl, nbl->ncjInUse, nbl->ncjInUse/static_cast<double>(grid->nc),
878             nbl->ncjInUse/static_cast<double>(grid->nc)*grid->na_cj,
879             nbl->ncjInUse/static_cast<double>(grid->nc)*grid->na_cj/(0.5*4.0/3.0*M_PI*rl*rl*rl*grid->nc*grid->na_cj/(grid->size[XX]*grid->size[YY]*grid->size[ZZ])));
880
881     fprintf(fp, "nbl average j cell list length %.1f\n",
882             0.25*nbl->ncjInUse/std::max(static_cast<double>(nbl->ci.size()), 1.0));
883
884     for (int s = 0; s < SHIFTS; s++)
885     {
886         cs[s] = 0;
887     }
888     npexcl = 0;
889     for (const nbnxn_ci_t &ciEntry : nbl->ci)
890     {
891         cs[ciEntry.shift & NBNXN_CI_SHIFT] +=
892             ciEntry.cj_ind_end - ciEntry.cj_ind_start;
893
894         int j = ciEntry.cj_ind_start;
895         while (j < ciEntry.cj_ind_end &&
896                nbl->cj[j].excl != NBNXN_INTERACTION_MASK_ALL)
897         {
898             npexcl++;
899             j++;
900         }
901     }
902     fprintf(fp, "nbl cell pairs, total: %zu excl: %d %.1f%%\n",
903             nbl->cj.size(), npexcl, 100*npexcl/std::max(static_cast<double>(nbl->cj.size()), 1.0));
904     for (int s = 0; s < SHIFTS; s++)
905     {
906         if (cs[s] > 0)
907         {
908             fprintf(fp, "nbl shift %2d ncj %3d\n", s, cs[s]);
909         }
910     }
911 }
912
913 /* Print statistics of a pair lists, used for debug output */
914 static void print_nblist_statistics(FILE *fp, const NbnxnPairlistGpu *nbl,
915                                     const nbnxn_search *nbs, real rl)
916 {
917     const nbnxn_grid_t *grid;
918     int                 b;
919     int                 c[c_gpuNumClusterPerCell + 1];
920     double              sum_nsp, sum_nsp2;
921     int                 nsp_max;
922
923     /* This code only produces correct statistics with domain decomposition */
924     grid = &nbs->grid[0];
925
926     fprintf(fp, "nbl nsci %zu ncj4 %zu nsi %d excl4 %zu\n",
927             nbl->sci.size(), nbl->cj4.size(), nbl->nci_tot, nbl->excl.size());
928     fprintf(fp, "nbl na_c %d rl %g ncp %d per cell %.1f atoms %.1f ratio %.2f\n",
929             nbl->na_ci, rl, nbl->nci_tot, nbl->nci_tot/static_cast<double>(grid->nsubc_tot),
930             nbl->nci_tot/static_cast<double>(grid->nsubc_tot)*grid->na_c,
931             nbl->nci_tot/static_cast<double>(grid->nsubc_tot)*grid->na_c/(0.5*4.0/3.0*M_PI*rl*rl*rl*grid->nsubc_tot*grid->na_c/(grid->size[XX]*grid->size[YY]*grid->size[ZZ])));
932
933     sum_nsp  = 0;
934     sum_nsp2 = 0;
935     nsp_max  = 0;
936     for (int si = 0; si <= c_gpuNumClusterPerCell; si++)
937     {
938         c[si] = 0;
939     }
940     for (const nbnxn_sci_t &sci : nbl->sci)
941     {
942         int nsp = 0;
943         for (int j4 = sci.cj4_ind_start; j4 < sci.cj4_ind_end; j4++)
944         {
945             for (int j = 0; j < c_nbnxnGpuJgroupSize; j++)
946             {
947                 b = 0;
948                 for (int si = 0; si < c_gpuNumClusterPerCell; si++)
949                 {
950                     if (nbl->cj4[j4].imei[0].imask & (1U << (j*c_gpuNumClusterPerCell + si)))
951                     {
952                         b++;
953                     }
954                 }
955                 nsp += b;
956                 c[b]++;
957             }
958         }
959         sum_nsp  += nsp;
960         sum_nsp2 += nsp*nsp;
961         nsp_max   = std::max(nsp_max, nsp);
962     }
963     if (!nbl->sci.empty())
964     {
965         sum_nsp  /= nbl->sci.size();
966         sum_nsp2 /= nbl->sci.size();
967     }
968     fprintf(fp, "nbl #cluster-pairs: av %.1f stddev %.1f max %d\n",
969             sum_nsp, std::sqrt(sum_nsp2 - sum_nsp*sum_nsp), nsp_max);
970
971     if (!nbl->cj4.empty())
972     {
973         for (b = 0; b <= c_gpuNumClusterPerCell; b++)
974         {
975             fprintf(fp, "nbl j-list #i-subcell %d %7d %4.1f\n",
976                     b, c[b], 100.0*c[b]/size_t {nbl->cj4.size()*c_nbnxnGpuJgroupSize});
977         }
978     }
979 }
980
981 /* Returns a pointer to the exclusion mask for j-cluster-group \p cj4 and warp \p warp
982  * Generates a new exclusion entry when the j-cluster-group uses
983  * the default all-interaction mask at call time, so the returned mask
984  * can be modified when needed.
985  */
986 static nbnxn_excl_t *get_exclusion_mask(NbnxnPairlistGpu *nbl,
987                                         int               cj4,
988                                         int               warp)
989 {
990     if (nbl->cj4[cj4].imei[warp].excl_ind == 0)
991     {
992         /* No exclusions set, make a new list entry */
993         const size_t oldSize = nbl->excl.size();
994         GMX_ASSERT(oldSize >= 1, "We should always have entry [0]");
995         /* Add entry with default values: no exclusions */
996         nbl->excl.resize(oldSize + 1);
997         nbl->cj4[cj4].imei[warp].excl_ind = oldSize;
998     }
999
1000     return &nbl->excl[nbl->cj4[cj4].imei[warp].excl_ind];
1001 }
1002
1003 static void set_self_and_newton_excls_supersub(NbnxnPairlistGpu *nbl,
1004                                                int cj4_ind, int sj_offset,
1005                                                int i_cluster_in_cell)
1006 {
1007     nbnxn_excl_t *excl[c_nbnxnGpuClusterpairSplit];
1008
1009     /* Here we only set the set self and double pair exclusions */
1010
1011     /* Reserve extra elements, so the resize() in get_exclusion_mask()
1012      * will not invalidate excl entries in the loop below
1013      */
1014     nbl->excl.reserve(nbl->excl.size() + c_nbnxnGpuClusterpairSplit);
1015     for (int w = 0; w < c_nbnxnGpuClusterpairSplit; w++)
1016     {
1017         excl[w] = get_exclusion_mask(nbl, cj4_ind, w);
1018     }
1019
1020     /* Only minor < major bits set */
1021     for (int ej = 0; ej < nbl->na_ci; ej++)
1022     {
1023         int w = (ej>>2);
1024         for (int ei = ej; ei < nbl->na_ci; ei++)
1025         {
1026             excl[w]->pair[(ej & (c_nbnxnGpuJgroupSize-1))*nbl->na_ci + ei] &=
1027                 ~(1U << (sj_offset*c_gpuNumClusterPerCell + i_cluster_in_cell));
1028         }
1029     }
1030 }
1031
1032 /* Returns a diagonal or off-diagonal interaction mask for plain C lists */
1033 static unsigned int get_imask(gmx_bool rdiag, int ci, int cj)
1034 {
1035     return (rdiag && ci == cj ? NBNXN_INTERACTION_MASK_DIAG : NBNXN_INTERACTION_MASK_ALL);
1036 }
1037
1038 /* Returns a diagonal or off-diagonal interaction mask for cj-size=2 */
1039 gmx_unused static unsigned int get_imask_simd_j2(gmx_bool rdiag, int ci, int cj)
1040 {
1041     return (rdiag && ci*2 == cj ? NBNXN_INTERACTION_MASK_DIAG_J2_0 :
1042             (rdiag && ci*2+1 == cj ? NBNXN_INTERACTION_MASK_DIAG_J2_1 :
1043              NBNXN_INTERACTION_MASK_ALL));
1044 }
1045
1046 /* Returns a diagonal or off-diagonal interaction mask for cj-size=4 */
1047 gmx_unused static unsigned int get_imask_simd_j4(gmx_bool rdiag, int ci, int cj)
1048 {
1049     return (rdiag && ci == cj ? NBNXN_INTERACTION_MASK_DIAG : NBNXN_INTERACTION_MASK_ALL);
1050 }
1051
1052 /* Returns a diagonal or off-diagonal interaction mask for cj-size=8 */
1053 gmx_unused static unsigned int get_imask_simd_j8(gmx_bool rdiag, int ci, int cj)
1054 {
1055     return (rdiag && ci == cj*2 ? NBNXN_INTERACTION_MASK_DIAG_J8_0 :
1056             (rdiag && ci == cj*2+1 ? NBNXN_INTERACTION_MASK_DIAG_J8_1 :
1057              NBNXN_INTERACTION_MASK_ALL));
1058 }
1059
1060 #if GMX_SIMD
1061 #if GMX_SIMD_REAL_WIDTH == 2
1062 #define get_imask_simd_4xn  get_imask_simd_j2
1063 #endif
1064 #if GMX_SIMD_REAL_WIDTH == 4
1065 #define get_imask_simd_4xn  get_imask_simd_j4
1066 #endif
1067 #if GMX_SIMD_REAL_WIDTH == 8
1068 #define get_imask_simd_4xn  get_imask_simd_j8
1069 #define get_imask_simd_2xnn get_imask_simd_j4
1070 #endif
1071 #if GMX_SIMD_REAL_WIDTH == 16
1072 #define get_imask_simd_2xnn get_imask_simd_j8
1073 #endif
1074 #endif
1075
1076 /* Plain C code for checking and adding cluster-pairs to the list.
1077  *
1078  * \param[in]     gridj               The j-grid
1079  * \param[in,out] nbl                 The pair-list to store the cluster pairs in
1080  * \param[in]     icluster            The index of the i-cluster
1081  * \param[in]     jclusterFirst       The first cluster in the j-range
1082  * \param[in]     jclusterLast        The last cluster in the j-range
1083  * \param[in]     excludeSubDiagonal  Exclude atom pairs with i-index > j-index
1084  * \param[in]     x_j                 Coordinates for the j-atom, in xyz format
1085  * \param[in]     rlist2              The squared list cut-off
1086  * \param[in]     rbb2                The squared cut-off for putting cluster-pairs in the list based on bounding box distance only
1087  * \param[in,out] numDistanceChecks   The number of distance checks performed
1088  */
1089 static void
1090 makeClusterListSimple(const nbnxn_grid_t       &jGrid,
1091                       NbnxnPairlistCpu *        nbl,
1092                       int                       icluster,
1093                       int                       jclusterFirst,
1094                       int                       jclusterLast,
1095                       bool                      excludeSubDiagonal,
1096                       const real * gmx_restrict x_j,
1097                       real                      rlist2,
1098                       float                     rbb2,
1099                       int * gmx_restrict        numDistanceChecks)
1100 {
1101     const nbnxn_bb_t * gmx_restrict bb_ci = nbl->work->iClusterData.bb.data();
1102     const real * gmx_restrict       x_ci  = nbl->work->iClusterData.x.data();
1103
1104     gmx_bool                        InRange;
1105
1106     InRange = FALSE;
1107     while (!InRange && jclusterFirst <= jclusterLast)
1108     {
1109         real d2  = subc_bb_dist2(0, bb_ci, jclusterFirst, jGrid.bb);
1110         *numDistanceChecks += 2;
1111
1112         /* Check if the distance is within the distance where
1113          * we use only the bounding box distance rbb,
1114          * or within the cut-off and there is at least one atom pair
1115          * within the cut-off.
1116          */
1117         if (d2 < rbb2)
1118         {
1119             InRange = TRUE;
1120         }
1121         else if (d2 < rlist2)
1122         {
1123             int cjf_gl = jGrid.cell0 + jclusterFirst;
1124             for (int i = 0; i < c_nbnxnCpuIClusterSize && !InRange; i++)
1125             {
1126                 for (int j = 0; j < c_nbnxnCpuIClusterSize; j++)
1127                 {
1128                     InRange = InRange ||
1129                         (gmx::square(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjf_gl*c_nbnxnCpuIClusterSize+j)*STRIDE_XYZ+XX]) +
1130                          gmx::square(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjf_gl*c_nbnxnCpuIClusterSize+j)*STRIDE_XYZ+YY]) +
1131                          gmx::square(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjf_gl*c_nbnxnCpuIClusterSize+j)*STRIDE_XYZ+ZZ]) < rlist2);
1132                 }
1133             }
1134             *numDistanceChecks += c_nbnxnCpuIClusterSize*c_nbnxnCpuIClusterSize;
1135         }
1136         if (!InRange)
1137         {
1138             jclusterFirst++;
1139         }
1140     }
1141     if (!InRange)
1142     {
1143         return;
1144     }
1145
1146     InRange = FALSE;
1147     while (!InRange && jclusterLast > jclusterFirst)
1148     {
1149         real d2  = subc_bb_dist2(0, bb_ci, jclusterLast, jGrid.bb);
1150         *numDistanceChecks += 2;
1151
1152         /* Check if the distance is within the distance where
1153          * we use only the bounding box distance rbb,
1154          * or within the cut-off and there is at least one atom pair
1155          * within the cut-off.
1156          */
1157         if (d2 < rbb2)
1158         {
1159             InRange = TRUE;
1160         }
1161         else if (d2 < rlist2)
1162         {
1163             int cjl_gl = jGrid.cell0 + jclusterLast;
1164             for (int i = 0; i < c_nbnxnCpuIClusterSize && !InRange; i++)
1165             {
1166                 for (int j = 0; j < c_nbnxnCpuIClusterSize; j++)
1167                 {
1168                     InRange = InRange ||
1169                         (gmx::square(x_ci[i*STRIDE_XYZ+XX] - x_j[(cjl_gl*c_nbnxnCpuIClusterSize+j)*STRIDE_XYZ+XX]) +
1170                          gmx::square(x_ci[i*STRIDE_XYZ+YY] - x_j[(cjl_gl*c_nbnxnCpuIClusterSize+j)*STRIDE_XYZ+YY]) +
1171                          gmx::square(x_ci[i*STRIDE_XYZ+ZZ] - x_j[(cjl_gl*c_nbnxnCpuIClusterSize+j)*STRIDE_XYZ+ZZ]) < rlist2);
1172                 }
1173             }
1174             *numDistanceChecks += c_nbnxnCpuIClusterSize*c_nbnxnCpuIClusterSize;
1175         }
1176         if (!InRange)
1177         {
1178             jclusterLast--;
1179         }
1180     }
1181
1182     if (jclusterFirst <= jclusterLast)
1183     {
1184         for (int jcluster = jclusterFirst; jcluster <= jclusterLast; jcluster++)
1185         {
1186             /* Store cj and the interaction mask */
1187             nbnxn_cj_t cjEntry;
1188             cjEntry.cj   = jGrid.cell0 + jcluster;
1189             cjEntry.excl = get_imask(excludeSubDiagonal, icluster, jcluster);
1190             nbl->cj.push_back(cjEntry);
1191         }
1192         /* Increase the closing index in the i list */
1193         nbl->ci.back().cj_ind_end = nbl->cj.size();
1194     }
1195 }
1196
1197 #ifdef GMX_NBNXN_SIMD_4XN
1198 #include "gromacs/nbnxm/pairlist_simd_4xm.h"
1199 #endif
1200 #ifdef GMX_NBNXN_SIMD_2XNN
1201 #include "gromacs/nbnxm/pairlist_simd_2xmm.h"
1202 #endif
1203
1204 /* Plain C or SIMD4 code for making a pair list of super-cell sci vs scj.
1205  * Checks bounding box distances and possibly atom pair distances.
1206  */
1207 static void make_cluster_list_supersub(const nbnxn_grid_t &iGrid,
1208                                        const nbnxn_grid_t &jGrid,
1209                                        NbnxnPairlistGpu   *nbl,
1210                                        const int           sci,
1211                                        const int           scj,
1212                                        const bool          excludeSubDiagonal,
1213                                        const int           stride,
1214                                        const real         *x,
1215                                        const real          rlist2,
1216                                        const float         rbb2,
1217                                        int                *numDistanceChecks)
1218 {
1219     NbnxnPairlistGpuWork &work   = *nbl->work;
1220
1221 #if NBNXN_BBXXXX
1222     const float          *pbb_ci = work.iSuperClusterData.bbPacked.data();
1223 #else
1224     const nbnxn_bb_t     *bb_ci  = work.iSuperClusterData.bb.data();
1225 #endif
1226
1227     assert(c_nbnxnGpuClusterSize == iGrid.na_c);
1228     assert(c_nbnxnGpuClusterSize == jGrid.na_c);
1229
1230     /* We generate the pairlist mainly based on bounding-box distances
1231      * and do atom pair distance based pruning on the GPU.
1232      * Only if a j-group contains a single cluster-pair, we try to prune
1233      * that pair based on atom distances on the CPU to avoid empty j-groups.
1234      */
1235 #define PRUNE_LIST_CPU_ONE 1
1236 #define PRUNE_LIST_CPU_ALL 0
1237
1238 #if PRUNE_LIST_CPU_ONE
1239     int  ci_last = -1;
1240 #endif
1241
1242     float *d2l = work.distanceBuffer.data();
1243
1244     for (int subc = 0; subc < jGrid.nsubc[scj]; subc++)
1245     {
1246         const int    cj4_ind   = work.cj_ind/c_nbnxnGpuJgroupSize;
1247         const int    cj_offset = work.cj_ind - cj4_ind*c_nbnxnGpuJgroupSize;
1248         const int    cj        = scj*c_gpuNumClusterPerCell + subc;
1249
1250         const int    cj_gl     = jGrid.cell0*c_gpuNumClusterPerCell + cj;
1251
1252         int          ci1;
1253         if (excludeSubDiagonal && sci == scj)
1254         {
1255             ci1 = subc + 1;
1256         }
1257         else
1258         {
1259             ci1 = iGrid.nsubc[sci];
1260         }
1261
1262 #if NBNXN_BBXXXX
1263         /* Determine all ci1 bb distances in one call with SIMD4 */
1264         subc_bb_dist2_simd4_xxxx(jGrid.pbb.data() + (cj >> STRIDE_PBB_2LOG)*NNBSBB_XXXX + (cj & (STRIDE_PBB-1)),
1265                                  ci1, pbb_ci, d2l);
1266         *numDistanceChecks += c_nbnxnGpuClusterSize*2;
1267 #endif
1268
1269         int          npair = 0;
1270         unsigned int imask = 0;
1271         /* We use a fixed upper-bound instead of ci1 to help optimization */
1272         for (int ci = 0; ci < c_gpuNumClusterPerCell; ci++)
1273         {
1274             if (ci == ci1)
1275             {
1276                 break;
1277             }
1278
1279 #if !NBNXN_BBXXXX
1280             /* Determine the bb distance between ci and cj */
1281             d2l[ci]             = subc_bb_dist2(ci, bb_ci, cj, jGrid.bb);
1282             *numDistanceChecks += 2;
1283 #endif
1284             float d2 = d2l[ci];
1285
1286 #if PRUNE_LIST_CPU_ALL
1287             /* Check if the distance is within the distance where
1288              * we use only the bounding box distance rbb,
1289              * or within the cut-off and there is at least one atom pair
1290              * within the cut-off. This check is very costly.
1291              */
1292             *numDistanceChecks += c_nbnxnGpuClusterSize*c_nbnxnGpuClusterSize;
1293             if (d2 < rbb2 ||
1294                 (d2 < rlist2 &&
1295                  clusterpair_in_range(work, ci, cj_gl, stride, x, rlist2)))
1296 #else
1297             /* Check if the distance between the two bounding boxes
1298              * in within the pair-list cut-off.
1299              */
1300             if (d2 < rlist2)
1301 #endif
1302             {
1303                 /* Flag this i-subcell to be taken into account */
1304                 imask |= (1U << (cj_offset*c_gpuNumClusterPerCell + ci));
1305
1306 #if PRUNE_LIST_CPU_ONE
1307                 ci_last = ci;
1308 #endif
1309
1310                 npair++;
1311             }
1312         }
1313
1314 #if PRUNE_LIST_CPU_ONE
1315         /* If we only found 1 pair, check if any atoms are actually
1316          * within the cut-off, so we could get rid of it.
1317          */
1318         if (npair == 1 && d2l[ci_last] >= rbb2 &&
1319             !clusterpair_in_range(work, ci_last, cj_gl, stride, x, rlist2))
1320         {
1321             imask &= ~(1U << (cj_offset*c_gpuNumClusterPerCell + ci_last));
1322             npair--;
1323         }
1324 #endif
1325
1326         if (npair > 0)
1327         {
1328             /* We have at least one cluster pair: add a j-entry */
1329             if (static_cast<size_t>(cj4_ind) == nbl->cj4.size())
1330             {
1331                 nbl->cj4.resize(nbl->cj4.size() + 1);
1332             }
1333             nbnxn_cj4_t *cj4   = &nbl->cj4[cj4_ind];
1334
1335             cj4->cj[cj_offset] = cj_gl;
1336
1337             /* Set the exclusions for the ci==sj entry.
1338              * Here we don't bother to check if this entry is actually flagged,
1339              * as it will nearly always be in the list.
1340              */
1341             if (excludeSubDiagonal && sci == scj)
1342             {
1343                 set_self_and_newton_excls_supersub(nbl, cj4_ind, cj_offset, subc);
1344             }
1345
1346             /* Copy the cluster interaction mask to the list */
1347             for (int w = 0; w < c_nbnxnGpuClusterpairSplit; w++)
1348             {
1349                 cj4->imei[w].imask |= imask;
1350             }
1351
1352             nbl->work->cj_ind++;
1353
1354             /* Keep the count */
1355             nbl->nci_tot += npair;
1356
1357             /* Increase the closing index in i super-cell list */
1358             nbl->sci.back().cj4_ind_end =
1359                 (nbl->work->cj_ind + c_nbnxnGpuJgroupSize - 1)/c_nbnxnGpuJgroupSize;
1360         }
1361     }
1362 }
1363
1364 /* Returns how many contiguous j-clusters we have starting in the i-list */
1365 template <typename CjListType>
1366 static int numContiguousJClusters(const int                       cjIndexStart,
1367                                   const int                       cjIndexEnd,
1368                                   gmx::ArrayRef<const CjListType> cjList)
1369 {
1370     const int firstJCluster = nblCj(cjList, cjIndexStart);
1371
1372     int       numContiguous = 0;
1373
1374     while (cjIndexStart + numContiguous < cjIndexEnd &&
1375            nblCj(cjList, cjIndexStart + numContiguous) == firstJCluster + numContiguous)
1376     {
1377         numContiguous++;
1378     }
1379
1380     return numContiguous;
1381 }
1382
1383 /*! \internal
1384  * \brief Helper struct for efficient searching for excluded atoms in a j-list
1385  */
1386 struct JListRanges
1387 {
1388     /*! \brief Constructs a j-list range from \p cjList with the given index range */
1389     template <typename CjListType>
1390     JListRanges(int                             cjIndexStart,
1391                 int                             cjIndexEnd,
1392                 gmx::ArrayRef<const CjListType> cjList);
1393
1394     int cjIndexStart; //!< The start index in the j-list
1395     int cjIndexEnd;   //!< The end index in the j-list
1396     int cjFirst;      //!< The j-cluster with index cjIndexStart
1397     int cjLast;       //!< The j-cluster with index cjIndexEnd-1
1398     int numDirect;    //!< Up to cjIndexStart+numDirect the j-clusters are cjFirst + the index offset
1399 };
1400
1401 #ifndef DOXYGEN
1402 template <typename CjListType>
1403 JListRanges::JListRanges(int                             cjIndexStart,
1404                          int                             cjIndexEnd,
1405                          gmx::ArrayRef<const CjListType> cjList) :
1406     cjIndexStart(cjIndexStart),
1407     cjIndexEnd(cjIndexEnd)
1408 {
1409     GMX_ASSERT(cjIndexEnd > cjIndexStart, "JListRanges should only be called with non-empty lists");
1410
1411     cjFirst   = nblCj(cjList, cjIndexStart);
1412     cjLast    = nblCj(cjList, cjIndexEnd - 1);
1413
1414     /* Determine how many contiguous j-cells we have starting
1415      * from the first i-cell. This number can be used to directly
1416      * calculate j-cell indices for excluded atoms.
1417      */
1418     numDirect = numContiguousJClusters(cjIndexStart, cjIndexEnd, cjList);
1419 }
1420 #endif // !DOXYGEN
1421
1422 /* Return the index of \p jCluster in the given range or -1 when not present
1423  *
1424  * Note: This code is executed very often and therefore performance is
1425  *       important. It should be inlined and fully optimized.
1426  */
1427 template <typename CjListType>
1428 static inline int
1429 findJClusterInJList(int                              jCluster,
1430                     const JListRanges               &ranges,
1431                     gmx::ArrayRef<const CjListType>  cjList)
1432 {
1433     int index;
1434
1435     if (jCluster < ranges.cjFirst + ranges.numDirect)
1436     {
1437         /* We can calculate the index directly using the offset */
1438         index = ranges.cjIndexStart + jCluster - ranges.cjFirst;
1439     }
1440     else
1441     {
1442         /* Search for jCluster using bisection */
1443         index           = -1;
1444         int rangeStart  = ranges.cjIndexStart + ranges.numDirect;
1445         int rangeEnd    = ranges.cjIndexEnd;
1446         int rangeMiddle;
1447         while (index == -1 && rangeStart < rangeEnd)
1448         {
1449             rangeMiddle = (rangeStart + rangeEnd) >> 1;
1450
1451             const int clusterMiddle = nblCj(cjList, rangeMiddle);
1452
1453             if (jCluster == clusterMiddle)
1454             {
1455                 index      = rangeMiddle;
1456             }
1457             else if (jCluster < clusterMiddle)
1458             {
1459                 rangeEnd   = rangeMiddle;
1460             }
1461             else
1462             {
1463                 rangeStart = rangeMiddle + 1;
1464             }
1465         }
1466     }
1467
1468     return index;
1469 }
1470
1471 // TODO: Get rid of the two functions below by renaming sci to ci (or something better)
1472
1473 /* Return the i-entry in the list we are currently operating on */
1474 static nbnxn_ci_t *getOpenIEntry(NbnxnPairlistCpu *nbl)
1475 {
1476     return &nbl->ci.back();
1477 }
1478
1479 /* Return the i-entry in the list we are currently operating on */
1480 static nbnxn_sci_t *getOpenIEntry(NbnxnPairlistGpu *nbl)
1481 {
1482     return &nbl->sci.back();
1483 }
1484
1485 /* Set all atom-pair exclusions for a simple type list i-entry
1486  *
1487  * Set all atom-pair exclusions from the topology stored in exclusions
1488  * as masks in the pair-list for simple list entry iEntry.
1489  */
1490 static void
1491 setExclusionsForIEntry(const nbnxn_search   *nbs,
1492                        NbnxnPairlistCpu     *nbl,
1493                        gmx_bool              diagRemoved,
1494                        int                   na_cj_2log,
1495                        const nbnxn_ci_t     &iEntry,
1496                        const t_blocka       &exclusions)
1497 {
1498     if (iEntry.cj_ind_end == iEntry.cj_ind_start)
1499     {
1500         /* Empty list: no exclusions */
1501         return;
1502     }
1503
1504     const JListRanges        ranges(iEntry.cj_ind_start, iEntry.cj_ind_end, gmx::makeConstArrayRef(nbl->cj));
1505
1506     const int                iCluster = iEntry.ci;
1507
1508     gmx::ArrayRef<const int> cell = nbs->cell;
1509
1510     /* Loop over the atoms in the i-cluster */
1511     for (int i = 0; i < nbl->na_ci; i++)
1512     {
1513         const int iIndex = iCluster*nbl->na_ci + i;
1514         const int iAtom  = nbs->a[iIndex];
1515         if (iAtom >= 0)
1516         {
1517             /* Loop over the topology-based exclusions for this i-atom */
1518             for (int exclIndex = exclusions.index[iAtom]; exclIndex < exclusions.index[iAtom + 1]; exclIndex++)
1519             {
1520                 const int jAtom = exclusions.a[exclIndex];
1521
1522                 if (jAtom == iAtom)
1523                 {
1524                     /* The self exclusion are already set, save some time */
1525                     continue;
1526                 }
1527
1528                 /* Get the index of the j-atom in the nbnxn atom data */
1529                 const int jIndex = cell[jAtom];
1530
1531                 /* Without shifts we only calculate interactions j>i
1532                  * for one-way pair-lists.
1533                  */
1534                 if (diagRemoved && jIndex <= iIndex)
1535                 {
1536                     continue;
1537                 }
1538
1539                 const int jCluster = (jIndex >> na_cj_2log);
1540
1541                 /* Could the cluster se be in our list? */
1542                 if (jCluster >= ranges.cjFirst && jCluster <= ranges.cjLast)
1543                 {
1544                     const int index =
1545                         findJClusterInJList(jCluster, ranges,
1546                                             gmx::makeConstArrayRef(nbl->cj));
1547
1548                     if (index >= 0)
1549                     {
1550                         /* We found an exclusion, clear the corresponding
1551                          * interaction bit.
1552                          */
1553                         const int innerJ     = jIndex - (jCluster << na_cj_2log);
1554
1555                         nbl->cj[index].excl &= ~(1U << ((i << na_cj_2log) + innerJ));
1556                     }
1557                 }
1558             }
1559         }
1560     }
1561 }
1562
1563 /* Add a new i-entry to the FEP list and copy the i-properties */
1564 static inline void fep_list_new_nri_copy(t_nblist *nlist)
1565 {
1566     /* Add a new i-entry */
1567     nlist->nri++;
1568
1569     assert(nlist->nri < nlist->maxnri);
1570
1571     /* Duplicate the last i-entry, except for jindex, which continues */
1572     nlist->iinr[nlist->nri]   = nlist->iinr[nlist->nri-1];
1573     nlist->shift[nlist->nri]  = nlist->shift[nlist->nri-1];
1574     nlist->gid[nlist->nri]    = nlist->gid[nlist->nri-1];
1575     nlist->jindex[nlist->nri] = nlist->nrj;
1576 }
1577
1578 /* For load balancing of the free-energy lists over threads, we set
1579  * the maximum nrj size of an i-entry to 40. This leads to good
1580  * load balancing in the worst case scenario of a single perturbed
1581  * particle on 16 threads, while not introducing significant overhead.
1582  * Note that half of the perturbed pairs will anyhow end up in very small lists,
1583  * since non perturbed i-particles will see few perturbed j-particles).
1584  */
1585 const int max_nrj_fep = 40;
1586
1587 /* Exclude the perturbed pairs from the Verlet list. This is only done to avoid
1588  * singularities for overlapping particles (0/0), since the charges and
1589  * LJ parameters have been zeroed in the nbnxn data structure.
1590  * Simultaneously make a group pair list for the perturbed pairs.
1591  */
1592 static void make_fep_list(const nbnxn_search     *nbs,
1593                           const nbnxn_atomdata_t *nbat,
1594                           NbnxnPairlistCpu       *nbl,
1595                           gmx_bool                bDiagRemoved,
1596                           nbnxn_ci_t             *nbl_ci,
1597                           real gmx_unused         shx,
1598                           real gmx_unused         shy,
1599                           real gmx_unused         shz,
1600                           real gmx_unused         rlist_fep2,
1601                           const nbnxn_grid_t     &iGrid,
1602                           const nbnxn_grid_t     &jGrid,
1603                           t_nblist               *nlist)
1604 {
1605     int      ci, cj_ind_start, cj_ind_end, cja, cjr;
1606     int      nri_max;
1607     int      ngid, gid_i = 0, gid_j, gid;
1608     int      egp_shift, egp_mask;
1609     int      gid_cj = 0;
1610     int      ind_i, ind_j, ai, aj;
1611     int      nri;
1612     gmx_bool bFEP_i, bFEP_i_all;
1613
1614     if (nbl_ci->cj_ind_end == nbl_ci->cj_ind_start)
1615     {
1616         /* Empty list */
1617         return;
1618     }
1619
1620     ci = nbl_ci->ci;
1621
1622     cj_ind_start = nbl_ci->cj_ind_start;
1623     cj_ind_end   = nbl_ci->cj_ind_end;
1624
1625     /* In worst case we have alternating energy groups
1626      * and create #atom-pair lists, which means we need the size
1627      * of a cluster pair (na_ci*na_cj) times the number of cj's.
1628      */
1629     nri_max = nbl->na_ci*nbl->na_cj*(cj_ind_end - cj_ind_start);
1630     if (nlist->nri + nri_max > nlist->maxnri)
1631     {
1632         nlist->maxnri = over_alloc_large(nlist->nri + nri_max);
1633         reallocate_nblist(nlist);
1634     }
1635
1636     const nbnxn_atomdata_t::Params &nbatParams = nbat->params();
1637
1638     ngid = nbatParams.nenergrp;
1639
1640     if (ngid*jGrid.na_cj > gmx::index(sizeof(gid_cj)*8))
1641     {
1642         gmx_fatal(FARGS, "The Verlet scheme with %dx%d kernels and free-energy only supports up to %zu energy groups",
1643                   iGrid.na_c, jGrid.na_cj, (sizeof(gid_cj)*8)/jGrid.na_cj);
1644     }
1645
1646     egp_shift = nbatParams.neg_2log;
1647     egp_mask  = (1 << egp_shift) - 1;
1648
1649     /* Loop over the atoms in the i sub-cell */
1650     bFEP_i_all = TRUE;
1651     for (int i = 0; i < nbl->na_ci; i++)
1652     {
1653         ind_i = ci*nbl->na_ci + i;
1654         ai    = nbs->a[ind_i];
1655         if (ai >= 0)
1656         {
1657             nri                  = nlist->nri;
1658             nlist->jindex[nri+1] = nlist->jindex[nri];
1659             nlist->iinr[nri]     = ai;
1660             /* The actual energy group pair index is set later */
1661             nlist->gid[nri]      = 0;
1662             nlist->shift[nri]    = nbl_ci->shift & NBNXN_CI_SHIFT;
1663
1664             bFEP_i = ((iGrid.fep[ci - iGrid.cell0] & (1 << i)) != 0u);
1665
1666             bFEP_i_all = bFEP_i_all && bFEP_i;
1667
1668             if (nlist->nrj + (cj_ind_end - cj_ind_start)*nbl->na_cj > nlist->maxnrj)
1669             {
1670                 nlist->maxnrj = over_alloc_small(nlist->nrj + (cj_ind_end - cj_ind_start)*nbl->na_cj);
1671                 srenew(nlist->jjnr,     nlist->maxnrj);
1672                 srenew(nlist->excl_fep, nlist->maxnrj);
1673             }
1674
1675             if (ngid > 1)
1676             {
1677                 gid_i = (nbatParams.energrp[ci] >> (egp_shift*i)) & egp_mask;
1678             }
1679
1680             for (int cj_ind = cj_ind_start; cj_ind < cj_ind_end; cj_ind++)
1681             {
1682                 unsigned int fep_cj;
1683
1684                 cja = nbl->cj[cj_ind].cj;
1685
1686                 if (jGrid.na_cj == jGrid.na_c)
1687                 {
1688                     cjr    = cja - jGrid.cell0;
1689                     fep_cj = jGrid.fep[cjr];
1690                     if (ngid > 1)
1691                     {
1692                         gid_cj = nbatParams.energrp[cja];
1693                     }
1694                 }
1695                 else if (2*jGrid.na_cj == jGrid.na_c)
1696                 {
1697                     cjr    = cja - jGrid.cell0*2;
1698                     /* Extract half of the ci fep/energrp mask */
1699                     fep_cj = (jGrid.fep[cjr>>1] >> ((cjr&1)*jGrid.na_cj)) & ((1<<jGrid.na_cj) - 1);
1700                     if (ngid > 1)
1701                     {
1702                         gid_cj = nbatParams.energrp[cja>>1] >> ((cja&1)*jGrid.na_cj*egp_shift) & ((1<<(jGrid.na_cj*egp_shift)) - 1);
1703                     }
1704                 }
1705                 else
1706                 {
1707                     cjr    = cja - (jGrid.cell0>>1);
1708                     /* Combine two ci fep masks/energrp */
1709                     fep_cj = jGrid.fep[cjr*2] + (jGrid.fep[cjr*2+1] << jGrid.na_c);
1710                     if (ngid > 1)
1711                     {
1712                         gid_cj = nbatParams.energrp[cja*2] + (nbatParams.energrp[cja*2+1] << (jGrid.na_c*egp_shift));
1713                     }
1714                 }
1715
1716                 if (bFEP_i || fep_cj != 0)
1717                 {
1718                     for (int j = 0; j < nbl->na_cj; j++)
1719                     {
1720                         /* Is this interaction perturbed and not excluded? */
1721                         ind_j = cja*nbl->na_cj + j;
1722                         aj    = nbs->a[ind_j];
1723                         if (aj >= 0 &&
1724                             (bFEP_i || (fep_cj & (1 << j))) &&
1725                             (!bDiagRemoved || ind_j >= ind_i))
1726                         {
1727                             if (ngid > 1)
1728                             {
1729                                 gid_j = (gid_cj >> (j*egp_shift)) & egp_mask;
1730                                 gid   = GID(gid_i, gid_j, ngid);
1731
1732                                 if (nlist->nrj > nlist->jindex[nri] &&
1733                                     nlist->gid[nri] != gid)
1734                                 {
1735                                     /* Energy group pair changed: new list */
1736                                     fep_list_new_nri_copy(nlist);
1737                                     nri = nlist->nri;
1738                                 }
1739                                 nlist->gid[nri] = gid;
1740                             }
1741
1742                             if (nlist->nrj - nlist->jindex[nri] >= max_nrj_fep)
1743                             {
1744                                 fep_list_new_nri_copy(nlist);
1745                                 nri = nlist->nri;
1746                             }
1747
1748                             /* Add it to the FEP list */
1749                             nlist->jjnr[nlist->nrj]     = aj;
1750                             nlist->excl_fep[nlist->nrj] = (nbl->cj[cj_ind].excl >> (i*nbl->na_cj + j)) & 1;
1751                             nlist->nrj++;
1752
1753                             /* Exclude it from the normal list.
1754                              * Note that the charge has been set to zero,
1755                              * but we need to avoid 0/0, as perturbed atoms
1756                              * can be on top of each other.
1757                              */
1758                             nbl->cj[cj_ind].excl &= ~(1U << (i*nbl->na_cj + j));
1759                         }
1760                     }
1761                 }
1762             }
1763
1764             if (nlist->nrj > nlist->jindex[nri])
1765             {
1766                 /* Actually add this new, non-empty, list */
1767                 nlist->nri++;
1768                 nlist->jindex[nlist->nri] = nlist->nrj;
1769             }
1770         }
1771     }
1772
1773     if (bFEP_i_all)
1774     {
1775         /* All interactions are perturbed, we can skip this entry */
1776         nbl_ci->cj_ind_end = cj_ind_start;
1777         nbl->ncjInUse     -= cj_ind_end - cj_ind_start;
1778     }
1779 }
1780
1781 /* Return the index of atom a within a cluster */
1782 static inline int cj_mod_cj4(int cj)
1783 {
1784     return cj & (c_nbnxnGpuJgroupSize - 1);
1785 }
1786
1787 /* Convert a j-cluster to a cj4 group */
1788 static inline int cj_to_cj4(int cj)
1789 {
1790     return cj/c_nbnxnGpuJgroupSize;
1791 }
1792
1793 /* Return the index of an j-atom within a warp */
1794 static inline int a_mod_wj(int a)
1795 {
1796     return a & (c_nbnxnGpuClusterSize/c_nbnxnGpuClusterpairSplit - 1);
1797 }
1798
1799 /* As make_fep_list above, but for super/sub lists. */
1800 static void make_fep_list(const nbnxn_search     *nbs,
1801                           const nbnxn_atomdata_t *nbat,
1802                           NbnxnPairlistGpu       *nbl,
1803                           gmx_bool                bDiagRemoved,
1804                           const nbnxn_sci_t      *nbl_sci,
1805                           real                    shx,
1806                           real                    shy,
1807                           real                    shz,
1808                           real                    rlist_fep2,
1809                           const nbnxn_grid_t     &iGrid,
1810                           const nbnxn_grid_t     &jGrid,
1811                           t_nblist               *nlist)
1812 {
1813     int                nri_max;
1814     int                c_abs;
1815     int                ind_i, ind_j, ai, aj;
1816     int                nri;
1817     gmx_bool           bFEP_i;
1818     real               xi, yi, zi;
1819     const nbnxn_cj4_t *cj4;
1820
1821     const int          numJClusterGroups = nbl_sci->numJClusterGroups();
1822     if (numJClusterGroups == 0)
1823     {
1824         /* Empty list */
1825         return;
1826     }
1827
1828     const int sci           = nbl_sci->sci;
1829
1830     const int cj4_ind_start = nbl_sci->cj4_ind_start;
1831     const int cj4_ind_end   = nbl_sci->cj4_ind_end;
1832
1833     /* Here we process one super-cell, max #atoms na_sc, versus a list
1834      * cj4 entries, each with max c_nbnxnGpuJgroupSize cj's, each
1835      * of size na_cj atoms.
1836      * On the GPU we don't support energy groups (yet).
1837      * So for each of the na_sc i-atoms, we need max one FEP list
1838      * for each max_nrj_fep j-atoms.
1839      */
1840     nri_max = nbl->na_sc*nbl->na_cj*(1 + (numJClusterGroups*c_nbnxnGpuJgroupSize)/max_nrj_fep);
1841     if (nlist->nri + nri_max > nlist->maxnri)
1842     {
1843         nlist->maxnri = over_alloc_large(nlist->nri + nri_max);
1844         reallocate_nblist(nlist);
1845     }
1846
1847     /* Loop over the atoms in the i super-cluster */
1848     for (int c = 0; c < c_gpuNumClusterPerCell; c++)
1849     {
1850         c_abs = sci*c_gpuNumClusterPerCell + c;
1851
1852         for (int i = 0; i < nbl->na_ci; i++)
1853         {
1854             ind_i = c_abs*nbl->na_ci + i;
1855             ai    = nbs->a[ind_i];
1856             if (ai >= 0)
1857             {
1858                 nri                  = nlist->nri;
1859                 nlist->jindex[nri+1] = nlist->jindex[nri];
1860                 nlist->iinr[nri]     = ai;
1861                 /* With GPUs, energy groups are not supported */
1862                 nlist->gid[nri]      = 0;
1863                 nlist->shift[nri]    = nbl_sci->shift & NBNXN_CI_SHIFT;
1864
1865                 bFEP_i = ((iGrid.fep[c_abs - iGrid.cell0*c_gpuNumClusterPerCell] & (1 << i)) != 0u);
1866
1867                 xi = nbat->x()[ind_i*nbat->xstride+XX] + shx;
1868                 yi = nbat->x()[ind_i*nbat->xstride+YY] + shy;
1869                 zi = nbat->x()[ind_i*nbat->xstride+ZZ] + shz;
1870
1871                 const int nrjMax = nlist->nrj + numJClusterGroups*c_nbnxnGpuJgroupSize*nbl->na_cj;
1872                 if (nrjMax > nlist->maxnrj)
1873                 {
1874                     nlist->maxnrj = over_alloc_small(nrjMax);
1875                     srenew(nlist->jjnr,     nlist->maxnrj);
1876                     srenew(nlist->excl_fep, nlist->maxnrj);
1877                 }
1878
1879                 for (int cj4_ind = cj4_ind_start; cj4_ind < cj4_ind_end; cj4_ind++)
1880                 {
1881                     cj4 = &nbl->cj4[cj4_ind];
1882
1883                     for (int gcj = 0; gcj < c_nbnxnGpuJgroupSize; gcj++)
1884                     {
1885                         unsigned int fep_cj;
1886
1887                         if ((cj4->imei[0].imask & (1U << (gcj*c_gpuNumClusterPerCell + c))) == 0)
1888                         {
1889                             /* Skip this ci for this cj */
1890                             continue;
1891                         }
1892
1893                         const int cjr =
1894                             cj4->cj[gcj] - jGrid.cell0*c_gpuNumClusterPerCell;
1895
1896                         fep_cj = jGrid.fep[cjr];
1897
1898                         if (bFEP_i || fep_cj != 0)
1899                         {
1900                             for (int j = 0; j < nbl->na_cj; j++)
1901                             {
1902                                 /* Is this interaction perturbed and not excluded? */
1903                                 ind_j = (jGrid.cell0*c_gpuNumClusterPerCell + cjr)*nbl->na_cj + j;
1904                                 aj    = nbs->a[ind_j];
1905                                 if (aj >= 0 &&
1906                                     (bFEP_i || (fep_cj & (1 << j))) &&
1907                                     (!bDiagRemoved || ind_j >= ind_i))
1908                                 {
1909                                     int           excl_pair;
1910                                     unsigned int  excl_bit;
1911                                     real          dx, dy, dz;
1912
1913                                     const int     jHalf = j/(c_nbnxnGpuClusterSize/c_nbnxnGpuClusterpairSplit);
1914                                     nbnxn_excl_t *excl  =
1915                                         get_exclusion_mask(nbl, cj4_ind, jHalf);
1916
1917                                     excl_pair = a_mod_wj(j)*nbl->na_ci + i;
1918                                     excl_bit  = (1U << (gcj*c_gpuNumClusterPerCell + c));
1919
1920                                     dx = nbat->x()[ind_j*nbat->xstride+XX] - xi;
1921                                     dy = nbat->x()[ind_j*nbat->xstride+YY] - yi;
1922                                     dz = nbat->x()[ind_j*nbat->xstride+ZZ] - zi;
1923
1924                                     /* The unpruned GPU list has more than 2/3
1925                                      * of the atom pairs beyond rlist. Using
1926                                      * this list will cause a lot of overhead
1927                                      * in the CPU FEP kernels, especially
1928                                      * relative to the fast GPU kernels.
1929                                      * So we prune the FEP list here.
1930                                      */
1931                                     if (dx*dx + dy*dy + dz*dz < rlist_fep2)
1932                                     {
1933                                         if (nlist->nrj - nlist->jindex[nri] >= max_nrj_fep)
1934                                         {
1935                                             fep_list_new_nri_copy(nlist);
1936                                             nri = nlist->nri;
1937                                         }
1938
1939                                         /* Add it to the FEP list */
1940                                         nlist->jjnr[nlist->nrj]     = aj;
1941                                         nlist->excl_fep[nlist->nrj] = (excl->pair[excl_pair] & excl_bit) ? 1 : 0;
1942                                         nlist->nrj++;
1943                                     }
1944
1945                                     /* Exclude it from the normal list.
1946                                      * Note that the charge and LJ parameters have
1947                                      * been set to zero, but we need to avoid 0/0,
1948                                      * as perturbed atoms can be on top of each other.
1949                                      */
1950                                     excl->pair[excl_pair] &= ~excl_bit;
1951                                 }
1952                             }
1953
1954                             /* Note that we could mask out this pair in imask
1955                              * if all i- and/or all j-particles are perturbed.
1956                              * But since the perturbed pairs on the CPU will
1957                              * take an order of magnitude more time, the GPU
1958                              * will finish before the CPU and there is no gain.
1959                              */
1960                         }
1961                     }
1962                 }
1963
1964                 if (nlist->nrj > nlist->jindex[nri])
1965                 {
1966                     /* Actually add this new, non-empty, list */
1967                     nlist->nri++;
1968                     nlist->jindex[nlist->nri] = nlist->nrj;
1969                 }
1970             }
1971         }
1972     }
1973 }
1974
1975 /* Set all atom-pair exclusions for a GPU type list i-entry
1976  *
1977  * Sets all atom-pair exclusions from the topology stored in exclusions
1978  * as masks in the pair-list for i-super-cluster list entry iEntry.
1979  */
1980 static void
1981 setExclusionsForIEntry(const nbnxn_search   *nbs,
1982                        NbnxnPairlistGpu     *nbl,
1983                        gmx_bool              diagRemoved,
1984                        int gmx_unused        na_cj_2log,
1985                        const nbnxn_sci_t    &iEntry,
1986                        const t_blocka       &exclusions)
1987 {
1988     if (iEntry.numJClusterGroups() == 0)
1989     {
1990         /* Empty list */
1991         return;
1992     }
1993
1994     /* Set the search ranges using start and end j-cluster indices.
1995      * Note that here we can not use cj4_ind_end, since the last cj4
1996      * can be only partially filled, so we use cj_ind.
1997      */
1998     const JListRanges ranges(iEntry.cj4_ind_start*c_nbnxnGpuJgroupSize,
1999                              nbl->work->cj_ind,
2000                              gmx::makeConstArrayRef(nbl->cj4));
2001
2002     GMX_ASSERT(nbl->na_ci == c_nbnxnGpuClusterSize, "na_ci should match the GPU cluster size");
2003     constexpr int            c_clusterSize      = c_nbnxnGpuClusterSize;
2004     constexpr int            c_superClusterSize = c_nbnxnGpuNumClusterPerSupercluster*c_nbnxnGpuClusterSize;
2005
2006     const int                iSuperCluster = iEntry.sci;
2007
2008     gmx::ArrayRef<const int> cell = nbs->cell;
2009
2010     /* Loop over the atoms in the i super-cluster */
2011     for (int i = 0; i < c_superClusterSize; i++)
2012     {
2013         const int iIndex = iSuperCluster*c_superClusterSize + i;
2014         const int iAtom  = nbs->a[iIndex];
2015         if (iAtom >= 0)
2016         {
2017             const int iCluster = i/c_clusterSize;
2018
2019             /* Loop over the topology-based exclusions for this i-atom */
2020             for (int exclIndex = exclusions.index[iAtom]; exclIndex < exclusions.index[iAtom + 1]; exclIndex++)
2021             {
2022                 const int jAtom = exclusions.a[exclIndex];
2023
2024                 if (jAtom == iAtom)
2025                 {
2026                     /* The self exclusions are already set, save some time */
2027                     continue;
2028                 }
2029
2030                 /* Get the index of the j-atom in the nbnxn atom data */
2031                 const int jIndex = cell[jAtom];
2032
2033                 /* Without shifts we only calculate interactions j>i
2034                  * for one-way pair-lists.
2035                  */
2036                 /* NOTE: We would like to use iIndex on the right hand side,
2037                  * but that makes this routine 25% slower with gcc6/7.
2038                  * Even using c_superClusterSize makes it slower.
2039                  * Either of these changes triggers peeling of the exclIndex
2040                  * loop, which apparently leads to far less efficient code.
2041                  */
2042                 if (diagRemoved && jIndex <= iSuperCluster*nbl->na_sc + i)
2043                 {
2044                     continue;
2045                 }
2046
2047                 const int jCluster = jIndex/c_clusterSize;
2048
2049                 /* Check whether the cluster is in our list? */
2050                 if (jCluster >= ranges.cjFirst && jCluster <= ranges.cjLast)
2051                 {
2052                     const int index =
2053                         findJClusterInJList(jCluster, ranges,
2054                                             gmx::makeConstArrayRef(nbl->cj4));
2055
2056                     if (index >= 0)
2057                     {
2058                         /* We found an exclusion, clear the corresponding
2059                          * interaction bit.
2060                          */
2061                         const unsigned int pairMask = (1U << (cj_mod_cj4(index)*c_gpuNumClusterPerCell + iCluster));
2062                         /* Check if the i-cluster interacts with the j-cluster */
2063                         if (nbl_imask0(nbl, index) & pairMask)
2064                         {
2065                             const int innerI = (i      & (c_clusterSize - 1));
2066                             const int innerJ = (jIndex & (c_clusterSize - 1));
2067
2068                             /* Determine which j-half (CUDA warp) we are in */
2069                             const int     jHalf = innerJ/(c_clusterSize/c_nbnxnGpuClusterpairSplit);
2070
2071                             nbnxn_excl_t *interactionMask =
2072                                 get_exclusion_mask(nbl, cj_to_cj4(index), jHalf);
2073
2074                             interactionMask->pair[a_mod_wj(innerJ)*c_clusterSize + innerI] &= ~pairMask;
2075                         }
2076                     }
2077                 }
2078             }
2079         }
2080     }
2081 }
2082
2083 /* Make a new ci entry at the back of nbl->ci */
2084 static void addNewIEntry(NbnxnPairlistCpu *nbl, int ci, int shift, int flags)
2085 {
2086     nbnxn_ci_t ciEntry;
2087     ciEntry.ci            = ci;
2088     ciEntry.shift         = shift;
2089     /* Store the interaction flags along with the shift */
2090     ciEntry.shift        |= flags;
2091     ciEntry.cj_ind_start  = nbl->cj.size();
2092     ciEntry.cj_ind_end    = nbl->cj.size();
2093     nbl->ci.push_back(ciEntry);
2094 }
2095
2096 /* Make a new sci entry at index nbl->nsci */
2097 static void addNewIEntry(NbnxnPairlistGpu *nbl, int sci, int shift, int gmx_unused flags)
2098 {
2099     nbnxn_sci_t sciEntry;
2100     sciEntry.sci           = sci;
2101     sciEntry.shift         = shift;
2102     sciEntry.cj4_ind_start = nbl->cj4.size();
2103     sciEntry.cj4_ind_end   = nbl->cj4.size();
2104
2105     nbl->sci.push_back(sciEntry);
2106 }
2107
2108 /* Sort the simple j-list cj on exclusions.
2109  * Entries with exclusions will all be sorted to the beginning of the list.
2110  */
2111 static void sort_cj_excl(nbnxn_cj_t *cj, int ncj,
2112                          NbnxnPairlistCpuWork *work)
2113 {
2114     work->cj.resize(ncj);
2115
2116     /* Make a list of the j-cells involving exclusions */
2117     int jnew = 0;
2118     for (int j = 0; j < ncj; j++)
2119     {
2120         if (cj[j].excl != NBNXN_INTERACTION_MASK_ALL)
2121         {
2122             work->cj[jnew++] = cj[j];
2123         }
2124     }
2125     /* Check if there are exclusions at all or not just the first entry */
2126     if (!((jnew == 0) ||
2127           (jnew == 1 && cj[0].excl != NBNXN_INTERACTION_MASK_ALL)))
2128     {
2129         for (int j = 0; j < ncj; j++)
2130         {
2131             if (cj[j].excl == NBNXN_INTERACTION_MASK_ALL)
2132             {
2133                 work->cj[jnew++] = cj[j];
2134             }
2135         }
2136         for (int j = 0; j < ncj; j++)
2137         {
2138             cj[j] = work->cj[j];
2139         }
2140     }
2141 }
2142
2143 /* Close this simple list i entry */
2144 static void closeIEntry(NbnxnPairlistCpu    *nbl,
2145                         int gmx_unused       sp_max_av,
2146                         gmx_bool gmx_unused  progBal,
2147                         float gmx_unused     nsp_tot_est,
2148                         int gmx_unused       thread,
2149                         int gmx_unused       nthread)
2150 {
2151     nbnxn_ci_t &ciEntry = nbl->ci.back();
2152
2153     /* All content of the new ci entry have already been filled correctly,
2154      * we only need to sort and increase counts or remove the entry when empty.
2155      */
2156     const int jlen = ciEntry.cj_ind_end - ciEntry.cj_ind_start;
2157     if (jlen > 0)
2158     {
2159         sort_cj_excl(nbl->cj.data() + ciEntry.cj_ind_start, jlen, nbl->work);
2160
2161         /* The counts below are used for non-bonded pair/flop counts
2162          * and should therefore match the available kernel setups.
2163          */
2164         if (!(ciEntry.shift & NBNXN_CI_DO_COUL(0)))
2165         {
2166             nbl->work->ncj_noq += jlen;
2167         }
2168         else if ((ciEntry.shift & NBNXN_CI_HALF_LJ(0)) ||
2169                  !(ciEntry.shift & NBNXN_CI_DO_LJ(0)))
2170         {
2171             nbl->work->ncj_hlj += jlen;
2172         }
2173     }
2174     else
2175     {
2176         /* Entry is empty: remove it  */
2177         nbl->ci.pop_back();
2178     }
2179 }
2180
2181 /* Split sci entry for load balancing on the GPU.
2182  * Splitting ensures we have enough lists to fully utilize the whole GPU.
2183  * With progBal we generate progressively smaller lists, which improves
2184  * load balancing. As we only know the current count on our own thread,
2185  * we will need to estimate the current total amount of i-entries.
2186  * As the lists get concatenated later, this estimate depends
2187  * both on nthread and our own thread index.
2188  */
2189 static void split_sci_entry(NbnxnPairlistGpu *nbl,
2190                             int nsp_target_av,
2191                             gmx_bool progBal, float nsp_tot_est,
2192                             int thread, int nthread)
2193 {
2194     int nsp_max;
2195
2196     if (progBal)
2197     {
2198         float nsp_est;
2199
2200         /* Estimate the total numbers of ci's of the nblist combined
2201          * over all threads using the target number of ci's.
2202          */
2203         nsp_est = (nsp_tot_est*thread)/nthread + nbl->nci_tot;
2204
2205         /* The first ci blocks should be larger, to avoid overhead.
2206          * The last ci blocks should be smaller, to improve load balancing.
2207          * The factor 3/2 makes the first block 3/2 times the target average
2208          * and ensures that the total number of blocks end up equal to
2209          * that of equally sized blocks of size nsp_target_av.
2210          */
2211         nsp_max = static_cast<int>(nsp_target_av*(nsp_tot_est*1.5/(nsp_est + nsp_tot_est)));
2212     }
2213     else
2214     {
2215         nsp_max = nsp_target_av;
2216     }
2217
2218     const int cj4_start = nbl->sci.back().cj4_ind_start;
2219     const int cj4_end   = nbl->sci.back().cj4_ind_end;
2220     const int j4len     = cj4_end - cj4_start;
2221
2222     if (j4len > 1 && j4len*c_gpuNumClusterPerCell*c_nbnxnGpuJgroupSize > nsp_max)
2223     {
2224         /* Modify the last ci entry and process the cj4's again */
2225
2226         int nsp        = 0;
2227         int nsp_sci    = 0;
2228         int nsp_cj4_e  = 0;
2229         int nsp_cj4    = 0;
2230         for (int cj4 = cj4_start; cj4 < cj4_end; cj4++)
2231         {
2232             int nsp_cj4_p = nsp_cj4;
2233             /* Count the number of cluster pairs in this cj4 group */
2234             nsp_cj4   = 0;
2235             for (int p = 0; p < c_gpuNumClusterPerCell*c_nbnxnGpuJgroupSize; p++)
2236             {
2237                 nsp_cj4 += (nbl->cj4[cj4].imei[0].imask >> p) & 1;
2238             }
2239
2240             /* If adding the current cj4 with nsp_cj4 pairs get us further
2241              * away from our target nsp_max, split the list before this cj4.
2242              */
2243             if (nsp > 0 && nsp_max - nsp < nsp + nsp_cj4 - nsp_max)
2244             {
2245                 /* Split the list at cj4 */
2246                 nbl->sci.back().cj4_ind_end = cj4;
2247                 /* Create a new sci entry */
2248                 nbnxn_sci_t sciNew;
2249                 sciNew.sci           = nbl->sci.back().sci;
2250                 sciNew.shift         = nbl->sci.back().shift;
2251                 sciNew.cj4_ind_start = cj4;
2252                 nbl->sci.push_back(sciNew);
2253
2254                 nsp_sci              = nsp;
2255                 nsp_cj4_e            = nsp_cj4_p;
2256                 nsp                  = 0;
2257             }
2258             nsp += nsp_cj4;
2259         }
2260
2261         /* Put the remaining cj4's in the last sci entry */
2262         nbl->sci.back().cj4_ind_end = cj4_end;
2263
2264         /* Possibly balance out the last two sci's
2265          * by moving the last cj4 of the second last sci.
2266          */
2267         if (nsp_sci - nsp_cj4_e >= nsp + nsp_cj4_e)
2268         {
2269             GMX_ASSERT(nbl->sci.size() >= 2, "We expect at least two elements");
2270             nbl->sci[nbl->sci.size() - 2].cj4_ind_end--;
2271             nbl->sci[nbl->sci.size() - 1].cj4_ind_start--;
2272         }
2273     }
2274 }
2275
2276 /* Clost this super/sub list i entry */
2277 static void closeIEntry(NbnxnPairlistGpu *nbl,
2278                         int nsp_max_av,
2279                         gmx_bool progBal, float nsp_tot_est,
2280                         int thread, int nthread)
2281 {
2282     nbnxn_sci_t &sciEntry = *getOpenIEntry(nbl);
2283
2284     /* All content of the new ci entry have already been filled correctly,
2285      * we only need to, potentially, split or remove the entry when empty.
2286      */
2287     int j4len = sciEntry.numJClusterGroups();
2288     if (j4len > 0)
2289     {
2290         /* We can only have complete blocks of 4 j-entries in a list,
2291          * so round the count up before closing.
2292          */
2293         int ncj4          = (nbl->work->cj_ind + c_nbnxnGpuJgroupSize - 1)/c_nbnxnGpuJgroupSize;
2294         nbl->work->cj_ind = ncj4*c_nbnxnGpuJgroupSize;
2295
2296         if (nsp_max_av > 0)
2297         {
2298             /* Measure the size of the new entry and potentially split it */
2299             split_sci_entry(nbl, nsp_max_av, progBal, nsp_tot_est,
2300                             thread, nthread);
2301         }
2302     }
2303     else
2304     {
2305         /* Entry is empty: remove it  */
2306         nbl->sci.pop_back();
2307     }
2308 }
2309
2310 /* Syncs the working array before adding another grid pair to the GPU list */
2311 static void sync_work(NbnxnPairlistCpu gmx_unused *nbl)
2312 {
2313 }
2314
2315 /* Syncs the working array before adding another grid pair to the GPU list */
2316 static void sync_work(NbnxnPairlistGpu *nbl)
2317 {
2318     nbl->work->cj_ind   = nbl->cj4.size()*c_nbnxnGpuJgroupSize;
2319 }
2320
2321 /* Clears an NbnxnPairlistCpu data structure */
2322 static void clear_pairlist(NbnxnPairlistCpu *nbl)
2323 {
2324     nbl->ci.clear();
2325     nbl->cj.clear();
2326     nbl->ncjInUse      = 0;
2327     nbl->nci_tot       = 0;
2328     nbl->ciOuter.clear();
2329     nbl->cjOuter.clear();
2330
2331     nbl->work->ncj_noq = 0;
2332     nbl->work->ncj_hlj = 0;
2333 }
2334
2335 /* Clears an NbnxnPairlistGpu data structure */
2336 static void clear_pairlist(NbnxnPairlistGpu *nbl)
2337 {
2338     nbl->sci.clear();
2339     nbl->cj4.clear();
2340     nbl->excl.resize(1);
2341     nbl->nci_tot = 0;
2342 }
2343
2344 /* Clears a group scheme pair list */
2345 static void clear_pairlist_fep(t_nblist *nl)
2346 {
2347     nl->nri = 0;
2348     nl->nrj = 0;
2349     if (nl->jindex == nullptr)
2350     {
2351         snew(nl->jindex, 1);
2352     }
2353     nl->jindex[0] = 0;
2354 }
2355
2356 /* Sets a simple list i-cell bounding box, including PBC shift */
2357 static inline void set_icell_bb_simple(gmx::ArrayRef<const nbnxn_bb_t> bb,
2358                                        int ci,
2359                                        real shx, real shy, real shz,
2360                                        nbnxn_bb_t *bb_ci)
2361 {
2362     bb_ci->lower[BB_X] = bb[ci].lower[BB_X] + shx;
2363     bb_ci->lower[BB_Y] = bb[ci].lower[BB_Y] + shy;
2364     bb_ci->lower[BB_Z] = bb[ci].lower[BB_Z] + shz;
2365     bb_ci->upper[BB_X] = bb[ci].upper[BB_X] + shx;
2366     bb_ci->upper[BB_Y] = bb[ci].upper[BB_Y] + shy;
2367     bb_ci->upper[BB_Z] = bb[ci].upper[BB_Z] + shz;
2368 }
2369
2370 /* Sets a simple list i-cell bounding box, including PBC shift */
2371 static inline void set_icell_bb(const nbnxn_grid_t &iGrid,
2372                                 int ci,
2373                                 real shx, real shy, real shz,
2374                                 NbnxnPairlistCpuWork *work)
2375 {
2376     set_icell_bb_simple(iGrid.bb, ci, shx, shy, shz, &work->iClusterData.bb[0]);
2377 }
2378
2379 #if NBNXN_BBXXXX
2380 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
2381 static void set_icell_bbxxxx_supersub(gmx::ArrayRef<const float> bb,
2382                                       int ci,
2383                                       real shx, real shy, real shz,
2384                                       float *bb_ci)
2385 {
2386     int ia = ci*(c_gpuNumClusterPerCell >> STRIDE_PBB_2LOG)*NNBSBB_XXXX;
2387     for (int m = 0; m < (c_gpuNumClusterPerCell >> STRIDE_PBB_2LOG)*NNBSBB_XXXX; m += NNBSBB_XXXX)
2388     {
2389         for (int i = 0; i < STRIDE_PBB; i++)
2390         {
2391             bb_ci[m+0*STRIDE_PBB+i] = bb[ia+m+0*STRIDE_PBB+i] + shx;
2392             bb_ci[m+1*STRIDE_PBB+i] = bb[ia+m+1*STRIDE_PBB+i] + shy;
2393             bb_ci[m+2*STRIDE_PBB+i] = bb[ia+m+2*STRIDE_PBB+i] + shz;
2394             bb_ci[m+3*STRIDE_PBB+i] = bb[ia+m+3*STRIDE_PBB+i] + shx;
2395             bb_ci[m+4*STRIDE_PBB+i] = bb[ia+m+4*STRIDE_PBB+i] + shy;
2396             bb_ci[m+5*STRIDE_PBB+i] = bb[ia+m+5*STRIDE_PBB+i] + shz;
2397         }
2398     }
2399 }
2400 #endif
2401
2402 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
2403 gmx_unused static void set_icell_bb_supersub(gmx::ArrayRef<const nbnxn_bb_t> bb,
2404                                              int ci,
2405                                              real shx, real shy, real shz,
2406                                              nbnxn_bb_t *bb_ci)
2407 {
2408     for (int i = 0; i < c_gpuNumClusterPerCell; i++)
2409     {
2410         set_icell_bb_simple(bb, ci*c_gpuNumClusterPerCell+i,
2411                             shx, shy, shz,
2412                             &bb_ci[i]);
2413     }
2414 }
2415
2416 /* Sets a super-cell and sub cell bounding boxes, including PBC shift */
2417 gmx_unused static void set_icell_bb(const nbnxn_grid_t &iGrid,
2418                                     int ci,
2419                                     real shx, real shy, real shz,
2420                                     NbnxnPairlistGpuWork *work)
2421 {
2422 #if NBNXN_BBXXXX
2423     set_icell_bbxxxx_supersub(iGrid.pbb, ci, shx, shy, shz,
2424                               work->iSuperClusterData.bbPacked.data());
2425 #else
2426     set_icell_bb_supersub(iGrid.bb, ci, shx, shy, shz,
2427                           work->iSuperClusterData.bb.data());
2428 #endif
2429 }
2430
2431 /* Copies PBC shifted i-cell atom coordinates x,y,z to working array */
2432 static void icell_set_x_simple(int ci,
2433                                real shx, real shy, real shz,
2434                                int stride, const real *x,
2435                                NbnxnPairlistCpuWork::IClusterData *iClusterData)
2436 {
2437     const int ia = ci*c_nbnxnCpuIClusterSize;
2438
2439     for (int i = 0; i < c_nbnxnCpuIClusterSize; i++)
2440     {
2441         iClusterData->x[i*STRIDE_XYZ+XX] = x[(ia+i)*stride+XX] + shx;
2442         iClusterData->x[i*STRIDE_XYZ+YY] = x[(ia+i)*stride+YY] + shy;
2443         iClusterData->x[i*STRIDE_XYZ+ZZ] = x[(ia+i)*stride+ZZ] + shz;
2444     }
2445 }
2446
2447 static void icell_set_x(int ci,
2448                         real shx, real shy, real shz,
2449                         int stride, const real *x,
2450                         const Nbnxm::KernelType kernelType,
2451                         NbnxnPairlistCpuWork *work)
2452 {
2453     switch (kernelType)
2454     {
2455 #if GMX_SIMD
2456 #ifdef GMX_NBNXN_SIMD_4XN
2457         case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
2458             icell_set_x_simd_4xn(ci, shx, shy, shz, stride, x, work);
2459             break;
2460 #endif
2461 #ifdef GMX_NBNXN_SIMD_2XNN
2462         case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
2463             icell_set_x_simd_2xnn(ci, shx, shy, shz, stride, x, work);
2464             break;
2465 #endif
2466 #endif
2467         case Nbnxm::KernelType::Cpu4x4_PlainC:
2468             icell_set_x_simple(ci, shx, shy, shz, stride, x, &work->iClusterData);
2469             break;
2470         default:
2471             GMX_ASSERT(false, "Unhandled case");
2472             break;
2473     }
2474 }
2475
2476 /* Copies PBC shifted super-cell atom coordinates x,y,z to working array */
2477 static void icell_set_x(int ci,
2478                         real shx, real shy, real shz,
2479                         int stride, const real *x,
2480                         Nbnxm::KernelType gmx_unused kernelType,
2481                         NbnxnPairlistGpuWork *work)
2482 {
2483 #if !GMX_SIMD4_HAVE_REAL
2484
2485     real * x_ci = work->iSuperClusterData.x.data();
2486
2487     int    ia = ci*c_gpuNumClusterPerCell*c_nbnxnGpuClusterSize;
2488     for (int i = 0; i < c_gpuNumClusterPerCell*c_nbnxnGpuClusterSize; i++)
2489     {
2490         x_ci[i*DIM + XX] = x[(ia+i)*stride + XX] + shx;
2491         x_ci[i*DIM + YY] = x[(ia+i)*stride + YY] + shy;
2492         x_ci[i*DIM + ZZ] = x[(ia+i)*stride + ZZ] + shz;
2493     }
2494
2495 #else /* !GMX_SIMD4_HAVE_REAL */
2496
2497     real * x_ci = work->iSuperClusterData.xSimd.data();
2498
2499     for (int si = 0; si < c_gpuNumClusterPerCell; si++)
2500     {
2501         for (int i = 0; i < c_nbnxnGpuClusterSize; i += GMX_SIMD4_WIDTH)
2502         {
2503             int io = si*c_nbnxnGpuClusterSize + i;
2504             int ia = ci*c_gpuNumClusterPerCell*c_nbnxnGpuClusterSize + io;
2505             for (int j = 0; j < GMX_SIMD4_WIDTH; j++)
2506             {
2507                 x_ci[io*DIM + j + XX*GMX_SIMD4_WIDTH] = x[(ia + j)*stride + XX] + shx;
2508                 x_ci[io*DIM + j + YY*GMX_SIMD4_WIDTH] = x[(ia + j)*stride + YY] + shy;
2509                 x_ci[io*DIM + j + ZZ*GMX_SIMD4_WIDTH] = x[(ia + j)*stride + ZZ] + shz;
2510             }
2511         }
2512     }
2513
2514 #endif /* !GMX_SIMD4_HAVE_REAL */
2515 }
2516
2517 static real minimum_subgrid_size_xy(const nbnxn_grid_t &grid)
2518 {
2519     if (grid.bSimple)
2520     {
2521         return std::min(grid.cellSize[XX], grid.cellSize[YY]);
2522     }
2523     else
2524     {
2525         return std::min(grid.cellSize[XX]/c_gpuNumClusterPerCellX,
2526                         grid.cellSize[YY]/c_gpuNumClusterPerCellY);
2527     }
2528 }
2529
2530 static real effective_buffer_1x1_vs_MxN(const nbnxn_grid_t &iGrid,
2531                                         const nbnxn_grid_t &jGrid)
2532 {
2533     const real eff_1x1_buffer_fac_overest = 0.1;
2534
2535     /* Determine an atom-pair list cut-off buffer size for atom pairs,
2536      * to be added to rlist (including buffer) used for MxN.
2537      * This is for converting an MxN list to a 1x1 list. This means we can't
2538      * use the normal buffer estimate, as we have an MxN list in which
2539      * some atom pairs beyond rlist are missing. We want to capture
2540      * the beneficial effect of buffering by extra pairs just outside rlist,
2541      * while removing the useless pairs that are further away from rlist.
2542      * (Also the buffer could have been set manually not using the estimate.)
2543      * This buffer size is an overestimate.
2544      * We add 10% of the smallest grid sub-cell dimensions.
2545      * Note that the z-size differs per cell and we don't use this,
2546      * so we overestimate.
2547      * With PME, the 10% value gives a buffer that is somewhat larger
2548      * than the effective buffer with a tolerance of 0.005 kJ/mol/ps.
2549      * Smaller tolerances or using RF lead to a smaller effective buffer,
2550      * so 10% gives a safe overestimate.
2551      */
2552     return eff_1x1_buffer_fac_overest*(minimum_subgrid_size_xy(iGrid) +
2553                                        minimum_subgrid_size_xy(jGrid));
2554 }
2555
2556 /* Estimates the interaction volume^2 for non-local interactions */
2557 static real nonlocal_vol2(const struct gmx_domdec_zones_t *zones, const rvec ls, real r)
2558 {
2559     real cl, ca, za;
2560     real vold_est;
2561     real vol2_est_tot;
2562
2563     vol2_est_tot = 0;
2564
2565     /* Here we simply add up the volumes of 1, 2 or 3 1D decomposition
2566      * not home interaction volume^2. As these volumes are not additive,
2567      * this is an overestimate, but it would only be significant in the limit
2568      * of small cells, where we anyhow need to split the lists into
2569      * as small parts as possible.
2570      */
2571
2572     for (int z = 0; z < zones->n; z++)
2573     {
2574         if (zones->shift[z][XX] + zones->shift[z][YY] + zones->shift[z][ZZ] == 1)
2575         {
2576             cl = 0;
2577             ca = 1;
2578             za = 1;
2579             for (int d = 0; d < DIM; d++)
2580             {
2581                 if (zones->shift[z][d] == 0)
2582                 {
2583                     cl += 0.5*ls[d];
2584                     ca *= ls[d];
2585                     za *= zones->size[z].x1[d] - zones->size[z].x0[d];
2586                 }
2587             }
2588
2589             /* 4 octants of a sphere */
2590             vold_est  = 0.25*M_PI*r*r*r*r;
2591             /* 4 quarter pie slices on the edges */
2592             vold_est += 4*cl*M_PI/6.0*r*r*r;
2593             /* One rectangular volume on a face */
2594             vold_est += ca*0.5*r*r;
2595
2596             vol2_est_tot += vold_est*za;
2597         }
2598     }
2599
2600     return vol2_est_tot;
2601 }
2602
2603 /* Estimates the average size of a full j-list for super/sub setup */
2604 static void get_nsubpair_target(const nbnxn_search        *nbs,
2605                                 const InteractionLocality  iloc,
2606                                 const real                 rlist,
2607                                 const int                  min_ci_balanced,
2608                                 int                       *nsubpair_target,
2609                                 float                     *nsubpair_tot_est)
2610 {
2611     /* The target value of 36 seems to be the optimum for Kepler.
2612      * Maxwell is less sensitive to the exact value.
2613      */
2614     const int           nsubpair_target_min = 36;
2615     real                r_eff_sup, vol_est, nsp_est, nsp_est_nl;
2616
2617     const nbnxn_grid_t &grid = nbs->grid[0];
2618
2619     /* We don't need to balance list sizes if:
2620      * - We didn't request balancing.
2621      * - The number of grid cells >= the number of lists requested,
2622      *   since we will always generate at least #cells lists.
2623      * - We don't have any cells, since then there won't be any lists.
2624      */
2625     if (min_ci_balanced <= 0 || grid.nc >= min_ci_balanced || grid.nc == 0)
2626     {
2627         /* nsubpair_target==0 signals no balancing */
2628         *nsubpair_target  = 0;
2629         *nsubpair_tot_est = 0;
2630
2631         return;
2632     }
2633
2634     gmx::RVec ls;
2635     ls[XX] = (grid.c1[XX] - grid.c0[XX])/(grid.numCells[XX]*c_gpuNumClusterPerCellX);
2636     ls[YY] = (grid.c1[YY] - grid.c0[YY])/(grid.numCells[YY]*c_gpuNumClusterPerCellY);
2637     ls[ZZ] = grid.na_c/(grid.atom_density*ls[XX]*ls[YY]);
2638
2639     /* The formulas below are a heuristic estimate of the average nsj per si*/
2640     r_eff_sup = rlist + nbnxn_get_rlist_effective_inc(grid.na_c, ls);
2641
2642     if (!nbs->DomDec || nbs->zones->n == 1)
2643     {
2644         nsp_est_nl = 0;
2645     }
2646     else
2647     {
2648         nsp_est_nl =
2649             gmx::square(grid.atom_density/grid.na_c)*
2650             nonlocal_vol2(nbs->zones, ls, r_eff_sup);
2651     }
2652
2653     if (iloc == InteractionLocality::Local)
2654     {
2655         /* Sub-cell interacts with itself */
2656         vol_est  = ls[XX]*ls[YY]*ls[ZZ];
2657         /* 6/2 rectangular volume on the faces */
2658         vol_est += (ls[XX]*ls[YY] + ls[XX]*ls[ZZ] + ls[YY]*ls[ZZ])*r_eff_sup;
2659         /* 12/2 quarter pie slices on the edges */
2660         vol_est += 2*(ls[XX] + ls[YY] + ls[ZZ])*0.25*M_PI*gmx::square(r_eff_sup);
2661         /* 4 octants of a sphere */
2662         vol_est += 0.5*4.0/3.0*M_PI*gmx::power3(r_eff_sup);
2663
2664         /* Estimate the number of cluster pairs as the local number of
2665          * clusters times the volume they interact with times the density.
2666          */
2667         nsp_est = grid.nsubc_tot*vol_est*grid.atom_density/grid.na_c;
2668
2669         /* Subtract the non-local pair count */
2670         nsp_est -= nsp_est_nl;
2671
2672         /* For small cut-offs nsp_est will be an underesimate.
2673          * With DD nsp_est_nl is an overestimate so nsp_est can get negative.
2674          * So to avoid too small or negative nsp_est we set a minimum of
2675          * all cells interacting with all 3^3 direct neighbors (3^3-1)/2+1=14.
2676          * This might be a slight overestimate for small non-periodic groups of
2677          * atoms as will occur for a local domain with DD, but for small
2678          * groups of atoms we'll anyhow be limited by nsubpair_target_min,
2679          * so this overestimation will not matter.
2680          */
2681         nsp_est = std::max(nsp_est, grid.nsubc_tot*14._real);
2682
2683         if (debug)
2684         {
2685             fprintf(debug, "nsp_est local %5.1f non-local %5.1f\n",
2686                     nsp_est, nsp_est_nl);
2687         }
2688     }
2689     else
2690     {
2691         nsp_est = nsp_est_nl;
2692     }
2693
2694     /* Thus the (average) maximum j-list size should be as follows.
2695      * Since there is overhead, we shouldn't make the lists too small
2696      * (and we can't chop up j-groups) so we use a minimum target size of 36.
2697      */
2698     *nsubpair_target  = std::max(nsubpair_target_min,
2699                                  roundToInt(nsp_est/min_ci_balanced));
2700     *nsubpair_tot_est = static_cast<int>(nsp_est);
2701
2702     if (debug)
2703     {
2704         fprintf(debug, "nbl nsp estimate %.1f, nsubpair_target %d\n",
2705                 nsp_est, *nsubpair_target);
2706     }
2707 }
2708
2709 /* Debug list print function */
2710 static void print_nblist_ci_cj(FILE *fp, const NbnxnPairlistCpu *nbl)
2711 {
2712     for (const nbnxn_ci_t &ciEntry : nbl->ci)
2713     {
2714         fprintf(fp, "ci %4d  shift %2d  ncj %3d\n",
2715                 ciEntry.ci, ciEntry.shift,
2716                 ciEntry.cj_ind_end - ciEntry.cj_ind_start);
2717
2718         for (int j = ciEntry.cj_ind_start; j < ciEntry.cj_ind_end; j++)
2719         {
2720             fprintf(fp, "  cj %5d  imask %x\n",
2721                     nbl->cj[j].cj,
2722                     nbl->cj[j].excl);
2723         }
2724     }
2725 }
2726
2727 /* Debug list print function */
2728 static void print_nblist_sci_cj(FILE *fp, const NbnxnPairlistGpu *nbl)
2729 {
2730     for (const nbnxn_sci_t &sci : nbl->sci)
2731     {
2732         fprintf(fp, "ci %4d  shift %2d  ncj4 %2d\n",
2733                 sci.sci, sci.shift,
2734                 sci.numJClusterGroups());
2735
2736         int ncp = 0;
2737         for (int j4 = sci.cj4_ind_start; j4 < sci.cj4_ind_end; j4++)
2738         {
2739             for (int j = 0; j < c_nbnxnGpuJgroupSize; j++)
2740             {
2741                 fprintf(fp, "  sj %5d  imask %x\n",
2742                         nbl->cj4[j4].cj[j],
2743                         nbl->cj4[j4].imei[0].imask);
2744                 for (int si = 0; si < c_gpuNumClusterPerCell; si++)
2745                 {
2746                     if (nbl->cj4[j4].imei[0].imask & (1U << (j*c_gpuNumClusterPerCell + si)))
2747                     {
2748                         ncp++;
2749                     }
2750                 }
2751             }
2752         }
2753         fprintf(fp, "ci %4d  shift %2d  ncj4 %2d ncp %3d\n",
2754                 sci.sci, sci.shift,
2755                 sci.numJClusterGroups(),
2756                 ncp);
2757     }
2758 }
2759
2760 /* Combine pair lists *nbl generated on multiple threads nblc */
2761 static void combine_nblists(int nnbl, NbnxnPairlistGpu **nbl,
2762                             NbnxnPairlistGpu *nblc)
2763 {
2764     int nsci  = nblc->sci.size();
2765     int ncj4  = nblc->cj4.size();
2766     int nexcl = nblc->excl.size();
2767     for (int i = 0; i < nnbl; i++)
2768     {
2769         nsci  += nbl[i]->sci.size();
2770         ncj4  += nbl[i]->cj4.size();
2771         nexcl += nbl[i]->excl.size();
2772     }
2773
2774     /* Resize with the final, combined size, so we can fill in parallel */
2775     /* NOTE: For better performance we should use default initialization */
2776     nblc->sci.resize(nsci);
2777     nblc->cj4.resize(ncj4);
2778     nblc->excl.resize(nexcl);
2779
2780     /* Each thread should copy its own data to the combined arrays,
2781      * as otherwise data will go back and forth between different caches.
2782      */
2783 #if GMX_OPENMP && !(defined __clang_analyzer__)
2784     int nthreads = gmx_omp_nthreads_get(emntPairsearch);
2785 #endif
2786
2787 #pragma omp parallel for num_threads(nthreads) schedule(static)
2788     for (int n = 0; n < nnbl; n++)
2789     {
2790         try
2791         {
2792             /* Determine the offset in the combined data for our thread.
2793              * Note that the original sizes in nblc are lost.
2794              */
2795             int sci_offset  = nsci;
2796             int cj4_offset  = ncj4;
2797             int excl_offset = nexcl;
2798
2799             for (int i = n; i < nnbl; i++)
2800             {
2801                 sci_offset  -= nbl[i]->sci.size();
2802                 cj4_offset  -= nbl[i]->cj4.size();
2803                 excl_offset -= nbl[i]->excl.size();
2804             }
2805
2806             const NbnxnPairlistGpu &nbli = *nbl[n];
2807
2808             for (size_t i = 0; i < nbli.sci.size(); i++)
2809             {
2810                 nblc->sci[sci_offset + i]                = nbli.sci[i];
2811                 nblc->sci[sci_offset + i].cj4_ind_start += cj4_offset;
2812                 nblc->sci[sci_offset + i].cj4_ind_end   += cj4_offset;
2813             }
2814
2815             for (size_t j4 = 0; j4 < nbli.cj4.size(); j4++)
2816             {
2817                 nblc->cj4[cj4_offset + j4]                   = nbli.cj4[j4];
2818                 nblc->cj4[cj4_offset + j4].imei[0].excl_ind += excl_offset;
2819                 nblc->cj4[cj4_offset + j4].imei[1].excl_ind += excl_offset;
2820             }
2821
2822             for (size_t j4 = 0; j4 < nbli.excl.size(); j4++)
2823             {
2824                 nblc->excl[excl_offset + j4] = nbli.excl[j4];
2825             }
2826         }
2827         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
2828     }
2829
2830     for (int n = 0; n < nnbl; n++)
2831     {
2832         nblc->nci_tot += nbl[n]->nci_tot;
2833     }
2834 }
2835
2836 static void balance_fep_lists(const nbnxn_search   *nbs,
2837                               nbnxn_pairlist_set_t *nbl_lists)
2838 {
2839     int       nnbl;
2840     int       nri_tot, nrj_tot, nrj_target;
2841     int       th_dest;
2842     t_nblist *nbld;
2843
2844     nnbl = nbl_lists->nnbl;
2845
2846     if (nnbl == 1)
2847     {
2848         /* Nothing to balance */
2849         return;
2850     }
2851
2852     /* Count the total i-lists and pairs */
2853     nri_tot = 0;
2854     nrj_tot = 0;
2855     for (int th = 0; th < nnbl; th++)
2856     {
2857         nri_tot += nbl_lists->nbl_fep[th]->nri;
2858         nrj_tot += nbl_lists->nbl_fep[th]->nrj;
2859     }
2860
2861     nrj_target = (nrj_tot + nnbl - 1)/nnbl;
2862
2863     assert(gmx_omp_nthreads_get(emntNonbonded) == nnbl);
2864
2865 #pragma omp parallel for schedule(static) num_threads(nnbl)
2866     for (int th = 0; th < nnbl; th++)
2867     {
2868         try
2869         {
2870             t_nblist *nbl = nbs->work[th].nbl_fep.get();
2871
2872             /* Note that here we allocate for the total size, instead of
2873              * a per-thread esimate (which is hard to obtain).
2874              */
2875             if (nri_tot > nbl->maxnri)
2876             {
2877                 nbl->maxnri = over_alloc_large(nri_tot);
2878                 reallocate_nblist(nbl);
2879             }
2880             if (nri_tot > nbl->maxnri || nrj_tot > nbl->maxnrj)
2881             {
2882                 nbl->maxnrj = over_alloc_small(nrj_tot);
2883                 srenew(nbl->jjnr, nbl->maxnrj);
2884                 srenew(nbl->excl_fep, nbl->maxnrj);
2885             }
2886
2887             clear_pairlist_fep(nbl);
2888         }
2889         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
2890     }
2891
2892     /* Loop over the source lists and assign and copy i-entries */
2893     th_dest = 0;
2894     nbld    = nbs->work[th_dest].nbl_fep.get();
2895     for (int th = 0; th < nnbl; th++)
2896     {
2897         t_nblist *nbls;
2898
2899         nbls = nbl_lists->nbl_fep[th];
2900
2901         for (int i = 0; i < nbls->nri; i++)
2902         {
2903             int nrj;
2904
2905             /* The number of pairs in this i-entry */
2906             nrj = nbls->jindex[i+1] - nbls->jindex[i];
2907
2908             /* Decide if list th_dest is too large and we should procede
2909              * to the next destination list.
2910              */
2911             if (th_dest+1 < nnbl && nbld->nrj > 0 &&
2912                 nbld->nrj + nrj - nrj_target > nrj_target - nbld->nrj)
2913             {
2914                 th_dest++;
2915                 nbld = nbs->work[th_dest].nbl_fep.get();
2916             }
2917
2918             nbld->iinr[nbld->nri]  = nbls->iinr[i];
2919             nbld->gid[nbld->nri]   = nbls->gid[i];
2920             nbld->shift[nbld->nri] = nbls->shift[i];
2921
2922             for (int j = nbls->jindex[i]; j < nbls->jindex[i+1]; j++)
2923             {
2924                 nbld->jjnr[nbld->nrj]     = nbls->jjnr[j];
2925                 nbld->excl_fep[nbld->nrj] = nbls->excl_fep[j];
2926                 nbld->nrj++;
2927             }
2928             nbld->nri++;
2929             nbld->jindex[nbld->nri] = nbld->nrj;
2930         }
2931     }
2932
2933     /* Swap the list pointers */
2934     for (int th = 0; th < nnbl; th++)
2935     {
2936         t_nblist *nbl_tmp      = nbs->work[th].nbl_fep.release();
2937         nbs->work[th].nbl_fep.reset(nbl_lists->nbl_fep[th]);
2938         nbl_lists->nbl_fep[th] = nbl_tmp;
2939
2940         if (debug)
2941         {
2942             fprintf(debug, "nbl_fep[%d] nri %4d nrj %4d\n",
2943                     th,
2944                     nbl_lists->nbl_fep[th]->nri,
2945                     nbl_lists->nbl_fep[th]->nrj);
2946         }
2947     }
2948 }
2949
2950 /* Returns the next ci to be processes by our thread */
2951 static gmx_bool next_ci(const nbnxn_grid_t &grid,
2952                         int nth, int ci_block,
2953                         int *ci_x, int *ci_y,
2954                         int *ci_b, int *ci)
2955 {
2956     (*ci_b)++;
2957     (*ci)++;
2958
2959     if (*ci_b == ci_block)
2960     {
2961         /* Jump to the next block assigned to this task */
2962         *ci   += (nth - 1)*ci_block;
2963         *ci_b  = 0;
2964     }
2965
2966     if (*ci >= grid.nc)
2967     {
2968         return FALSE;
2969     }
2970
2971     while (*ci >= grid.cxy_ind[*ci_x*grid.numCells[YY] + *ci_y + 1])
2972     {
2973         *ci_y += 1;
2974         if (*ci_y == grid.numCells[YY])
2975         {
2976             *ci_x += 1;
2977             *ci_y  = 0;
2978         }
2979     }
2980
2981     return TRUE;
2982 }
2983
2984 /* Returns the distance^2 for which we put cell pairs in the list
2985  * without checking atom pair distances. This is usually < rlist^2.
2986  */
2987 static float boundingbox_only_distance2(const nbnxn_grid_t &iGrid,
2988                                         const nbnxn_grid_t &jGrid,
2989                                         real                rlist,
2990                                         gmx_bool            simple)
2991 {
2992     /* If the distance between two sub-cell bounding boxes is less
2993      * than this distance, do not check the distance between
2994      * all particle pairs in the sub-cell, since then it is likely
2995      * that the box pair has atom pairs within the cut-off.
2996      * We use the nblist cut-off minus 0.5 times the average x/y diagonal
2997      * spacing of the sub-cells. Around 40% of the checked pairs are pruned.
2998      * Using more than 0.5 gains at most 0.5%.
2999      * If forces are calculated more than twice, the performance gain
3000      * in the force calculation outweighs the cost of checking.
3001      * Note that with subcell lists, the atom-pair distance check
3002      * is only performed when only 1 out of 8 sub-cells in within range,
3003      * this is because the GPU is much faster than the cpu.
3004      */
3005     real bbx, bby;
3006     real rbb2;
3007
3008     bbx = 0.5*(iGrid.cellSize[XX] + jGrid.cellSize[XX]);
3009     bby = 0.5*(iGrid.cellSize[YY] + jGrid.cellSize[YY]);
3010     if (!simple)
3011     {
3012         bbx /= c_gpuNumClusterPerCellX;
3013         bby /= c_gpuNumClusterPerCellY;
3014     }
3015
3016     rbb2 = std::max(0.0, rlist - 0.5*std::sqrt(bbx*bbx + bby*bby));
3017     rbb2 = rbb2 * rbb2;
3018
3019 #if !GMX_DOUBLE
3020     return rbb2;
3021 #else
3022     return (float)((1+GMX_FLOAT_EPS)*rbb2);
3023 #endif
3024 }
3025
3026 static int get_ci_block_size(const nbnxn_grid_t &iGrid,
3027                              gmx_bool bDomDec, int nth)
3028 {
3029     const int ci_block_enum      = 5;
3030     const int ci_block_denom     = 11;
3031     const int ci_block_min_atoms = 16;
3032     int       ci_block;
3033
3034     /* Here we decide how to distribute the blocks over the threads.
3035      * We use prime numbers to try to avoid that the grid size becomes
3036      * a multiple of the number of threads, which would lead to some
3037      * threads getting "inner" pairs and others getting boundary pairs,
3038      * which in turns will lead to load imbalance between threads.
3039      * Set the block size as 5/11/ntask times the average number of cells
3040      * in a y,z slab. This should ensure a quite uniform distribution
3041      * of the grid parts of the different thread along all three grid
3042      * zone boundaries with 3D domain decomposition. At the same time
3043      * the blocks will not become too small.
3044      */
3045     ci_block = (iGrid.nc*ci_block_enum)/(ci_block_denom*iGrid.numCells[XX]*nth);
3046
3047     /* Ensure the blocks are not too small: avoids cache invalidation */
3048     if (ci_block*iGrid.na_sc < ci_block_min_atoms)
3049     {
3050         ci_block = (ci_block_min_atoms + iGrid.na_sc - 1)/iGrid.na_sc;
3051     }
3052
3053     /* Without domain decomposition
3054      * or with less than 3 blocks per task, divide in nth blocks.
3055      */
3056     if (!bDomDec || nth*3*ci_block > iGrid.nc)
3057     {
3058         ci_block = (iGrid.nc + nth - 1)/nth;
3059     }
3060
3061     if (ci_block > 1 && (nth - 1)*ci_block >= iGrid.nc)
3062     {
3063         /* Some threads have no work. Although reducing the block size
3064          * does not decrease the block count on the first few threads,
3065          * with GPUs better mixing of "upper" cells that have more empty
3066          * clusters results in a somewhat lower max load over all threads.
3067          * Without GPUs the regime of so few atoms per thread is less
3068          * performance relevant, but with 8-wide SIMD the same reasoning
3069          * applies, since the pair list uses 4 i-atom "sub-clusters".
3070          */
3071         ci_block--;
3072     }
3073
3074     return ci_block;
3075 }
3076
3077 /* Returns the number of bits to right-shift a cluster index to obtain
3078  * the corresponding force buffer flag index.
3079  */
3080 static int getBufferFlagShift(int numAtomsPerCluster)
3081 {
3082     int bufferFlagShift = 0;
3083     while ((numAtomsPerCluster << bufferFlagShift) < NBNXN_BUFFERFLAG_SIZE)
3084     {
3085         bufferFlagShift++;
3086     }
3087
3088     return bufferFlagShift;
3089 }
3090
3091 static bool pairlistIsSimple(const NbnxnPairlistCpu gmx_unused &pairlist)
3092 {
3093     return true;
3094 }
3095
3096 static bool pairlistIsSimple(const NbnxnPairlistGpu gmx_unused &pairlist)
3097 {
3098     return false;
3099 }
3100
3101 static void makeClusterListWrapper(NbnxnPairlistCpu              *nbl,
3102                                    const nbnxn_grid_t gmx_unused &iGrid,
3103                                    const int                      ci,
3104                                    const nbnxn_grid_t            &jGrid,
3105                                    const int                      firstCell,
3106                                    const int                      lastCell,
3107                                    const bool                     excludeSubDiagonal,
3108                                    const nbnxn_atomdata_t        *nbat,
3109                                    const real                     rlist2,
3110                                    const real                     rbb2,
3111                                    const Nbnxm::KernelType        kernelType,
3112                                    int                           *numDistanceChecks)
3113 {
3114     switch (kernelType)
3115     {
3116         case Nbnxm::KernelType::Cpu4x4_PlainC:
3117             makeClusterListSimple(jGrid,
3118                                   nbl, ci, firstCell, lastCell,
3119                                   excludeSubDiagonal,
3120                                   nbat->x().data(),
3121                                   rlist2, rbb2,
3122                                   numDistanceChecks);
3123             break;
3124 #ifdef GMX_NBNXN_SIMD_4XN
3125         case Nbnxm::KernelType::Cpu4xN_Simd_4xN:
3126             makeClusterListSimd4xn(jGrid,
3127                                    nbl, ci, firstCell, lastCell,
3128                                    excludeSubDiagonal,
3129                                    nbat->x().data(),
3130                                    rlist2, rbb2,
3131                                    numDistanceChecks);
3132             break;
3133 #endif
3134 #ifdef GMX_NBNXN_SIMD_2XNN
3135         case Nbnxm::KernelType::Cpu4xN_Simd_2xNN:
3136             makeClusterListSimd2xnn(jGrid,
3137                                     nbl, ci, firstCell, lastCell,
3138                                     excludeSubDiagonal,
3139                                     nbat->x().data(),
3140                                     rlist2, rbb2,
3141                                     numDistanceChecks);
3142             break;
3143 #endif
3144         default:
3145             GMX_ASSERT(false, "Unhandled kernel type");
3146     }
3147 }
3148
3149 static void makeClusterListWrapper(NbnxnPairlistGpu              *nbl,
3150                                    const nbnxn_grid_t &gmx_unused iGrid,
3151                                    const int                      ci,
3152                                    const nbnxn_grid_t            &jGrid,
3153                                    const int                      firstCell,
3154                                    const int                      lastCell,
3155                                    const bool                     excludeSubDiagonal,
3156                                    const nbnxn_atomdata_t        *nbat,
3157                                    const real                     rlist2,
3158                                    const real                     rbb2,
3159                                    Nbnxm::KernelType gmx_unused   kernelType,
3160                                    int                           *numDistanceChecks)
3161 {
3162     for (int cj = firstCell; cj <= lastCell; cj++)
3163     {
3164         make_cluster_list_supersub(iGrid, jGrid,
3165                                    nbl, ci, cj,
3166                                    excludeSubDiagonal,
3167                                    nbat->xstride, nbat->x().data(),
3168                                    rlist2, rbb2,
3169                                    numDistanceChecks);
3170     }
3171 }
3172
3173 static int getNumSimpleJClustersInList(const NbnxnPairlistCpu &nbl)
3174 {
3175     return nbl.cj.size();
3176 }
3177
3178 static int getNumSimpleJClustersInList(const gmx_unused NbnxnPairlistGpu &nbl)
3179 {
3180     return 0;
3181 }
3182
3183 static void incrementNumSimpleJClustersInList(NbnxnPairlistCpu *nbl,
3184                                               int               ncj_old_j)
3185 {
3186     nbl->ncjInUse += nbl->cj.size() - ncj_old_j;
3187 }
3188
3189 static void incrementNumSimpleJClustersInList(NbnxnPairlistGpu gmx_unused *nbl,
3190                                               int              gmx_unused  ncj_old_j)
3191 {
3192 }
3193
3194 static void checkListSizeConsistency(const NbnxnPairlistCpu &nbl,
3195                                      const bool              haveFreeEnergy)
3196 {
3197     GMX_RELEASE_ASSERT(static_cast<size_t>(nbl.ncjInUse) == nbl.cj.size() || haveFreeEnergy,
3198                        "Without free-energy all cj pair-list entries should be in use. "
3199                        "Note that subsequent code does not make use of the equality, "
3200                        "this check is only here to catch bugs");
3201 }
3202
3203 static void checkListSizeConsistency(const NbnxnPairlistGpu gmx_unused &nbl,
3204                                      bool gmx_unused                    haveFreeEnergy)
3205 {
3206     /* We currently can not check consistency here */
3207 }
3208
3209 /* Set the buffer flags for newly added entries in the list */
3210 static void setBufferFlags(const NbnxnPairlistCpu &nbl,
3211                            const int               ncj_old_j,
3212                            const int               gridj_flag_shift,
3213                            gmx_bitmask_t          *gridj_flag,
3214                            const int               th)
3215 {
3216     if (gmx::ssize(nbl.cj) > ncj_old_j)
3217     {
3218         int cbFirst = nbl.cj[ncj_old_j].cj >> gridj_flag_shift;
3219         int cbLast  = nbl.cj.back().cj >> gridj_flag_shift;
3220         for (int cb = cbFirst; cb <= cbLast; cb++)
3221         {
3222             bitmask_init_bit(&gridj_flag[cb], th);
3223         }
3224     }
3225 }
3226
3227 static void setBufferFlags(const NbnxnPairlistGpu gmx_unused &nbl,
3228                            int gmx_unused                     ncj_old_j,
3229                            int gmx_unused                     gridj_flag_shift,
3230                            gmx_bitmask_t gmx_unused          *gridj_flag,
3231                            int gmx_unused                     th)
3232 {
3233     GMX_ASSERT(false, "This function should never be called");
3234 }
3235
3236 /* Generates the part of pair-list nbl assigned to our thread */
3237 template <typename T>
3238 static void nbnxn_make_pairlist_part(const nbnxn_search *nbs,
3239                                      const nbnxn_grid_t &iGrid,
3240                                      const nbnxn_grid_t &jGrid,
3241                                      nbnxn_search_work_t *work,
3242                                      const nbnxn_atomdata_t *nbat,
3243                                      const t_blocka &exclusions,
3244                                      real rlist,
3245                                      const Nbnxm::KernelType kernelType,
3246                                      int ci_block,
3247                                      gmx_bool bFBufferFlag,
3248                                      int nsubpair_max,
3249                                      gmx_bool progBal,
3250                                      float nsubpair_tot_est,
3251                                      int th, int nth,
3252                                      T *nbl,
3253                                      t_nblist *nbl_fep)
3254 {
3255     int               na_cj_2log;
3256     matrix            box;
3257     real              rlist2, rl_fep2 = 0;
3258     float             rbb2;
3259     int               ci_b, ci, ci_x, ci_y, ci_xy;
3260     ivec              shp;
3261     real              bx0, bx1, by0, by1, bz0, bz1;
3262     real              bz1_frac;
3263     real              d2cx, d2z, d2z_cx, d2z_cy, d2zx, d2zxy, d2xy;
3264     int               cxf, cxl, cyf, cyf_x, cyl;
3265     int               numDistanceChecks;
3266     int               gridi_flag_shift = 0, gridj_flag_shift = 0;
3267     gmx_bitmask_t    *gridj_flag       = nullptr;
3268     int               ncj_old_i, ncj_old_j;
3269
3270     nbs_cycle_start(&work->cc[enbsCCsearch]);
3271
3272     if (jGrid.bSimple != pairlistIsSimple(*nbl) ||
3273         iGrid.bSimple != pairlistIsSimple(*nbl))
3274     {
3275         gmx_incons("Grid incompatible with pair-list");
3276     }
3277
3278     sync_work(nbl);
3279     GMX_ASSERT(nbl->na_ci == jGrid.na_c, "The cluster sizes in the list and grid should match");
3280     nbl->na_cj = Nbnxm::JClusterSizePerKernelType[kernelType];
3281     na_cj_2log = get_2log(nbl->na_cj);
3282
3283     nbl->rlist  = rlist;
3284
3285     if (bFBufferFlag)
3286     {
3287         /* Determine conversion of clusters to flag blocks */
3288         gridi_flag_shift = getBufferFlagShift(nbl->na_ci);
3289         gridj_flag_shift = getBufferFlagShift(nbl->na_cj);
3290
3291         gridj_flag       = work->buffer_flags.flag;
3292     }
3293
3294     copy_mat(nbs->box, box);
3295
3296     rlist2 = nbl->rlist*nbl->rlist;
3297
3298     if (nbs->bFEP && !pairlistIsSimple(*nbl))
3299     {
3300         /* Determine an atom-pair list cut-off distance for FEP atom pairs.
3301          * We should not simply use rlist, since then we would not have
3302          * the small, effective buffering of the NxN lists.
3303          * The buffer is on overestimate, but the resulting cost for pairs
3304          * beyond rlist is neglible compared to the FEP pairs within rlist.
3305          */
3306         rl_fep2 = nbl->rlist + effective_buffer_1x1_vs_MxN(iGrid, jGrid);
3307
3308         if (debug)
3309         {
3310             fprintf(debug, "nbl_fep atom-pair rlist %f\n", rl_fep2);
3311         }
3312         rl_fep2 = rl_fep2*rl_fep2;
3313     }
3314
3315     rbb2 = boundingbox_only_distance2(iGrid, jGrid, nbl->rlist, pairlistIsSimple(*nbl));
3316
3317     if (debug)
3318     {
3319         fprintf(debug, "nbl bounding box only distance %f\n", std::sqrt(rbb2));
3320     }
3321
3322     const bool isIntraGridList = (&iGrid == &jGrid);
3323
3324     /* Set the shift range */
3325     for (int d = 0; d < DIM; d++)
3326     {
3327         /* Check if we need periodicity shifts.
3328          * Without PBC or with domain decomposition we don't need them.
3329          */
3330         if (d >= ePBC2npbcdim(nbs->ePBC) || nbs->dd_dim[d])
3331         {
3332             shp[d] = 0;
3333         }
3334         else
3335         {
3336             const real listRangeCellToCell = listRangeForGridCellToGridCell(rlist, iGrid, jGrid);
3337             if (d == XX &&
3338                 box[XX][XX] - fabs(box[YY][XX]) - fabs(box[ZZ][XX]) < listRangeCellToCell)
3339             {
3340                 shp[d] = 2;
3341             }
3342             else
3343             {
3344                 shp[d] = 1;
3345             }
3346         }
3347     }
3348     const bool bSimple = pairlistIsSimple(*nbl);
3349     gmx::ArrayRef<const nbnxn_bb_t> bb_i;
3350 #if NBNXN_BBXXXX
3351     gmx::ArrayRef<const float>      pbb_i;
3352     if (bSimple)
3353     {
3354         bb_i  = iGrid.bb;
3355     }
3356     else
3357     {
3358         pbb_i = iGrid.pbb;
3359     }
3360 #else
3361     /* We use the normal bounding box format for both grid types */
3362     bb_i  = iGrid.bb;
3363 #endif
3364     gmx::ArrayRef<const float> bbcz_i  = iGrid.bbcz;
3365     gmx::ArrayRef<const int>   flags_i = iGrid.flags;
3366     gmx::ArrayRef<const float> bbcz_j  = jGrid.bbcz;
3367     int                        cell0_i = iGrid.cell0;
3368
3369     if (debug)
3370     {
3371         fprintf(debug, "nbl nc_i %d col.av. %.1f ci_block %d\n",
3372                 iGrid.nc, iGrid.nc/static_cast<double>(iGrid.numCells[XX]*iGrid.numCells[YY]), ci_block);
3373     }
3374
3375     numDistanceChecks = 0;
3376
3377     const real listRangeBBToJCell2 = gmx::square(listRangeForBoundingBoxToGridCell(rlist, jGrid));
3378
3379     /* Initially ci_b and ci to 1 before where we want them to start,
3380      * as they will both be incremented in next_ci.
3381      */
3382     ci_b = -1;
3383     ci   = th*ci_block - 1;
3384     ci_x = 0;
3385     ci_y = 0;
3386     while (next_ci(iGrid, nth, ci_block, &ci_x, &ci_y, &ci_b, &ci))
3387     {
3388         if (bSimple && flags_i[ci] == 0)
3389         {
3390             continue;
3391         }
3392
3393         ncj_old_i = getNumSimpleJClustersInList(*nbl);
3394
3395         d2cx = 0;
3396         if (!isIntraGridList && shp[XX] == 0)
3397         {
3398             if (bSimple)
3399             {
3400                 bx1 = bb_i[ci].upper[BB_X];
3401             }
3402             else
3403             {
3404                 bx1 = iGrid.c0[XX] + (ci_x+1)*iGrid.cellSize[XX];
3405             }
3406             if (bx1 < jGrid.c0[XX])
3407             {
3408                 d2cx = gmx::square(jGrid.c0[XX] - bx1);
3409
3410                 if (d2cx >= listRangeBBToJCell2)
3411                 {
3412                     continue;
3413                 }
3414             }
3415         }
3416
3417         ci_xy = ci_x*iGrid.numCells[YY] + ci_y;
3418
3419         /* Loop over shift vectors in three dimensions */
3420         for (int tz = -shp[ZZ]; tz <= shp[ZZ]; tz++)
3421         {
3422             const real shz = tz*box[ZZ][ZZ];
3423
3424             bz0 = bbcz_i[ci*NNBSBB_D  ] + shz;
3425             bz1 = bbcz_i[ci*NNBSBB_D+1] + shz;
3426
3427             if (tz == 0)
3428             {
3429                 d2z = 0;
3430             }
3431             else if (tz < 0)
3432             {
3433                 d2z = gmx::square(bz1);
3434             }
3435             else
3436             {
3437                 d2z = gmx::square(bz0 - box[ZZ][ZZ]);
3438             }
3439
3440             d2z_cx = d2z + d2cx;
3441
3442             if (d2z_cx >= rlist2)
3443             {
3444                 continue;
3445             }
3446
3447             bz1_frac = bz1/(iGrid.cxy_ind[ci_xy+1] - iGrid.cxy_ind[ci_xy]);
3448             if (bz1_frac < 0)
3449             {
3450                 bz1_frac = 0;
3451             }
3452             /* The check with bz1_frac close to or larger than 1 comes later */
3453
3454             for (int ty = -shp[YY]; ty <= shp[YY]; ty++)
3455             {
3456                 const real shy = ty*box[YY][YY] + tz*box[ZZ][YY];
3457
3458                 if (bSimple)
3459                 {
3460                     by0 = bb_i[ci].lower[BB_Y] + shy;
3461                     by1 = bb_i[ci].upper[BB_Y] + shy;
3462                 }
3463                 else
3464                 {
3465                     by0 = iGrid.c0[YY] + (ci_y  )*iGrid.cellSize[YY] + shy;
3466                     by1 = iGrid.c0[YY] + (ci_y+1)*iGrid.cellSize[YY] + shy;
3467                 }
3468
3469                 get_cell_range<YY>(by0, by1,
3470                                    jGrid,
3471                                    d2z_cx, rlist,
3472                                    &cyf, &cyl);
3473
3474                 if (cyf > cyl)
3475                 {
3476                     continue;
3477                 }
3478
3479                 d2z_cy = d2z;
3480                 if (by1 < jGrid.c0[YY])
3481                 {
3482                     d2z_cy += gmx::square(jGrid.c0[YY] - by1);
3483                 }
3484                 else if (by0 > jGrid.c1[YY])
3485                 {
3486                     d2z_cy += gmx::square(by0 - jGrid.c1[YY]);
3487                 }
3488
3489                 for (int tx = -shp[XX]; tx <= shp[XX]; tx++)
3490                 {
3491                     const int  shift              = XYZ2IS(tx, ty, tz);
3492
3493                     const bool excludeSubDiagonal = (isIntraGridList && shift == CENTRAL);
3494
3495                     if (c_pbcShiftBackward && isIntraGridList && shift > CENTRAL)
3496                     {
3497                         continue;
3498                     }
3499
3500                     const real shx = tx*box[XX][XX] + ty*box[YY][XX] + tz*box[ZZ][XX];
3501
3502                     if (bSimple)
3503                     {
3504                         bx0 = bb_i[ci].lower[BB_X] + shx;
3505                         bx1 = bb_i[ci].upper[BB_X] + shx;
3506                     }
3507                     else
3508                     {
3509                         bx0 = iGrid.c0[XX] + (ci_x  )*iGrid.cellSize[XX] + shx;
3510                         bx1 = iGrid.c0[XX] + (ci_x+1)*iGrid.cellSize[XX] + shx;
3511                     }
3512
3513                     get_cell_range<XX>(bx0, bx1,
3514                                        jGrid,
3515                                        d2z_cy, rlist,
3516                                        &cxf, &cxl);
3517
3518                     if (cxf > cxl)
3519                     {
3520                         continue;
3521                     }
3522
3523                     addNewIEntry(nbl, cell0_i+ci, shift, flags_i[ci]);
3524
3525                     if ((!c_pbcShiftBackward || excludeSubDiagonal) &&
3526                         cxf < ci_x)
3527                     {
3528                         /* Leave the pairs with i > j.
3529                          * x is the major index, so skip half of it.
3530                          */
3531                         cxf = ci_x;
3532                     }
3533
3534                     set_icell_bb(iGrid, ci, shx, shy, shz,
3535                                  nbl->work);
3536
3537                     icell_set_x(cell0_i+ci, shx, shy, shz,
3538                                 nbat->xstride, nbat->x().data(),
3539                                 kernelType,
3540                                 nbl->work);
3541
3542                     for (int cx = cxf; cx <= cxl; cx++)
3543                     {
3544                         d2zx = d2z;
3545                         if (jGrid.c0[XX] + cx*jGrid.cellSize[XX] > bx1)
3546                         {
3547                             d2zx += gmx::square(jGrid.c0[XX] + cx*jGrid.cellSize[XX] - bx1);
3548                         }
3549                         else if (jGrid.c0[XX] + (cx+1)*jGrid.cellSize[XX] < bx0)
3550                         {
3551                             d2zx += gmx::square(jGrid.c0[XX] + (cx+1)*jGrid.cellSize[XX] - bx0);
3552                         }
3553
3554                         if (isIntraGridList &&
3555                             cx == 0 &&
3556                             (!c_pbcShiftBackward || shift == CENTRAL) &&
3557                             cyf < ci_y)
3558                         {
3559                             /* Leave the pairs with i > j.
3560                              * Skip half of y when i and j have the same x.
3561                              */
3562                             cyf_x = ci_y;
3563                         }
3564                         else
3565                         {
3566                             cyf_x = cyf;
3567                         }
3568
3569                         for (int cy = cyf_x; cy <= cyl; cy++)
3570                         {
3571                             const int columnStart = jGrid.cxy_ind[cx*jGrid.numCells[YY] + cy];
3572                             const int columnEnd   = jGrid.cxy_ind[cx*jGrid.numCells[YY] + cy + 1];
3573
3574                             d2zxy = d2zx;
3575                             if (jGrid.c0[YY] + cy*jGrid.cellSize[YY] > by1)
3576                             {
3577                                 d2zxy += gmx::square(jGrid.c0[YY] + cy*jGrid.cellSize[YY] - by1);
3578                             }
3579                             else if (jGrid.c0[YY] + (cy+1)*jGrid.cellSize[YY] < by0)
3580                             {
3581                                 d2zxy += gmx::square(jGrid.c0[YY] + (cy+1)*jGrid.cellSize[YY] - by0);
3582                             }
3583                             if (columnStart < columnEnd && d2zxy < listRangeBBToJCell2)
3584                             {
3585                                 /* To improve efficiency in the common case
3586                                  * of a homogeneous particle distribution,
3587                                  * we estimate the index of the middle cell
3588                                  * in range (midCell). We search down and up
3589                                  * starting from this index.
3590                                  *
3591                                  * Note that the bbcz_j array contains bounds
3592                                  * for i-clusters, thus for clusters of 4 atoms.
3593                                  * For the common case where the j-cluster size
3594                                  * is 8, we could step with a stride of 2,
3595                                  * but we do not do this because it would
3596                                  * complicate this code even more.
3597                                  */
3598                                 int midCell = columnStart + static_cast<int>(bz1_frac*(columnEnd - columnStart));
3599                                 if (midCell >= columnEnd)
3600                                 {
3601                                     midCell = columnEnd - 1;
3602                                 }
3603
3604                                 d2xy = d2zxy - d2z;
3605
3606                                 /* Find the lowest cell that can possibly
3607                                  * be within range.
3608                                  * Check if we hit the bottom of the grid,
3609                                  * if the j-cell is below the i-cell and if so,
3610                                  * if it is within range.
3611                                  */
3612                                 int downTestCell = midCell;
3613                                 while (downTestCell >= columnStart &&
3614                                        (bbcz_j[downTestCell*NNBSBB_D + 1] >= bz0 ||
3615                                         d2xy + gmx::square(bbcz_j[downTestCell*NNBSBB_D + 1] - bz0) < rlist2))
3616                                 {
3617                                     downTestCell--;
3618                                 }
3619                                 int firstCell = downTestCell + 1;
3620
3621                                 /* Find the highest cell that can possibly
3622                                  * be within range.
3623                                  * Check if we hit the top of the grid,
3624                                  * if the j-cell is above the i-cell and if so,
3625                                  * if it is within range.
3626                                  */
3627                                 int upTestCell = midCell + 1;
3628                                 while (upTestCell < columnEnd &&
3629                                        (bbcz_j[upTestCell*NNBSBB_D] <= bz1 ||
3630                                         d2xy + gmx::square(bbcz_j[upTestCell*NNBSBB_D] - bz1) < rlist2))
3631                                 {
3632                                     upTestCell++;
3633                                 }
3634                                 int lastCell = upTestCell - 1;
3635
3636 #define NBNXN_REFCODE 0
3637 #if NBNXN_REFCODE
3638                                 {
3639                                     /* Simple reference code, for debugging,
3640                                      * overrides the more complex code above.
3641                                      */
3642                                     firstCell = columnEnd;
3643                                     lastCell  = -1;
3644                                     for (int k = columnStart; k < columnEnd; k++)
3645                                     {
3646                                         if (d2xy + gmx::square(bbcz_j[k*NNBSBB_D + 1] - bz0) < rlist2 &&
3647                                             k < firstCell)
3648                                         {
3649                                             firstCell = k;
3650                                         }
3651                                         if (d2xy + gmx::square(bbcz_j[k*NNBSBB_D] - bz1) < rlist2 &&
3652                                             k > lastCell)
3653                                         {
3654                                             lastCell = k;
3655                                         }
3656                                     }
3657                                 }
3658 #endif
3659
3660                                 if (isIntraGridList)
3661                                 {
3662                                     /* We want each atom/cell pair only once,
3663                                      * only use cj >= ci.
3664                                      */
3665                                     if (!c_pbcShiftBackward || shift == CENTRAL)
3666                                     {
3667                                         firstCell = std::max(firstCell, ci);
3668                                     }
3669                                 }
3670
3671                                 if (firstCell <= lastCell)
3672                                 {
3673                                     GMX_ASSERT(firstCell >= columnStart && lastCell < columnEnd, "The range should reside within the current grid column");
3674
3675                                     /* For f buffer flags with simple lists */
3676                                     ncj_old_j = getNumSimpleJClustersInList(*nbl);
3677
3678                                     makeClusterListWrapper(nbl,
3679                                                            iGrid, ci,
3680                                                            jGrid, firstCell, lastCell,
3681                                                            excludeSubDiagonal,
3682                                                            nbat,
3683                                                            rlist2, rbb2,
3684                                                            kernelType,
3685                                                            &numDistanceChecks);
3686
3687                                     if (bFBufferFlag)
3688                                     {
3689                                         setBufferFlags(*nbl, ncj_old_j, gridj_flag_shift,
3690                                                        gridj_flag, th);
3691                                     }
3692
3693                                     incrementNumSimpleJClustersInList(nbl, ncj_old_j);
3694                                 }
3695                             }
3696                         }
3697                     }
3698
3699                     /* Set the exclusions for this ci list */
3700                     setExclusionsForIEntry(nbs,
3701                                            nbl,
3702                                            excludeSubDiagonal,
3703                                            na_cj_2log,
3704                                            *getOpenIEntry(nbl),
3705                                            exclusions);
3706
3707                     if (nbs->bFEP)
3708                     {
3709                         make_fep_list(nbs, nbat, nbl,
3710                                       excludeSubDiagonal,
3711                                       getOpenIEntry(nbl),
3712                                       shx, shy, shz,
3713                                       rl_fep2,
3714                                       iGrid, jGrid, nbl_fep);
3715                     }
3716
3717                     /* Close this ci list */
3718                     closeIEntry(nbl,
3719                                 nsubpair_max,
3720                                 progBal, nsubpair_tot_est,
3721                                 th, nth);
3722                 }
3723             }
3724         }
3725
3726         if (bFBufferFlag && getNumSimpleJClustersInList(*nbl) > ncj_old_i)
3727         {
3728             bitmask_init_bit(&(work->buffer_flags.flag[(iGrid.cell0+ci) >> gridi_flag_shift]), th);
3729         }
3730     }
3731
3732     work->ndistc = numDistanceChecks;
3733
3734     nbs_cycle_stop(&work->cc[enbsCCsearch]);
3735
3736     checkListSizeConsistency(*nbl, nbs->bFEP);
3737
3738     if (debug)
3739     {
3740         fprintf(debug, "number of distance checks %d\n", numDistanceChecks);
3741
3742         print_nblist_statistics(debug, nbl, nbs, rlist);
3743
3744         if (nbs->bFEP)
3745         {
3746             fprintf(debug, "nbl FEP list pairs: %d\n", nbl_fep->nrj);
3747         }
3748     }
3749 }
3750
3751 static void reduce_buffer_flags(const nbnxn_search         *nbs,
3752                                 int                         nsrc,
3753                                 const nbnxn_buffer_flags_t *dest)
3754 {
3755     for (int s = 0; s < nsrc; s++)
3756     {
3757         gmx_bitmask_t * flag = nbs->work[s].buffer_flags.flag;
3758
3759         for (int b = 0; b < dest->nflag; b++)
3760         {
3761             bitmask_union(&(dest->flag[b]), flag[b]);
3762         }
3763     }
3764 }
3765
3766 static void print_reduction_cost(const nbnxn_buffer_flags_t *flags, int nout)
3767 {
3768     int           nelem, nkeep, ncopy, nred, out;
3769     gmx_bitmask_t mask_0;
3770
3771     nelem = 0;
3772     nkeep = 0;
3773     ncopy = 0;
3774     nred  = 0;
3775     bitmask_init_bit(&mask_0, 0);
3776     for (int b = 0; b < flags->nflag; b++)
3777     {
3778         if (bitmask_is_equal(flags->flag[b], mask_0))
3779         {
3780             /* Only flag 0 is set, no copy of reduction required */
3781             nelem++;
3782             nkeep++;
3783         }
3784         else if (!bitmask_is_zero(flags->flag[b]))
3785         {
3786             int c = 0;
3787             for (out = 0; out < nout; out++)
3788             {
3789                 if (bitmask_is_set(flags->flag[b], out))
3790                 {
3791                     c++;
3792                 }
3793             }
3794             nelem += c;
3795             if (c == 1)
3796             {
3797                 ncopy++;
3798             }
3799             else
3800             {
3801                 nred += c;
3802             }
3803         }
3804     }
3805
3806     fprintf(debug, "nbnxn reduction: #flag %d #list %d elem %4.2f, keep %4.2f copy %4.2f red %4.2f\n",
3807             flags->nflag, nout,
3808             nelem/static_cast<double>(flags->nflag),
3809             nkeep/static_cast<double>(flags->nflag),
3810             ncopy/static_cast<double>(flags->nflag),
3811             nred/static_cast<double>(flags->nflag));
3812 }
3813
3814 /* Copies the list entries from src to dest when cjStart <= *cjGlobal < cjEnd.
3815  * *cjGlobal is updated with the cj count in src.
3816  * When setFlags==true, flag bit t is set in flag for all i and j clusters.
3817  */
3818 template<bool setFlags>
3819 static void copySelectedListRange(const nbnxn_ci_t * gmx_restrict srcCi,
3820                                   const NbnxnPairlistCpu * gmx_restrict src,
3821                                   NbnxnPairlistCpu * gmx_restrict dest,
3822                                   gmx_bitmask_t *flag,
3823                                   int iFlagShift, int jFlagShift, int t)
3824 {
3825     const int ncj = srcCi->cj_ind_end - srcCi->cj_ind_start;
3826
3827     dest->ci.push_back(*srcCi);
3828     dest->ci.back().cj_ind_start = dest->cj.size();
3829     dest->ci.back().cj_ind_end   = dest->cj.size() + ncj;
3830
3831     if (setFlags)
3832     {
3833         bitmask_init_bit(&flag[srcCi->ci >> iFlagShift], t);
3834     }
3835
3836     for (int j = srcCi->cj_ind_start; j < srcCi->cj_ind_end; j++)
3837     {
3838         dest->cj.push_back(src->cj[j]);
3839
3840         if (setFlags)
3841         {
3842             /* NOTE: This is relatively expensive, since this
3843              * operation is done for all elements in the list,
3844              * whereas at list generation this is done only
3845              * once for each flag entry.
3846              */
3847             bitmask_init_bit(&flag[src->cj[j].cj >> jFlagShift], t);
3848         }
3849     }
3850 }
3851
3852 /* This routine re-balances the pairlists such that all are nearly equally
3853  * sized. Only whole i-entries are moved between lists. These are moved
3854  * between the ends of the lists, such that the buffer reduction cost should
3855  * not change significantly.
3856  * Note that all original reduction flags are currently kept. This can lead
3857  * to reduction of parts of the force buffer that could be avoided. But since
3858  * the original lists are quite balanced, this will only give minor overhead.
3859  */
3860 static void rebalanceSimpleLists(int                                  numLists,
3861                                  NbnxnPairlistCpu * const * const     srcSet,
3862                                  NbnxnPairlistCpu                   **destSet,
3863                                  gmx::ArrayRef<nbnxn_search_work_t>   searchWork)
3864 {
3865     int ncjTotal = 0;
3866     for (int s = 0; s < numLists; s++)
3867     {
3868         ncjTotal += srcSet[s]->ncjInUse;
3869     }
3870     int ncjTarget = (ncjTotal + numLists - 1)/numLists;
3871
3872 #pragma omp parallel num_threads(numLists)
3873     {
3874         int t       = gmx_omp_get_thread_num();
3875
3876         int cjStart = ncjTarget* t;
3877         int cjEnd   = ncjTarget*(t + 1);
3878
3879         /* The destination pair-list for task/thread t */
3880         NbnxnPairlistCpu *dest = destSet[t];
3881
3882         clear_pairlist(dest);
3883         dest->na_cj   = srcSet[0]->na_cj;
3884
3885         /* Note that the flags in the work struct (still) contain flags
3886          * for all entries that are present in srcSet->nbl[t].
3887          */
3888         gmx_bitmask_t *flag       = searchWork[t].buffer_flags.flag;
3889
3890         int            iFlagShift = getBufferFlagShift(dest->na_ci);
3891         int            jFlagShift = getBufferFlagShift(dest->na_cj);
3892
3893         int            cjGlobal   = 0;
3894         for (int s = 0; s < numLists && cjGlobal < cjEnd; s++)
3895         {
3896             const NbnxnPairlistCpu *src = srcSet[s];
3897
3898             if (cjGlobal + src->ncjInUse > cjStart)
3899             {
3900                 for (gmx::index i = 0; i < gmx::ssize(src->ci) && cjGlobal < cjEnd; i++)
3901                 {
3902                     const nbnxn_ci_t *srcCi = &src->ci[i];
3903                     int               ncj   = srcCi->cj_ind_end - srcCi->cj_ind_start;
3904                     if (cjGlobal >= cjStart)
3905                     {
3906                         /* If the source list is not our own, we need to set
3907                          * extra flags (the template bool parameter).
3908                          */
3909                         if (s != t)
3910                         {
3911                             copySelectedListRange
3912                             <true>
3913                                 (srcCi, src, dest,
3914                                 flag, iFlagShift, jFlagShift, t);
3915                         }
3916                         else
3917                         {
3918                             copySelectedListRange
3919                             <false>
3920                                 (srcCi, src,
3921                                 dest, flag, iFlagShift, jFlagShift, t);
3922                         }
3923                     }
3924                     cjGlobal += ncj;
3925                 }
3926             }
3927             else
3928             {
3929                 cjGlobal += src->ncjInUse;
3930             }
3931         }
3932
3933         dest->ncjInUse = dest->cj.size();
3934     }
3935
3936 #ifndef NDEBUG
3937     int ncjTotalNew = 0;
3938     for (int s = 0; s < numLists; s++)
3939     {
3940         ncjTotalNew += destSet[s]->ncjInUse;
3941     }
3942     GMX_RELEASE_ASSERT(ncjTotalNew == ncjTotal, "The total size of the lists before and after rebalancing should match");
3943 #endif
3944 }
3945
3946 /* Returns if the pairlists are so imbalanced that it is worth rebalancing. */
3947 static bool checkRebalanceSimpleLists(const nbnxn_pairlist_set_t *listSet)
3948 {
3949     int numLists = listSet->nnbl;
3950     int ncjMax   = 0;
3951     int ncjTotal = 0;
3952     for (int s = 0; s < numLists; s++)
3953     {
3954         ncjMax    = std::max(ncjMax, listSet->nbl[s]->ncjInUse);
3955         ncjTotal += listSet->nbl[s]->ncjInUse;
3956     }
3957     if (debug)
3958     {
3959         fprintf(debug, "Pair-list ncjMax %d ncjTotal %d\n", ncjMax, ncjTotal);
3960     }
3961     /* The rebalancing adds 3% extra time to the search. Heuristically we
3962      * determined that under common conditions the non-bonded kernel balance
3963      * improvement will outweigh this when the imbalance is more than 3%.
3964      * But this will, obviously, depend on search vs kernel time and nstlist.
3965      */
3966     const real rebalanceTolerance = 1.03;
3967
3968     return numLists*ncjMax > ncjTotal*rebalanceTolerance;
3969 }
3970
3971 /* Perform a count (linear) sort to sort the smaller lists to the end.
3972  * This avoids load imbalance on the GPU, as large lists will be
3973  * scheduled and executed first and the smaller lists later.
3974  * Load balancing between multi-processors only happens at the end
3975  * and there smaller lists lead to more effective load balancing.
3976  * The sorting is done on the cj4 count, not on the actual pair counts.
3977  * Not only does this make the sort faster, but it also results in
3978  * better load balancing than using a list sorted on exact load.
3979  * This function swaps the pointer in the pair list to avoid a copy operation.
3980  */
3981 static void sort_sci(NbnxnPairlistGpu *nbl)
3982 {
3983     if (nbl->cj4.size() <= nbl->sci.size())
3984     {
3985         /* nsci = 0 or all sci have size 1, sorting won't change the order */
3986         return;
3987     }
3988
3989     NbnxnPairlistGpuWork &work = *nbl->work;
3990
3991     /* We will distinguish differences up to double the average */
3992     const int m = (2*nbl->cj4.size())/nbl->sci.size();
3993
3994     /* Resize work.sci_sort so we can sort into it */
3995     work.sci_sort.resize(nbl->sci.size());
3996
3997     std::vector<int> &sort = work.sortBuffer;
3998     /* Set up m + 1 entries in sort, initialized at 0 */
3999     sort.clear();
4000     sort.resize(m + 1, 0);
4001     /* Count the entries of each size */
4002     for (const nbnxn_sci_t &sci : nbl->sci)
4003     {
4004         int i = std::min(m, sci.numJClusterGroups());
4005         sort[i]++;
4006     }
4007     /* Calculate the offset for each count */
4008     int s0  = sort[m];
4009     sort[m] = 0;
4010     for (int i = m - 1; i >= 0; i--)
4011     {
4012         int s1  = sort[i];
4013         sort[i] = sort[i + 1] + s0;
4014         s0      = s1;
4015     }
4016
4017     /* Sort entries directly into place */
4018     gmx::ArrayRef<nbnxn_sci_t> sci_sort = work.sci_sort;
4019     for (const nbnxn_sci_t &sci : nbl->sci)
4020     {
4021         int i = std::min(m, sci.numJClusterGroups());
4022         sci_sort[sort[i]++] = sci;
4023     }
4024
4025     /* Swap the sci pointers so we use the new, sorted list */
4026     std::swap(nbl->sci, work.sci_sort);
4027 }
4028
4029 void nbnxn_make_pairlist(nonbonded_verlet_t        *nbv,
4030                          const InteractionLocality  iLocality,
4031                          nbnxn_pairlist_set_t      *nbl_list,
4032                          const t_blocka            *excl,
4033                          const int64_t              step,
4034                          t_nrnb                    *nrnb)
4035 {
4036     nbnxn_search         *nbs      = nbv->nbs.get();
4037     nbnxn_atomdata_t     *nbat     = nbv->nbat;
4038     const real            rlist    = nbv->listParams->rlistOuter;
4039
4040     int                nsubpair_target;
4041     float              nsubpair_tot_est;
4042     int                nnbl;
4043     int                ci_block;
4044     gmx_bool           CombineNBLists;
4045     gmx_bool           progBal;
4046     int                np_tot, np_noq, np_hlj, nap;
4047
4048     nnbl            = nbl_list->nnbl;
4049     CombineNBLists  = nbl_list->bCombined;
4050
4051     if (debug)
4052     {
4053         fprintf(debug, "ns making %d nblists\n", nnbl);
4054     }
4055
4056     nbat->bUseBufferFlags = (nbat->out.size() > 1);
4057     /* We should re-init the flags before making the first list */
4058     if (nbat->bUseBufferFlags && iLocality == InteractionLocality::Local)
4059     {
4060         init_buffer_flags(&nbat->buffer_flags, nbat->numAtoms());
4061     }
4062
4063     int nzi;
4064     if (iLocality == InteractionLocality::Local)
4065     {
4066         /* Only zone (grid) 0 vs 0 */
4067         nzi = 1;
4068     }
4069     else
4070     {
4071         nzi = nbs->zones->nizone;
4072     }
4073
4074     if (!nbl_list->bSimple && nbv->min_ci_balanced > 0)
4075     {
4076         get_nsubpair_target(nbs, iLocality, rlist, nbv->min_ci_balanced,
4077                             &nsubpair_target, &nsubpair_tot_est);
4078     }
4079     else
4080     {
4081         nsubpair_target  = 0;
4082         nsubpair_tot_est = 0;
4083     }
4084
4085     /* Clear all pair-lists */
4086     for (int th = 0; th < nnbl; th++)
4087     {
4088         if (nbl_list->bSimple)
4089         {
4090             clear_pairlist(nbl_list->nbl[th]);
4091         }
4092         else
4093         {
4094             clear_pairlist(nbl_list->nblGpu[th]);
4095         }
4096
4097         if (nbs->bFEP)
4098         {
4099             clear_pairlist_fep(nbl_list->nbl_fep[th]);
4100         }
4101     }
4102
4103     for (int zi = 0; zi < nzi; zi++)
4104     {
4105         const nbnxn_grid_t &iGrid = nbs->grid[zi];
4106
4107         int                 zj0;
4108         int                 zj1;
4109         if (iLocality == InteractionLocality::Local)
4110         {
4111             zj0 = 0;
4112             zj1 = 1;
4113         }
4114         else
4115         {
4116             zj0 = nbs->zones->izone[zi].j0;
4117             zj1 = nbs->zones->izone[zi].j1;
4118             if (zi == 0)
4119             {
4120                 zj0++;
4121             }
4122         }
4123         for (int zj = zj0; zj < zj1; zj++)
4124         {
4125             const nbnxn_grid_t &jGrid = nbs->grid[zj];
4126
4127             if (debug)
4128             {
4129                 fprintf(debug, "ns search grid %d vs %d\n", zi, zj);
4130             }
4131
4132             nbs_cycle_start(&nbs->cc[enbsCCsearch]);
4133
4134             ci_block = get_ci_block_size(iGrid, nbs->DomDec, nnbl);
4135
4136             /* With GPU: generate progressively smaller lists for
4137              * load balancing for local only or non-local with 2 zones.
4138              */
4139             progBal = (iLocality == InteractionLocality::Local || nbs->zones->n <= 2);
4140
4141 #pragma omp parallel for num_threads(nnbl) schedule(static)
4142             for (int th = 0; th < nnbl; th++)
4143             {
4144                 try
4145                 {
4146                     /* Re-init the thread-local work flag data before making
4147                      * the first list (not an elegant conditional).
4148                      */
4149                     if (nbat->bUseBufferFlags && ((zi == 0 && zj == 0)))
4150                     {
4151                         init_buffer_flags(&nbs->work[th].buffer_flags, nbat->numAtoms());
4152                     }
4153
4154                     if (CombineNBLists && th > 0)
4155                     {
4156                         GMX_ASSERT(!nbl_list->bSimple, "Can only combine GPU lists");
4157
4158                         clear_pairlist(nbl_list->nblGpu[th]);
4159                     }
4160
4161                     /* Divide the i super cell equally over the nblists */
4162                     if (nbl_list->bSimple)
4163                     {
4164                         nbnxn_make_pairlist_part(nbs, iGrid, jGrid,
4165                                                  &nbs->work[th], nbat, *excl,
4166                                                  rlist,
4167                                                  nbv->kernelSetup().kernelType,
4168                                                  ci_block,
4169                                                  nbat->bUseBufferFlags,
4170                                                  nsubpair_target,
4171                                                  progBal, nsubpair_tot_est,
4172                                                  th, nnbl,
4173                                                  nbl_list->nbl[th],
4174                                                  nbl_list->nbl_fep[th]);
4175                     }
4176                     else
4177                     {
4178                         nbnxn_make_pairlist_part(nbs, iGrid, jGrid,
4179                                                  &nbs->work[th], nbat, *excl,
4180                                                  rlist,
4181                                                  nbv->kernelSetup().kernelType,
4182                                                  ci_block,
4183                                                  nbat->bUseBufferFlags,
4184                                                  nsubpair_target,
4185                                                  progBal, nsubpair_tot_est,
4186                                                  th, nnbl,
4187                                                  nbl_list->nblGpu[th],
4188                                                  nbl_list->nbl_fep[th]);
4189                     }
4190                 }
4191                 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
4192             }
4193             nbs_cycle_stop(&nbs->cc[enbsCCsearch]);
4194
4195             np_tot = 0;
4196             np_noq = 0;
4197             np_hlj = 0;
4198             for (int th = 0; th < nnbl; th++)
4199             {
4200                 inc_nrnb(nrnb, eNR_NBNXN_DIST2, nbs->work[th].ndistc);
4201
4202                 if (nbl_list->bSimple)
4203                 {
4204                     NbnxnPairlistCpu *nbl = nbl_list->nbl[th];
4205                     np_tot += nbl->cj.size();
4206                     np_noq += nbl->work->ncj_noq;
4207                     np_hlj += nbl->work->ncj_hlj;
4208                 }
4209                 else
4210                 {
4211                     NbnxnPairlistGpu *nbl = nbl_list->nblGpu[th];
4212                     /* This count ignores potential subsequent pair pruning */
4213                     np_tot += nbl->nci_tot;
4214                 }
4215             }
4216             if (nbl_list->bSimple)
4217             {
4218                 nap               = nbl_list->nbl[0]->na_ci*nbl_list->nbl[0]->na_cj;
4219             }
4220             else
4221             {
4222                 nap               = gmx::square(nbl_list->nblGpu[0]->na_ci);
4223             }
4224             nbl_list->natpair_ljq = (np_tot - np_noq)*nap - np_hlj*nap/2;
4225             nbl_list->natpair_lj  = np_noq*nap;
4226             nbl_list->natpair_q   = np_hlj*nap/2;
4227
4228             if (CombineNBLists && nnbl > 1)
4229             {
4230                 GMX_ASSERT(!nbl_list->bSimple, "Can only combine GPU lists");
4231                 NbnxnPairlistGpu **nbl = nbl_list->nblGpu;
4232
4233                 nbs_cycle_start(&nbs->cc[enbsCCcombine]);
4234
4235                 combine_nblists(nnbl-1, nbl+1, nbl[0]);
4236
4237                 nbs_cycle_stop(&nbs->cc[enbsCCcombine]);
4238             }
4239         }
4240     }
4241
4242     if (nbl_list->bSimple)
4243     {
4244         if (nnbl > 1 && checkRebalanceSimpleLists(nbl_list))
4245         {
4246             rebalanceSimpleLists(nbl_list->nnbl, nbl_list->nbl, nbl_list->nbl_work, nbs->work);
4247
4248             /* Swap the pointer of the sets of pair lists */
4249             NbnxnPairlistCpu **tmp = nbl_list->nbl;
4250             nbl_list->nbl          = nbl_list->nbl_work;
4251             nbl_list->nbl_work     = tmp;
4252         }
4253     }
4254     else
4255     {
4256         /* Sort the entries on size, large ones first */
4257         if (CombineNBLists || nnbl == 1)
4258         {
4259             sort_sci(nbl_list->nblGpu[0]);
4260         }
4261         else
4262         {
4263 #pragma omp parallel for num_threads(nnbl) schedule(static)
4264             for (int th = 0; th < nnbl; th++)
4265             {
4266                 try
4267                 {
4268                     sort_sci(nbl_list->nblGpu[th]);
4269                 }
4270                 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
4271             }
4272         }
4273     }
4274
4275     if (nbat->bUseBufferFlags)
4276     {
4277         reduce_buffer_flags(nbs, nbl_list->nnbl, &nbat->buffer_flags);
4278     }
4279
4280     if (nbs->bFEP)
4281     {
4282         /* Balance the free-energy lists over all the threads */
4283         balance_fep_lists(nbs, nbl_list);
4284     }
4285
4286     if (nbl_list->bSimple)
4287     {
4288         /* This is a fresh list, so not pruned, stored using ci.
4289          * ciOuter is invalid at this point.
4290          */
4291         GMX_ASSERT(nbl_list->nbl[0]->ciOuter.empty(), "ciOuter is invalid so it should be empty");
4292     }
4293
4294     nbl_list->outerListCreationStep = step;
4295
4296     /* Special performance logging stuff (env.var. GMX_NBNXN_CYCLE) */
4297     if (iLocality == InteractionLocality::Local)
4298     {
4299         nbs->search_count++;
4300     }
4301     if (nbs->print_cycles &&
4302         (!nbs->DomDec || iLocality == InteractionLocality::NonLocal) &&
4303         nbs->search_count % 100 == 0)
4304     {
4305         nbs_cycle_print(stderr, nbs);
4306     }
4307
4308     /* If we have more than one list, they either got rebalancing (CPU)
4309      * or combined (GPU), so we should dump the final result to debug.
4310      */
4311     if (debug && nbl_list->nnbl > 1)
4312     {
4313         if (nbl_list->bSimple)
4314         {
4315             for (int t = 0; t < nbl_list->nnbl; t++)
4316             {
4317                 print_nblist_statistics(debug, nbl_list->nbl[t], nbs, rlist);
4318             }
4319         }
4320         else
4321         {
4322             print_nblist_statistics(debug, nbl_list->nblGpu[0], nbs, rlist);
4323         }
4324     }
4325
4326     if (debug)
4327     {
4328         if (gmx_debug_at)
4329         {
4330             if (nbl_list->bSimple)
4331             {
4332                 for (int t = 0; t < nbl_list->nnbl; t++)
4333                 {
4334                     print_nblist_ci_cj(debug, nbl_list->nbl[t]);
4335                 }
4336             }
4337             else
4338             {
4339                 print_nblist_sci_cj(debug, nbl_list->nblGpu[0]);
4340             }
4341         }
4342
4343         if (nbat->bUseBufferFlags)
4344         {
4345             print_reduction_cost(&nbat->buffer_flags, nbl_list->nnbl);
4346         }
4347     }
4348
4349     if (nbv->listParams->useDynamicPruning && !nbv->useGpu())
4350     {
4351         nbnxnPrepareListForDynamicPruning(nbl_list);
4352     }
4353
4354     if (nbv->useGpu())
4355     {
4356         /* Launch the transfer of the pairlist to the GPU.
4357          *
4358          * NOTE: The launch overhead is currently not timed separately
4359          */
4360         Nbnxm::gpu_init_pairlist(nbv->gpu_nbv,
4361                                  nbl_list->nblGpu[0],
4362                                  iLocality);
4363     }
4364 }
4365
4366 void nbnxnPrepareListForDynamicPruning(nbnxn_pairlist_set_t *listSet)
4367 {
4368     GMX_RELEASE_ASSERT(listSet->bSimple, "Should only be called for simple lists");
4369
4370     /* TODO: Restructure the lists so we have actual outer and inner
4371      *       list objects so we can set a single pointer instead of
4372      *       swapping several pointers.
4373      */
4374
4375     for (int i = 0; i < listSet->nnbl; i++)
4376     {
4377         NbnxnPairlistCpu &list = *listSet->nbl[i];
4378
4379         /* The search produced a list in ci/cj.
4380          * Swap the list pointers so we get the outer list is ciOuter,cjOuter
4381          * and we can prune that to get an inner list in ci/cj.
4382          */
4383         GMX_RELEASE_ASSERT(list.ciOuter.empty() && list.cjOuter.empty(),
4384                            "The outer lists should be empty before preparation");
4385
4386         std::swap(list.ci, list.ciOuter);
4387         std::swap(list.cj, list.cjOuter);
4388     }
4389 }