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
This commit is contained in:
Sun Xiaoxia
2023-05-17 08:08:38 +00:00
committed by GitHub
parent d26c587198
commit ae1beb296e
2 changed files with 16 additions and 4 deletions

View File

@@ -31,6 +31,8 @@ public:
int _num_threads = 0;
};
CPU& cpu_info();
#ifdef __linux__
/**
* @brief Parse processors infomation on Linux

View File

@@ -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<int>& 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<int> get_available_numa_nodes() {
CPU& cpu = cpu_info();
std::vector<int> 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<int> get_available_numa_nodes() {
# endif
std::vector<std::vector<int>> get_proc_type_table() {
CPU& cpu = cpu_info();
std::lock_guard<std::mutex> lock{cpu._cpu_mutex};
return cpu._proc_type_table;
}
bool is_cpu_map_available() {
CPU& cpu = cpu_info();
std::lock_guard<std::mutex> 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<std::vector<int>> reserve_available_cpus(const std::vector<std::vector<int>> streams_info_table) {
@@ -242,6 +250,7 @@ std::vector<std::vector<int>> reserve_available_cpus(const std::vector<std::vect
int info_table_size = static_cast<int>(streams_info_table.size());
std::vector<std::vector<int>> stream_ids;
std::vector<std::vector<std::vector<int>>> res_stream_ids;
CPU& cpu = cpu_info();
stream_ids.assign(info_table_size, std::vector<int>());
res_stream_ids.assign(info_table_size, std::vector<std::vector<int>>());
@@ -275,6 +284,7 @@ std::vector<std::vector<int>> reserve_available_cpus(const std::vector<std::vect
}
void set_cpu_used(const std::vector<int>& cpu_ids, const int used) {
CPU& cpu = cpu_info();
std::lock_guard<std::mutex> lock{cpu._cpu_mutex};
const auto cpu_size = static_cast<int>(cpu_ids.size());
for (int i = 0; i < cpu_size; i++) {
@@ -286,7 +296,7 @@ void set_cpu_used(const std::vector<int>& cpu_ids, const int used) {
if (used == NOT_USED || used >= PLUGIN_USED_START) {
std::vector<int> 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<int>(PROC_TYPE_TABLE_SIZE, 0));
all_table.resize(PROC_TYPE_TABLE_SIZE, 0);
for (int i = 0; i < cpu._processors; i++) {