diff --git a/src/plugins/auto/src/common.hpp b/src/plugins/auto/src/common.hpp index 28567eb23392c4..bfcb30441f71f2 100644 --- a/src/plugins/auto/src/common.hpp +++ b/src/plugins/auto/src/common.hpp @@ -220,6 +220,10 @@ class ScheduleContext : public std::enable_shared_from_this { std::mutex m_fallback_mutex; SoCompiledModel m_hw_compiled_model; std::string m_model_precision; + // hold the resource of static variable to avoid the unexpected destruction. + std::shared_ptr m_mtx; + std::shared_ptr>> m_priority_map; + std::shared_ptr m_logger = Log::instance(); virtual ~ScheduleContext() = default; }; diff --git a/src/plugins/auto/src/plugin.cpp b/src/plugins/auto/src/plugin.cpp index 06b3b7dbc947e4..2371107281d630 100644 --- a/src/plugins/auto/src/plugin.cpp +++ b/src/plugins/auto/src/plugin.cpp @@ -75,8 +75,9 @@ namespace { namespace ov { namespace auto_plugin { -std::mutex Plugin::m_mtx; -std::map> Plugin::m_priority_map; +std::shared_ptr Plugin::m_mtx = std::make_shared(); +std::shared_ptr>> Plugin::m_priority_map = + std::make_shared>>(); ov::SoPtr Plugin::create_context(const ov::AnyMap& remote_properties) const { OPENVINO_NOT_IMPLEMENTED; @@ -440,9 +441,12 @@ std::shared_ptr Plugin::compile_model_impl(const std::string auto_s_context->m_runtime_fallback = load_config.get_property(ov::intel_auto::enable_runtime_fallback); auto_s_context->m_bind_buffer = load_config.get_property(ov::intel_auto::device_bind_buffer); auto_s_context->m_schedule_policy = load_config.get_property(ov::intel_auto::schedule_policy); + auto_s_context->m_mtx = m_mtx; + auto_s_context->m_priority_map = m_priority_map; std::shared_ptr impl; - std::shared_ptr scheduler = is_cumulative ? std::static_pointer_cast(std::make_shared()) : - std::static_pointer_cast(std::make_shared()); + std::shared_ptr scheduler = + is_cumulative ? std::static_pointer_cast(std::make_shared()) + : std::static_pointer_cast(std::make_shared()); scheduler->launch(auto_s_context); ov::SoPtr device_context; try { @@ -591,19 +595,25 @@ DeviceInformation Plugin::select_device(const std::vector& me DeviceInformation last_device = valid_devices.back(); { // begin to filter devices - std::lock_guard lck(m_mtx); - for (auto && kvp : m_priority_map) { - if (kvp.first >= priority) { - continue; + if (m_mtx && m_priority_map) { + std::lock_guard lck(*m_mtx); + for (auto&& kvp : *m_priority_map) { + if (kvp.first >= priority) { + continue; + } + auto& filter_devices = kvp.second; + auto sd = std::remove_if(valid_devices.begin(), + valid_devices.end(), + [&filter_devices](const DeviceInformation& device) { + auto iter = std::find_if(filter_devices.begin(), + filter_devices.end(), + [&device](std::string uniqueName) { + return (uniqueName == device.unique_name); + }); + return iter != filter_devices.end() ? true : false; + }); + valid_devices.erase(sd, valid_devices.end()); } - auto& filter_devices = kvp.second; - auto sd = std::remove_if(valid_devices.begin(), valid_devices.end(), [&filter_devices](const DeviceInformation& device) { - auto iter = std::find_if(filter_devices.begin(), filter_devices.end(), [&device](std::string uniqueName) { - return (uniqueName == device.unique_name); - }); - return iter != filter_devices.end() ? true : false; - }); - valid_devices.erase(sd, valid_devices.end()); } } @@ -621,24 +631,26 @@ DeviceInformation Plugin::select_device(const std::vector& me return *ptr_select_device; } -void Plugin::unregister_priority(const unsigned int& priority, - const std::string& device_name) { - std::lock_guard lck(m_mtx); - auto& priority_devices = m_priority_map[priority]; - for (auto iter = priority_devices.begin(); iter != priority_devices.end();) { - if (*iter == device_name) { - priority_devices.erase(iter); - break; +void Plugin::unregister_priority(const unsigned int& priority, const std::string& device_name) { + if (m_mtx && m_priority_map) { + std::lock_guard lck(*m_mtx); + auto& priority_devices = (*m_priority_map)[priority]; + for (auto iter = priority_devices.begin(); iter != priority_devices.end();) { + if (*iter == device_name) { + priority_devices.erase(iter); + break; + } + iter++; } - iter++; } } -void Plugin::register_priority(const unsigned int& priority, - const std::string& device_name) { - std::lock_guard lck(m_mtx); - auto& priority_devices = m_priority_map[priority]; - priority_devices.push_back(device_name); +void Plugin::register_priority(const unsigned int& priority, const std::string& device_name) { + if (m_mtx && m_priority_map) { + std::lock_guard lck(*m_mtx); + auto& priority_devices = (*m_priority_map)[priority]; + priority_devices.push_back(device_name); + } } std::string Plugin::get_device_list(const ov::AnyMap& properties) const { diff --git a/src/plugins/auto/src/plugin.hpp b/src/plugins/auto/src/plugin.hpp index 75ad7ce5919afb..521a87f08bea46 100644 --- a/src/plugins/auto/src/plugin.hpp +++ b/src/plugins/auto/src/plugin.hpp @@ -78,8 +78,8 @@ class Plugin : public ov::IPlugin { const std::shared_ptr& model, PluginConfig& load_config) const; std::string get_log_tag() const noexcept; - static std::mutex m_mtx; - static std::map> m_priority_map; + static std::shared_ptr m_mtx; + static std::shared_ptr>> m_priority_map; PluginConfig m_plugin_config; };