SYCL PME Spread kernel
[alexxy/gromacs.git] / src / gromacs / ewald / pme_gpu_program_impl_sycl.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2021, 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
36 /*! \internal \file
37  * \brief
38  * Implements PmeGpuProgramImpl, which stores permanent PME GPU context-derived data,
39  * such as (compiled) kernel handles.
40  *
41  * \author Aleksei Iupinov <a.yupinov@gmail.com>
42  * \author Andrey Alekseenko <al42and@gmail.com>
43  * \ingroup module_ewald
44  */
45 #include "gmxpre.h"
46
47 #include "gromacs/hardware/device_information.h"
48 #include "gromacs/gpu_utils/gmxsycl.h"
49 #include "gromacs/gpu_utils/syclutils.h"
50
51 #include "pme_gpu_program_impl.h"
52 #include "pme_spread_sycl.h"
53
54 #include "pme_gpu_constants.h"
55 #include "pme_gpu_internal.h" // for GridOrdering enum
56 #include "pme_gpu_types_host.h"
57
58 // PME interpolation order
59 constexpr int c_pmeOrder = 4;
60 // These hardcoded spread/gather parameters refer to not-implemented PME GPU 2D decomposition in X/Y
61 constexpr bool c_wrapX = true;
62 constexpr bool c_wrapY = true;
63
64 static int subGroupSizeFromVendor(const DeviceInformation& deviceInfo)
65 {
66     switch (deviceInfo.deviceVendor)
67     {
68         case DeviceVendor::Amd: return 64;   // Handle RDNA2 devices, Issue #3972.
69         case DeviceVendor::Intel: return 16; // TODO: Choose best value, Issue #4153.
70         case DeviceVendor::Nvidia: return 32;
71         default: GMX_RELEASE_ASSERT(false, "Unknown device vendor"); return 0;
72     }
73 }
74
75 #define INSTANTIATE_3(order, computeSplines, spreadCharges, numGrids, writeGlobal, threadsPerAtom, subGroupSize) \
76     extern template class PmeSplineAndSpreadKernel<order, computeSplines, spreadCharges, true, true, numGrids, writeGlobal, threadsPerAtom, subGroupSize>;
77
78 #define INSTANTIATE_2(order, numGrids, threadsPerAtom, subGroupSize)                 \
79     INSTANTIATE_3(order, true, true, numGrids, true, threadsPerAtom, subGroupSize);  \
80     INSTANTIATE_3(order, true, false, numGrids, true, threadsPerAtom, subGroupSize); \
81     INSTANTIATE_3(order, false, true, numGrids, true, threadsPerAtom, subGroupSize); \
82     INSTANTIATE_3(order, true, true, numGrids, false, threadsPerAtom, subGroupSize);
83
84 #define INSTANTIATE(order, subGroupSize)                                 \
85     INSTANTIATE_2(order, 1, ThreadsPerAtom::Order, subGroupSize);        \
86     INSTANTIATE_2(order, 1, ThreadsPerAtom::OrderSquared, subGroupSize); \
87     INSTANTIATE_2(order, 2, ThreadsPerAtom::Order, subGroupSize);        \
88     INSTANTIATE_2(order, 2, ThreadsPerAtom::OrderSquared, subGroupSize);
89
90 #if GMX_SYCL_DPCPP
91 INSTANTIATE(4, 16);
92 #elif GMX_SYCL_HIPSYCL
93 INSTANTIATE(4, 32);
94 INSTANTIATE(4, 64);
95 #endif
96
97
98 //! Helper function to set proper kernel functor pointers
99 template<int subGroupSize>
100 static void setKernelPointers(struct PmeGpuProgramImpl* pmeGpuProgram)
101 {
102     /* Not all combinations of the splineAndSpread, spline and Spread kernels are required
103      * If only the spline (without the spread) then it does not make sense not to write the data to global memory
104      * Similarly the spread kernel (without the spline) implies that we should read the spline data from global memory
105      */
106     pmeGpuProgram->splineAndSpreadKernelSingle =
107             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 1, false, ThreadsPerAtom::OrderSquared, subGroupSize>();
108     pmeGpuProgram->splineAndSpreadKernelThPerAtom4Single =
109             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 1, false, ThreadsPerAtom::Order, subGroupSize>();
110     pmeGpuProgram->splineAndSpreadKernelWriteSplinesSingle =
111             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 1, true, ThreadsPerAtom::OrderSquared, subGroupSize>();
112     pmeGpuProgram->splineAndSpreadKernelWriteSplinesThPerAtom4Single =
113             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 1, true, ThreadsPerAtom::Order, subGroupSize>();
114     pmeGpuProgram->splineKernelSingle =
115             new PmeSplineAndSpreadKernel<c_pmeOrder, true, false, c_wrapX, c_wrapY, 1, true, ThreadsPerAtom::OrderSquared, subGroupSize>();
116     pmeGpuProgram->splineKernelThPerAtom4Single =
117             new PmeSplineAndSpreadKernel<c_pmeOrder, true, false, c_wrapX, c_wrapY, 1, true, ThreadsPerAtom::Order, subGroupSize>();
118     pmeGpuProgram->spreadKernelSingle =
119             new PmeSplineAndSpreadKernel<c_pmeOrder, false, true, c_wrapX, c_wrapY, 1, true, ThreadsPerAtom::OrderSquared, subGroupSize>();
120     pmeGpuProgram->spreadKernelThPerAtom4Single =
121             new PmeSplineAndSpreadKernel<c_pmeOrder, false, true, c_wrapX, c_wrapY, 1, true, ThreadsPerAtom::Order, subGroupSize>();
122     pmeGpuProgram->splineAndSpreadKernelDual =
123             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 2, false, ThreadsPerAtom::OrderSquared, subGroupSize>();
124     pmeGpuProgram->splineAndSpreadKernelThPerAtom4Dual =
125             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 2, false, ThreadsPerAtom::Order, subGroupSize>();
126     pmeGpuProgram->splineAndSpreadKernelWriteSplinesDual =
127             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 2, true, ThreadsPerAtom::OrderSquared, subGroupSize>();
128     pmeGpuProgram->splineAndSpreadKernelWriteSplinesThPerAtom4Dual =
129             new PmeSplineAndSpreadKernel<c_pmeOrder, true, true, c_wrapX, c_wrapY, 2, true, ThreadsPerAtom::Order, subGroupSize>();
130     pmeGpuProgram->splineKernelDual =
131             new PmeSplineAndSpreadKernel<c_pmeOrder, true, false, c_wrapX, c_wrapY, 2, true, ThreadsPerAtom::OrderSquared, subGroupSize>();
132     pmeGpuProgram->splineKernelThPerAtom4Dual =
133             new PmeSplineAndSpreadKernel<c_pmeOrder, true, false, c_wrapX, c_wrapY, 2, true, ThreadsPerAtom::Order, subGroupSize>();
134     pmeGpuProgram->spreadKernelDual =
135             new PmeSplineAndSpreadKernel<c_pmeOrder, false, true, c_wrapX, c_wrapY, 2, true, ThreadsPerAtom::OrderSquared, subGroupSize>();
136     pmeGpuProgram->spreadKernelThPerAtom4Dual =
137             new PmeSplineAndSpreadKernel<c_pmeOrder, false, true, c_wrapX, c_wrapY, 2, true, ThreadsPerAtom::Order, subGroupSize>();
138 }
139
140 PmeGpuProgramImpl::PmeGpuProgramImpl(const DeviceContext& deviceContext) :
141     deviceContext_(deviceContext)
142 {
143     // kernel parameters
144     warpSize_             = subGroupSizeFromVendor(deviceContext.deviceInfo());
145     spreadWorkGroupSize   = c_spreadMaxWarpsPerBlock * warpSize_;
146     solveMaxWorkGroupSize = c_solveMaxWarpsPerBlock * warpSize_;
147     gatherWorkGroupSize   = c_gatherMaxWarpsPerBlock * warpSize_;
148
149     switch (warpSize_)
150     {
151 #if GMX_SYCL_DPCPP
152         case 16: setKernelPointers<16>(this); break;
153 #elif GMX_SYCL_HIPSYCL
154         case 32: setKernelPointers<32>(this); break;
155         case 64: setKernelPointers<64>(this); break;
156 #endif
157         default: GMX_RELEASE_ASSERT(false, "Invalid sub group size");
158     }
159 }
160
161 PmeGpuProgramImpl::~PmeGpuProgramImpl()
162 {
163     delete splineKernelSingle;
164     delete splineKernelThPerAtom4Single;
165     delete spreadKernelSingle;
166     delete spreadKernelThPerAtom4Single;
167     delete splineAndSpreadKernelSingle;
168     delete splineAndSpreadKernelThPerAtom4Single;
169     delete splineAndSpreadKernelWriteSplinesSingle;
170     delete splineAndSpreadKernelWriteSplinesThPerAtom4Single;
171     delete splineKernelDual;
172     delete splineKernelThPerAtom4Dual;
173     delete spreadKernelDual;
174     delete spreadKernelThPerAtom4Dual;
175     delete splineAndSpreadKernelDual;
176     delete splineAndSpreadKernelThPerAtom4Dual;
177     delete splineAndSpreadKernelWriteSplinesDual;
178     delete splineAndSpreadKernelWriteSplinesThPerAtom4Dual;
179 }