From c019f4df5515cbee29a71331a2d2a8a1e5854bbc Mon Sep 17 00:00:00 2001 From: Noel Chalmers Date: Mon, 3 Oct 2022 11:06:35 -0500 Subject: [PATCH] [CUDA][HIP][DPC++] Fix an issue with double frees when OKL has multiple kernels (#624) --- src/occa/internal/modes/cuda/device.cpp | 2 +- src/occa/internal/modes/cuda/kernel.cpp | 12 +++++++++++- src/occa/internal/modes/cuda/kernel.hpp | 7 +++++++ src/occa/internal/modes/dpcpp/device.cpp | 6 +++--- src/occa/internal/modes/dpcpp/kernel.cpp | 14 +++++++++++++- src/occa/internal/modes/dpcpp/kernel.hpp | 7 +++++++ src/occa/internal/modes/hip/device.cpp | 2 +- src/occa/internal/modes/hip/kernel.cpp | 12 +++++++++++- src/occa/internal/modes/hip/kernel.hpp | 7 +++++++ 9 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/occa/internal/modes/cuda/device.cpp b/src/occa/internal/modes/cuda/device.cpp index c61b560de..a1bd1684e 100644 --- a/src/occa/internal/modes/cuda/device.cpp +++ b/src/occa/internal/modes/cuda/device.cpp @@ -348,6 +348,7 @@ namespace occa { kernel &k = *(new kernel(this, kernelName, sourceFilename, + cuModule, kernelProps)); k.launcherKernel = buildLauncherKernel(kernelHash, @@ -377,7 +378,6 @@ namespace occa { kernel *cuKernel = new kernel(this, metadata.name, sourceFilename, - cuModule, cuFunction, kernelProps); cuKernel->metadata = metadata; diff --git a/src/occa/internal/modes/cuda/kernel.cpp b/src/occa/internal/modes/cuda/kernel.cpp index 568e7d47b..292a3cbf7 100644 --- a/src/occa/internal/modes/cuda/kernel.cpp +++ b/src/occa/internal/modes/cuda/kernel.cpp @@ -10,11 +10,21 @@ namespace occa { kernel::kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, + CUmodule cuModule_, const occa::json &properties_) : occa::launchedModeKernel_t(modeDevice_, name_, sourceFilename_, properties_), - cuModule(NULL), + cuModule(cuModule_), cuFunction(NULL) {} + kernel::kernel(modeDevice_t *modeDevice_, + const std::string &name_, + const std::string &sourceFilename_, + CUfunction cuFunction_, + const occa::json &properties_) : + occa::launchedModeKernel_t(modeDevice_, name_, sourceFilename_, properties_), + cuModule(NULL), + cuFunction(cuFunction_) {} + kernel::kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, diff --git a/src/occa/internal/modes/cuda/kernel.hpp b/src/occa/internal/modes/cuda/kernel.hpp index 0b4cb68f6..f7bb9c8a5 100644 --- a/src/occa/internal/modes/cuda/kernel.hpp +++ b/src/occa/internal/modes/cuda/kernel.hpp @@ -23,6 +23,13 @@ namespace occa { kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, + CUmodule cuModule_, + const occa::json &properties_); + + kernel(modeDevice_t *modeDevice_, + const std::string &name_, + const std::string &sourceFilename_, + CUfunction cuFunction_, const occa::json &properties_); kernel(modeDevice_t *modeDevice_, diff --git a/src/occa/internal/modes/dpcpp/device.cpp b/src/occa/internal/modes/dpcpp/device.cpp index de3b00bfb..d2a1a1377 100644 --- a/src/occa/internal/modes/dpcpp/device.cpp +++ b/src/occa/internal/modes/dpcpp/device.cpp @@ -246,9 +246,12 @@ namespace occa lang::sourceMetadata_t &deviceMetadata, const occa::json &kernelProps) { + void *dl_handle = sys::dlopen(binaryFilename); + dpcpp::kernel &k = *(new dpcpp::kernel(this, kernelName, sourceFilename, + dl_handle, kernelProps)); k.launcherKernel = buildLauncherKernel(kernelHash, @@ -260,8 +263,6 @@ namespace occa kernelName, deviceMetadata); - void *dl_handle = sys::dlopen(binaryFilename); - const int launchedKernelsCount = (int)launchedKernelsMetadata.size(); for (int i = 0; i < launchedKernelsCount; ++i) { @@ -279,7 +280,6 @@ namespace occa kernel *dpcppKernel = new dpcpp::kernel(this, metadata.name, sourceFilename, - dl_handle, kernel_function, kernelProps); diff --git a/src/occa/internal/modes/dpcpp/kernel.cpp b/src/occa/internal/modes/dpcpp/kernel.cpp index 735d0c6a2..8f0c357ee 100644 --- a/src/occa/internal/modes/dpcpp/kernel.cpp +++ b/src/occa/internal/modes/dpcpp/kernel.cpp @@ -14,13 +14,25 @@ namespace occa kernel::kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, + void *dlHandle_, const occa::json &properties_) : occa::launchedModeKernel_t(modeDevice_, name_, sourceFilename_, properties_), - dlHandle{nullptr}, + dlHandle{dlHandle_}, function{nullptr} { } + kernel::kernel(modeDevice_t *modeDevice_, + const std::string &name_, + const std::string &sourceFilename_, + functionPtr_t function_, + const occa::json &properties_) + : occa::launchedModeKernel_t(modeDevice_, name_, sourceFilename_, properties_), + dlHandle(nullptr), + function(function_) + { + } + kernel::kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, diff --git a/src/occa/internal/modes/dpcpp/kernel.hpp b/src/occa/internal/modes/dpcpp/kernel.hpp index c2b41a65d..26d208300 100644 --- a/src/occa/internal/modes/dpcpp/kernel.hpp +++ b/src/occa/internal/modes/dpcpp/kernel.hpp @@ -24,6 +24,13 @@ namespace occa kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, + void* dlHandle_, + const occa::json &properties_); + + kernel(modeDevice_t *modeDevice_, + const std::string &name_, + const std::string &sourceFilename_, + functionPtr_t function_, const occa::json &properties_); kernel(modeDevice_t *modeDevice_, diff --git a/src/occa/internal/modes/hip/device.cpp b/src/occa/internal/modes/hip/device.cpp index 0b7453fae..f8f4977d0 100644 --- a/src/occa/internal/modes/hip/device.cpp +++ b/src/occa/internal/modes/hip/device.cpp @@ -333,6 +333,7 @@ namespace occa { kernel &k = *(new kernel(this, kernelName, sourceFilename, + hipModule, kernelProps)); k.launcherKernel = buildLauncherKernel(kernelHash, @@ -360,7 +361,6 @@ namespace occa { kernel *hipKernel = new kernel(this, metadata.name, sourceFilename, - hipModule, hipFunction, kernelProps); hipKernel->metadata = metadata; diff --git a/src/occa/internal/modes/hip/kernel.cpp b/src/occa/internal/modes/hip/kernel.cpp index 3e2d6ba69..b381a3438 100644 --- a/src/occa/internal/modes/hip/kernel.cpp +++ b/src/occa/internal/modes/hip/kernel.cpp @@ -10,11 +10,21 @@ namespace occa { kernel::kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, + hipModule_t hipModule_, const occa::json &properties_) : occa::launchedModeKernel_t(modeDevice_, name_, sourceFilename_, properties_), - hipModule(NULL), + hipModule(hipModule_), hipFunction(NULL) {} + kernel::kernel(modeDevice_t *modeDevice_, + const std::string &name_, + const std::string &sourceFilename_, + hipFunction_t hipFunction_, + const occa::json &properties_) : + occa::launchedModeKernel_t(modeDevice_, name_, sourceFilename_, properties_), + hipModule(NULL), + hipFunction(hipFunction_) {} + kernel::kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, diff --git a/src/occa/internal/modes/hip/kernel.hpp b/src/occa/internal/modes/hip/kernel.hpp index d0863894a..967513734 100644 --- a/src/occa/internal/modes/hip/kernel.hpp +++ b/src/occa/internal/modes/hip/kernel.hpp @@ -21,6 +21,13 @@ namespace occa { kernel(modeDevice_t *modeDevice_, const std::string &name_, const std::string &sourceFilename_, + hipModule_t hipModule_, + const occa::json &properties_); + + kernel(modeDevice_t *modeDevice_, + const std::string &name_, + const std::string &sourceFilename_, + hipFunction_t hipFunction_, const occa::json &properties_); kernel(modeDevice_t *modeDevice_,