2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, 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.
36 * \brief Define OpenCL implementation of nbnxm_gpu_data_mgmt.h
38 * \author Anca Hamuraru <anca@streamcomputing.eu>
39 * \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
40 * \author Teemu Virolainen <teemu@streamcomputing.eu>
41 * \author Szilárd Páll <pall.szilard@gmail.com>
42 * \ingroup module_nbnxm
54 // TODO We would like to move this down, but the way gmx_nbnxn_gpu_t
55 // is currently declared means this has to be before gpu_types.h
56 #include "nbnxm_ocl_types.h"
58 // TODO Remove this comment when the above order issue is resolved
59 #include "gromacs/gpu_utils/gpu_utils.h"
60 #include "gromacs/gpu_utils/oclutils.h"
61 #include "gromacs/hardware/gpu_hw_info.h"
62 #include "gromacs/math/vectypes.h"
63 #include "gromacs/mdlib/force_flags.h"
64 #include "gromacs/mdtypes/interaction_const.h"
65 #include "gromacs/mdtypes/md_enums.h"
66 #include "gromacs/nbnxm/atomdata.h"
67 #include "gromacs/nbnxm/gpu_data_mgmt.h"
68 #include "gromacs/nbnxm/gpu_jit_support.h"
69 #include "gromacs/nbnxm/nbnxm.h"
70 #include "gromacs/nbnxm/nbnxm_gpu.h"
71 #include "gromacs/nbnxm/pairlistsets.h"
72 #include "gromacs/pbcutil/ishift.h"
73 #include "gromacs/timing/gpu_timing.h"
74 #include "gromacs/utility/cstringutil.h"
75 #include "gromacs/utility/fatalerror.h"
76 #include "gromacs/utility/gmxassert.h"
77 #include "gromacs/utility/real.h"
78 #include "gromacs/utility/smalloc.h"
80 #include "nbnxm_ocl_internal.h"
85 /*! \brief Copies of values from cl_driver_diagnostics_intel.h,
86 * which isn't guaranteed to be available. */
88 #define CL_CONTEXT_SHOW_DIAGNOSTICS_INTEL 0x4106
89 #define CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL 0x1
90 #define CL_CONTEXT_DIAGNOSTICS_LEVEL_BAD_INTEL 0x2
91 #define CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL 0x4
94 /*! \brief This parameter should be determined heuristically from the
95 * kernel execution times
97 * This value is best for small systems on a single AMD Radeon R9 290X
98 * (and about 5% faster than 40, which is the default for CUDA
99 * devices). Larger simulation systems were quite insensitive to the
100 * value of this parameter.
102 static unsigned int gpu_min_ci_balanced_factor = 50;
105 /*! \brief Returns true if LJ combination rules are used in the non-bonded kernels.
107 * Full doc in nbnxn_ocl_internal.h */
108 bool useLjCombRule(int vdwType)
110 return (vdwType == evdwOclCUTCOMBGEOM ||
111 vdwType == evdwOclCUTCOMBLB);
114 /*! \brief Tabulates the Ewald Coulomb force and initializes the size/scale
115 * and the table GPU array.
117 * If called with an already allocated table, it just re-uploads the
120 static void init_ewald_coulomb_force_table(const EwaldCorrectionTables &tables,
122 const gmx_device_runtime_data_t *runData)
128 if (nbp->coulomb_tab_climg2d != nullptr)
130 freeDeviceBuffer(&(nbp->coulomb_tab_climg2d));
133 /* Switched from using textures to using buffers */
134 // TODO: decide which alternative is most efficient - textures or buffers.
136 cl_image_format array_format;
138 array_format.image_channel_data_type = CL_FLOAT;
139 array_format.image_channel_order = CL_R;
141 coul_tab = clCreateImage2D(runData->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
142 &array_format, tabsize, 1, 0, ftmp, &cl_error);
145 coul_tab = clCreateBuffer(runData->context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
146 tables.tableF.size()*sizeof(cl_float), const_cast<real *>(tables.tableF.data()), &cl_error);
147 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
148 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
150 nbp->coulomb_tab_climg2d = coul_tab;
151 nbp->coulomb_tab_scale = tables.scale;
155 /*! \brief Initializes the atomdata structure first time, it only gets filled at
158 static void init_atomdata_first(cl_atomdata_t *ad, int ntypes, gmx_device_runtime_data_t *runData)
164 /* An element of the shift_vec device buffer has the same size as one element
165 of the host side shift_vec buffer. */
166 ad->shift_vec_elem_size = sizeof(*nbnxn_atomdata_t::shift_vec.data());
168 ad->shift_vec = clCreateBuffer(runData->context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY,
169 SHIFTS * ad->shift_vec_elem_size, nullptr, &cl_error);
170 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
171 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
172 ad->bShiftVecUploaded = CL_FALSE;
174 /* An element of the fshift device buffer has the same size as one element
175 of the host side fshift buffer. */
176 ad->fshift_elem_size = sizeof(*cl_nb_staging_t::fshift);
178 ad->fshift = clCreateBuffer(runData->context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY,
179 SHIFTS * ad->fshift_elem_size, nullptr, &cl_error);
180 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
181 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
183 ad->e_lj = clCreateBuffer(runData->context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY,
184 sizeof(float), nullptr, &cl_error);
185 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
186 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
188 ad->e_el = clCreateBuffer(runData->context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY,
189 sizeof(float), nullptr, &cl_error);
190 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
191 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
193 /* initialize to nullptr pointers to data that is not allocated here and will
194 need reallocation in nbnxn_gpu_init_atomdata */
198 /* size -1 indicates that the respective array hasn't been initialized yet */
203 /*! \brief Copies all parameters related to the cut-off from ic to nbp
205 static void set_cutoff_parameters(cl_nbparam_t *nbp,
206 const interaction_const_t *ic,
207 const PairlistParams &listParams)
209 nbp->ewald_beta = ic->ewaldcoeff_q;
210 nbp->sh_ewald = ic->sh_ewald;
211 nbp->epsfac = ic->epsfac;
212 nbp->two_k_rf = 2.0 * ic->k_rf;
213 nbp->c_rf = ic->c_rf;
214 nbp->rvdw_sq = ic->rvdw * ic->rvdw;
215 nbp->rcoulomb_sq = ic->rcoulomb * ic->rcoulomb;
216 nbp->rlistOuter_sq = listParams.rlistOuter * listParams.rlistOuter;
217 nbp->rlistInner_sq = listParams.rlistInner * listParams.rlistInner;
218 nbp->useDynamicPruning = listParams.useDynamicPruning;
220 nbp->sh_lj_ewald = ic->sh_lj_ewald;
221 nbp->ewaldcoeff_lj = ic->ewaldcoeff_lj;
223 nbp->rvdw_switch = ic->rvdw_switch;
224 nbp->dispersion_shift = ic->dispersion_shift;
225 nbp->repulsion_shift = ic->repulsion_shift;
226 nbp->vdw_switch = ic->vdw_switch;
229 /*! \brief Returns the kinds of electrostatics and Vdw OpenCL
230 * kernels that will be used.
232 * Respectively, these values are from enum eelOcl and enum
235 map_interaction_types_to_gpu_kernel_flavors(const interaction_const_t *ic,
240 if (ic->vdwtype == evdwCUT)
242 switch (ic->vdw_modifier)
245 case eintmodPOTSHIFT:
249 *gpu_vdwtype = evdwOclCUT;
252 *gpu_vdwtype = evdwOclCUTCOMBGEOM;
255 *gpu_vdwtype = evdwOclCUTCOMBLB;
258 gmx_incons("The requested LJ combination rule is not implemented in the OpenCL GPU accelerated kernels!");
261 case eintmodFORCESWITCH:
262 *gpu_vdwtype = evdwOclFSWITCH;
264 case eintmodPOTSWITCH:
265 *gpu_vdwtype = evdwOclPSWITCH;
268 gmx_incons("The requested VdW interaction modifier is not implemented in the GPU accelerated kernels!");
271 else if (ic->vdwtype == evdwPME)
273 if (ic->ljpme_comb_rule == ljcrGEOM)
275 *gpu_vdwtype = evdwOclEWALDGEOM;
279 *gpu_vdwtype = evdwOclEWALDLB;
284 gmx_incons("The requested VdW type is not implemented in the GPU accelerated kernels!");
287 if (ic->eeltype == eelCUT)
289 *gpu_eeltype = eelOclCUT;
291 else if (EEL_RF(ic->eeltype))
293 *gpu_eeltype = eelOclRF;
295 else if ((EEL_PME(ic->eeltype) || ic->eeltype == eelEWALD))
297 *gpu_eeltype = nbnxn_gpu_pick_ewald_kernel_type(*ic);
301 /* Shouldn't happen, as this is checked when choosing Verlet-scheme */
302 gmx_incons("The requested electrostatics type is not implemented in the GPU accelerated kernels!");
306 /*! \brief Initializes the nonbonded parameter data structure.
308 static void init_nbparam(cl_nbparam_t *nbp,
309 const interaction_const_t *ic,
310 const PairlistParams &listParams,
311 const nbnxn_atomdata_t::Params &nbatParams,
312 const gmx_device_runtime_data_t *runData)
316 set_cutoff_parameters(nbp, ic, listParams);
318 map_interaction_types_to_gpu_kernel_flavors(ic,
319 nbatParams.comb_rule,
323 if (ic->vdwtype == evdwPME)
325 if (ic->ljpme_comb_rule == ljcrGEOM)
327 GMX_ASSERT(nbatParams.comb_rule == ljcrGEOM, "Combination rule mismatch!");
331 GMX_ASSERT(nbatParams.comb_rule == ljcrLB, "Combination rule mismatch!");
334 /* generate table for PME */
335 nbp->coulomb_tab_climg2d = nullptr;
336 if (nbp->eeltype == eelOclEWALD_TAB || nbp->eeltype == eelOclEWALD_TAB_TWIN)
338 GMX_RELEASE_ASSERT(ic->coulombEwaldTables, "Need valid Coulomb Ewald correction tables");
339 init_ewald_coulomb_force_table(*ic->coulombEwaldTables, nbp, runData);
342 // TODO: improvement needed.
343 // The image2d is created here even if eeltype is not eelCuEWALD_TAB or eelCuEWALD_TAB_TWIN because the OpenCL kernels
344 // don't accept nullptr values for image2D parameters.
346 /* Switched from using textures to using buffers */
347 // TODO: decide which alternative is most efficient - textures or buffers.
349 cl_image_format array_format;
351 array_format.image_channel_data_type = CL_FLOAT;
352 array_format.image_channel_order = CL_R;
354 nbp->coulomb_tab_climg2d = clCreateImage2D(runData->context, CL_MEM_READ_WRITE,
355 &array_format, 1, 1, 0, nullptr, &cl_error);
358 nbp->coulomb_tab_climg2d = clCreateBuffer(runData->context, CL_MEM_READ_ONLY, sizeof(cl_float), nullptr, &cl_error);
359 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
360 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
363 const int nnbfp = 2*nbatParams.numTypes*nbatParams.numTypes;
364 const int nnbfp_comb = 2*nbatParams.numTypes;
367 /* Switched from using textures to using buffers */
368 // TODO: decide which alternative is most efficient - textures or buffers.
370 cl_image_format array_format;
372 array_format.image_channel_data_type = CL_FLOAT;
373 array_format.image_channel_order = CL_R;
375 nbp->nbfp_climg2d = clCreateImage2D(runData->context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
376 &array_format, nnbfp, 1, 0, nbat->nbfp, &cl_error);
380 clCreateBuffer(runData->context,
381 CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
382 nnbfp*sizeof(cl_float),
383 const_cast<float *>(nbatParams.nbfp.data()),
385 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
386 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
388 if (ic->vdwtype == evdwPME)
390 /* Switched from using textures to using buffers */
391 // TODO: decide which alternative is most efficient - textures or buffers.
392 /* nbp->nbfp_comb_climg2d = clCreateImage2D(runData->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
393 &array_format, nnbfp_comb, 1, 0, nbat->nbfp_comb, &cl_error);*/
394 nbp->nbfp_comb_climg2d =
395 clCreateBuffer(runData->context,
396 CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
397 nnbfp_comb*sizeof(cl_float),
398 const_cast<float *>(nbatParams.nbfp_comb.data()),
400 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
401 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
405 // TODO: improvement needed.
406 // The image2d is created here even if vdwtype is not evdwPME because the OpenCL kernels
407 // don't accept nullptr values for image2D parameters.
408 /* Switched from using textures to using buffers */
409 // TODO: decide which alternative is most efficient - textures or buffers.
410 /* nbp->nbfp_comb_climg2d = clCreateImage2D(runData->context, CL_MEM_READ_WRITE,
411 &array_format, 1, 1, 0, nullptr, &cl_error);*/
412 nbp->nbfp_comb_climg2d = clCreateBuffer(runData->context, CL_MEM_READ_ONLY, sizeof(cl_float), nullptr, &cl_error);
413 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
414 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
419 //! This function is documented in the header file
420 void gpu_pme_loadbal_update_param(const nonbonded_verlet_t *nbv,
421 const interaction_const_t *ic)
423 if (!nbv || !nbv->useGpu())
427 gmx_nbnxn_ocl_t *nb = nbv->gpu_nbv;
428 cl_nbparam_t *nbp = nb->nbparam;
430 set_cutoff_parameters(nbp, ic, nbv->pairlistSets().params());
432 nbp->eeltype = nbnxn_gpu_pick_ewald_kernel_type(*ic);
434 GMX_RELEASE_ASSERT(ic->coulombEwaldTables, "Need valid Coulomb Ewald correction tables");
435 init_ewald_coulomb_force_table(*ic->coulombEwaldTables, nbp, nb->dev_rundata);
438 /*! \brief Initializes the pair list data structure.
440 static void init_plist(cl_plist_t *pl)
442 /* initialize to nullptr pointers to data that is not allocated here and will
443 need reallocation in nbnxn_gpu_init_pairlist */
449 /* size -1 indicates that the respective array hasn't been initialized yet */
456 pl->imask_nalloc = -1;
458 pl->excl_nalloc = -1;
459 pl->haveFreshList = false;
462 /*! \brief Initializes the timings data structure.
464 static void init_timings(gmx_wallclock_gpu_nbnxn_t *t)
473 for (i = 0; i < 2; i++)
475 for (j = 0; j < 2; j++)
477 t->ktime[i][j].t = 0.0;
478 t->ktime[i][j].c = 0;
483 t->pruneTime.t = 0.0;
484 t->dynamicPruneTime.c = 0;
485 t->dynamicPruneTime.t = 0.0;
489 //! OpenCL notification callback function
490 static void CL_CALLBACK
491 ocl_notify_fn(const char *pErrInfo, const void gmx_unused *private_info, size_t gmx_unused cb, void gmx_unused *user_data)
493 if (pErrInfo != nullptr)
495 printf("%s\n", pErrInfo ); // Print error/hint
499 /*! \brief Creates context for OpenCL GPU given by \p mygpu
501 * A fatal error results if creation fails.
503 * \param[inout] runtimeData runtime data including program and context
504 * \param[in] devInfo device info struct
505 * \param[in] rank MPI rank (for error reporting)
508 nbnxn_gpu_create_context(gmx_device_runtime_data_t *runtimeData,
509 const gmx_device_info_t *devInfo,
512 cl_context_properties context_properties[5];
513 cl_platform_id platform_id;
514 cl_device_id device_id;
518 assert(runtimeData != nullptr);
519 assert(devInfo != nullptr);
521 platform_id = devInfo->ocl_gpu_id.ocl_platform_id;
522 device_id = devInfo->ocl_gpu_id.ocl_device_id;
525 context_properties[i++] = CL_CONTEXT_PLATFORM;
526 context_properties[i++] = reinterpret_cast<cl_context_properties>(platform_id);
527 if (getenv("GMX_OCL_SHOW_DIAGNOSTICS"))
529 context_properties[i++] = CL_CONTEXT_SHOW_DIAGNOSTICS_INTEL;
530 context_properties[i++] = CL_CONTEXT_DIAGNOSTICS_LEVEL_BAD_INTEL | CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL;
532 context_properties[i++] = 0; /* Terminates the list of properties */
534 context = clCreateContext(context_properties, 1, &device_id, ocl_notify_fn, nullptr, &cl_error);
535 if (CL_SUCCESS != cl_error)
537 gmx_fatal(FARGS, "On rank %d failed to create context for GPU #%s:\n OpenCL error %d: %s",
539 devInfo->device_name,
540 cl_error, ocl_get_error_string(cl_error).c_str());
543 runtimeData->context = context;
546 /*! \brief Initializes the OpenCL kernel pointers of the nbnxn_ocl_ptr_t input data structure. */
547 static cl_kernel nbnxn_gpu_create_kernel(gmx_nbnxn_ocl_t *nb,
548 const char *kernel_name)
553 kernel = clCreateKernel(nb->dev_rundata->program, kernel_name, &cl_error);
554 if (CL_SUCCESS != cl_error)
556 gmx_fatal(FARGS, "Failed to create kernel '%s' for GPU #%s: OpenCL error %d",
558 nb->dev_info->device_name,
565 /*! \brief Clears nonbonded shift force output array and energy outputs on the GPU.
568 nbnxn_ocl_clear_e_fshift(gmx_nbnxn_ocl_t *nb)
572 cl_atomdata_t * adat = nb->atdat;
573 cl_command_queue ls = nb->stream[InteractionLocality::Local];
575 size_t local_work_size[3] = {1, 1, 1};
576 size_t global_work_size[3] = {1, 1, 1};
578 cl_int shifts = SHIFTS*3;
582 cl_kernel zero_e_fshift = nb->kernel_zero_e_fshift;
584 local_work_size[0] = 64;
585 // Round the total number of threads up from the array size
586 global_work_size[0] = ((shifts + local_work_size[0] - 1)/local_work_size[0])*local_work_size[0];
589 cl_error = clSetKernelArg(zero_e_fshift, arg_no++, sizeof(cl_mem), &(adat->fshift));
590 cl_error |= clSetKernelArg(zero_e_fshift, arg_no++, sizeof(cl_mem), &(adat->e_lj));
591 cl_error |= clSetKernelArg(zero_e_fshift, arg_no++, sizeof(cl_mem), &(adat->e_el));
592 cl_error |= clSetKernelArg(zero_e_fshift, arg_no++, sizeof(cl_uint), &shifts);
593 GMX_ASSERT(cl_error == CL_SUCCESS, ocl_get_error_string(cl_error).c_str());
595 cl_error = clEnqueueNDRangeKernel(ls, zero_e_fshift, 3, nullptr, global_work_size, local_work_size, 0, nullptr, nullptr);
596 GMX_ASSERT(cl_error == CL_SUCCESS, ocl_get_error_string(cl_error).c_str());
599 /*! \brief Initializes the OpenCL kernel pointers of the nbnxn_ocl_ptr_t input data structure. */
600 static void nbnxn_gpu_init_kernels(gmx_nbnxn_ocl_t *nb)
602 /* Init to 0 main kernel arrays */
603 /* They will be later on initialized in select_nbnxn_kernel */
604 // TODO: consider always creating all variants of the kernels here so that there is no
605 // need for late call to clCreateKernel -- if that gives any advantage?
606 memset(nb->kernel_ener_noprune_ptr, 0, sizeof(nb->kernel_ener_noprune_ptr));
607 memset(nb->kernel_ener_prune_ptr, 0, sizeof(nb->kernel_ener_prune_ptr));
608 memset(nb->kernel_noener_noprune_ptr, 0, sizeof(nb->kernel_noener_noprune_ptr));
609 memset(nb->kernel_noener_prune_ptr, 0, sizeof(nb->kernel_noener_prune_ptr));
611 /* Init pruning kernels
613 * TODO: we could avoid creating kernels if dynamic pruning is turned off,
614 * but ATM that depends on force flags not passed into the initialization.
616 nb->kernel_pruneonly[epruneFirst] = nbnxn_gpu_create_kernel(nb, "nbnxn_kernel_prune_opencl");
617 nb->kernel_pruneonly[epruneRolling] = nbnxn_gpu_create_kernel(nb, "nbnxn_kernel_prune_rolling_opencl");
619 /* Init auxiliary kernels */
620 nb->kernel_zero_e_fshift = nbnxn_gpu_create_kernel(nb, "zero_e_fshift");
623 /*! \brief Initializes simulation constant data.
625 * Initializes members of the atomdata and nbparam structs and
626 * clears e/fshift output buffers.
628 static void nbnxn_ocl_init_const(gmx_nbnxn_ocl_t *nb,
629 const interaction_const_t *ic,
630 const PairlistParams &listParams,
631 const nbnxn_atomdata_t::Params &nbatParams)
633 init_atomdata_first(nb->atdat, nbatParams.numTypes, nb->dev_rundata);
634 init_nbparam(nb->nbparam, ic, listParams, nbatParams, nb->dev_rundata);
638 //! This function is documented in the header file
640 gpu_init(const gmx_device_info_t *deviceInfo,
641 const interaction_const_t *ic,
642 const PairlistParams &listParams,
643 const nbnxn_atomdata_t *nbat,
645 const gmx_bool bLocalAndNonlocal)
649 cl_command_queue_properties queue_properties;
655 snew(nb->nbparam, 1);
656 snew(nb->plist[InteractionLocality::Local], 1);
657 if (bLocalAndNonlocal)
659 snew(nb->plist[InteractionLocality::NonLocal], 1);
662 nb->bUseTwoStreams = static_cast<cl_bool>(bLocalAndNonlocal);
664 nb->timers = new cl_timers_t();
665 snew(nb->timings, 1);
667 /* set device info, just point it to the right GPU among the detected ones */
668 nb->dev_info = deviceInfo;
669 snew(nb->dev_rundata, 1);
672 pmalloc(reinterpret_cast<void**>(&nb->nbst.e_lj), sizeof(*nb->nbst.e_lj));
673 pmalloc(reinterpret_cast<void**>(&nb->nbst.e_el), sizeof(*nb->nbst.e_el));
674 pmalloc(reinterpret_cast<void**>(&nb->nbst.fshift), SHIFTS * sizeof(*nb->nbst.fshift));
676 init_plist(nb->plist[InteractionLocality::Local]);
678 /* OpenCL timing disabled if GMX_DISABLE_GPU_TIMING is defined. */
679 nb->bDoTime = static_cast<cl_bool>(getenv("GMX_DISABLE_GPU_TIMING") == nullptr);
681 /* Create queues only after bDoTime has been initialized */
684 queue_properties = CL_QUEUE_PROFILING_ENABLE;
688 queue_properties = 0;
691 nbnxn_gpu_create_context(nb->dev_rundata, nb->dev_info, rank);
693 /* local/non-local GPU streams */
694 nb->stream[InteractionLocality::Local] =
695 clCreateCommandQueue(nb->dev_rundata->context, nb->dev_info->ocl_gpu_id.ocl_device_id, queue_properties, &cl_error);
696 if (CL_SUCCESS != cl_error)
698 gmx_fatal(FARGS, "On rank %d failed to create context for GPU #%s: OpenCL error %d",
700 nb->dev_info->device_name,
704 if (nb->bUseTwoStreams)
706 init_plist(nb->plist[InteractionLocality::NonLocal]);
708 nb->stream[InteractionLocality::NonLocal] =
709 clCreateCommandQueue(nb->dev_rundata->context, nb->dev_info->ocl_gpu_id.ocl_device_id, queue_properties, &cl_error);
710 if (CL_SUCCESS != cl_error)
712 gmx_fatal(FARGS, "On rank %d failed to create context for GPU #%s: OpenCL error %d",
714 nb->dev_info->device_name,
721 init_timings(nb->timings);
724 nbnxn_ocl_init_const(nb, ic, listParams, nbat->params());
726 /* Enable LJ param manual prefetch for AMD or Intel or if we request through env. var.
727 * TODO: decide about NVIDIA
729 nb->bPrefetchLjParam =
730 (getenv("GMX_OCL_DISABLE_I_PREFETCH") == nullptr) &&
731 ((nb->dev_info->vendor_e == OCL_VENDOR_AMD) || (nb->dev_info->vendor_e == OCL_VENDOR_INTEL)
732 || (getenv("GMX_OCL_ENABLE_I_PREFETCH") != nullptr));
734 /* NOTE: in CUDA we pick L1 cache configuration for the nbnxn kernels here,
735 * but sadly this is not supported in OpenCL (yet?). Consider adding it if
736 * it becomes supported.
738 nbnxn_gpu_compile_kernels(nb);
739 nbnxn_gpu_init_kernels(nb);
741 /* clear energy and shift force outputs */
742 nbnxn_ocl_clear_e_fshift(nb);
746 fprintf(debug, "Initialized OpenCL data structures.\n");
752 /*! \brief Clears the first natoms_clear elements of the GPU nonbonded force output array.
754 static void nbnxn_ocl_clear_f(gmx_nbnxn_ocl_t *nb, int natoms_clear)
756 if (natoms_clear == 0)
761 cl_int gmx_used_in_debug cl_error;
763 cl_atomdata_t *atomData = nb->atdat;
764 cl_command_queue ls = nb->stream[InteractionLocality::Local];
765 cl_float value = 0.0F;
767 cl_error = clEnqueueFillBuffer(ls, atomData->f, &value, sizeof(cl_float),
768 0, natoms_clear*sizeof(rvec), 0, nullptr, nullptr);
769 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS, ("clEnqueueFillBuffer failed: " +
770 ocl_get_error_string(cl_error)).c_str());
773 //! This function is documented in the header file
774 void gpu_clear_outputs(gmx_nbnxn_ocl_t *nb,
777 nbnxn_ocl_clear_f(nb, nb->atdat->natoms);
778 /* clear shift force array and energies if the outputs were
779 used in the current step */
782 nbnxn_ocl_clear_e_fshift(nb);
785 /* kick off buffer clearing kernel to ensure concurrency with constraints/update */
786 cl_int gmx_unused cl_error;
787 cl_error = clFlush(nb->stream[InteractionLocality::Local]);
788 assert(CL_SUCCESS == cl_error);
791 //! This function is documented in the header file
792 void gpu_init_pairlist(gmx_nbnxn_ocl_t *nb,
793 const NbnxnPairlistGpu *h_plist,
794 const InteractionLocality iloc)
797 // Timing accumulation should happen only if there was work to do
798 // because getLastRangeTime() gets skipped with empty lists later
799 // which leads to the counter not being reset.
800 bool bDoTime = ((nb->bDoTime == CL_TRUE) && !h_plist->sci.empty());
801 cl_command_queue stream = nb->stream[iloc];
802 cl_plist_t *d_plist = nb->plist[iloc];
804 if (d_plist->na_c < 0)
806 d_plist->na_c = h_plist->na_ci;
810 if (d_plist->na_c != h_plist->na_ci)
812 sprintf(sbuf, "In cu_init_plist: the #atoms per cell has changed (from %d to %d)",
813 d_plist->na_c, h_plist->na_ci);
818 gpu_timers_t::Interaction &iTimers = nb->timers->interaction[iloc];
822 iTimers.pl_h2d.openTimingRegion(stream);
823 iTimers.didPairlistH2D = true;
826 // TODO most of this function is same in CUDA and OpenCL, move into the header
827 DeviceContext context = nb->dev_rundata->context;
829 reallocateDeviceBuffer(&d_plist->sci, h_plist->sci.size(),
830 &d_plist->nsci, &d_plist->sci_nalloc, context);
831 copyToDeviceBuffer(&d_plist->sci, h_plist->sci.data(), 0, h_plist->sci.size(),
832 stream, GpuApiCallBehavior::Async,
833 bDoTime ? iTimers.pl_h2d.fetchNextEvent() : nullptr);
835 reallocateDeviceBuffer(&d_plist->cj4, h_plist->cj4.size(),
836 &d_plist->ncj4, &d_plist->cj4_nalloc, context);
837 copyToDeviceBuffer(&d_plist->cj4, h_plist->cj4.data(), 0, h_plist->cj4.size(),
838 stream, GpuApiCallBehavior::Async,
839 bDoTime ? iTimers.pl_h2d.fetchNextEvent() : nullptr);
841 reallocateDeviceBuffer(&d_plist->imask, h_plist->cj4.size()*c_nbnxnGpuClusterpairSplit,
842 &d_plist->nimask, &d_plist->imask_nalloc, context);
844 reallocateDeviceBuffer(&d_plist->excl, h_plist->excl.size(),
845 &d_plist->nexcl, &d_plist->excl_nalloc, context);
846 copyToDeviceBuffer(&d_plist->excl, h_plist->excl.data(), 0, h_plist->excl.size(),
847 stream, GpuApiCallBehavior::Async,
848 bDoTime ? iTimers.pl_h2d.fetchNextEvent() : nullptr);
852 iTimers.pl_h2d.closeTimingRegion(stream);
855 /* need to prune the pair list during the next step */
856 d_plist->haveFreshList = true;
859 //! This function is documented in the header file
860 void gpu_upload_shiftvec(gmx_nbnxn_ocl_t *nb,
861 const nbnxn_atomdata_t *nbatom)
863 cl_atomdata_t *adat = nb->atdat;
864 cl_command_queue ls = nb->stream[InteractionLocality::Local];
866 /* only if we have a dynamic box */
867 if (nbatom->bDynamicBox || !adat->bShiftVecUploaded)
869 ocl_copy_H2D_async(adat->shift_vec, nbatom->shift_vec.data(), 0,
870 SHIFTS * adat->shift_vec_elem_size, ls, nullptr);
871 adat->bShiftVecUploaded = CL_TRUE;
875 //! This function is documented in the header file
876 void gpu_init_atomdata(gmx_nbnxn_ocl_t *nb,
877 const nbnxn_atomdata_t *nbat)
882 bool bDoTime = nb->bDoTime == CL_TRUE;
883 cl_timers_t *timers = nb->timers;
884 cl_atomdata_t *d_atdat = nb->atdat;
885 cl_command_queue ls = nb->stream[InteractionLocality::Local];
887 natoms = nbat->numAtoms();
892 /* time async copy */
893 timers->atdat.openTimingRegion(ls);
896 /* need to reallocate if we have to copy more atoms than the amount of space
897 available and only allocate if we haven't initialized yet, i.e d_atdat->natoms == -1 */
898 if (natoms > d_atdat->nalloc)
900 nalloc = over_alloc_small(natoms);
902 /* free up first if the arrays have already been initialized */
903 if (d_atdat->nalloc != -1)
905 freeDeviceBuffer(&d_atdat->f);
906 freeDeviceBuffer(&d_atdat->xq);
907 freeDeviceBuffer(&d_atdat->lj_comb);
908 freeDeviceBuffer(&d_atdat->atom_types);
911 d_atdat->f_elem_size = sizeof(rvec);
913 d_atdat->f = clCreateBuffer(nb->dev_rundata->context, CL_MEM_READ_WRITE | CL_MEM_HOST_READ_ONLY,
914 nalloc * d_atdat->f_elem_size, nullptr, &cl_error);
915 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
916 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
918 d_atdat->xq = clCreateBuffer(nb->dev_rundata->context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY,
919 nalloc * sizeof(cl_float4), nullptr, &cl_error);
920 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
921 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
923 if (useLjCombRule(nb->nbparam->vdwtype))
925 d_atdat->lj_comb = clCreateBuffer(nb->dev_rundata->context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY,
926 nalloc * sizeof(cl_float2), nullptr, &cl_error);
927 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
928 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
932 d_atdat->atom_types = clCreateBuffer(nb->dev_rundata->context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY,
933 nalloc * sizeof(int), nullptr, &cl_error);
934 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
935 ("clCreateBuffer failed: " + ocl_get_error_string(cl_error)).c_str());
938 d_atdat->nalloc = nalloc;
942 d_atdat->natoms = natoms;
943 d_atdat->natoms_local = nbat->natoms_local;
945 /* need to clear GPU f output if realloc happened */
948 nbnxn_ocl_clear_f(nb, nalloc);
951 if (useLjCombRule(nb->nbparam->vdwtype))
953 ocl_copy_H2D_async(d_atdat->lj_comb, nbat->params().lj_comb.data(), 0,
954 natoms*sizeof(cl_float2), ls, bDoTime ? timers->atdat.fetchNextEvent() : nullptr);
958 ocl_copy_H2D_async(d_atdat->atom_types, nbat->params().type.data(), 0,
959 natoms*sizeof(int), ls, bDoTime ? timers->atdat.fetchNextEvent() : nullptr);
965 timers->atdat.closeTimingRegion(ls);
968 /* kick off the tasks enqueued above to ensure concurrency with the search */
969 cl_error = clFlush(ls);
970 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS,
971 ("clFlush failed: " + ocl_get_error_string(cl_error)).c_str());
974 /*! \brief Releases an OpenCL kernel pointer */
975 static void free_kernel(cl_kernel *kernel_ptr)
977 cl_int gmx_unused cl_error;
979 assert(nullptr != kernel_ptr);
983 cl_error = clReleaseKernel(*kernel_ptr);
984 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS, ("clReleaseKernel failed: " + ocl_get_error_string(cl_error)).c_str());
986 *kernel_ptr = nullptr;
990 /*! \brief Releases a list of OpenCL kernel pointers */
991 static void free_kernels(cl_kernel *kernels, int count)
995 for (i = 0; i < count; i++)
997 free_kernel(kernels + i);
1001 /*! \brief Free the OpenCL runtime data (context and program).
1003 * The function releases the OpenCL context and program assuciated with the
1004 * device that the calling PP rank is running on.
1006 * \param runData [in] porinter to the structure with runtime data.
1008 static void free_gpu_device_runtime_data(gmx_device_runtime_data_t *runData)
1010 if (runData == nullptr)
1015 cl_int gmx_unused cl_error;
1017 if (runData->context)
1019 cl_error = clReleaseContext(runData->context);
1020 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS, ("clReleaseContext failed: " + ocl_get_error_string(cl_error)).c_str());
1021 runData->context = nullptr;
1024 if (runData->program)
1026 cl_error = clReleaseProgram(runData->program);
1027 GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS, ("clReleaseProgram failed: " + ocl_get_error_string(cl_error)).c_str());
1028 runData->program = nullptr;
1033 //! This function is documented in the header file
1034 void gpu_free(gmx_nbnxn_ocl_t *nb)
1042 int kernel_count = sizeof(nb->kernel_ener_noprune_ptr) / sizeof(nb->kernel_ener_noprune_ptr[0][0]);
1043 free_kernels(nb->kernel_ener_noprune_ptr[0], kernel_count);
1045 kernel_count = sizeof(nb->kernel_ener_prune_ptr) / sizeof(nb->kernel_ener_prune_ptr[0][0]);
1046 free_kernels(nb->kernel_ener_prune_ptr[0], kernel_count);
1048 kernel_count = sizeof(nb->kernel_noener_noprune_ptr) / sizeof(nb->kernel_noener_noprune_ptr[0][0]);
1049 free_kernels(nb->kernel_noener_noprune_ptr[0], kernel_count);
1051 kernel_count = sizeof(nb->kernel_noener_prune_ptr) / sizeof(nb->kernel_noener_prune_ptr[0][0]);
1052 free_kernels(nb->kernel_noener_prune_ptr[0], kernel_count);
1054 free_kernel(&(nb->kernel_zero_e_fshift));
1057 freeDeviceBuffer(&(nb->atdat->xq));
1058 freeDeviceBuffer(&(nb->atdat->f));
1059 freeDeviceBuffer(&(nb->atdat->e_lj));
1060 freeDeviceBuffer(&(nb->atdat->e_el));
1061 freeDeviceBuffer(&(nb->atdat->fshift));
1062 freeDeviceBuffer(&(nb->atdat->lj_comb));
1063 freeDeviceBuffer(&(nb->atdat->atom_types));
1064 freeDeviceBuffer(&(nb->atdat->shift_vec));
1068 freeDeviceBuffer(&(nb->nbparam->nbfp_climg2d));
1069 freeDeviceBuffer(&(nb->nbparam->nbfp_comb_climg2d));
1070 freeDeviceBuffer(&(nb->nbparam->coulomb_tab_climg2d));
1074 auto *plist = nb->plist[InteractionLocality::Local];
1075 freeDeviceBuffer(&plist->sci);
1076 freeDeviceBuffer(&plist->cj4);
1077 freeDeviceBuffer(&plist->imask);
1078 freeDeviceBuffer(&plist->excl);
1080 if (nb->bUseTwoStreams)
1082 auto *plist_nl = nb->plist[InteractionLocality::NonLocal];
1083 freeDeviceBuffer(&plist_nl->sci);
1084 freeDeviceBuffer(&plist_nl->cj4);
1085 freeDeviceBuffer(&plist_nl->imask);
1086 freeDeviceBuffer(&plist_nl->excl);
1091 pfree(nb->nbst.e_lj);
1092 nb->nbst.e_lj = nullptr;
1094 pfree(nb->nbst.e_el);
1095 nb->nbst.e_el = nullptr;
1097 pfree(nb->nbst.fshift);
1098 nb->nbst.fshift = nullptr;
1100 /* Free command queues */
1101 clReleaseCommandQueue(nb->stream[InteractionLocality::Local]);
1102 nb->stream[InteractionLocality::Local] = nullptr;
1103 if (nb->bUseTwoStreams)
1105 clReleaseCommandQueue(nb->stream[InteractionLocality::NonLocal]);
1106 nb->stream[InteractionLocality::NonLocal] = nullptr;
1108 /* Free other events */
1109 if (nb->nonlocal_done)
1111 clReleaseEvent(nb->nonlocal_done);
1112 nb->nonlocal_done = nullptr;
1114 if (nb->misc_ops_and_local_H2D_done)
1116 clReleaseEvent(nb->misc_ops_and_local_H2D_done);
1117 nb->misc_ops_and_local_H2D_done = nullptr;
1120 free_gpu_device_runtime_data(nb->dev_rundata);
1121 sfree(nb->dev_rundata);
1123 /* Free timers and timings */
1130 fprintf(debug, "Cleaned up OpenCL data structures.\n");
1134 //! This function is documented in the header file
1135 gmx_wallclock_gpu_nbnxn_t *gpu_get_timings(gmx_nbnxn_ocl_t *nb)
1137 return (nb != nullptr && nb->bDoTime) ? nb->timings : nullptr;
1140 //! This function is documented in the header file
1141 void gpu_reset_timings(nonbonded_verlet_t* nbv)
1143 if (nbv->gpu_nbv && nbv->gpu_nbv->bDoTime)
1145 init_timings(nbv->gpu_nbv->timings);
1149 //! This function is documented in the header file
1150 int gpu_min_ci_balanced(gmx_nbnxn_ocl_t *nb)
1152 return nb != nullptr ?
1153 gpu_min_ci_balanced_factor * nb->dev_info->compute_units : 0;
1156 //! This function is documented in the header file
1157 gmx_bool gpu_is_kernel_ewald_analytical(const gmx_nbnxn_ocl_t *nb)
1159 return ((nb->nbparam->eeltype == eelOclEWALD_ANA) ||
1160 (nb->nbparam->eeltype == eelOclEWALD_ANA_TWIN));
1163 } // namespace Nbnxm