Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_atomdata.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2012, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <math.h>
38 #include <string.h>
39 #include "smalloc.h"
40 #include "macros.h"
41 #include "vec.h"
42 #include "nbnxn_consts.h"
43 #include "nbnxn_internal.h"
44 #include "nbnxn_search.h"
45 #include "nbnxn_atomdata.h"
46 #include "gmx_omp_nthreads.h"
47
48 /* Default nbnxn allocation routine, allocates NBNXN_MEM_ALIGN byte aligned */
49 void nbnxn_alloc_aligned(void **ptr, size_t nbytes)
50 {
51     *ptr = save_malloc_aligned("ptr", __FILE__, __LINE__, nbytes, 1, NBNXN_MEM_ALIGN);
52 }
53
54 /* Free function for memory allocated with nbnxn_alloc_aligned */
55 void nbnxn_free_aligned(void *ptr)
56 {
57     sfree_aligned(ptr);
58 }
59
60 /* Reallocation wrapper function for nbnxn data structures */
61 void nbnxn_realloc_void(void **ptr,
62                         int nbytes_copy, int nbytes_new,
63                         nbnxn_alloc_t *ma,
64                         nbnxn_free_t  *mf)
65 {
66     void *ptr_new;
67
68     ma(&ptr_new, nbytes_new);
69
70     if (nbytes_new > 0 && ptr_new == NULL)
71     {
72         gmx_fatal(FARGS, "Allocation of %d bytes failed", nbytes_new);
73     }
74
75     if (nbytes_copy > 0)
76     {
77         if (nbytes_new < nbytes_copy)
78         {
79             gmx_incons("In nbnxn_realloc_void: new size less than copy size");
80         }
81         memcpy(ptr_new, *ptr, nbytes_copy);
82     }
83     if (*ptr != NULL)
84     {
85         mf(*ptr);
86     }
87     *ptr = ptr_new;
88 }
89
90 /* Reallocate the nbnxn_atomdata_t for a size of n atoms */
91 void nbnxn_atomdata_realloc(nbnxn_atomdata_t *nbat, int n)
92 {
93     int t;
94
95     nbnxn_realloc_void((void **)&nbat->type,
96                        nbat->natoms*sizeof(*nbat->type),
97                        n*sizeof(*nbat->type),
98                        nbat->alloc, nbat->free);
99     nbnxn_realloc_void((void **)&nbat->lj_comb,
100                        nbat->natoms*2*sizeof(*nbat->lj_comb),
101                        n*2*sizeof(*nbat->lj_comb),
102                        nbat->alloc, nbat->free);
103     if (nbat->XFormat != nbatXYZQ)
104     {
105         nbnxn_realloc_void((void **)&nbat->q,
106                            nbat->natoms*sizeof(*nbat->q),
107                            n*sizeof(*nbat->q),
108                            nbat->alloc, nbat->free);
109     }
110     if (nbat->nenergrp > 1)
111     {
112         nbnxn_realloc_void((void **)&nbat->energrp,
113                            nbat->natoms/nbat->na_c*sizeof(*nbat->energrp),
114                            n/nbat->na_c*sizeof(*nbat->energrp),
115                            nbat->alloc, nbat->free);
116     }
117     nbnxn_realloc_void((void **)&nbat->x,
118                        nbat->natoms*nbat->xstride*sizeof(*nbat->x),
119                        n*nbat->xstride*sizeof(*nbat->x),
120                        nbat->alloc, nbat->free);
121     for (t = 0; t < nbat->nout; t++)
122     {
123         /* Allocate one element extra for possible signaling with CUDA */
124         nbnxn_realloc_void((void **)&nbat->out[t].f,
125                            nbat->natoms*nbat->fstride*sizeof(*nbat->out[t].f),
126                            n*nbat->fstride*sizeof(*nbat->out[t].f),
127                            nbat->alloc, nbat->free);
128     }
129     nbat->nalloc = n;
130 }
131
132 /* Initializes an nbnxn_atomdata_output_t data structure */
133 static void nbnxn_atomdata_output_init(nbnxn_atomdata_output_t *out,
134                                        int nb_kernel_type,
135                                        int nenergrp, int stride,
136                                        nbnxn_alloc_t *ma)
137 {
138     int cj_size;
139
140     out->f = NULL;
141     ma((void **)&out->fshift, SHIFTS*DIM*sizeof(*out->fshift));
142     out->nV = nenergrp*nenergrp;
143     ma((void **)&out->Vvdw, out->nV*sizeof(*out->Vvdw));
144     ma((void **)&out->Vc, out->nV*sizeof(*out->Vc  ));
145
146     if (nb_kernel_type == nbnxnk4xN_SIMD_4xN ||
147         nb_kernel_type == nbnxnk4xN_SIMD_2xNN)
148     {
149         cj_size  = nbnxn_kernel_to_cj_size(nb_kernel_type);
150         out->nVS = nenergrp*nenergrp*stride*(cj_size>>1)*cj_size;
151         ma((void **)&out->VSvdw, out->nVS*sizeof(*out->VSvdw));
152         ma((void **)&out->VSc, out->nVS*sizeof(*out->VSc  ));
153     }
154     else
155     {
156         out->nVS = 0;
157     }
158 }
159
160 static void copy_int_to_nbat_int(const int *a, int na, int na_round,
161                                  const int *in, int fill, int *innb)
162 {
163     int i, j;
164
165     j = 0;
166     for (i = 0; i < na; i++)
167     {
168         innb[j++] = in[a[i]];
169     }
170     /* Complete the partially filled last cell with fill */
171     for (; i < na_round; i++)
172     {
173         innb[j++] = fill;
174     }
175 }
176
177 static void clear_nbat_real(int na, int nbatFormat, real *xnb, int a0)
178 {
179     int a, d, j, c;
180
181     switch (nbatFormat)
182     {
183         case nbatXYZ:
184             for (a = 0; a < na; a++)
185             {
186                 for (d = 0; d < DIM; d++)
187                 {
188                     xnb[(a0+a)*STRIDE_XYZ+d] = 0;
189                 }
190             }
191             break;
192         case nbatXYZQ:
193             for (a = 0; a < na; a++)
194             {
195                 for (d = 0; d < DIM; d++)
196                 {
197                     xnb[(a0+a)*STRIDE_XYZQ+d] = 0;
198                 }
199             }
200             break;
201         case nbatX4:
202             j = X4_IND_A(a0);
203             c = a0 & (PACK_X4-1);
204             for (a = 0; a < na; a++)
205             {
206                 xnb[j+XX*PACK_X4] = 0;
207                 xnb[j+YY*PACK_X4] = 0;
208                 xnb[j+ZZ*PACK_X4] = 0;
209                 j++;
210                 c++;
211                 if (c == PACK_X4)
212                 {
213                     j += (DIM-1)*PACK_X4;
214                     c  = 0;
215                 }
216             }
217             break;
218         case nbatX8:
219             j = X8_IND_A(a0);
220             c = a0 & (PACK_X8-1);
221             for (a = 0; a < na; a++)
222             {
223                 xnb[j+XX*PACK_X8] = 0;
224                 xnb[j+YY*PACK_X8] = 0;
225                 xnb[j+ZZ*PACK_X8] = 0;
226                 j++;
227                 c++;
228                 if (c == PACK_X8)
229                 {
230                     j += (DIM-1)*PACK_X8;
231                     c  = 0;
232                 }
233             }
234             break;
235     }
236 }
237
238 void copy_rvec_to_nbat_real(const int *a, int na, int na_round,
239                             rvec *x, int nbatFormat, real *xnb, int a0,
240                             int cx, int cy, int cz)
241 {
242     int i, j, c;
243
244 /* We might need to place filler particles to fill up the cell to na_round.
245  * The coefficients (LJ and q) for such particles are zero.
246  * But we might still get NaN as 0*NaN when distances are too small.
247  * We hope that -107 nm is far away enough from to zero
248  * to avoid accidental short distances to particles shifted down for pbc.
249  */
250 #define NBAT_FAR_AWAY 107
251
252     switch (nbatFormat)
253     {
254         case nbatXYZ:
255             j = a0*STRIDE_XYZ;
256             for (i = 0; i < na; i++)
257             {
258                 xnb[j++] = x[a[i]][XX];
259                 xnb[j++] = x[a[i]][YY];
260                 xnb[j++] = x[a[i]][ZZ];
261             }
262             /* Complete the partially filled last cell with copies of the last element.
263              * This simplifies the bounding box calculation and avoid
264              * numerical issues with atoms that are coincidentally close.
265              */
266             for (; i < na_round; i++)
267             {
268                 xnb[j++] = -NBAT_FAR_AWAY*(1 + cx);
269                 xnb[j++] = -NBAT_FAR_AWAY*(1 + cy);
270                 xnb[j++] = -NBAT_FAR_AWAY*(1 + cz + i);
271             }
272             break;
273         case nbatXYZQ:
274             j = a0*STRIDE_XYZQ;
275             for (i = 0; i < na; i++)
276             {
277                 xnb[j++] = x[a[i]][XX];
278                 xnb[j++] = x[a[i]][YY];
279                 xnb[j++] = x[a[i]][ZZ];
280                 j++;
281             }
282             /* Complete the partially filled last cell with particles far apart */
283             for (; i < na_round; i++)
284             {
285                 xnb[j++] = -NBAT_FAR_AWAY*(1 + cx);
286                 xnb[j++] = -NBAT_FAR_AWAY*(1 + cy);
287                 xnb[j++] = -NBAT_FAR_AWAY*(1 + cz + i);
288                 j++;
289             }
290             break;
291         case nbatX4:
292             j = X4_IND_A(a0);
293             c = a0 & (PACK_X4-1);
294             for (i = 0; i < na; i++)
295             {
296                 xnb[j+XX*PACK_X4] = x[a[i]][XX];
297                 xnb[j+YY*PACK_X4] = x[a[i]][YY];
298                 xnb[j+ZZ*PACK_X4] = x[a[i]][ZZ];
299                 j++;
300                 c++;
301                 if (c == PACK_X4)
302                 {
303                     j += (DIM-1)*PACK_X4;
304                     c  = 0;
305                 }
306             }
307             /* Complete the partially filled last cell with particles far apart */
308             for (; i < na_round; i++)
309             {
310                 xnb[j+XX*PACK_X4] = -NBAT_FAR_AWAY*(1 + cx);
311                 xnb[j+YY*PACK_X4] = -NBAT_FAR_AWAY*(1 + cy);
312                 xnb[j+ZZ*PACK_X4] = -NBAT_FAR_AWAY*(1 + cz + i);
313                 j++;
314                 c++;
315                 if (c == PACK_X4)
316                 {
317                     j += (DIM-1)*PACK_X4;
318                     c  = 0;
319                 }
320             }
321             break;
322         case nbatX8:
323             j = X8_IND_A(a0);
324             c = a0 & (PACK_X8 - 1);
325             for (i = 0; i < na; i++)
326             {
327                 xnb[j+XX*PACK_X8] = x[a[i]][XX];
328                 xnb[j+YY*PACK_X8] = x[a[i]][YY];
329                 xnb[j+ZZ*PACK_X8] = x[a[i]][ZZ];
330                 j++;
331                 c++;
332                 if (c == PACK_X8)
333                 {
334                     j += (DIM-1)*PACK_X8;
335                     c  = 0;
336                 }
337             }
338             /* Complete the partially filled last cell with particles far apart */
339             for (; i < na_round; i++)
340             {
341                 xnb[j+XX*PACK_X8] = -NBAT_FAR_AWAY*(1 + cx);
342                 xnb[j+YY*PACK_X8] = -NBAT_FAR_AWAY*(1 + cy);
343                 xnb[j+ZZ*PACK_X8] = -NBAT_FAR_AWAY*(1 + cz + i);
344                 j++;
345                 c++;
346                 if (c == PACK_X8)
347                 {
348                     j += (DIM-1)*PACK_X8;
349                     c  = 0;
350                 }
351             }
352             break;
353         default:
354             gmx_incons("Unsupported nbnxn_atomdata_t format");
355     }
356 }
357
358 /* Determines the combination rule (or none) to be used, stores it,
359  * and sets the LJ parameters required with the rule.
360  */
361 static void set_combination_rule_data(nbnxn_atomdata_t *nbat)
362 {
363     int  nt, i, j;
364     real c6, c12;
365
366     nt = nbat->ntype;
367
368     switch (nbat->comb_rule)
369     {
370         case  ljcrGEOM:
371             nbat->comb_rule = ljcrGEOM;
372
373             for (i = 0; i < nt; i++)
374             {
375                 /* Copy the diagonal from the nbfp matrix */
376                 nbat->nbfp_comb[i*2  ] = sqrt(nbat->nbfp[(i*nt+i)*2  ]);
377                 nbat->nbfp_comb[i*2+1] = sqrt(nbat->nbfp[(i*nt+i)*2+1]);
378             }
379             break;
380         case ljcrLB:
381             for (i = 0; i < nt; i++)
382             {
383                 /* Get 6*C6 and 12*C12 from the diagonal of the nbfp matrix */
384                 c6  = nbat->nbfp[(i*nt+i)*2  ];
385                 c12 = nbat->nbfp[(i*nt+i)*2+1];
386                 if (c6 > 0 && c12 > 0)
387                 {
388                     /* We store 0.5*2^1/6*sigma and sqrt(4*3*eps),
389                      * so we get 6*C6 and 12*C12 after combining.
390                      */
391                     nbat->nbfp_comb[i*2  ] = 0.5*pow(c12/c6, 1.0/6.0);
392                     nbat->nbfp_comb[i*2+1] = sqrt(c6*c6/c12);
393                 }
394                 else
395                 {
396                     nbat->nbfp_comb[i*2  ] = 0;
397                     nbat->nbfp_comb[i*2+1] = 0;
398                 }
399             }
400             break;
401         case ljcrNONE:
402             /* nbfp_s4 stores two parameters using a stride of 4,
403              * because this would suit x86 SIMD single-precision
404              * quad-load intrinsics. There's a slight inefficiency in
405              * allocating and initializing nbfp_s4 when it might not
406              * be used, but introducing the conditional code is not
407              * really worth it. */
408             nbat->alloc((void **)&nbat->nbfp_s4, nt*nt*4*sizeof(*nbat->nbfp_s4));
409             for (i = 0; i < nt; i++)
410             {
411                 for (j = 0; j < nt; j++)
412                 {
413                     nbat->nbfp_s4[(i*nt+j)*4+0] = nbat->nbfp[(i*nt+j)*2+0];
414                     nbat->nbfp_s4[(i*nt+j)*4+1] = nbat->nbfp[(i*nt+j)*2+1];
415                     nbat->nbfp_s4[(i*nt+j)*4+2] = 0;
416                     nbat->nbfp_s4[(i*nt+j)*4+3] = 0;
417                 }
418             }
419             break;
420         default:
421             gmx_incons("Unknown combination rule");
422             break;
423     }
424 }
425
426 /* Initializes an nbnxn_atomdata_t data structure */
427 void nbnxn_atomdata_init(FILE *fp,
428                          nbnxn_atomdata_t *nbat,
429                          int nb_kernel_type,
430                          int ntype, const real *nbfp,
431                          int n_energygroups,
432                          int nout,
433                          nbnxn_alloc_t *alloc,
434                          nbnxn_free_t  *free)
435 {
436     int      i, j;
437     real     c6, c12, tol;
438     char    *ptr;
439     gmx_bool simple, bCombGeom, bCombLB;
440
441     if (alloc == NULL)
442     {
443         nbat->alloc = nbnxn_alloc_aligned;
444     }
445     else
446     {
447         nbat->alloc = alloc;
448     }
449     if (free == NULL)
450     {
451         nbat->free = nbnxn_free_aligned;
452     }
453     else
454     {
455         nbat->free = free;
456     }
457
458     if (debug)
459     {
460         fprintf(debug, "There are %d atom types in the system, adding one for nbnxn_atomdata_t\n", ntype);
461     }
462     nbat->ntype = ntype + 1;
463     nbat->alloc((void **)&nbat->nbfp,
464                 nbat->ntype*nbat->ntype*2*sizeof(*nbat->nbfp));
465     nbat->alloc((void **)&nbat->nbfp_comb, nbat->ntype*2*sizeof(*nbat->nbfp_comb));
466
467     /* A tolerance of 1e-5 seems reasonable for (possibly hand-typed)
468      * force-field floating point parameters.
469      */
470     tol = 1e-5;
471     ptr = getenv("GMX_LJCOMB_TOL");
472     if (ptr != NULL)
473     {
474         double dbl;
475
476         sscanf(ptr, "%lf", &dbl);
477         tol = dbl;
478     }
479     bCombGeom = TRUE;
480     bCombLB   = TRUE;
481
482     /* Temporarily fill nbat->nbfp_comb with sigma and epsilon
483      * to check for the LB rule.
484      */
485     for (i = 0; i < ntype; i++)
486     {
487         c6  = nbfp[(i*ntype+i)*2  ]/6.0;
488         c12 = nbfp[(i*ntype+i)*2+1]/12.0;
489         if (c6 > 0 && c12 > 0)
490         {
491             nbat->nbfp_comb[i*2  ] = pow(c12/c6, 1.0/6.0);
492             nbat->nbfp_comb[i*2+1] = 0.25*c6*c6/c12;
493         }
494         else if (c6 == 0 && c12 == 0)
495         {
496             nbat->nbfp_comb[i*2  ] = 0;
497             nbat->nbfp_comb[i*2+1] = 0;
498         }
499         else
500         {
501             /* Can not use LB rule with only dispersion or repulsion */
502             bCombLB = FALSE;
503         }
504     }
505
506     for (i = 0; i < nbat->ntype; i++)
507     {
508         for (j = 0; j < nbat->ntype; j++)
509         {
510             if (i < ntype && j < ntype)
511             {
512                 /* fr->nbfp has been updated, so that array too now stores c6/c12 including
513                  * the 6.0/12.0 prefactors to save 2 flops in the most common case (force-only).
514                  */
515                 c6  = nbfp[(i*ntype+j)*2  ];
516                 c12 = nbfp[(i*ntype+j)*2+1];
517                 nbat->nbfp[(i*nbat->ntype+j)*2  ] = c6;
518                 nbat->nbfp[(i*nbat->ntype+j)*2+1] = c12;
519
520                 /* Compare 6*C6 and 12*C12 for geometric cobination rule */
521                 bCombGeom = bCombGeom &&
522                     gmx_within_tol(c6*c6, nbfp[(i*ntype+i)*2  ]*nbfp[(j*ntype+j)*2  ], tol) &&
523                     gmx_within_tol(c12*c12, nbfp[(i*ntype+i)*2+1]*nbfp[(j*ntype+j)*2+1], tol);
524
525                 /* Compare C6 and C12 for Lorentz-Berthelot combination rule */
526                 c6     /= 6.0;
527                 c12    /= 12.0;
528                 bCombLB = bCombLB &&
529                     ((c6 == 0 && c12 == 0 &&
530                       (nbat->nbfp_comb[i*2+1] == 0 || nbat->nbfp_comb[j*2+1] == 0)) ||
531                      (c6 > 0 && c12 > 0 &&
532                       gmx_within_tol(pow(c12/c6, 1.0/6.0), 0.5*(nbat->nbfp_comb[i*2]+nbat->nbfp_comb[j*2]), tol) &&
533                       gmx_within_tol(0.25*c6*c6/c12, sqrt(nbat->nbfp_comb[i*2+1]*nbat->nbfp_comb[j*2+1]), tol)));
534             }
535             else
536             {
537                 /* Add zero parameters for the additional dummy atom type */
538                 nbat->nbfp[(i*nbat->ntype+j)*2  ] = 0;
539                 nbat->nbfp[(i*nbat->ntype+j)*2+1] = 0;
540             }
541         }
542     }
543     if (debug)
544     {
545         fprintf(debug, "Combination rules: geometric %d Lorentz-Berthelot %d\n",
546                 bCombGeom, bCombLB);
547     }
548
549     simple = nbnxn_kernel_pairlist_simple(nb_kernel_type);
550
551     if (simple)
552     {
553         /* We prefer the geometic combination rule,
554          * as that gives a slightly faster kernel than the LB rule.
555          */
556         if (bCombGeom)
557         {
558             nbat->comb_rule = ljcrGEOM;
559         }
560         else if (bCombLB)
561         {
562             nbat->comb_rule = ljcrLB;
563         }
564         else
565         {
566             nbat->comb_rule = ljcrNONE;
567
568             nbat->free(nbat->nbfp_comb);
569         }
570
571         if (fp)
572         {
573             if (nbat->comb_rule == ljcrNONE)
574             {
575                 fprintf(fp, "Using full Lennard-Jones parameter combination matrix\n\n");
576             }
577             else
578             {
579                 fprintf(fp, "Using %s Lennard-Jones combination rule\n\n",
580                         nbat->comb_rule == ljcrGEOM ? "geometric" : "Lorentz-Berthelot");
581             }
582         }
583
584         set_combination_rule_data(nbat);
585     }
586     else
587     {
588         nbat->comb_rule = ljcrNONE;
589
590         nbat->free(nbat->nbfp_comb);
591     }
592
593     nbat->natoms  = 0;
594     nbat->type    = NULL;
595     nbat->lj_comb = NULL;
596     if (simple)
597     {
598         int pack_x;
599
600         switch (nb_kernel_type)
601         {
602             case nbnxnk4xN_SIMD_4xN:
603             case nbnxnk4xN_SIMD_2xNN:
604                 pack_x = max(NBNXN_CPU_CLUSTER_I_SIZE,
605                              nbnxn_kernel_to_cj_size(nb_kernel_type));
606                 switch (pack_x)
607                 {
608                     case 4:
609                         nbat->XFormat = nbatX4;
610                         break;
611                     case 8:
612                         nbat->XFormat = nbatX8;
613                         break;
614                     default:
615                         gmx_incons("Unsupported packing width");
616                 }
617                 break;
618             default:
619                 nbat->XFormat = nbatXYZ;
620                 break;
621         }
622
623         nbat->FFormat = nbat->XFormat;
624     }
625     else
626     {
627         nbat->XFormat = nbatXYZQ;
628         nbat->FFormat = nbatXYZ;
629     }
630     nbat->q        = NULL;
631     nbat->nenergrp = n_energygroups;
632     if (!simple)
633     {
634         /* Energy groups not supported yet for super-sub lists */
635         if (n_energygroups > 1 && fp != NULL)
636         {
637             fprintf(fp, "\nNOTE: With GPUs, reporting energy group contributions is not supported\n\n");
638         }
639         nbat->nenergrp = 1;
640     }
641     /* Temporary storage goes as #grp^3*simd_width^2/2, so limit to 64 */
642     if (nbat->nenergrp > 64)
643     {
644         gmx_fatal(FARGS, "With NxN kernels not more than 64 energy groups are supported\n");
645     }
646     nbat->neg_2log = 1;
647     while (nbat->nenergrp > (1<<nbat->neg_2log))
648     {
649         nbat->neg_2log++;
650     }
651     nbat->energrp = NULL;
652     nbat->alloc((void **)&nbat->shift_vec, SHIFTS*sizeof(*nbat->shift_vec));
653     nbat->xstride = (nbat->XFormat == nbatXYZQ ? STRIDE_XYZQ : DIM);
654     nbat->fstride = (nbat->FFormat == nbatXYZQ ? STRIDE_XYZQ : DIM);
655     nbat->x       = NULL;
656
657 #ifdef GMX_NBNXN_SIMD
658     if (simple)
659     {
660         /* Set the diagonal cluster pair exclusion mask setup data.
661          * In the kernel we check 0 < j - i to generate the masks.
662          * Here we store j - i for generating the mask for the first i,
663          * we substract 0.5 to avoid rounding issues.
664          * In the kernel we can subtract 1 to generate the subsequent mask.
665          */
666         const int simd_width = GMX_NBNXN_SIMD_BITWIDTH/(sizeof(real)*8);
667         int       simd_4xn_diag_size, j;
668
669         simd_4xn_diag_size = max(NBNXN_CPU_CLUSTER_I_SIZE, simd_width);
670         snew_aligned(nbat->simd_4xn_diag, simd_4xn_diag_size, NBNXN_MEM_ALIGN);
671         for (j = 0; j < simd_4xn_diag_size; j++)
672         {
673             nbat->simd_4xn_diag[j] = j - 0.5;
674         }
675
676         snew_aligned(nbat->simd_2xnn_diag, simd_width, NBNXN_MEM_ALIGN);
677         for (j = 0; j < simd_width/2; j++)
678         {
679             /* The j-cluster size is half the SIMD width */
680             nbat->simd_2xnn_diag[j]              = j - 0.5;
681             /* The next half of the SIMD width is for i + 1 */
682             nbat->simd_2xnn_diag[simd_width/2+j] = j - 1 - 0.5;
683         }
684     }
685 #endif
686
687     /* Initialize the output data structures */
688     nbat->nout    = nout;
689     snew(nbat->out, nbat->nout);
690     nbat->nalloc  = 0;
691     for (i = 0; i < nbat->nout; i++)
692     {
693         nbnxn_atomdata_output_init(&nbat->out[i],
694                                    nb_kernel_type,
695                                    nbat->nenergrp, 1<<nbat->neg_2log,
696                                    nbat->alloc);
697     }
698     nbat->buffer_flags.flag        = NULL;
699     nbat->buffer_flags.flag_nalloc = 0;
700 }
701
702 static void copy_lj_to_nbat_lj_comb_x4(const real *ljparam_type,
703                                        const int *type, int na,
704                                        real *ljparam_at)
705 {
706     int is, k, i;
707
708     /* The LJ params follow the combination rule:
709      * copy the params for the type array to the atom array.
710      */
711     for (is = 0; is < na; is += PACK_X4)
712     {
713         for (k = 0; k < PACK_X4; k++)
714         {
715             i = is + k;
716             ljparam_at[is*2        +k] = ljparam_type[type[i]*2  ];
717             ljparam_at[is*2+PACK_X4+k] = ljparam_type[type[i]*2+1];
718         }
719     }
720 }
721
722 static void copy_lj_to_nbat_lj_comb_x8(const real *ljparam_type,
723                                        const int *type, int na,
724                                        real *ljparam_at)
725 {
726     int is, k, i;
727
728     /* The LJ params follow the combination rule:
729      * copy the params for the type array to the atom array.
730      */
731     for (is = 0; is < na; is += PACK_X8)
732     {
733         for (k = 0; k < PACK_X8; k++)
734         {
735             i = is + k;
736             ljparam_at[is*2        +k] = ljparam_type[type[i]*2  ];
737             ljparam_at[is*2+PACK_X8+k] = ljparam_type[type[i]*2+1];
738         }
739     }
740 }
741
742 /* Sets the atom type and LJ data in nbnxn_atomdata_t */
743 static void nbnxn_atomdata_set_atomtypes(nbnxn_atomdata_t    *nbat,
744                                          int                  ngrid,
745                                          const nbnxn_search_t nbs,
746                                          const int           *type)
747 {
748     int                 g, i, ncz, ash;
749     const nbnxn_grid_t *grid;
750
751     for (g = 0; g < ngrid; g++)
752     {
753         grid = &nbs->grid[g];
754
755         /* Loop over all columns and copy and fill */
756         for (i = 0; i < grid->ncx*grid->ncy; i++)
757         {
758             ncz = grid->cxy_ind[i+1] - grid->cxy_ind[i];
759             ash = (grid->cell0 + grid->cxy_ind[i])*grid->na_sc;
760
761             copy_int_to_nbat_int(nbs->a+ash, grid->cxy_na[i], ncz*grid->na_sc,
762                                  type, nbat->ntype-1, nbat->type+ash);
763
764             if (nbat->comb_rule != ljcrNONE)
765             {
766                 if (nbat->XFormat == nbatX4)
767                 {
768                     copy_lj_to_nbat_lj_comb_x4(nbat->nbfp_comb,
769                                                nbat->type+ash, ncz*grid->na_sc,
770                                                nbat->lj_comb+ash*2);
771                 }
772                 else if (nbat->XFormat == nbatX8)
773                 {
774                     copy_lj_to_nbat_lj_comb_x8(nbat->nbfp_comb,
775                                                nbat->type+ash, ncz*grid->na_sc,
776                                                nbat->lj_comb+ash*2);
777                 }
778             }
779         }
780     }
781 }
782
783 /* Sets the charges in nbnxn_atomdata_t *nbat */
784 static void nbnxn_atomdata_set_charges(nbnxn_atomdata_t    *nbat,
785                                        int                  ngrid,
786                                        const nbnxn_search_t nbs,
787                                        const real          *charge)
788 {
789     int                 g, cxy, ncz, ash, na, na_round, i, j;
790     real               *q;
791     const nbnxn_grid_t *grid;
792
793     for (g = 0; g < ngrid; g++)
794     {
795         grid = &nbs->grid[g];
796
797         /* Loop over all columns and copy and fill */
798         for (cxy = 0; cxy < grid->ncx*grid->ncy; cxy++)
799         {
800             ash      = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
801             na       = grid->cxy_na[cxy];
802             na_round = (grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy])*grid->na_sc;
803
804             if (nbat->XFormat == nbatXYZQ)
805             {
806                 q = nbat->x + ash*STRIDE_XYZQ + ZZ + 1;
807                 for (i = 0; i < na; i++)
808                 {
809                     *q = charge[nbs->a[ash+i]];
810                     q += STRIDE_XYZQ;
811                 }
812                 /* Complete the partially filled last cell with zeros */
813                 for (; i < na_round; i++)
814                 {
815                     *q = 0;
816                     q += STRIDE_XYZQ;
817                 }
818             }
819             else
820             {
821                 q = nbat->q + ash;
822                 for (i = 0; i < na; i++)
823                 {
824                     *q = charge[nbs->a[ash+i]];
825                     q++;
826                 }
827                 /* Complete the partially filled last cell with zeros */
828                 for (; i < na_round; i++)
829                 {
830                     *q = 0;
831                     q++;
832                 }
833             }
834         }
835     }
836 }
837
838 /* Copies the energy group indices to a reordered and packed array */
839 static void copy_egp_to_nbat_egps(const int *a, int na, int na_round,
840                                   int na_c, int bit_shift,
841                                   const int *in, int *innb)
842 {
843     int i, j, sa, at;
844     int comb;
845
846     j = 0;
847     for (i = 0; i < na; i += na_c)
848     {
849         /* Store na_c energy group numbers into one int */
850         comb = 0;
851         for (sa = 0; sa < na_c; sa++)
852         {
853             at = a[i+sa];
854             if (at >= 0)
855             {
856                 comb |= (GET_CGINFO_GID(in[at]) << (sa*bit_shift));
857             }
858         }
859         innb[j++] = comb;
860     }
861     /* Complete the partially filled last cell with fill */
862     for (; i < na_round; i += na_c)
863     {
864         innb[j++] = 0;
865     }
866 }
867
868 /* Set the energy group indices for atoms in nbnxn_atomdata_t */
869 static void nbnxn_atomdata_set_energygroups(nbnxn_atomdata_t    *nbat,
870                                             int                  ngrid,
871                                             const nbnxn_search_t nbs,
872                                             const int           *atinfo)
873 {
874     int                 g, i, ncz, ash;
875     const nbnxn_grid_t *grid;
876
877     for (g = 0; g < ngrid; g++)
878     {
879         grid = &nbs->grid[g];
880
881         /* Loop over all columns and copy and fill */
882         for (i = 0; i < grid->ncx*grid->ncy; i++)
883         {
884             ncz = grid->cxy_ind[i+1] - grid->cxy_ind[i];
885             ash = (grid->cell0 + grid->cxy_ind[i])*grid->na_sc;
886
887             copy_egp_to_nbat_egps(nbs->a+ash, grid->cxy_na[i], ncz*grid->na_sc,
888                                   nbat->na_c, nbat->neg_2log,
889                                   atinfo, nbat->energrp+(ash>>grid->na_c_2log));
890         }
891     }
892 }
893
894 /* Sets all required atom parameter data in nbnxn_atomdata_t */
895 void nbnxn_atomdata_set(nbnxn_atomdata_t    *nbat,
896                         int                  locality,
897                         const nbnxn_search_t nbs,
898                         const t_mdatoms     *mdatoms,
899                         const int           *atinfo)
900 {
901     int ngrid;
902
903     if (locality == eatLocal)
904     {
905         ngrid = 1;
906     }
907     else
908     {
909         ngrid = nbs->ngrid;
910     }
911
912     nbnxn_atomdata_set_atomtypes(nbat, ngrid, nbs, mdatoms->typeA);
913
914     nbnxn_atomdata_set_charges(nbat, ngrid, nbs, mdatoms->chargeA);
915
916     if (nbat->nenergrp > 1)
917     {
918         nbnxn_atomdata_set_energygroups(nbat, ngrid, nbs, atinfo);
919     }
920 }
921
922 /* Copies the shift vector array to nbnxn_atomdata_t */
923 void nbnxn_atomdata_copy_shiftvec(gmx_bool          bDynamicBox,
924                                   rvec             *shift_vec,
925                                   nbnxn_atomdata_t *nbat)
926 {
927     int i;
928
929     nbat->bDynamicBox = bDynamicBox;
930     for (i = 0; i < SHIFTS; i++)
931     {
932         copy_rvec(shift_vec[i], nbat->shift_vec[i]);
933     }
934 }
935
936 /* Copies (and reorders) the coordinates to nbnxn_atomdata_t */
937 void nbnxn_atomdata_copy_x_to_nbat_x(const nbnxn_search_t nbs,
938                                      int                  locality,
939                                      gmx_bool             FillLocal,
940                                      rvec                *x,
941                                      nbnxn_atomdata_t    *nbat)
942 {
943     int g0 = 0, g1 = 0;
944     int nth, th;
945
946     switch (locality)
947     {
948         case eatAll:
949             g0 = 0;
950             g1 = nbs->ngrid;
951             break;
952         case eatLocal:
953             g0 = 0;
954             g1 = 1;
955             break;
956         case eatNonlocal:
957             g0 = 1;
958             g1 = nbs->ngrid;
959             break;
960     }
961
962     if (FillLocal)
963     {
964         nbat->natoms_local = nbs->grid[0].nc*nbs->grid[0].na_sc;
965     }
966
967     nth = gmx_omp_nthreads_get(emntPairsearch);
968
969 #pragma omp parallel for num_threads(nth) schedule(static)
970     for (th = 0; th < nth; th++)
971     {
972         int g;
973
974         for (g = g0; g < g1; g++)
975         {
976             const nbnxn_grid_t *grid;
977             int                 cxy0, cxy1, cxy;
978
979             grid = &nbs->grid[g];
980
981             cxy0 = (grid->ncx*grid->ncy* th   +nth-1)/nth;
982             cxy1 = (grid->ncx*grid->ncy*(th+1)+nth-1)/nth;
983
984             for (cxy = cxy0; cxy < cxy1; cxy++)
985             {
986                 int na, ash, na_fill;
987
988                 na  = grid->cxy_na[cxy];
989                 ash = (grid->cell0 + grid->cxy_ind[cxy])*grid->na_sc;
990
991                 if (g == 0 && FillLocal)
992                 {
993                     na_fill =
994                         (grid->cxy_ind[cxy+1] - grid->cxy_ind[cxy])*grid->na_sc;
995                 }
996                 else
997                 {
998                     /* We fill only the real particle locations.
999                      * We assume the filling entries at the end have been
1000                      * properly set before during ns.
1001                      */
1002                     na_fill = na;
1003                 }
1004                 copy_rvec_to_nbat_real(nbs->a+ash, na, na_fill, x,
1005                                        nbat->XFormat, nbat->x, ash,
1006                                        0, 0, 0);
1007             }
1008         }
1009     }
1010 }
1011
1012 static void
1013 nbnxn_atomdata_clear_reals(real * gmx_restrict dest,
1014                            int i0, int i1)
1015 {
1016     int i;
1017
1018     for (i = i0; i < i1; i++)
1019     {
1020         dest[i] = 0;
1021     }
1022 }
1023
1024 static void
1025 nbnxn_atomdata_reduce_reals(real * gmx_restrict dest,
1026                             gmx_bool bDestSet,
1027                             real ** gmx_restrict src,
1028                             int nsrc,
1029                             int i0, int i1)
1030 {
1031     int i, s;
1032
1033     if (bDestSet)
1034     {
1035         /* The destination buffer contains data, add to it */
1036         for (i = i0; i < i1; i++)
1037         {
1038             for (s = 0; s < nsrc; s++)
1039             {
1040                 dest[i] += src[s][i];
1041             }
1042         }
1043     }
1044     else
1045     {
1046         /* The destination buffer is unitialized, set it first */
1047         for (i = i0; i < i1; i++)
1048         {
1049             dest[i] = src[0][i];
1050             for (s = 1; s < nsrc; s++)
1051             {
1052                 dest[i] += src[s][i];
1053             }
1054         }
1055     }
1056 }
1057
1058 static void
1059 nbnxn_atomdata_reduce_reals_simd(real * gmx_restrict dest,
1060                                  gmx_bool bDestSet,
1061                                  real ** gmx_restrict src,
1062                                  int nsrc,
1063                                  int i0, int i1)
1064 {
1065 #ifdef GMX_NBNXN_SIMD
1066 /* The SIMD width here is actually independent of that in the kernels,
1067  * but we use the same width for simplicity (usually optimal anyhow).
1068  */
1069 #if GMX_NBNXN_SIMD_BITWIDTH == 128
1070 #define GMX_MM128_HERE
1071 #endif
1072 #if GMX_NBNXN_SIMD_BITWIDTH == 256
1073 #define GMX_MM256_HERE
1074 #endif
1075 #include "gmx_simd_macros.h"
1076
1077     int       i, s;
1078     gmx_mm_pr dest_SSE, src_SSE;
1079
1080     if (bDestSet)
1081     {
1082         for (i = i0; i < i1; i += GMX_SIMD_WIDTH_HERE)
1083         {
1084             dest_SSE = gmx_load_pr(dest+i);
1085             for (s = 0; s < nsrc; s++)
1086             {
1087                 src_SSE  = gmx_load_pr(src[s]+i);
1088                 dest_SSE = gmx_add_pr(dest_SSE, src_SSE);
1089             }
1090             gmx_store_pr(dest+i, dest_SSE);
1091         }
1092     }
1093     else
1094     {
1095         for (i = i0; i < i1; i += GMX_SIMD_WIDTH_HERE)
1096         {
1097             dest_SSE = gmx_load_pr(src[0]+i);
1098             for (s = 1; s < nsrc; s++)
1099             {
1100                 src_SSE  = gmx_load_pr(src[s]+i);
1101                 dest_SSE = gmx_add_pr(dest_SSE, src_SSE);
1102             }
1103             gmx_store_pr(dest+i, dest_SSE);
1104         }
1105     }
1106
1107 #undef GMX_MM128_HERE
1108 #undef GMX_MM256_HERE
1109 #endif
1110 }
1111
1112 /* Add part of the force array(s) from nbnxn_atomdata_t to f */
1113 static void
1114 nbnxn_atomdata_add_nbat_f_to_f_part(const nbnxn_search_t nbs,
1115                                     const nbnxn_atomdata_t *nbat,
1116                                     nbnxn_atomdata_output_t *out,
1117                                     int nfa,
1118                                     int a0, int a1,
1119                                     rvec *f)
1120 {
1121     int         a, i, fa;
1122     const int  *cell;
1123     const real *fnb;
1124
1125     cell = nbs->cell;
1126
1127     /* Loop over all columns and copy and fill */
1128     switch (nbat->FFormat)
1129     {
1130         case nbatXYZ:
1131         case nbatXYZQ:
1132             if (nfa == 1)
1133             {
1134                 fnb = out[0].f;
1135
1136                 for (a = a0; a < a1; a++)
1137                 {
1138                     i = cell[a]*nbat->fstride;
1139
1140                     f[a][XX] += fnb[i];
1141                     f[a][YY] += fnb[i+1];
1142                     f[a][ZZ] += fnb[i+2];
1143                 }
1144             }
1145             else
1146             {
1147                 for (a = a0; a < a1; a++)
1148                 {
1149                     i = cell[a]*nbat->fstride;
1150
1151                     for (fa = 0; fa < nfa; fa++)
1152                     {
1153                         f[a][XX] += out[fa].f[i];
1154                         f[a][YY] += out[fa].f[i+1];
1155                         f[a][ZZ] += out[fa].f[i+2];
1156                     }
1157                 }
1158             }
1159             break;
1160         case nbatX4:
1161             if (nfa == 1)
1162             {
1163                 fnb = out[0].f;
1164
1165                 for (a = a0; a < a1; a++)
1166                 {
1167                     i = X4_IND_A(cell[a]);
1168
1169                     f[a][XX] += fnb[i+XX*PACK_X4];
1170                     f[a][YY] += fnb[i+YY*PACK_X4];
1171                     f[a][ZZ] += fnb[i+ZZ*PACK_X4];
1172                 }
1173             }
1174             else
1175             {
1176                 for (a = a0; a < a1; a++)
1177                 {
1178                     i = X4_IND_A(cell[a]);
1179
1180                     for (fa = 0; fa < nfa; fa++)
1181                     {
1182                         f[a][XX] += out[fa].f[i+XX*PACK_X4];
1183                         f[a][YY] += out[fa].f[i+YY*PACK_X4];
1184                         f[a][ZZ] += out[fa].f[i+ZZ*PACK_X4];
1185                     }
1186                 }
1187             }
1188             break;
1189         case nbatX8:
1190             if (nfa == 1)
1191             {
1192                 fnb = out[0].f;
1193
1194                 for (a = a0; a < a1; a++)
1195                 {
1196                     i = X8_IND_A(cell[a]);
1197
1198                     f[a][XX] += fnb[i+XX*PACK_X8];
1199                     f[a][YY] += fnb[i+YY*PACK_X8];
1200                     f[a][ZZ] += fnb[i+ZZ*PACK_X8];
1201                 }
1202             }
1203             else
1204             {
1205                 for (a = a0; a < a1; a++)
1206                 {
1207                     i = X8_IND_A(cell[a]);
1208
1209                     for (fa = 0; fa < nfa; fa++)
1210                     {
1211                         f[a][XX] += out[fa].f[i+XX*PACK_X8];
1212                         f[a][YY] += out[fa].f[i+YY*PACK_X8];
1213                         f[a][ZZ] += out[fa].f[i+ZZ*PACK_X8];
1214                     }
1215                 }
1216             }
1217             break;
1218     default:
1219         gmx_incons("Unsupported nbnxn_atomdata_t format");
1220     }
1221 }
1222
1223 /* Add the force array(s) from nbnxn_atomdata_t to f */
1224 void nbnxn_atomdata_add_nbat_f_to_f(const nbnxn_search_t    nbs,
1225                                     int                     locality,
1226                                     const nbnxn_atomdata_t *nbat,
1227                                     rvec                   *f)
1228 {
1229     int a0 = 0, na = 0;
1230     int nth, th;
1231
1232     nbs_cycle_start(&nbs->cc[enbsCCreducef]);
1233
1234     switch (locality)
1235     {
1236         case eatAll:
1237             a0 = 0;
1238             na = nbs->natoms_nonlocal;
1239             break;
1240         case eatLocal:
1241             a0 = 0;
1242             na = nbs->natoms_local;
1243             break;
1244         case eatNonlocal:
1245             a0 = nbs->natoms_local;
1246             na = nbs->natoms_nonlocal - nbs->natoms_local;
1247             break;
1248     }
1249
1250     nth = gmx_omp_nthreads_get(emntNonbonded);
1251
1252     if (nbat->nout > 1)
1253     {
1254         if (locality != eatAll)
1255         {
1256             gmx_incons("add_f_to_f called with nout>1 and locality!=eatAll");
1257         }
1258
1259         /* Reduce the force thread output buffers into buffer 0, before adding
1260          * them to the, differently ordered, "real" force buffer.
1261          */
1262 #pragma omp parallel for num_threads(nth) schedule(static)
1263         for (th = 0; th < nth; th++)
1264         {
1265             const nbnxn_buffer_flags_t *flags;
1266             int   b0, b1, b;
1267             int   i0, i1;
1268             int   nfptr;
1269             real *fptr[NBNXN_BUFFERFLAG_MAX_THREADS];
1270             int   out;
1271
1272             flags = &nbat->buffer_flags;
1273
1274             /* Calculate the cell-block range for our thread */
1275             b0 = (flags->nflag* th   )/nth;
1276             b1 = (flags->nflag*(th+1))/nth;
1277
1278             for (b = b0; b < b1; b++)
1279             {
1280                 i0 =  b   *NBNXN_BUFFERFLAG_SIZE*nbat->fstride;
1281                 i1 = (b+1)*NBNXN_BUFFERFLAG_SIZE*nbat->fstride;
1282
1283                 nfptr = 0;
1284                 for (out = 1; out < nbat->nout; out++)
1285                 {
1286                     if (flags->flag[b] & (1U<<out))
1287                     {
1288                         fptr[nfptr++] = nbat->out[out].f;
1289                     }
1290                 }
1291                 if (nfptr > 0)
1292                 {
1293 #ifdef GMX_NBNXN_SIMD
1294                     nbnxn_atomdata_reduce_reals_simd
1295 #else
1296                     nbnxn_atomdata_reduce_reals
1297 #endif
1298                         (nbat->out[0].f,
1299                         flags->flag[b] & (1U<<0),
1300                         fptr, nfptr,
1301                         i0, i1);
1302                 }
1303                 else if (!(flags->flag[b] & (1U<<0)))
1304                 {
1305                     nbnxn_atomdata_clear_reals(nbat->out[0].f,
1306                                                i0, i1);
1307                 }
1308             }
1309         }
1310     }
1311
1312 #pragma omp parallel for num_threads(nth) schedule(static)
1313     for (th = 0; th < nth; th++)
1314     {
1315         nbnxn_atomdata_add_nbat_f_to_f_part(nbs, nbat,
1316                                             nbat->out,
1317                                             1,
1318                                             a0+((th+0)*na)/nth,
1319                                             a0+((th+1)*na)/nth,
1320                                             f);
1321     }
1322
1323     nbs_cycle_stop(&nbs->cc[enbsCCreducef]);
1324 }
1325
1326 /* Adds the shift forces from nbnxn_atomdata_t to fshift */
1327 void nbnxn_atomdata_add_nbat_fshift_to_fshift(const nbnxn_atomdata_t *nbat,
1328                                               rvec                   *fshift)
1329 {
1330     const nbnxn_atomdata_output_t *out;
1331     int  th;
1332     int  s;
1333     rvec sum;
1334
1335     out = nbat->out;
1336
1337     for (s = 0; s < SHIFTS; s++)
1338     {
1339         clear_rvec(sum);
1340         for (th = 0; th < nbat->nout; th++)
1341         {
1342             sum[XX] += out[th].fshift[s*DIM+XX];
1343             sum[YY] += out[th].fshift[s*DIM+YY];
1344             sum[ZZ] += out[th].fshift[s*DIM+ZZ];
1345         }
1346         rvec_inc(fshift[s], sum);
1347     }
1348 }