Unify gpu_free(...) function in OpenCL, CUDA and SYCL versions of NBNXM
authorArtem Zhmurov <zhmurov@gmail.com>
Tue, 30 Mar 2021 15:08:15 +0000 (18:08 +0300)
committerPaul Bauer <paul.bauer.q@gmail.com>
Wed, 31 Mar 2021 06:54:36 +0000 (06:54 +0000)
Refs #2608

src/gromacs/nbnxm/cuda/nbnxm_cuda_data_mgmt.cu
src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.cpp
src/gromacs/nbnxm/nbnxm_gpu_data_mgmt.h
src/gromacs/nbnxm/opencl/nbnxm_ocl_data_mgmt.cpp
src/gromacs/nbnxm/sycl/nbnxm_sycl_data_mgmt.cpp

index 1edb0b8797ee2271298a68863e1faec3313212fc..3477f2e19251e1ab49f0efae43f18c9b9df6057d 100644 (file)
@@ -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)
index 63f8317443314c3886a53f24a1a8724d6324b55c..534d2b706d04b502015c4ce61332ba0a227aa290 100644 (file)
@@ -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
index 333b059a63001b3750e0ff20f74d94822deedb32..41b38027330bf4f49ec6cbb808da0a23c8c6cad3 100644 (file)
@@ -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
index 1df8cbe58a10464895362d64f47d72fa141864d5..e9ae6dfc75f8c9ea027fedc87395058c267ae325 100644 (file)
@@ -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
index bff590c0a5104adf7391e30d5316dd985ddfa1aa..eb942448e3bb16c387f940bd3ea94c0b560225b0 100644 (file)
@@ -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)