From ae1beb296e08ac37818fe44273cb30f92b2eee41 Mon Sep 17 00:00:00 2001 From: Sun Xiaoxia Date: Wed, 17 May 2023 08:08:38 +0000 Subject: [PATCH] remove definition of static CPU (#17393) * remove definition of static CPU * revert old format * definition of cpu_info covers macOS * add lock in is_cpu_map_available --- src/inference/src/streams_executor.hpp | 2 ++ src/inference/src/system_conf.cpp | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/inference/src/streams_executor.hpp b/src/inference/src/streams_executor.hpp index 0fd7bc424df..d2af0454850 100644 --- a/src/inference/src/streams_executor.hpp +++ b/src/inference/src/streams_executor.hpp @@ -31,6 +31,8 @@ public: int _num_threads = 0; }; +CPU& cpu_info(); + #ifdef __linux__ /** * @brief Parse processors infomation on Linux diff --git a/src/inference/src/system_conf.cpp b/src/inference/src/system_conf.cpp index 19b42b9d3e2..3117ae1a0ba 100644 --- a/src/inference/src/system_conf.cpp +++ b/src/inference/src/system_conf.cpp @@ -151,6 +151,11 @@ bool check_open_mp_env_vars(bool include_omp_num_threads) { return false; } +CPU& cpu_info() { + static CPU cpu; + return cpu; +} + #if defined(__APPLE__) || defined(__EMSCRIPTEN__) // for Linux and Windows the getNumberOfCPUCores (that accounts only for physical cores) implementation is OS-specific // (see cpp files in corresponding folders), for __APPLE__ it is default : @@ -181,10 +186,9 @@ void set_cpu_used(const std::vector& cpu_ids, const int used) {} #else -static CPU cpu; - # ifndef _WIN32 int get_number_of_cpu_cores(bool bigCoresOnly) { + CPU& cpu = cpu_info(); unsigned numberOfProcessors = cpu._processors; unsigned totalNumberOfCpuCores = cpu._cores; IE_ASSERT(totalNumberOfCpuCores != 0); @@ -217,6 +221,7 @@ int get_number_of_cpu_cores(bool bigCoresOnly) { # if !((OV_THREAD == OV_THREAD_TBB || OV_THREAD == OV_THREAD_TBB_AUTO)) std::vector get_available_numa_nodes() { + CPU& cpu = cpu_info(); std::vector nodes((0 == cpu._numa_nodes) ? 1 : cpu._numa_nodes); std::iota(std::begin(nodes), std::end(nodes), 0); return nodes; @@ -225,16 +230,19 @@ std::vector get_available_numa_nodes() { # endif std::vector> get_proc_type_table() { + CPU& cpu = cpu_info(); std::lock_guard lock{cpu._cpu_mutex}; return cpu._proc_type_table; } bool is_cpu_map_available() { + CPU& cpu = cpu_info(); + std::lock_guard lock{cpu._cpu_mutex}; return cpu._proc_type_table.size() > 0 && cpu._num_threads == cpu._proc_type_table[0][ALL_PROC]; } int get_num_numa_nodes() { - return cpu._numa_nodes; + return cpu_info()._numa_nodes; } std::vector> reserve_available_cpus(const std::vector> streams_info_table) { @@ -242,6 +250,7 @@ std::vector> reserve_available_cpus(const std::vector(streams_info_table.size()); std::vector> stream_ids; std::vector>> res_stream_ids; + CPU& cpu = cpu_info(); stream_ids.assign(info_table_size, std::vector()); res_stream_ids.assign(info_table_size, std::vector>()); @@ -275,6 +284,7 @@ std::vector> reserve_available_cpus(const std::vector& cpu_ids, const int used) { + CPU& cpu = cpu_info(); std::lock_guard lock{cpu._cpu_mutex}; const auto cpu_size = static_cast(cpu_ids.size()); for (int i = 0; i < cpu_size; i++) { @@ -286,7 +296,7 @@ void set_cpu_used(const std::vector& cpu_ids, const int used) { if (used == NOT_USED || used >= PLUGIN_USED_START) { std::vector all_table; int start = cpu._numa_nodes > 1 ? 1 : 0; - if (is_cpu_map_available()) { + if (cpu._proc_type_table.size() > 0 && cpu._num_threads == cpu._proc_type_table[0][ALL_PROC]) { cpu._proc_type_table.assign(cpu._proc_type_table.size(), std::vector(PROC_TYPE_TABLE_SIZE, 0)); all_table.resize(PROC_TYPE_TABLE_SIZE, 0); for (int i = 0; i < cpu._processors; i++) {