From: Artem Zhmurov Date: Tue, 30 Mar 2021 15:08:15 +0000 (+0300) Subject: Unify gpu_free(...) function in OpenCL, CUDA and SYCL versions of NBNXM X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=1f97d7fa25de4e82a2ea3f6c2f6e8157d3ed97f5;p=alexxy%2Fgromacs.git Unify gpu_free(...) function in OpenCL, CUDA and SYCL versions of NBNXM Refs #2608 --- diff --git a/src/gromacs/nbnxm/cuda/nbnxm_cuda_data_mgmt.cu b/src/gromacs/nbnxm/cuda/nbnxm_cuda_data_mgmt.cu index 1edb0b8797..3477f2e192 100644 --- a/src/gromacs/nbnxm/cuda/nbnxm_cuda_data_mgmt.cu +++ b/src/gromacs/nbnxm/cuda/nbnxm_cuda_data_mgmt.cu @@ -98,86 +98,9 @@ void gpu_init_platform_specific(NbnxmGpu* /* nb */) cuda_set_cacheconfig(); } -void gpu_free(NbnxmGpu* nb) +void gpu_free_platform_specific(NbnxmGpu* /* nb */) { - if (nb == nullptr) - { - return; - } - - delete nb->timers; - sfree(nb->timings); - - NBAtomData* atdat = nb->atdat; - NBParamGpu* nbparam = nb->nbparam; - - if (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin) - { - destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj); - } - - if (!useLjCombRule(nb->nbparam->vdwType)) - { - destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj); - } - - if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB) - { - destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj); - } - - freeDeviceBuffer(&atdat->shiftVec); - freeDeviceBuffer(&atdat->fShift); - - freeDeviceBuffer(&atdat->eLJ); - freeDeviceBuffer(&atdat->eElec); - - freeDeviceBuffer(&atdat->f); - freeDeviceBuffer(&atdat->xq); - if (useLjCombRule(nb->nbparam->vdwType)) - { - freeDeviceBuffer(&atdat->ljComb); - } - else - { - freeDeviceBuffer(&atdat->atomTypes); - } - - /* Free plist */ - auto* plist = nb->plist[InteractionLocality::Local]; - freeDeviceBuffer(&plist->sci); - freeDeviceBuffer(&plist->cj4); - freeDeviceBuffer(&plist->imask); - freeDeviceBuffer(&plist->excl); - delete plist; - if (nb->bUseTwoStreams) - { - auto* plist_nl = nb->plist[InteractionLocality::NonLocal]; - freeDeviceBuffer(&plist_nl->sci); - freeDeviceBuffer(&plist_nl->cj4); - freeDeviceBuffer(&plist_nl->imask); - freeDeviceBuffer(&plist_nl->excl); - delete plist_nl; - } - - /* Free nbst */ - pfree(nb->nbst.eLJ); - nb->nbst.eLJ = nullptr; - - pfree(nb->nbst.eElec); - nb->nbst.eElec = nullptr; - - pfree(nb->nbst.fShift); - nb->nbst.fShift = nullptr; - - delete atdat; - delete nbparam; - delete nb; - - if (debug) - { - fprintf(debug, "Cleaned up CUDA data structures.\n"); - } + // Nothing specific in CUDA } int gpu_min_ci_balanced(NbnxmGpu* nb) diff --git a/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.cpp b/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.cpp index 63f8317443..534d2b706d 100644 --- a/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.cpp +++ b/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.cpp @@ -1091,4 +1091,89 @@ void nbnxn_gpu_init_x_to_nbat_x(const Nbnxm::GridSet& gridSet, NbnxmGpu* gpu_nbv nbnxnInsertNonlocalGpuDependency(gpu_nbv, Nbnxm::InteractionLocality::NonLocal); } +//! This function is documented in the header file +void gpu_free(NbnxmGpu* nb) +{ + if (nb == nullptr) + { + return; + } + + gpu_free_platform_specific(nb); + + delete nb->timers; + sfree(nb->timings); + + NBAtomData* atdat = nb->atdat; + NBParamGpu* nbparam = nb->nbparam; + + /* Free atdat */ + freeDeviceBuffer(&(nb->atdat->xq)); + freeDeviceBuffer(&(nb->atdat->f)); + freeDeviceBuffer(&(nb->atdat->eLJ)); + freeDeviceBuffer(&(nb->atdat->eElec)); + freeDeviceBuffer(&(nb->atdat->fShift)); + freeDeviceBuffer(&(nb->atdat->shiftVec)); + if (useLjCombRule(nb->nbparam->vdwType)) + { + freeDeviceBuffer(&atdat->ljComb); + } + else + { + freeDeviceBuffer(&atdat->atomTypes); + } + + /* Free nbparam */ + if (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin) + { + destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj); + } + + if (!useLjCombRule(nb->nbparam->vdwType)) + { + destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj); + } + + if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB) + { + destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj); + } + + /* Free plist */ + auto* plist = nb->plist[InteractionLocality::Local]; + freeDeviceBuffer(&plist->sci); + freeDeviceBuffer(&plist->cj4); + freeDeviceBuffer(&plist->imask); + freeDeviceBuffer(&plist->excl); + delete plist; + if (nb->bUseTwoStreams) + { + auto* plist_nl = nb->plist[InteractionLocality::NonLocal]; + freeDeviceBuffer(&plist_nl->sci); + freeDeviceBuffer(&plist_nl->cj4); + freeDeviceBuffer(&plist_nl->imask); + freeDeviceBuffer(&plist_nl->excl); + delete plist_nl; + } + + /* Free nbst */ + pfree(nb->nbst.eLJ); + nb->nbst.eLJ = nullptr; + + pfree(nb->nbst.eElec); + nb->nbst.eElec = nullptr; + + pfree(nb->nbst.fShift); + nb->nbst.fShift = nullptr; + + delete atdat; + delete nbparam; + delete nb; + + if (debug) + { + fprintf(debug, "Cleaned up NBNXM GPU data structures.\n"); + } +} + } // namespace Nbnxm diff --git a/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.h b/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.h index 333b059a63..41b3802733 100644 --- a/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.h +++ b/src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.h @@ -83,8 +83,12 @@ void init_plist(gpu_plist* pl); /*! \brief Initializes the timings data structure. */ void init_timings(gmx_wallclock_gpu_nbnxn_t* t); +/*! \brief Initializes the NBNXM GPU data structures. */ void gpu_init_platform_specific(NbnxmGpu* nb); +/*! \brief Releases the NBNXM GPU data structures. */ +void gpu_free_platform_specific(NbnxmGpu* nb); + } // namespace Nbnxm #endif // GMX_NBNXM_NBNXM_GPU_DATA_MGMT_H diff --git a/src/gromacs/nbnxm/opencl/nbnxm_ocl_data_mgmt.cpp b/src/gromacs/nbnxm/opencl/nbnxm_ocl_data_mgmt.cpp index 1df8cbe58a..e9ae6dfc75 100644 --- a/src/gromacs/nbnxm/opencl/nbnxm_ocl_data_mgmt.cpp +++ b/src/gromacs/nbnxm/opencl/nbnxm_ocl_data_mgmt.cpp @@ -163,13 +163,11 @@ void gpu_init_platform_specific(NbnxmGpu* nb) /*! \brief Releases an OpenCL kernel pointer */ static void free_kernel(cl_kernel* kernel_ptr) { - cl_int gmx_unused cl_error; - GMX_ASSERT(kernel_ptr, "Need a valid kernel pointer"); if (*kernel_ptr) { - cl_error = clReleaseKernel(*kernel_ptr); + cl_int cl_error = clReleaseKernel(*kernel_ptr); GMX_RELEASE_ASSERT(cl_error == CL_SUCCESS, ("clReleaseKernel failed: " + ocl_get_error_string(cl_error)).c_str()); @@ -180,9 +178,7 @@ static void free_kernel(cl_kernel* kernel_ptr) /*! \brief Releases a list of OpenCL kernel pointers */ static void free_kernels(cl_kernel* kernels, int count) { - int i; - - for (i = 0; i < count; i++) + for (int i = 0; i < count; i++) { free_kernel(kernels + i); } @@ -207,19 +203,8 @@ static void freeGpuProgram(cl_program program) } //! This function is documented in the header file -void gpu_free(NbnxmGpu* nb) +void gpu_free_platform_specific(NbnxmGpu* nb) { - if (nb == nullptr) - { - return; - } - - delete nb->timers; - sfree(nb->timings); - - NBAtomData* atdat = nb->atdat; - NBParamGpu* nbparam = nb->nbparam; - /* Free kernels */ // NOLINTNEXTLINE(bugprone-sizeof-expression) int kernel_count = sizeof(nb->kernel_ener_noprune_ptr) / sizeof(nb->kernel_ener_noprune_ptr[0][0]); @@ -237,76 +222,8 @@ void gpu_free(NbnxmGpu* nb) kernel_count = sizeof(nb->kernel_noener_prune_ptr) / sizeof(nb->kernel_noener_prune_ptr[0][0]); free_kernels(nb->kernel_noener_prune_ptr[0], kernel_count); - /* Free atdat */ - freeDeviceBuffer(&(nb->atdat->xq)); - freeDeviceBuffer(&(nb->atdat->f)); - freeDeviceBuffer(&(nb->atdat->eLJ)); - freeDeviceBuffer(&(nb->atdat->eElec)); - freeDeviceBuffer(&(nb->atdat->fShift)); - freeDeviceBuffer(&(nb->atdat->shiftVec)); - if (useLjCombRule(nb->nbparam->vdwType)) - { - freeDeviceBuffer(&atdat->ljComb); - } - else - { - freeDeviceBuffer(&atdat->atomTypes); - } - - /* Free nbparam */ - if (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin) - { - destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj); - } - - if (!useLjCombRule(nb->nbparam->vdwType)) - { - destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj); - } - - if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB) - { - destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj); - } - - /* Free plist */ - auto* plist = nb->plist[InteractionLocality::Local]; - freeDeviceBuffer(&plist->sci); - freeDeviceBuffer(&plist->cj4); - freeDeviceBuffer(&plist->imask); - freeDeviceBuffer(&plist->excl); - delete plist; - if (nb->bUseTwoStreams) - { - auto* plist_nl = nb->plist[InteractionLocality::NonLocal]; - freeDeviceBuffer(&plist_nl->sci); - freeDeviceBuffer(&plist_nl->cj4); - freeDeviceBuffer(&plist_nl->imask); - freeDeviceBuffer(&plist_nl->excl); - delete plist_nl; - } - - /* Free nbst */ - pfree(nb->nbst.eLJ); - nb->nbst.eLJ = nullptr; - - pfree(nb->nbst.eElec); - nb->nbst.eElec = nullptr; - - pfree(nb->nbst.fShift); - nb->nbst.fShift = nullptr; - freeGpuProgram(nb->dev_rundata->program); delete nb->dev_rundata; - - delete atdat; - delete nbparam; - delete nb; - - if (debug) - { - fprintf(debug, "Cleaned up OpenCL data structures.\n"); - } } //! This function is documented in the header file diff --git a/src/gromacs/nbnxm/sycl/nbnxm_sycl_data_mgmt.cpp b/src/gromacs/nbnxm/sycl/nbnxm_sycl_data_mgmt.cpp index bff590c0a5..eb942448e3 100644 --- a/src/gromacs/nbnxm/sycl/nbnxm_sycl_data_mgmt.cpp +++ b/src/gromacs/nbnxm/sycl/nbnxm_sycl_data_mgmt.cpp @@ -61,56 +61,9 @@ void gpu_init_platform_specific(NbnxmGpu* /* nb */) // Nothing specific in SYCL } -void gpu_free(NbnxmGpu* nb) +void gpu_free_platform_specific(NbnxmGpu* /* nb */) { - if (nb == nullptr) - { - return; - } - - delete nb->timers; - sfree(nb->timings); - - NBAtomData* atdat = nb->atdat; - NBParamGpu* nbparam = nb->nbparam; - - if (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin) - { - destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj); - } - - if (!useLjCombRule(nb->nbparam->vdwType)) - { - destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj); - } - - if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB) - { - destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj); - } - - /* Free plist */ - auto* plist = nb->plist[InteractionLocality::Local]; - delete plist; - if (nb->bUseTwoStreams) - { - auto* plist_nl = nb->plist[InteractionLocality::NonLocal]; - delete plist_nl; - } - - /* Free nbst */ - pfree(nb->nbst.eLJ); - nb->nbst.eLJ = nullptr; - - pfree(nb->nbst.eElec); - nb->nbst.eElec = nullptr; - - pfree(nb->nbst.fShift); - nb->nbst.fShift = nullptr; - - delete atdat; - delete nbparam; - delete nb; + // Nothing specific in SYCL } int gpu_min_ci_balanced(NbnxmGpu* nb)