From 5fbb4ce6f6c00fc72dfd2f91b016a72e2a374516 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Tue, 3 Nov 2020 08:59:32 +0000 Subject: [PATCH] Added function in sequence freeMemoryDestroyGPUResources to de-init --- single_include/kompute/Kompute.hpp | 5 ++ src/Manager.cpp | 2 +- src/Sequence.cpp | 88 +++++++++++++++++------------- src/include/kompute/Sequence.hpp | 5 ++ 4 files changed, 62 insertions(+), 38 deletions(-) diff --git a/single_include/kompute/Kompute.hpp b/single_include/kompute/Kompute.hpp index 932375c..c1dfd87 100755 --- a/single_include/kompute/Kompute.hpp +++ b/single_include/kompute/Kompute.hpp @@ -1100,6 +1100,11 @@ class Sequence */ bool isInit(); + /** + * Destroys and frees the GPU resources which include the buffer and memory. + */ + void freeMemoryDestroyGPUResources(); + /** * Record function for operation to be added to the GPU queue in batch. This * template requires classes to be derived from the OpBase class. This diff --git a/src/Manager.cpp b/src/Manager.cpp index b763f2e..df9d64d 100755 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -63,7 +63,7 @@ Manager::~Manager() "managed sequences"); for (const std::pair>& sqPair : this->mManagedSequences) { - sqPair.second->~Sequence(); + sqPair.second->freeMemoryDestroyGPUResources(); } this->mManagedSequences.clear(); } diff --git a/src/Sequence.cpp b/src/Sequence.cpp index b27c547..4f01891 100644 --- a/src/Sequence.cpp +++ b/src/Sequence.cpp @@ -28,46 +28,13 @@ Sequence::~Sequence() SPDLOG_DEBUG("Kompute Sequence Destructor started"); if (!this->mIsInit) { - SPDLOG_WARN("Kompute Sequence destructor called but sequence is not " - "initialized."); + SPDLOG_INFO("Kompute Sequence destructor called but sequence is not " + "initialized so no need to removing GPU resources."); return; } - - if (!this->mDevice) { - SPDLOG_ERROR( - "Kompute Sequence destructor reached with null Device pointer"); - this->mIsInit = false; - return; + else { + this->freeMemoryDestroyGPUResources(); } - - if (this->mFreeCommandBuffer) { - SPDLOG_INFO("Freeing CommandBuffer"); - if (!this->mCommandBuffer) { - SPDLOG_ERROR("Kompute Sequence destructor reached with null " - "CommandPool pointer"); - this->mIsInit = false; - return; - } - this->mDevice->freeCommandBuffers( - *this->mCommandPool, 1, this->mCommandBuffer.get()); - SPDLOG_DEBUG("Kompute Sequence Freed CommandBuffer"); - } - - if (this->mFreeCommandPool) { - SPDLOG_INFO("Destroying CommandPool"); - if (this->mCommandPool == nullptr) { - SPDLOG_ERROR("Kompute Sequence destructor reached with null " - "CommandPool pointer"); - this->mIsInit = false; - return; - } - this->mDevice->destroy( - *this->mCommandPool, - (vk::Optional)nullptr); - SPDLOG_DEBUG("Kompute Sequence Destroyed CommandPool"); - } - - this->mIsInit = false; } void @@ -234,6 +201,53 @@ Sequence::isInit() return this->mIsInit; } +void +Sequence::freeMemoryDestroyGPUResources() +{ + if (!this->mIsInit) { + SPDLOG_ERROR("Kompute Sequence freeMemoryDestroyGPUResources called " + "but Sequence is not initialized so there's no relevant GPU resources."); + return; + } + + if (!this->mDevice) { + SPDLOG_ERROR( + "Kompute Sequence freeMemoryDestroyGPUResources called with null Device pointer"); + this->mIsInit = false; + return; + } + + if (this->mFreeCommandBuffer) { + SPDLOG_INFO("Freeing CommandBuffer"); + if (!this->mCommandBuffer) { + SPDLOG_ERROR("Kompute Sequence freeMemoryDestroyGPUResources called with null " + "CommandPool pointer"); + this->mIsInit = false; + return; + } + this->mDevice->freeCommandBuffers( + *this->mCommandPool, 1, this->mCommandBuffer.get()); + SPDLOG_DEBUG("Kompute Sequence Freed CommandBuffer"); + } + + if (this->mFreeCommandPool) { + SPDLOG_INFO("Destroying CommandPool"); + if (this->mCommandPool == nullptr) { + SPDLOG_ERROR("Kompute Sequence freeMemoryDestroyGPUResources called with null " + "CommandPool pointer"); + this->mIsInit = false; + return; + } + this->mDevice->destroy( + *this->mCommandPool, + (vk::Optional)nullptr); + SPDLOG_DEBUG("Kompute Sequence Destroyed CommandPool"); + } + + this->mIsInit = false; + +} + void Sequence::createCommandPool() { diff --git a/src/include/kompute/Sequence.hpp b/src/include/kompute/Sequence.hpp index 314de66..09247fe 100644 --- a/src/include/kompute/Sequence.hpp +++ b/src/include/kompute/Sequence.hpp @@ -106,6 +106,11 @@ class Sequence */ bool isInit(); + /** + * Destroys and frees the GPU resources which include the buffer and memory. + */ + void freeMemoryDestroyGPUResources(); + /** * Record function for operation to be added to the GPU queue in batch. This * template requires classes to be derived from the OpBase class. This