2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief Define OpenCL implementation of nbnxm_gpu_data_mgmt.h
39 * \author Anca Hamuraru <anca@streamcomputing.eu>
40 * \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
41 * \author Teemu Virolainen <teemu@streamcomputing.eu>
42 * \author Szilárd Páll <pall.szilard@gmail.com>
43 * \ingroup module_nbnxm
55 #include "gromacs/gpu_utils/pmalloc.h"
56 #include "gromacs/hardware/device_information.h"
57 #include "gromacs/hardware/device_management.h"
58 #include "gromacs/math/vectypes.h"
59 #include "gromacs/mdlib/force_flags.h"
60 #include "gromacs/mdtypes/interaction_const.h"
61 #include "gromacs/mdtypes/md_enums.h"
62 #include "gromacs/nbnxm/atomdata.h"
63 #include "gromacs/nbnxm/gpu_data_mgmt.h"
64 #include "gromacs/nbnxm/gpu_jit_support.h"
65 #include "gromacs/nbnxm/nbnxm.h"
66 #include "gromacs/nbnxm/nbnxm_gpu.h"
67 #include "gromacs/nbnxm/nbnxm_gpu_data_mgmt.h"
68 #include "gromacs/nbnxm/pairlistsets.h"
69 #include "gromacs/timing/gpu_timing.h"
70 #include "gromacs/utility/cstringutil.h"
71 #include "gromacs/utility/fatalerror.h"
72 #include "gromacs/utility/gmxassert.h"
73 #include "gromacs/utility/real.h"
74 #include "gromacs/utility/smalloc.h"
76 #include "nbnxm_ocl_types.h"
81 /*! \brief Copies of values from cl_driver_diagnostics_intel.h,
82 * which isn't guaranteed to be available. */
84 #define CL_CONTEXT_SHOW_DIAGNOSTICS_INTEL 0x4106
85 #define CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL 0x1
86 #define CL_CONTEXT_DIAGNOSTICS_LEVEL_BAD_INTEL 0x2
87 #define CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL 0x4
90 /*! \brief This parameter should be determined heuristically from the
91 * kernel execution times
93 * This value is best for small systems on a single AMD Radeon R9 290X
94 * (and about 5% faster than 40, which is the default for CUDA
95 * devices). Larger simulation systems were quite insensitive to the
96 * value of this parameter.
98 static unsigned int gpu_min_ci_balanced_factor = 50;
100 /*! \brief Initializes the OpenCL kernel pointers of the nbnxn_ocl_ptr_t input data structure. */
101 static cl_kernel nbnxn_gpu_create_kernel(NbnxmGpu* nb, const char* kernel_name)
106 kernel = clCreateKernel(nb->dev_rundata->program, kernel_name, &cl_error);
107 if (CL_SUCCESS != cl_error)
110 "Failed to create kernel '%s' for GPU #%s: OpenCL error %d",
112 nb->deviceContext_->deviceInfo().device_name,
119 /*! \brief Initializes the OpenCL kernel pointers of the nbnxn_ocl_ptr_t input data structure. */
120 static void nbnxn_gpu_init_kernels(NbnxmGpu* nb)
122 /* Init to 0 main kernel arrays */
123 /* They will be later on initialized in select_nbnxn_kernel */
124 // TODO: consider always creating all variants of the kernels here so that there is no
125 // need for late call to clCreateKernel -- if that gives any advantage?
126 memset(nb->kernel_ener_noprune_ptr, 0, sizeof(nb->kernel_ener_noprune_ptr));
127 memset(nb->kernel_ener_prune_ptr, 0, sizeof(nb->kernel_ener_prune_ptr));
128 memset(nb->kernel_noener_noprune_ptr, 0, sizeof(nb->kernel_noener_noprune_ptr));
129 memset(nb->kernel_noener_prune_ptr, 0, sizeof(nb->kernel_noener_prune_ptr));
131 /* Init pruning kernels
133 * TODO: we could avoid creating kernels if dynamic pruning is turned off,
134 * but ATM that depends on force flags not passed into the initialization.
136 nb->kernel_pruneonly[epruneFirst] = nbnxn_gpu_create_kernel(nb, "nbnxn_kernel_prune_opencl");
137 nb->kernel_pruneonly[epruneRolling] =
138 nbnxn_gpu_create_kernel(nb, "nbnxn_kernel_prune_rolling_opencl");
141 void gpu_init_platform_specific(NbnxmGpu* nb)
143 /* set device info, just point it to the right GPU among the detected ones */
144 nb->dev_rundata = new gmx_device_runtime_data_t();
146 /* Enable LJ param manual prefetch for AMD or Intel or if we request through env. var.
147 * TODO: decide about NVIDIA
149 nb->bPrefetchLjParam = (getenv("GMX_OCL_DISABLE_I_PREFETCH") == nullptr)
150 && ((nb->deviceContext_->deviceInfo().deviceVendor == DeviceVendor::Amd)
151 || (nb->deviceContext_->deviceInfo().deviceVendor == DeviceVendor::Intel)
152 || (getenv("GMX_OCL_ENABLE_I_PREFETCH") != nullptr));
154 /* NOTE: in CUDA we pick L1 cache configuration for the nbnxn kernels here,
155 * but sadly this is not supported in OpenCL (yet?). Consider adding it if
156 * it becomes supported.
158 nbnxn_gpu_compile_kernels(nb);
159 nbnxn_gpu_init_kernels(nb);
162 /*! \brief Releases an OpenCL kernel pointer */
163 static void free_kernel(cl_kernel* kernel_ptr)
165 GMX_ASSERT(kernel_ptr, "Need a valid kernel pointer");
169 cl_int cl_error = clReleaseKernel(*kernel_ptr);
170 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
171 ("clReleaseKernel failed: " + ocl_get_error_string(cl_error)).c_str());
173 *kernel_ptr = nullptr;
177 /*! \brief Releases a list of OpenCL kernel pointers */
178 static void free_kernels(cl_kernel* kernels, int count)
180 for (int i = 0; i < count; i++)
182 free_kernel(kernels + i);
186 /*! \brief Free the OpenCL program.
188 * The function releases the OpenCL program assuciated with the
189 * device that the calling PP rank is running on.
191 * \param program [in] OpenCL program to release.
193 static void freeGpuProgram(cl_program program)
197 cl_int cl_error = clReleaseProgram(program);
198 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
199 ("clReleaseProgram failed: " + ocl_get_error_string(cl_error)).c_str());
204 //! This function is documented in the header file
205 void gpu_free_platform_specific(NbnxmGpu* nb)
208 // NOLINTNEXTLINE(bugprone-sizeof-expression)
209 int kernel_count = sizeof(nb->kernel_ener_noprune_ptr) / sizeof(nb->kernel_ener_noprune_ptr[0][0]);
210 free_kernels(nb->kernel_ener_noprune_ptr[0], kernel_count);
212 // NOLINTNEXTLINE(bugprone-sizeof-expression)
213 kernel_count = sizeof(nb->kernel_ener_prune_ptr) / sizeof(nb->kernel_ener_prune_ptr[0][0]);
214 free_kernels(nb->kernel_ener_prune_ptr[0], kernel_count);
216 // NOLINTNEXTLINE(bugprone-sizeof-expression)
217 kernel_count = sizeof(nb->kernel_noener_noprune_ptr) / sizeof(nb->kernel_noener_noprune_ptr[0][0]);
218 free_kernels(nb->kernel_noener_noprune_ptr[0], kernel_count);
220 // NOLINTNEXTLINE(bugprone-sizeof-expression)
221 kernel_count = sizeof(nb->kernel_noener_prune_ptr) / sizeof(nb->kernel_noener_prune_ptr[0][0]);
222 free_kernels(nb->kernel_noener_prune_ptr[0], kernel_count);
224 freeGpuProgram(nb->dev_rundata->program);
225 delete nb->dev_rundata;
228 //! This function is documented in the header file
229 int gpu_min_ci_balanced(NbnxmGpu* nb)
231 return nb != nullptr ? gpu_min_ci_balanced_factor * nb->deviceContext_->deviceInfo().compute_units : 0;