Ensure minimum exec width of the PME OpenCL kernels
[alexxy/gromacs.git] / src / gromacs / ewald / pme-gpu-program-impl-ocl.cpp
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
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  * \ingroup module_ewald
43  */
44 #include "gmxpre.h"
45
46 #include "gromacs/gpu_utils/gmxopencl.h"
47 #include "gromacs/gpu_utils/ocl_compiler.h"
48 #include "gromacs/utility/stringutil.h"
49
50 #include "pme-gpu-constants.h"
51 #include "pme-gpu-internal.h" // for GridOrdering enum
52 #include "pme-gpu-program-impl.h"
53 #include "pme-gpu-types-host.h"
54 #include "pme-grid.h"
55
56 PmeGpuProgramImpl::PmeGpuProgramImpl(const gmx_device_info_t *deviceInfo)
57 {
58     // Context creation (which should happen outside of this class: #2522)
59     cl_platform_id        platformId = deviceInfo->ocl_gpu_id.ocl_platform_id;
60     cl_device_id          deviceId   = deviceInfo->ocl_gpu_id.ocl_device_id;
61     cl_context_properties contextProperties[3];
62     contextProperties[0] = CL_CONTEXT_PLATFORM;
63     contextProperties[1] = reinterpret_cast<cl_context_properties>(platformId);
64     contextProperties[2] = 0; /* Terminates the list of properties */
65
66     cl_int  clError;
67     context = clCreateContext(contextProperties, 1, &deviceId, nullptr, nullptr, &clError);
68     if (clError != CL_SUCCESS)
69     {
70         const std::string errorString = gmx::formatString("Failed to create context for PME on GPU #%s:\n OpenCL error %d: %s",
71                                                           deviceInfo->device_name, clError, ocl_get_error_string(clError).c_str());
72         GMX_THROW(gmx::InternalError(errorString));
73     }
74
75     // kernel parameters
76     warpSize            = gmx::ocl::getDeviceWarpSize(context, deviceId);
77     // TODO: for Intel ideally we'd want to set these based on the compiler warp size
78     // but given that we've done no tuning for Intel iGPU, this is as good as anything.
79     spreadWorkGroupSize = std::min(c_spreadMaxWarpsPerBlock * warpSize,
80                                    deviceInfo->maxWorkGroupSize);
81     solveMaxWorkGroupSize = std::min(c_solveMaxWarpsPerBlock * warpSize,
82                                      deviceInfo->maxWorkGroupSize);
83     gatherWorkGroupSize = std::min(c_gatherMaxWarpsPerBlock * warpSize,
84                                    deviceInfo->maxWorkGroupSize);
85
86     compileKernels(deviceInfo);
87 }
88
89 PmeGpuProgramImpl::~PmeGpuProgramImpl()
90 {
91     // TODO: log releasing errors
92     cl_int gmx_used_in_debug stat = 0;
93     stat |= clReleaseKernel(splineAndSpreadKernel);
94     stat |= clReleaseKernel(splineKernel);
95     stat |= clReleaseKernel(spreadKernel);
96     stat |= clReleaseKernel(gatherKernel);
97     stat |= clReleaseKernel(gatherReduceWithInputKernel);
98     stat |= clReleaseKernel(solveXYZKernel);
99     stat |= clReleaseKernel(solveXYZEnergyKernel);
100     stat |= clReleaseKernel(solveYZXKernel);
101     stat |= clReleaseKernel(solveYZXEnergyKernel);
102     stat |= clReleaseContext(context);
103     GMX_ASSERT(stat == CL_SUCCESS, gmx::formatString("Failed to release PME OpenCL resources %d: %s",
104                                                      stat, ocl_get_error_string(stat).c_str()).c_str());
105 }
106
107 /*! \brief Ensure that spread/gather kernels have been compiled to a suitable warp size
108  *
109  * On Intel the exec width/warp is decided at compile-time and can be
110  * smaller than the minimum order^2 required in spread/gather ATM which
111  * we need to check for.
112  */
113 static void checkRequiredWarpSize(const cl_kernel          kernel,
114                                   const char*              kernelName,
115                                   const gmx_device_info_t *deviceInfo)
116 {
117     if (deviceInfo->vendor_e == OCL_VENDOR_INTEL)
118     {
119         size_t kernelWarpSize = gmx::ocl::getKernelWarpSize(kernel, deviceInfo->ocl_gpu_id.ocl_device_id);
120
121         if (kernelWarpSize < c_pmeSpreadGatherMinWarpSize)
122         {
123             const std::string errorString = gmx::formatString("PME OpenCL kernels require >=%d execution width, but the %s kernel "
124                                                               "has been compiled for the device %s to a %zu width and therefore it can not execute correctly.",
125                                                               c_pmeSpreadGatherMinWarpSize, kernelName,
126                                                               deviceInfo->device_name, kernelWarpSize);
127             GMX_THROW(gmx::InternalError(errorString));
128         }
129     }
130 }
131
132 void PmeGpuProgramImpl::compileKernels(const gmx_device_info_t *deviceInfo)
133 {
134     // We might consider storing program as a member variable if it's needed later
135     cl_program program = nullptr;
136     /* Need to catch std::bad_alloc here and during compilation string handling. */
137     try
138     {
139         /* Here we pass macros and static const int variables defined in include
140          * files outside as macros, to avoid including those files
141          * in the JIT compilation that happens at runtime.
142          */
143         constexpr int     order         = 4;
144         const std::string commonDefines = gmx::formatString(
145                     "-Dwarp_size=%zd "
146                     "-Dorder=%d "
147                     "-DPME_SPREADGATHER_ATOMS_PER_WARP=%zd "
148                     "-DPME_SPREADGATHER_THREADS_PER_ATOM=%d "
149                     // forwarding from pme-grid.h, used for spline computation table sizes only
150                     "-Dc_pmeMaxUnitcellShift=%f "
151                     // forwarding PME behavior constants from pme-gpu-constants.h
152                     "-Dc_usePadding=%d "
153                     "-Dc_skipNeutralAtoms=%d "
154                     "-Dc_virialAndEnergyCount=%d "
155                     // forwarding kernel work sizes
156                     "-Dc_spreadWorkGroupSize=%zd "
157                     "-Dc_solveMaxWorkGroupSize=%zd "
158                     "-Dc_gatherWorkGroupSize=%zd "
159                     // forwarding from vectypes.h
160                     "-DDIM=%d -DXX=%d -DYY=%d -DZZ=%d "
161                     // decomposition parameter placeholders
162                     "-DwrapX=true -DwrapY=true ",
163                     warpSize,
164                     order,
165                     warpSize / PME_SPREADGATHER_THREADS_PER_ATOM,
166                     PME_SPREADGATHER_THREADS_PER_ATOM,
167                     static_cast<float>(c_pmeMaxUnitcellShift),
168                     c_usePadding,
169                     c_skipNeutralAtoms,
170                     c_virialAndEnergyCount,
171                     spreadWorkGroupSize,
172                     solveMaxWorkGroupSize,
173                     gatherWorkGroupSize,
174                     DIM, XX, YY, ZZ);
175         try
176         {
177             /* TODO when we have a proper MPI-aware logging module,
178                the log output here should be written there */
179             program = gmx::ocl::compileProgram(stderr,
180                                                "src/gromacs/ewald",
181                                                "pme-program.cl",
182                                                commonDefines,
183                                                context,
184                                                deviceInfo->ocl_gpu_id.ocl_device_id,
185                                                deviceInfo->vendor_e);
186         }
187         catch (gmx::GromacsException &e)
188         {
189             e.prependContext(gmx::formatString("Failed to compile PME kernels for GPU #%s\n",
190                                                deviceInfo->device_name));
191             throw;
192         }
193     }
194     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
195
196     constexpr cl_uint      expectedKernelCount = 9;
197     // Has to be equal or larger than the number of kernel instances.
198     // If it is not, CL_INVALID_VALUE will be thrown.
199     std::vector<cl_kernel> kernels(expectedKernelCount, nullptr);
200     cl_uint                actualKernelCount = 0;
201     cl_int                 clError           = clCreateKernelsInProgram(program, kernels.size(), kernels.data(), &actualKernelCount);
202     if (clError != CL_SUCCESS)
203     {
204         const std::string errorString = gmx::formatString("Failed to create kernels for PME on GPU #%s:\n OpenCL error %d: %s",
205                                                           deviceInfo->device_name, clError, ocl_get_error_string(clError).c_str());
206         GMX_THROW(gmx::InternalError(errorString));
207     }
208     kernels.resize(actualKernelCount);
209
210     std::array<char, 100> kernelNamesBuffer;
211     for (const auto &kernel : kernels)
212     {
213         clError = clGetKernelInfo(kernel, CL_KERNEL_FUNCTION_NAME,
214                                   kernelNamesBuffer.size(), kernelNamesBuffer.data(), nullptr);
215         if (clError != CL_SUCCESS)
216         {
217             const std::string errorString = gmx::formatString("Failed to parse kernels for PME on GPU #%s:\n OpenCL error %d: %s",
218                                                               deviceInfo->device_name, clError, ocl_get_error_string(clError).c_str());
219             GMX_THROW(gmx::InternalError(errorString));
220         }
221
222         // The names below must correspond to those defined in pme-program.cl
223         // TODO use a map with string key instead?
224         if (!strcmp(kernelNamesBuffer.data(), "pmeSplineKernel"))
225         {
226             splineKernel = kernel;
227         }
228         else if (!strcmp(kernelNamesBuffer.data(), "pmeSplineAndSpreadKernel"))
229         {
230             splineAndSpreadKernel = kernel;
231             checkRequiredWarpSize(splineAndSpreadKernel, kernelNamesBuffer.data(), deviceInfo);
232         }
233         else if (!strcmp(kernelNamesBuffer.data(), "pmeSpreadKernel"))
234         {
235             spreadKernel = kernel;
236             checkRequiredWarpSize(spreadKernel, kernelNamesBuffer.data(), deviceInfo);
237         }
238         else if (!strcmp(kernelNamesBuffer.data(), "pmeGatherKernel"))
239         {
240             gatherKernel = kernel;
241             checkRequiredWarpSize(gatherKernel, kernelNamesBuffer.data(), deviceInfo);
242         }
243         else if (!strcmp(kernelNamesBuffer.data(), "pmeGatherReduceWithInputKernel"))
244         {
245             gatherReduceWithInputKernel = kernel;
246             checkRequiredWarpSize(gatherReduceWithInputKernel, kernelNamesBuffer.data(), deviceInfo);
247         }
248         else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveYZXKernel"))
249         {
250             solveYZXKernel = kernel;
251         }
252         else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveYZXEnergyKernel"))
253         {
254             solveYZXEnergyKernel = kernel;
255         }
256         else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveXYZKernel"))
257         {
258             solveXYZKernel = kernel;
259         }
260         else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveXYZEnergyKernel"))
261         {
262             solveXYZEnergyKernel = kernel;
263         }
264     }
265     clReleaseProgram(program);
266 }