Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / 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  * Copyright (c) 2012,2013, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef NBNXN_CUDA_TYPES_H
39 #define NBNXN_CUDA_TYPES_H
40
41 #include "types/nbnxn_pairlist.h"
42 #include "types/nbnxn_cuda_types_ext.h"
43 #include "../../gmxlib/cuda_tools/cudautils.cuh"
44
45 /* CUDA versions from 5.0 above support texture objects. */
46 #if CUDA_VERSION >= 5000
47 #define TEXOBJ_SUPPORTED
48 #else  /* CUDA_VERSION */
49 /* This typedef allows us to define only one version of struct cu_nbparam */
50 typedef int cudaTextureObject_t;
51 #endif /* CUDA_VERSION */
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 /** Types of electrostatics implementations available in the CUDA non-bonded
58  *  force kernels. These represent both the electrostatics types implemented
59  *  by the kernels (cut-off, RF, and Ewald - a subset of what's defined in
60  *  enums.h) as well as encode implementation details analytical/tabulated
61  *  and single or twin cut-off (for Ewald kernels).
62  *  Note that the cut-off and RF kernels have only analytical flavor and unlike
63  *  in the CPU kernels, the tabulated kernels are ATM Ewald-only.
64  *
65  *  The order of pointers to different electrostatic kernels defined in
66  *  nbnxn_cuda.cu by the nb_default_kfunc_ptr array
67  *  should match the order of enumerated types below. */
68 enum {
69     eelCuCUT, eelCuRF, eelCuEWALD_TAB, eelCuEWALD_TAB_TWIN, eelCuEWALD_ANA, eelCuEWALD_ANA_TWIN, eelCuNR
70 };
71
72 /* All structs prefixed with "cu_" hold data used in GPU calculations and
73  * are passed to the kernels, except cu_timers_t. */
74 typedef struct cu_plist     cu_plist_t;
75 typedef struct cu_atomdata  cu_atomdata_t;
76 typedef struct cu_nbparam   cu_nbparam_t;
77 typedef struct cu_timers    cu_timers_t;
78 typedef struct nb_staging   nb_staging_t;
79
80
81 /** Staging area for temporary data. The energies get downloaded here first,
82  *  before getting added to the CPU-side aggregate values.
83  */
84 struct nb_staging
85 {
86     float   *e_lj;      /**< LJ energy            */
87     float   *e_el;      /**< electrostatic energy */
88     float3  *fshift;    /**< shift forces         */
89 };
90
91 /** Nonbonded atom data -- both inputs and outputs. */
92 struct cu_atomdata
93 {
94     int      natoms;            /**< number of atoms                              */
95     int      natoms_local;      /**< number of local atoms                        */
96     int      nalloc;            /**< allocation size for the atom data (xq, f)    */
97
98     float4  *xq;                /**< atom coordinates + charges, size natoms      */
99     float3  *f;                 /**< force output array, size natoms              */
100     /* TODO: try float2 for the energies */
101     float   *e_lj,              /**< LJ energy output, size 1                     */
102             *e_el;              /**< Electrostatics energy input, size 1          */
103
104     float3  *fshift;            /**< shift forces                                 */
105
106     int      ntypes;            /**< number of atom types                         */
107     int     *atom_types;        /**< atom type indices, size natoms               */
108
109     float3  *shift_vec;         /**< shifts                                       */
110     bool     bShiftVecUploaded; /**< true if the shift vector has been uploaded   */
111 };
112
113 /** Parameters required for the CUDA nonbonded calculations. */
114 struct cu_nbparam
115 {
116     int      eeltype;        /**< type of electrostatics                            */
117
118     float    epsfac;         /**< charge multiplication factor                      */
119     float    c_rf;           /**< Reaction-field/plain cutoff electrostatics const. */
120     float    two_k_rf;       /**< Reaction-field electrostatics constant            */
121     float    ewald_beta;     /**< Ewald/PME parameter                               */
122     float    sh_ewald;       /**< Ewald/PME  correction term                        */
123     float    rvdw_sq;        /**< VdW cut-off                                       */
124     float    rcoulomb_sq;    /**< Coulomb cut-off                                   */
125     float    rlist_sq;       /**< pair-list cut-off                                 */
126     float    sh_invrc6;      /**< LJ potential correction term                      */
127
128     /* Non-bonded parameters - accessed through texture memory */
129     float            *nbfp;          /**< nonbonded parameter table with C6/C12 pairs  */
130     cudaTextureObject_t nbfp_texobj; /**< texture object bound to nbfp                 */
131
132     /* Ewald Coulomb force table data - accessed through texture memory */
133     int               coulomb_tab_size;      /**< table size (s.t. it fits in texture cache) */
134     float             coulomb_tab_scale;     /**< table scale/spacing                        */
135     float            *coulomb_tab;           /**< pointer to the table in the device memory  */
136     cudaTextureObject_t  coulomb_tab_texobj; /**< texture object bound to coulomb_tab        */
137 };
138
139 /** Pair list data */
140 struct cu_plist
141 {
142     int              na_c;        /**< number of atoms per cluster                  */
143
144     int              nsci;        /**< size of sci, # of i clusters in the list     */
145     int              sci_nalloc;  /**< allocation size of sci                       */
146     nbnxn_sci_t     *sci;         /**< list of i-cluster ("super-clusters")         */
147
148     int              ncj4;        /**< total # of 4*j clusters                      */
149     int              cj4_nalloc;  /**< allocation size of cj4                       */
150     nbnxn_cj4_t     *cj4;         /**< 4*j cluster list, contains j cluster number
151                                        and index into the i cluster list            */
152     nbnxn_excl_t    *excl;        /**< atom interaction bits                        */
153     int              nexcl;       /**< count for excl                               */
154     int              excl_nalloc; /**< allocation size of excl                      */
155
156     bool             bDoPrune;    /**< true if pair-list pruning needs to be
157                                        done during the  current step                */
158 };
159
160 /** CUDA events used for timing GPU kernels and H2D/D2H transfers.
161  * The two-sized arrays hold the local and non-local values and should always
162  * be indexed with eintLocal/eintNonlocal.
163  */
164 struct cu_timers
165 {
166     cudaEvent_t start_atdat;     /**< start event for atom data transfer (every PS step)             */
167     cudaEvent_t stop_atdat;      /**< stop event for atom data transfer (every PS step)              */
168     cudaEvent_t start_nb_h2d[2]; /**< start events for x/q H2D transfers (l/nl, every step)          */
169     cudaEvent_t stop_nb_h2d[2];  /**< stop events for x/q H2D transfers (l/nl, every step)           */
170     cudaEvent_t start_nb_d2h[2]; /**< start events for f D2H transfer (l/nl, every step)             */
171     cudaEvent_t stop_nb_d2h[2];  /**< stop events for f D2H transfer (l/nl, every step)              */
172     cudaEvent_t start_pl_h2d[2]; /**< start events for pair-list H2D transfers (l/nl, every PS step) */
173     cudaEvent_t stop_pl_h2d[2];  /**< start events for pair-list H2D transfers (l/nl, every PS step) */
174     cudaEvent_t start_nb_k[2];   /**< start event for non-bonded kernels (l/nl, every step)          */
175     cudaEvent_t stop_nb_k[2];    /**< stop event non-bonded kernels (l/nl, every step)               */
176 };
177
178 /** Main data structure for CUDA nonbonded force calculations. */
179 struct nbnxn_cuda
180 {
181     cuda_dev_info_t *dev_info;       /**< CUDA device information                              */
182     bool             bUseTwoStreams; /**< true if doing both local/non-local NB work on GPU    */
183     bool             bUseStreamSync; /**< true if the standard cudaStreamSynchronize is used
184                                           and not memory polling-based waiting                 */
185     cu_atomdata_t   *atdat;          /**< atom data                                            */
186     cu_nbparam_t    *nbparam;        /**< parameters required for the non-bonded calc.         */
187     cu_plist_t      *plist[2];       /**< pair-list data structures (local and non-local)      */
188     nb_staging_t     nbst;           /**< staging area where fshift/energies get downloaded    */
189
190     cudaStream_t     stream[2];      /**< local and non-local GPU streams                      */
191
192     /** events used for synchronization */
193     cudaEvent_t    nonlocal_done;    /**< event triggered when the non-local non-bonded kernel
194                                         is done (and the local transfer can proceed)           */
195     cudaEvent_t    misc_ops_done;    /**< event triggered when the operations that precede the
196                                           main force calculations are done (e.g. buffer 0-ing) */
197
198     /* NOTE: With current CUDA versions (<=5.0) timing doesn't work with multiple
199      * concurrent streams, so we won't time if both l/nl work is done on GPUs.
200      * Timer init/uninit is still done even with timing off so only the condition
201      * setting bDoTime needs to be change if this CUDA "feature" gets fixed. */
202     bool             bDoTime;       /**< True if event-based timing is enabled.               */
203     cu_timers_t     *timers;        /**< CUDA event-based timers.                             */
204     wallclock_gpu_t *timings;       /**< Timing data.                                         */
205 };
206
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif  /* NBNXN_CUDA_TYPES_H */