1e841d904ebee29ea76774bda18759cbb3b2c57e
[alexxy/gromacs.git] / src / gromacs / ewald / pme-gpu-constants.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief This file defines the PME GPU compile-time constants/macros,
37  * used both in device and host code.
38  *
39  * As OpenCL C is not aware of constexpr, most of this file is
40  * forwarded to the OpenCL kernel compilation as defines with same
41  * names, for the sake of code similarity.
42  *
43  * \todo The values are currently common to both CUDA and OpenCL
44  * implementations, but should be reconsidered when we tune the OpenCL
45  * implementation. See Redmine #2528.
46  *
47  * \author Aleksei Iupinov <a.yupinov@gmail.com>
48  * \ingroup module_ewald
49  */
50
51 #ifndef GMX_EWALD_PME_GPU_CONSTANTS_H
52 #define GMX_EWALD_PME_GPU_CONSTANTS_H
53
54 #include "config.h"
55
56 #if GMX_GPU == GMX_GPU_CUDA
57 #include "gromacs/gpu_utils/cuda_arch_utils.cuh"
58 #else
59 #define warp_size 32 // FIXME remove this and rework macros
60 #define PME_SPREADGATHER_ATOMS_PER_WARP 2
61 #endif
62
63 /* General settings for PME GPU behaviour */
64
65 /*! \brief
66  * false: The atom data GPU buffers are sized precisely according to the number of atoms.
67  *        (Except GPU spline data layout which is regardless intertwined for 2 atoms per warp).
68  *        The atom index checks in the spread/gather code potentially hinder the performance.
69  * true:  The atom data GPU buffers are padded with zeroes so that the possible number of atoms
70  *        fitting in is divisible by PME_ATOM_DATA_ALIGNMENT.
71  *        The atom index checks are not performed. There should be a performance win, but how big is it, remains to be seen.
72  *        Additional cudaMemsetAsync calls are done occasionally (only charges/coordinates; spline data is always recalculated now).
73  * \todo Estimate performance differences
74  */
75 constexpr bool c_usePadding = true;
76
77 /*! \brief
78  * false: Atoms with zero charges are processed by PME. Could introduce some overhead.
79  * true:  Atoms with zero charges are not processed by PME. Adds branching to the spread/gather.
80  *        Could be good for performance in specific systems with lots of neutral atoms.
81  * \todo Estimate performance differences.
82  */
83 constexpr bool c_skipNeutralAtoms = false;
84
85 /*! \brief
86  * Number of PME solve output floating point numbers.
87  * 6 for symmetric virial matrix + 1 for reciprocal energy.
88  */
89 constexpr int c_virialAndEnergyCount = 7;
90
91
92 /* Macros concerning the data layout */
93
94 /*
95     Here is a current memory layout for the theta/dtheta B-spline float parameter arrays.
96     This is the data in global memory used both by spreading and gathering kernels (with same scheduling).
97     This example has PME order 4 and 2 particles per warp/data chunk.
98     Each particle has 16 threads assigned to it, each thread works on 4 non-sequential global grid contributions.
99
100     ----------------------------------------------------------------------------
101     particles 0, 1                                        | particles 2, 3     | ...
102     ----------------------------------------------------------------------------
103     order index 0           | index 1 | index 2 | index 3 | order index 0 .....
104     ----------------------------------------------------------------------------
105     tx0 tx1 ty0 ty1 tz0 tz1 | ..........
106     ----------------------------------------------------------------------------
107
108     Each data chunk for a single warp is 24 floats. This goes both for theta and dtheta.
109     24 = 2 particles per warp * order 4 * 3 dimensions. 48 floats (1.5 warp size) per warp in total.
110     I have also tried intertwining theta and theta in a single array (they are used in pairs in gathering stage anyway)
111     and it didn't seem to make a performance difference.
112
113     The spline indexing is isolated in the 2 inline functions:
114     getSplineParamIndexBase() return a base shared memory index corresponding to the atom in the block;
115     getSplineParamIndex() consumes its results and adds offsets for dimension and spline value index.
116
117     The corresponding defines follow.
118  */
119
120 /*! \brief
121  * The number of GPU threads used for computing spread/gather contributions of a single atom as function of the PME order.
122  * The assumption is currently that any thread processes only a single atom's contributions.
123  */
124 #define PME_SPREADGATHER_THREADS_PER_ATOM (order * order)
125
126 /*! \brief
127  * Atom data alignment (in terms of number of atoms).
128  * The value is (16 * PME_SPREADGATHER_ATOMS_PER_WARP).
129  * If the GPU atom data buffers are padded (c_usePadding == true),
130  * Then the numbers of atoms which would fit in the padded GPU buffers has to be divisible by this.
131  * The literal number (16) expresses maximum spread/gather block width in warps.
132  * Accordingly, spread and gather block widths in warps should be divisors of this
133  * (e.g. in the pme-spread.cu: constexpr int c_spreadMaxThreadsPerBlock = 8 * warp_size;).
134  * There are debug asserts for this divisibility.
135  */
136 #define PME_ATOM_DATA_ALIGNMENT 32
137
138 /*
139  * The execution widths for PME GPU kernels, used both on host and device for correct scheduling.
140  * TODO: adjust those for OpenCL.
141  */
142
143 #if GMX_GPU == GMX_GPU_CUDA
144
145 /*! \brief
146  * The number of atoms processed by a single warp in spread/gather.
147  * This macro depends on the templated order parameter (2 atoms per warp for order 4).
148  * It is mostly used for spline data layout tweaked for coalesced access.
149  */
150 #define PME_SPREADGATHER_ATOMS_PER_WARP (warp_size / PME_SPREADGATHER_THREADS_PER_ATOM)
151
152 //! Spreading max block width in warps picked among powers of 2 (2, 4, 8, 16) for max. occupancy and min. runtime in most cases
153 constexpr int c_spreadMaxWarpsPerBlock = 8;
154 /* TODO: it has been observed that the kernel can be faster with smaller block sizes (2 or 4 warps)
155  * only on some GPUs (660Ti) with large enough grid (>= 48^3) due to load/store units being overloaded
156  * (ldst_fu_utilization metric maxed out in nvprof). Runtime block size choice might be nice to have.
157  * This has been tried on architectures up to Maxwell (GTX 750) and it would be good to revisit this.
158  */
159 //! Spreading max block size in threads
160 constexpr int c_spreadMaxThreadsPerBlock = c_spreadMaxWarpsPerBlock * warp_size;
161
162 //! Solving kernel max block width in warps picked among powers of 2 (2, 4, 8, 16) for max. occupancy and min. runtime
163 //! (560Ti (CC2.1), 660Ti (CC3.0) and 750 (CC5.0)))
164 constexpr int c_solveMaxWarpsPerBlock = 8;
165 //! Solving kernel max block size in threads
166 constexpr int c_solveMaxThreadsPerBlock = (c_solveMaxWarpsPerBlock * warp_size);
167
168 //! Gathering max block width in warps - picked empirically among 2, 4, 8, 16 for max. occupancy and min. runtime
169 constexpr int c_gatherMaxWarpsPerBlock = 4;
170 //! Gathering max block size in threads
171 constexpr int c_gatherMaxThreadsPerBlock = c_gatherMaxWarpsPerBlock * warp_size;
172 //! Gathering min blocks per CUDA multiprocessor - for CC2.x, we just take the CUDA limit of 8 to avoid the warning
173 constexpr int c_gatherMinBlocksPerMP = (GMX_PTX_ARCH < 300) ? GMX_CUDA_MAX_BLOCKS_PER_MP : (GMX_CUDA_MAX_THREADS_PER_MP / c_gatherMaxThreadsPerBlock);
174
175 #endif // GMX_GPU == GMX_GPU_CUDA
176
177 #endif