2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018,2019,2020, 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.
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.
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.
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.
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.
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.
38 * Implements PmeGpuProgramImpl, which stores permanent PME GPU context-derived data,
39 * such as (compiled) kernel handles.
41 * \author Aleksei Iupinov <a.yupinov@gmail.com>
42 * \ingroup module_ewald
46 #include "gromacs/gpu_utils/gmxopencl.h"
47 #include "gromacs/gpu_utils/ocl_compiler.h"
48 #include "gromacs/utility/stringutil.h"
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"
56 PmeGpuProgramImpl::PmeGpuProgramImpl(const DeviceInformation& deviceInfo, const DeviceContext& deviceContext) :
57 deviceContext_(deviceContext)
60 warpSize_ = gmx::ocl::getDeviceWarpSize(deviceContext_.context(), deviceInfo.oclDeviceId);
61 // TODO: for Intel ideally we'd want to set these based on the compiler warp size
62 // but given that we've done no tuning for Intel iGPU, this is as good as anything.
63 spreadWorkGroupSize = std::min(c_spreadMaxWarpsPerBlock * warpSize_, deviceInfo.maxWorkGroupSize);
64 solveMaxWorkGroupSize = std::min(c_solveMaxWarpsPerBlock * warpSize_, deviceInfo.maxWorkGroupSize);
65 gatherWorkGroupSize = std::min(c_gatherMaxWarpsPerBlock * warpSize_, deviceInfo.maxWorkGroupSize);
67 compileKernels(deviceInfo);
70 PmeGpuProgramImpl::~PmeGpuProgramImpl()
72 // TODO: log releasing errors
73 cl_int gmx_used_in_debug stat = 0;
74 stat |= clReleaseKernel(splineAndSpreadKernel);
75 stat |= clReleaseKernel(splineKernel);
76 stat |= clReleaseKernel(spreadKernel);
77 stat |= clReleaseKernel(gatherKernel);
78 stat |= clReleaseKernel(solveXYZKernel);
79 stat |= clReleaseKernel(solveXYZEnergyKernel);
80 stat |= clReleaseKernel(solveYZXKernel);
81 stat |= clReleaseKernel(solveYZXEnergyKernel);
82 GMX_ASSERT(stat == CL_SUCCESS,
83 gmx::formatString("Failed to release PME OpenCL resources %d: %s", stat,
84 ocl_get_error_string(stat).c_str())
88 /*! \brief Ensure that spread/gather kernels have been compiled to a suitable warp size
90 * On Intel the exec width/warp is decided at compile-time and can be
91 * smaller than the minimum order^2 required in spread/gather ATM which
92 * we need to check for.
94 * Due to the one thread per atom and order=4 implementation
95 * constraints, order^2 threads should execute without synchronization
98 static void checkRequiredWarpSize(cl_kernel kernel, const char* kernelName, const DeviceInformation& deviceInfo)
100 if (deviceInfo.deviceVendor == DeviceVendor::Intel)
102 int kernelWarpSize = gmx::ocl::getKernelWarpSize(kernel, deviceInfo.oclDeviceId);
103 const int minKernelWarpSize = c_pmeGpuOrder * c_pmeGpuOrder;
104 if (kernelWarpSize < minKernelWarpSize)
106 const std::string errorString = gmx::formatString(
107 "PME OpenCL kernels require >=%d execution width, but the %s kernel "
108 "has been compiled for the device %s to a %d width and therefore it can not "
109 "execute correctly.",
110 minKernelWarpSize, kernelName, deviceInfo.device_name, kernelWarpSize);
111 GMX_THROW(gmx::InternalError(errorString));
116 void PmeGpuProgramImpl::compileKernels(const DeviceInformation& deviceInfo)
118 // We might consider storing program as a member variable if it's needed later
119 cl_program program = nullptr;
120 /* Need to catch std::bad_alloc here and during compilation string handling. */
123 /* Here we pass macros and static const int variables defined in include
124 * files outside as macros, to avoid including those files
125 * in the JIT compilation that happens at runtime.
127 const std::string commonDefines = gmx::formatString(
130 "-DthreadsPerAtom=%d "
131 // forwarding from pme_grid.h, used for spline computation table sizes only
132 "-Dc_pmeMaxUnitcellShift=%f "
133 // forwarding PME behavior constants from pme_gpu_constants.h
134 "-Dc_skipNeutralAtoms=%d "
135 "-Dc_virialAndEnergyCount=%d "
136 // forwarding kernel work sizes
137 "-Dc_spreadWorkGroupSize=%zd "
138 "-Dc_solveMaxWorkGroupSize=%zd "
139 "-Dc_gatherWorkGroupSize=%zd "
140 // forwarding from vectypes.h
141 "-DDIM=%d -DXX=%d -DYY=%d -DZZ=%d "
142 // decomposition parameter placeholders
143 "-DwrapX=true -DwrapY=true ",
144 warpSize_, c_pmeGpuOrder, c_pmeGpuOrder * c_pmeGpuOrder,
145 static_cast<float>(c_pmeMaxUnitcellShift), static_cast<int>(c_skipNeutralAtoms),
146 c_virialAndEnergyCount, spreadWorkGroupSize, solveMaxWorkGroupSize,
147 gatherWorkGroupSize, DIM, XX, YY, ZZ);
150 /* TODO when we have a proper MPI-aware logging module,
151 the log output here should be written there */
152 program = gmx::ocl::compileProgram(stderr, "gromacs/ewald", "pme_program.cl",
153 commonDefines, deviceContext_.context(),
154 deviceInfo.oclDeviceId, deviceInfo.deviceVendor);
156 catch (gmx::GromacsException& e)
158 e.prependContext(gmx::formatString("Failed to compile PME kernels for GPU #%s\n",
159 deviceInfo.device_name));
163 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
165 constexpr cl_uint expectedKernelCount = 9;
166 // Has to be equal or larger than the number of kernel instances.
167 // If it is not, CL_INVALID_VALUE will be thrown.
168 std::vector<cl_kernel> kernels(expectedKernelCount, nullptr);
169 cl_uint actualKernelCount = 0;
170 cl_int clError = clCreateKernelsInProgram(program, kernels.size(), kernels.data(), &actualKernelCount);
171 if (clError != CL_SUCCESS)
173 const std::string errorString = gmx::formatString(
174 "Failed to create kernels for PME on GPU #%s:\n OpenCL error %d: %s",
175 deviceInfo.device_name, clError, ocl_get_error_string(clError).c_str());
176 GMX_THROW(gmx::InternalError(errorString));
178 kernels.resize(actualKernelCount);
180 std::array<char, 100> kernelNamesBuffer;
181 for (const auto& kernel : kernels)
183 clError = clGetKernelInfo(kernel, CL_KERNEL_FUNCTION_NAME, kernelNamesBuffer.size(),
184 kernelNamesBuffer.data(), nullptr);
185 if (clError != CL_SUCCESS)
187 const std::string errorString = gmx::formatString(
188 "Failed to parse kernels for PME on GPU #%s:\n OpenCL error %d: %s",
189 deviceInfo.device_name, clError, ocl_get_error_string(clError).c_str());
190 GMX_THROW(gmx::InternalError(errorString));
193 // The names below must correspond to those defined in pme_program.cl
194 // TODO use a map with string key instead?
195 if (!strcmp(kernelNamesBuffer.data(), "pmeSplineKernel"))
197 splineKernel = kernel;
199 else if (!strcmp(kernelNamesBuffer.data(), "pmeSplineAndSpreadKernel"))
201 splineAndSpreadKernel = kernel;
202 splineAndSpreadKernelWriteSplines = kernel;
203 checkRequiredWarpSize(splineAndSpreadKernel, kernelNamesBuffer.data(), deviceInfo);
205 else if (!strcmp(kernelNamesBuffer.data(), "pmeSpreadKernel"))
207 spreadKernel = kernel;
208 checkRequiredWarpSize(spreadKernel, kernelNamesBuffer.data(), deviceInfo);
210 else if (!strcmp(kernelNamesBuffer.data(), "pmeGatherKernel"))
212 gatherKernel = kernel;
213 gatherKernelReadSplines = kernel;
214 checkRequiredWarpSize(gatherKernel, kernelNamesBuffer.data(), deviceInfo);
216 else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveYZXKernel"))
218 solveYZXKernel = kernel;
220 else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveYZXEnergyKernel"))
222 solveYZXEnergyKernel = kernel;
224 else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveXYZKernel"))
226 solveXYZKernel = kernel;
228 else if (!strcmp(kernelNamesBuffer.data(), "pmeSolveXYZEnergyKernel"))
230 solveXYZEnergyKernel = kernel;
233 clReleaseProgram(program);