From f9a86e3f1833c4fdee502dac8e0c532a2942db2a Mon Sep 17 00:00:00 2001 From: ronnywang Date: Fri, 8 Sep 2023 23:42:07 +0800 Subject: [PATCH] [CustomDevice] Improve device initialize & release (#57099) --- paddle/phi/backends/device_manager.cc | 11 +++++++++-- paddle/phi/backends/device_manager.h | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/paddle/phi/backends/device_manager.cc b/paddle/phi/backends/device_manager.cc index d95cb12646f..24ad5087769 100644 --- a/paddle/phi/backends/device_manager.cc +++ b/paddle/phi/backends/device_manager.cc @@ -30,10 +30,17 @@ namespace phi { void Device::CheckInitialized() { - std::call_once(initialized_, [&]() { this->impl_->InitDevice(dev_id_); }); + std::call_once(initialized_once_flag_, [&]() { + this->impl_->InitDevice(dev_id_); + this->initialized_ = true; + }); } -Device::~Device() { impl_->DeInitDevice(dev_id_); } +Device::~Device() { + if (initialized_) { + impl_->DeInitDevice(dev_id_); + } +} void Device::CreateStream(stream::Stream* stream, const stream::Stream::Priority& priority, diff --git a/paddle/phi/backends/device_manager.h b/paddle/phi/backends/device_manager.h index 62c85aeb526..58a9e6ebe7a 100644 --- a/paddle/phi/backends/device_manager.h +++ b/paddle/phi/backends/device_manager.h @@ -127,7 +127,8 @@ class Device final { private: size_t dev_id_; DeviceInterface* impl_; - std::once_flag initialized_; + std::once_flag initialized_once_flag_; + bool initialized_{false}; }; class DeviceManager { -- GitLab