16512941c89da4e9464e08de56c7d93460d42a28
[alexxy/gromacs.git] / include / types / nbnxn_pairlist.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2012, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012,2013, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38
39 #ifndef _nbnxn_pairlist_h
40 #define _nbnxn_pairlist_h
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 /* A buffer data structure of 64 bytes
47  * to be placed at the beginning and end of structs
48  * to avoid cache invalidation of the real contents
49  * of the struct by writes to neighboring memory.
50  */
51 typedef struct {
52     int dummy[16];
53 } gmx_cache_protect_t;
54
55 /* Abstract type for pair searching data */
56 typedef struct nbnxn_search * nbnxn_search_t;
57
58 /* Function that should return a pointer *ptr to memory
59  * of size nbytes.
60  * Error handling should be done within this function.
61  */
62 typedef void nbnxn_alloc_t (void **ptr, size_t nbytes);
63
64 /* Function that should free the memory pointed to by *ptr.
65  * NULL should not be passed to this function.
66  */
67 typedef void nbnxn_free_t (void *ptr);
68
69 /* This is the actual cluster-pair list j-entry.
70  * cj is the j-cluster.
71  * The interaction bits in excl are indexed i-major, j-minor.
72  * The cj entries are sorted such that ones with exclusions come first.
73  * This means that once a full mask (=NBNXN_INTERACTION_MASK_ALL)
74  * is found, all subsequent j-entries in the i-entry also have full masks.
75  */
76 typedef struct {
77     int      cj;    /* The j-cluster                             */
78     unsigned excl;  /* The topology exclusion (interaction) bits */
79 } nbnxn_cj_t;
80
81 /* In nbnxn_ci_t the integer shift contains the shift in the lower 7 bits.
82  * The upper bits contain information for non-bonded kernel optimization.
83  * Simply calculating LJ and Coulomb for all pairs in a cluster pair is fine.
84  * But three flags can be used to skip interactions, currently only for subc=0
85  * !(shift & NBNXN_CI_DO_LJ(subc))   => we can skip LJ for all pairs
86  * shift & NBNXN_CI_HALF_LJ(subc)    => we can skip LJ for the second half of i
87  * !(shift & NBNXN_CI_DO_COUL(subc)) => we can skip Coulomb for all pairs
88  */
89 #define NBNXN_CI_SHIFT          127
90 #define NBNXN_CI_DO_LJ(subc)    (1<<(7+3*(subc)))
91 #define NBNXN_CI_HALF_LJ(subc)  (1<<(8+3*(subc)))
92 #define NBNXN_CI_DO_COUL(subc)  (1<<(9+3*(subc)))
93
94 /* Simple pair-list i-unit */
95 typedef struct {
96     int ci;             /* i-cluster             */
97     int shift;          /* Shift vector index plus possible flags, see above */
98     int cj_ind_start;   /* Start index into cj   */
99     int cj_ind_end;     /* End index into cj     */
100 } nbnxn_ci_t;
101
102 /* Grouped pair-list i-unit */
103 typedef struct {
104     int sci;            /* i-super-cluster       */
105     int shift;          /* Shift vector index plus possible flags */
106     int cj4_ind_start;  /* Start index into cj4  */
107     int cj4_ind_end;    /* End index into cj4    */
108 } nbnxn_sci_t;
109
110 typedef struct {
111     unsigned imask;        /* The i-cluster interactions mask for 1 warp  */
112     int      excl_ind;     /* Index into the exclusion array for 1 warp   */
113 } nbnxn_im_ei_t;
114
115 typedef struct {
116     int           cj[4];   /* The 4 j-clusters                            */
117     nbnxn_im_ei_t imei[2]; /* The i-cluster mask data       for 2 warps   */
118 } nbnxn_cj4_t;
119
120 typedef struct {
121     unsigned pair[32];     /* Topology exclusion interaction bits for one warp,
122                             * each unsigned has bitS for 4*8 i clusters
123                             */
124 } nbnxn_excl_t;
125
126 typedef struct {
127     gmx_cache_protect_t cp0;
128
129     nbnxn_alloc_t      *alloc;
130     nbnxn_free_t       *free;
131
132     gmx_bool            bSimple;         /* Simple list has na_sc=na_s and uses cj   *
133                                           * Complex list uses cj4                    */
134
135     int                     na_ci;       /* The number of atoms per i-cluster        */
136     int                     na_cj;       /* The number of atoms per j-cluster        */
137     int                     na_sc;       /* The number of atoms per super cluster    */
138     real                    rlist;       /* The radius for constructing the list     */
139     int                     nci;         /* The number of i-clusters in the list     */
140     nbnxn_ci_t             *ci;          /* The i-cluster list, size nci             */
141     int                     ci_nalloc;   /* The allocation size of ci                */
142     int                     nsci;        /* The number of i-super-clusters in the list */
143     nbnxn_sci_t            *sci;         /* The i-super-cluster list                 */
144     int                     sci_nalloc;  /* The allocation size of sci               */
145
146     int                     ncj;         /* The number of j-clusters in the list     */
147     nbnxn_cj_t             *cj;          /* The j-cluster list, size ncj             */
148     int                     cj_nalloc;   /* The allocation size of cj                */
149
150     int                     ncj4;        /* The total number of 4*j clusters         */
151     nbnxn_cj4_t            *cj4;         /* The 4*j cluster list, size ncj4          */
152     int                     cj4_nalloc;  /* The allocation size of cj4               */
153     int                     nexcl;       /* The count for excl                       */
154     nbnxn_excl_t           *excl;        /* Atom interaction bits (non-exclusions)   */
155     int                     excl_nalloc; /* The allocation size for excl             */
156     int                     nci_tot;     /* The total number of i clusters           */
157
158     struct nbnxn_list_work *work;
159
160     gmx_cache_protect_t     cp1;
161 } nbnxn_pairlist_t;
162
163 typedef struct {
164     int                nnbl;        /* number of lists */
165     nbnxn_pairlist_t **nbl;         /* lists */
166     gmx_bool           bCombined;   /* TRUE if lists get combined into one (the 1st) */
167     gmx_bool           bSimple;     /* TRUE if the list of of type "simple"
168                                        (na_sc=na_s, no super-clusters used) */
169     int                natpair_ljq; /* Total number of atom pairs for LJ+Q kernel */
170     int                natpair_lj;  /* Total number of atom pairs for LJ kernel   */
171     int                natpair_q;   /* Total number of atom pairs for Q kernel    */
172 } nbnxn_pairlist_set_t;
173
174 enum {
175     nbatXYZ, nbatXYZQ, nbatX4, nbatX8
176 };
177
178 typedef struct {
179     real *f;      /* f, size natoms*fstride                             */
180     real *fshift; /* Shift force array, size SHIFTS*DIM                 */
181     int   nV;     /* The size of *Vvdw and *Vc                          */
182     real *Vvdw;   /* Temporary Van der Waals group energy storage       */
183     real *Vc;     /* Temporary Coulomb group energy storage             */
184     int   nVS;    /* The size of *VSvdw and *VSc                        */
185     real *VSvdw;  /* Temporary SIMD Van der Waals group energy storage  */
186     real *VSc;    /* Temporary SIMD Coulomb group energy storage        */
187 } nbnxn_atomdata_output_t;
188
189 /* Block size in atoms for the non-bonded thread force-buffer reduction,
190  * should be a multiple of all cell and x86 SIMD sizes (i.e. 2, 4 and 8).
191  * Should be small to reduce the reduction and zeroing cost,
192  * but too small will result in overhead.
193  * Currently the block size is NBNXN_BUFFERFLAG_SIZE*3*sizeof(real)=192 bytes.
194  */
195 #ifdef GMX_DOUBLE
196 #define NBNXN_BUFFERFLAG_SIZE   8
197 #else
198 #define NBNXN_BUFFERFLAG_SIZE  16
199 #endif
200
201 /* We currently store the reduction flags as bits in an unsigned int.
202  * In most cases this limits the number of flags to 32.
203  * The reduction will automatically disable the flagging and do a full
204  * reduction when the flags won't fit, but this will lead to very slow
205  * reduction. As we anyhow don't expect reasonable performance with
206  * more than 32 threads, we put in this hard limit.
207  * You can increase this number, but the reduction will be very slow.
208  */
209 #define NBNXN_BUFFERFLAG_MAX_THREADS  32
210
211 /* Flags for telling if threads write to force output buffers */
212 typedef struct {
213     int       nflag;       /* The number of flag blocks                         */
214     unsigned *flag;        /* Bit i is set when thread i writes to a cell-block */
215     int       flag_nalloc; /* Allocation size of cxy_flag                       */
216 } nbnxn_buffer_flags_t;
217
218 /* LJ combination rules: geometric, Lorentz-Berthelot, none */
219 enum {
220     ljcrGEOM, ljcrLB, ljcrNONE, ljcrNR
221 };
222
223 typedef struct {
224     nbnxn_alloc_t           *alloc;
225     nbnxn_free_t            *free;
226     int                      ntype;           /* The number of different atom types                 */
227     real                    *nbfp;            /* Lennard-Jones 6*C6 and 12*C12 params, size ntype^2*2 */
228     int                      comb_rule;       /* Combination rule, see enum above                   */
229     real                    *nbfp_comb;       /* LJ parameter per atom type, size ntype*2           */
230     real                    *nbfp_s4;         /* As nbfp, but with stride 4, size ntype^2*4. This
231                                                * might suit 4-wide SIMD loads of two values (e.g.
232                                                * two floats in single precision on x86).            */
233     int                      natoms;          /* Number of atoms                                    */
234     int                      natoms_local;    /* Number of local atoms                           */
235     int                     *type;            /* Atom types                                         */
236     real                    *lj_comb;         /* LJ parameters per atom for combining for pairs     */
237     int                      XFormat;         /* The format of x (and q), enum                      */
238     int                      FFormat;         /* The format of f, enum                              */
239     real                    *q;               /* Charges, can be NULL if incorporated in x          */
240     int                      na_c;            /* The number of atoms per cluster                    */
241     int                      nenergrp;        /* The number of energy groups                        */
242     int                      neg_2log;        /* Log2 of nenergrp                                   */
243     int                     *energrp;         /* The energy groups per cluster, can be NULL         */
244     gmx_bool                 bDynamicBox;     /* Do we need to update shift_vec every step?    */
245     rvec                    *shift_vec;       /* Shift vectors, copied from t_forcerec              */
246     int                      xstride;         /* stride for a coordinate in x (usually 3 or 4)      */
247     int                      fstride;         /* stride for a coordinate in f (usually 3 or 4)      */
248     real                    *x;               /* x and possibly q, size natoms*xstride              */
249
250     /* j-atom minus i-atom index for generating self and Newton exclusions
251      * cluster-cluster pairs of the diagonal, for 4xn and 2xnn kernels.
252      */
253     real                    *simd_4xn_diagonal_j_minus_i;
254     real                    *simd_2xnn_diagonal_j_minus_i;
255     /* Filters for topology exclusion masks for the SIMD kernels.
256      * filter2 is the same as filter1, but with each element duplicated.
257      */
258     unsigned                *simd_exclusion_filter1;
259     unsigned                *simd_exclusion_filter2;
260
261     int                      nout;            /* The number of force arrays                         */
262     nbnxn_atomdata_output_t *out;             /* Output data structures               */
263     int                      nalloc;          /* Allocation size of all arrays (for x/f *x/fstride) */
264     gmx_bool                 bUseBufferFlags; /* Use the flags or operate on all atoms     */
265     nbnxn_buffer_flags_t     buffer_flags;    /* Flags for buffer zeroing+reduc.  */
266 } nbnxn_atomdata_t;
267
268 #ifdef __cplusplus
269 }
270 #endif
271
272 #endif