CUDA PME kernels with analytical Ewald correction
[alexxy/gromacs.git] / src / mdlib / nbnxn_cuda / nbnxn_cuda_types.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_CUDA_TYPES_H
40 #define NBNXN_CUDA_TYPES_H
41
42 #include "types/nbnxn_pairlist.h"
43 #include "types/nbnxn_cuda_types_ext.h"
44 #include "../../gmxlib/cuda_tools/cudautils.cuh"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /*! Types of electrostatics implementations available in the CUDA non-bonded
51  *  force kernels. These represent both the electrostatics types implemented
52  *  by the kernels (cut-off, RF, and Ewald - a subset of what's defined in
53  *  enums.h) as well as encode implementation details analytical/tabulated
54  *  and single or twin cut-off (for Ewald kernels).
55  *  Note that the cut-off and RF kernels have only analytical flavor and unlike
56  *  in the CPU kernels, the tabulated kernels are ATM Ewald-only.
57  *
58  *  The order of pointers to different electrostatic kernels defined in
59  *  nbnxn_cuda.cu by the nb_default_kfunc_ptr and nb_legacy_kfunc_ptr arrays
60  *  should match the order of enumerated types below. */
61 enum {
62     eelCuCUT, eelCuRF, eelCuEWALD_TAB, eelCuEWALD_TAB_TWIN, eelCuEWALD_ANA, eelCuEWALD_ANA_TWIN, eelCuNR
63 };
64
65 /*! Kernel flavors with different set of optimizations: default for CUDA <=v4.1
66  *  compilers and legacy for earlier, 3.2 and 4.0 CUDA compilers. */
67 enum {
68     eNbnxnCuKDefault, eNbnxnCuKLegacy, eNbnxnCuKNR
69 };
70
71 #define NBNXN_KVER_OLD(k)      (k == eNbnxnCuKOld)
72 #define NBNXN_KVER_LEGACY(k)   (k == eNbnxnCuKLegacy)
73 #define NBNXN_KVER_DEFAULT(k)  (k == eNbnxnCuKDefault)
74
75 /*! Non-bonded kernel versions. */
76
77 /* All structs prefixed with "cu_" hold data used in GPU calculations and
78  * are passed to the kernels, except cu_timers_t. */
79 typedef struct cu_plist     cu_plist_t;
80 typedef struct cu_atomdata  cu_atomdata_t;
81 typedef struct cu_nbparam   cu_nbparam_t;
82 typedef struct cu_timers    cu_timers_t;
83 typedef struct nb_staging   nb_staging_t;
84
85
86 /*! Staging area for temporary data. The energies get downloaded here first,
87  *  before getting added to the CPU-side aggregate values.
88  */
89 struct nb_staging
90 {
91     float   *e_lj;      /* LJ energy            */
92     float   *e_el;      /* electrostatic energy */
93     float3  *fshift;    /* shift forces         */
94 };
95
96 /*! Nonbonded atom data -- both inputs and outputs. */
97 struct cu_atomdata
98 {
99     int      natoms;            /* number of atoms                              */
100     int      natoms_local;      /* number of local atoms                        */
101     int      nalloc;            /* allocation size for the atom data (xq, f)    */
102
103     float4  *xq;                /* atom coordinates + charges, size natoms      */
104     float3  *f;                 /* force output array, size natoms              */
105     /* TODO: try float2 for the energies */
106     float   *e_lj,              /* LJ energy output, size 1                     */
107             *e_el;              /* Electrostatics energy input, size 1          */
108
109     float3  *fshift;            /* shift forces                                 */
110
111     int      ntypes;            /* number of atom types                         */
112     int     *atom_types;        /* atom type indices, size natoms               */
113
114     float3  *shift_vec;         /* shifts                                       */
115     bool     bShiftVecUploaded; /* true if the shift vector has been uploaded   */
116 };
117
118 /*! Parameters required for the CUDA nonbonded calculations. */
119 struct cu_nbparam
120 {
121     int      eeltype;        /* type of electrostatics                       */
122
123     float    epsfac;         /* charge multiplication factor                 */
124     float    c_rf, two_k_rf; /* Reaction-Field constants                     */
125     float    ewald_beta;     /* Ewald/PME parameter                          */
126     float    sh_ewald;       /* Ewald/PME  correction term                   */
127     float    rvdw_sq;        /* VdW cut-off                                  */
128     float    rcoulomb_sq;    /* Coulomb cut-off                              */
129     float    rlist_sq;       /* pair-list cut-off                            */
130     float    sh_invrc6;      /* LJ potential correction term                 */
131
132     float   *nbfp;           /* nonbonded parameter table with C6/C12 pairs  */
133
134     /* Ewald Coulomb force table */
135     int      coulomb_tab_size;
136     float    coulomb_tab_scale;
137     float   *coulomb_tab;
138 };
139
140 /*! Pair list data */
141 struct cu_plist
142 {
143     int              na_c;        /* number of atoms per cluster                  */
144
145     int              nsci;        /* size of sci, # of i clusters in the list     */
146     int              sci_nalloc;  /* allocation size of sci                       */
147     nbnxn_sci_t     *sci;         /* list of i-cluster ("super-clusters")         */
148
149     int              ncj4;        /* total # of 4*j clusters                      */
150     int              cj4_nalloc;  /* allocation size of cj4                       */
151     nbnxn_cj4_t     *cj4;         /* 4*j cluster list, contains j cluster number
152                                      and index into the i cluster list            */
153     nbnxn_excl_t    *excl;        /* atom interaction bits                        */
154     int              nexcl;       /* count for excl                               */
155     int              excl_nalloc; /* allocation size of excl                      */
156
157     bool             bDoPrune;    /* true if pair-list pruning needs to be
158                                      done during the  current step                */
159 };
160
161 /* CUDA events used for timing GPU kernels and H2D/D2H transfers.
162  * The two-sized arrays hold the local and non-local values and should always
163  * be indexed with eintLocal/eintNonlocal.
164  */
165 struct cu_timers
166 {
167     cudaEvent_t start_atdat, stop_atdat;         /* atom data transfer (every PS step)      */
168     cudaEvent_t start_nb_h2d[2], stop_nb_h2d[2]; /* x/q H2D transfer (every step)           */
169     cudaEvent_t start_nb_d2h[2], stop_nb_d2h[2]; /* f D2H transfer (every step)             */
170     cudaEvent_t start_pl_h2d[2], stop_pl_h2d[2]; /* pair-list H2D transfer (every PS step)  */
171     cudaEvent_t start_nb_k[2], stop_nb_k[2];     /* non-bonded kernels (every step)         */
172 };
173
174 /* Main data structure for CUDA nonbonded force calculations. */
175 struct nbnxn_cuda
176 {
177     cuda_dev_info_t *dev_info;       /* CUDA device information                              */
178     int              kernel_ver;     /* The version of the kernel to be executed on the
179                                         device in use, possible values: eNbnxnCuK*           */
180     bool             bUseTwoStreams; /* true if doing both local/non-local NB work on GPU    */
181     bool             bUseStreamSync; /* true if the standard cudaStreamSynchronize is used
182                                         and not memory polling-based waiting                 */
183     cu_atomdata_t   *atdat;          /* atom data                                            */
184     cu_nbparam_t    *nbparam;        /* parameters required for the non-bonded calc.         */
185     cu_plist_t      *plist[2];       /* pair-list data structures (local and non-local)      */
186     nb_staging_t     nbst;           /* staging area where fshift/energies get downloaded    */
187
188     cudaStream_t     stream[2];      /* local and non-local GPU streams                      */
189
190     /* events used for synchronization */
191     cudaEvent_t    nonlocal_done, misc_ops_done;
192
193     /* NOTE: With current CUDA versions (<=5.0) timing doesn't work with multiple
194      * concurrent streams, so we won't time if both l/nl work is done on GPUs.
195      * Timer init/uninit is still done even with timing off so only the condition
196      * setting bDoTime needs to be change if this CUDA "feature" gets fixed. */
197     bool             bDoTime;       /* True if event-based timing is enabled.               */
198     cu_timers_t     *timers;        /* CUDA event-based timers.                             */
199     wallclock_gpu_t *timings;       /* Timing data.                                         */
200 };
201
202 #ifdef __cplusplus
203 }
204 #endif
205
206 #endif  /* NBNXN_CUDA_TYPES_H */