Merge origin/release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_internal.h
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustr
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  * And Hey:
33  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
34  */
35
36 #ifndef _nbnxn_internal_h
37 #define _nsnxn_internal_h
38
39 #include "typedefs.h"
40 #include "domdec.h"
41 #include "gmx_cyclecounter.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 #ifdef GMX_X86_SSE2
49 #define NBNXN_SEARCH_SSE
50 #endif
51
52
53 /* A pair-search grid struct for one domain decomposition zone */
54 typedef struct {
55     rvec c0;             /* The lower corner of the (local) grid        */
56     rvec c1;             /* The upper corner of the (local) grid        */
57     real atom_density;   /* The atom number density for the local grid  */
58
59     gmx_bool bSimple;    /* Is this grid simple or super/sub            */
60     int  na_c;           /* Number of atoms per cluster                 */
61     int  na_cj;          /* Number of atoms for list j-clusters         */
62     int  na_sc;          /* Number of atoms per super-cluster           */
63     int  na_c_2log;      /* 2log of na_c                                */
64
65     int  ncx;            /* Number of (super-)cells along x             */
66     int  ncy;            /* Number of (super-)cells along y             */
67     int  nc;             /* Total number of (super-)cells               */
68
69     real sx;             /* x-size of a (super-)cell                    */
70     real sy;             /* y-size of a (super-)cell                    */
71     real inv_sx;         /* 1/sx                                        */
72     real inv_sy;         /* 1/sy                                        */
73
74     int  cell0;          /* Index in nbs->cell corresponding to cell 0  */
75
76     int  *cxy_na;        /* The number of atoms for each column in x,y  */
77     int  *cxy_ind;       /* Grid (super)cell index, offset from cell0   */
78     int  cxy_nalloc;     /* Allocation size for cxy_na and cxy_ind      */
79
80     int   *nsubc;        /* The number of sub cells for each super cell */
81     float *bbcz;         /* Bounding boxes in z for the super cells     */
82     float *bb;           /* 3D bounding boxes for the sub cells         */
83     float *bbj;          /* 3D j-b.boxes for SSE-double or AVX-single   */
84     int   *flags;        /* Flag for the super cells                    */
85     int   nc_nalloc;     /* Allocation size for the pointers above      */
86
87     float *bbcz_simple;  /* bbcz for simple grid converted from super   */
88     float *bb_simple;    /* bb for simple grid converted from super     */
89     int   *flags_simple; /* flags for simple grid converted from super  */
90     int   nc_nalloc_simple; /* Allocation size for the pointers above   */
91
92     int  nsubc_tot;      /* Total number of subcell, used for printing  */
93 } nbnxn_grid_t;
94
95 #ifdef NBNXN_SEARCH_SSE
96 #define GMX_MM128_HERE
97 #include "gmx_x86_simd_macros.h"
98 typedef struct nbnxn_x_ci_x86_simd128 {
99     /* The i-cluster coordinates for simple search */
100     gmx_mm_pr ix_SSE0,iy_SSE0,iz_SSE0;
101     gmx_mm_pr ix_SSE1,iy_SSE1,iz_SSE1;
102     gmx_mm_pr ix_SSE2,iy_SSE2,iz_SSE2;
103     gmx_mm_pr ix_SSE3,iy_SSE3,iz_SSE3;
104 } nbnxn_x_ci_x86_simd128_t;
105 #undef GMX_MM128_HERE
106 #ifdef GMX_X86_AVX_256
107 #define GMX_MM256_HERE
108 #include "gmx_x86_simd_macros.h"
109 typedef struct nbnxn_x_ci_x86_simd256 {
110     /* The i-cluster coordinates for simple search */
111     gmx_mm_pr ix_SSE0,iy_SSE0,iz_SSE0;
112     gmx_mm_pr ix_SSE1,iy_SSE1,iz_SSE1;
113     gmx_mm_pr ix_SSE2,iy_SSE2,iz_SSE2;
114     gmx_mm_pr ix_SSE3,iy_SSE3,iz_SSE3;
115 } nbnxn_x_ci_x86_simd256_t;
116 #undef GMX_MM256_HERE
117 #endif
118 #endif
119
120 /* Working data for the actual i-supercell during pair search */
121 typedef struct nbnxn_list_work {
122     gmx_cache_protect_t cp0; /* Protect cache between threads               */
123
124     float *bb_ci;      /* The bounding boxes, pbc shifted, for each cluster */
125     real  *x_ci;       /* The coordinates, pbc shifted, for each atom       */
126 #ifdef NBNXN_SEARCH_SSE
127     nbnxn_x_ci_x86_simd128_t *x_ci_x86_simd128;
128 #ifdef GMX_X86_AVX_256
129     nbnxn_x_ci_x86_simd256_t *x_ci_x86_simd256;
130 #endif
131 #endif
132     int  cj_ind;       /* The current cj_ind index for the current list     */
133     int  cj4_init;     /* The first unitialized cj4 block                   */
134
135     float *d2;         /* Bounding box distance work array                  */
136
137     nbnxn_cj_t *cj;    /* The j-cell list                                   */
138     int  cj_nalloc;    /* Allocation size of cj                             */
139
140     int ncj_noq;       /* Nr. of cluster pairs without Coul for flop count  */
141     int ncj_hlj;       /* Nr. of cluster pairs with 1/2 LJ for flop count   */
142
143     gmx_cache_protect_t cp1; /* Protect cache between threads               */
144 } nbnxn_list_work_t;
145
146 /* Function type for setting the i-atom coordinate working data */
147 typedef void
148 gmx_icell_set_x_t(int ci,
149                   real shx,real shy,real shz,
150                   int na_c,
151                   int stride,const real *x,
152                   nbnxn_list_work_t *work);
153
154 static gmx_icell_set_x_t icell_set_x_simple;
155 #ifdef NBNXN_SEARCH_SSE
156 static gmx_icell_set_x_t icell_set_x_simple_x86_simd128;
157 #ifdef GMX_X86_AVX_256
158 static gmx_icell_set_x_t icell_set_x_simple_x86_simd256;
159 #endif
160 #endif
161 static gmx_icell_set_x_t icell_set_x_supersub;
162 #ifdef NBNXN_SEARCH_SSE
163 static gmx_icell_set_x_t icell_set_x_supersub_sse8;
164 #endif
165
166 /* Local cycle count struct for profiling */
167 typedef struct {
168     int          count;
169     gmx_cycles_t c;
170     gmx_cycles_t start;
171 } nbnxn_cycle_t;
172
173 /* Local cycle count enum for profiling */
174 enum { enbsCCgrid, enbsCCsearch, enbsCCcombine, enbsCCreducef, enbsCCnr };
175
176 /* Thread-local work struct, contains part of nbnxn_grid_t */
177 typedef struct {
178     gmx_cache_protect_t cp0;
179
180     int *cxy_na;
181     int cxy_na_nalloc;
182
183     int  *sort_work;
184     int  sort_work_nalloc;
185
186     int  ndistc;         /* Number of distance checks for flop counting */
187
188     nbnxn_cycle_t cc[enbsCCnr];
189
190     gmx_cache_protect_t cp1;
191 } nbnxn_search_work_t;
192
193 /* Main pair-search struct, contains the grid(s), not the pair-list(s) */
194 typedef struct nbnxn_search {
195     int  ePBC;            /* PBC type enum                              */
196     matrix box;           /* The periodic unit-cell                     */
197
198     gmx_bool DomDec;      /* Are we doing domain decomposition?         */
199     ivec dd_dim;          /* Are we doing DD in x,y,z?                  */
200     gmx_domdec_zones_t *zones; /* The domain decomposition zones        */
201
202     int  ngrid;           /* The number of grids, equal to #DD-zones    */
203     nbnxn_grid_t *grid;   /* Array of grids, size ngrid                 */
204     int  *cell;           /* Actual allocated cell array for all grids  */
205     int  cell_nalloc;     /* Allocation size of cell                    */
206     int  *a;              /* Atom index for grid, the inverse of cell   */
207     int  a_nalloc;        /* Allocation size of a                       */
208
209     int  natoms_local;    /* The local atoms run from 0 to natoms_local */
210     int  natoms_nonlocal; /* The non-local atoms run from natoms_local
211                            * to natoms_nonlocal */
212
213     gmx_bool print_cycles;
214     int      search_count;
215     nbnxn_cycle_t cc[enbsCCnr];
216
217     gmx_icell_set_x_t *icell_set_x; /* Function for setting i-coords    */
218
219     int  nthread_max;     /* Maximum number of threads for pair-search  */
220     nbnxn_search_work_t *work; /* Work array, size nthread_max          */
221 } nbnxn_search_t_t;
222
223
224 static void nbs_cycle_start(nbnxn_cycle_t *cc)
225 {
226     cc->start = gmx_cycles_read();
227 }
228
229 static void nbs_cycle_stop(nbnxn_cycle_t *cc)
230 {
231     cc->c += gmx_cycles_read() - cc->start;
232     cc->count++;
233 }
234
235
236 #ifdef __cplusplus
237 }
238 #endif
239
240 #endif