Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Fix creating too many useless threads in cpu executor (#25704)

Sun Xiaoxia52f6fe7d2b31
diff --git a/src/inference/src/dev/threading/cpu_streams_executor.cpp b/src/inference/src/dev/threading/cpu_streams_executor.cpp
index 9e55cd2be18..4fca252e13e 100644
--- a/src/inference/src/dev/threading/cpu_streams_executor.cpp
+++ b/src/inference/src/dev/threading/cpu_streams_executor.cpp
@@ -313,6 +313,21 @@ struct CPUStreamsExecutor::Impl {
}
}
+ bool find_thread_id() {
+ auto id = std::this_thread::get_id();
+ auto search = _thread_ids.find(id);
+ if (search != _thread_ids.end()) {
+ return true;
+ }
+ std::lock_guard<std::mutex> guard(_stream_map_mutex);
+ for (auto& item : _stream_map) {
+ if (item.first->get_id() == id) {
+ return true;
+ }
+ }
+ return false;
+ }
+
private:
std::set<std::thread::id> _thread_ids;
Impl* _impl;
@@ -445,6 +460,9 @@ struct CPUStreamsExecutor::Impl {
};
int CPUStreamsExecutor::get_stream_id() {
+ if (!_impl->_streams.find_thread_id()) {
+ return 0;
+ }
auto stream = _impl->_streams.local();
return stream->_streamId;
}
@@ -454,11 +472,17 @@ int CPUStreamsExecutor::get_streams_num() {
}
int CPUStreamsExecutor::get_numa_node_id() {
+ if (!_impl->_streams.find_thread_id()) {
+ return 0;
+ }
auto stream = _impl->_streams.local();
return stream->_numaNodeId;
}
int CPUStreamsExecutor::get_socket_id() {
+ if (!_impl->_streams.find_thread_id()) {
+ return 0;
+ }
auto stream = _impl->_streams.local();
return stream->_socketId;
}