diff --git a/projects/clr/hipamd/src/hip_device.cpp b/projects/clr/hipamd/src/hip_device.cpp index 02d2e9aaf4..fedd62e56d 100644 --- a/projects/clr/hipamd/src/hip_device.cpp +++ b/projects/clr/hipamd/src/hip_device.cpp @@ -105,6 +105,21 @@ void Device::RemoveStreamFromPools(Stream* stream) { } } +// ================================================================================================ +void Device::Reset() { + auto it = mem_pools_.begin(); + while (it != mem_pools_.end()) { + auto current = it++; + (*current)->ReleaseAllMemory(); + delete *current; + } + mem_pools_.clear(); + flags_ = hipDeviceScheduleSpin; + hip::Stream::destroyAllStreams(deviceId_); + amd::MemObjMap::Purge(devices()[0]); + Create(); +} + // ================================================================================================ Device::~Device() { if (default_mem_pool_ != nullptr) { diff --git a/projects/clr/hipamd/src/hip_device_runtime.cpp b/projects/clr/hipamd/src/hip_device_runtime.cpp index 0a3d9ada30..871a9420fc 100644 --- a/projects/clr/hipamd/src/hip_device_runtime.cpp +++ b/projects/clr/hipamd/src/hip_device_runtime.cpp @@ -464,9 +464,7 @@ hipError_t hipDeviceGetSharedMemConfig ( hipSharedMemConfig * pConfig ) { hipError_t hipDeviceReset ( void ) { HIP_INIT_API(hipDeviceReset); - hip::Device* dev = hip::getCurrentDevice(); - hip::Stream::destroyAllStreams(dev->deviceId()); - amd::MemObjMap::Purge(dev->devices()[0]); + hip::getCurrentDevice()->Reset(); HIP_RETURN(hipSuccess); } diff --git a/projects/clr/hipamd/src/hip_internal.hpp b/projects/clr/hipamd/src/hip_internal.hpp index ff8eecbbc1..9ff0690fc9 100644 --- a/projects/clr/hipamd/src/hip_internal.hpp +++ b/projects/clr/hipamd/src/hip_internal.hpp @@ -428,6 +428,8 @@ namespace hip { } unsigned int getFlags() const { return flags_; } void setFlags(unsigned int flags) { flags_ = flags; } + void Reset(); + amd::HostQueue* NullStream(bool skip_alloc = false); Stream* GetNullStream(); diff --git a/projects/clr/hipamd/src/hip_mempool_impl.cpp b/projects/clr/hipamd/src/hip_mempool_impl.cpp index 6d23a986a8..959413e0e9 100644 --- a/projects/clr/hipamd/src/hip_mempool_impl.cpp +++ b/projects/clr/hipamd/src/hip_mempool_impl.cpp @@ -226,6 +226,13 @@ bool MemoryPool::FreeMemory(amd::Memory* memory, hip::Stream* stream) { return true; } +// ================================================================================================ +void MemoryPool::ReleaseAllMemory() { + constexpr bool kSafeRelease = true; + free_heap_.ReleaseAllMemory(0, kSafeRelease); + busy_heap_.ReleaseAllMemory(0, kSafeRelease); +} + // ================================================================================================ void MemoryPool::ReleaseFreedMemory(hip::Stream* stream) { amd::ScopedLock lock(lock_pool_ops_); diff --git a/projects/clr/hipamd/src/hip_mempool_impl.hpp b/projects/clr/hipamd/src/hip_mempool_impl.hpp index 69ac92a50d..dbc9c419fa 100644 --- a/projects/clr/hipamd/src/hip_mempool_impl.hpp +++ b/projects/clr/hipamd/src/hip_mempool_impl.hpp @@ -161,9 +161,10 @@ public: state_.internal_dependencies_ = 1; } virtual ~MemoryPool() { - assert(busy_heap_.IsEmpty() && "Can't destroy pool with busy allocations!"); - constexpr bool kSafeRelease = true; - free_heap_.ReleaseAllMemory(0, kSafeRelease); + if (!busy_heap_.IsEmpty()) { + LogError("Shouldn't destroy pool with busy allocations!"); + } + ReleaseAllMemory(); // Remove memory pool from the list of all pool on the current device device_->RemoveMemoryPool(this); }