Skip to content

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

[CPU] Distribute inference streams across Windows processor groups when core pinning is off (#37632)

Maksim Kutakovf9f5700c78b2
diff --git a/src/inference/dev_api/openvino/runtime/threading/cpu_streams_executor_internal.hpp b/src/inference/dev_api/openvino/runtime/threading/cpu_streams_executor_internal.hpp
index e29c388755f..7386b3e616d 100644
--- a/src/inference/dev_api/openvino/runtime/threading/cpu_streams_executor_internal.hpp
+++ b/src/inference/dev_api/openvino/runtime/threading/cpu_streams_executor_internal.hpp
@@ -74,5 +74,27 @@ void update_proc_type_table(const std::vector<std::vector<int>> _cpu_mapping_tab
const int _numa_nodes,
std::vector<std::vector<int>>& _proc_type_table);
+/**
+ * @brief Select the processor group a stream's worker thread is soft-bound to when spreading
+ * work across Windows processor groups (machines with more than 64 logical processors).
+ *
+ * Distribution is per worker thread, not per stream: a single stream whose concurrency exceeds one
+ * group's core count is spread across several groups so all cores are used. Streams are offset by
+ * stream_id so that many single-thread streams still fan out across groups.
+ *
+ * @param[in] stream_id the stream index, used as the per-stream group offset
+ * @param[in] thread_index the worker thread's slot index within the stream's arena
+ * @param[in] group_count the number of active processor groups
+ * @return the target processor group id in [0, group_count), or -1 when group_count <= 1
+ * (single group / no distribution needed)
+ */
+int get_thread_processor_group(int stream_id, int thread_index, int group_count);
+
+/**
+ * @brief Get the number of active processor groups on the system.
+ * @return the number of Windows processor groups, or 1 on platforms without processor groups
+ */
+int get_num_processor_groups();
+
} // namespace threading
} // namespace ov
\ No newline at end of file
diff --git a/src/inference/src/dev/threading/cpu_streams_executor.cpp b/src/inference/src/dev/threading/cpu_streams_executor.cpp
index 669727b029f..ad20a6f2f67 100644
--- a/src/inference/src/dev/threading/cpu_streams_executor.cpp
+++ b/src/inference/src/dev/threading/cpu_streams_executor.cpp
@@ -169,10 +169,19 @@ struct CPUStreamsExecutor::Impl {
_stream_type = STREAM_WITHOUT_PARAM;
}
}
+ const int group_base = get_num_processor_groups() > 1 ? stream_id : -1;
if (_stream_type == STREAM_WITHOUT_PARAM) {
- _taskArena.reset(new custom::task_arena{custom::task_arena::constraints{}
- .set_max_concurrency(concurrency)
- .set_max_threads_per_core(max_threads_per_core)});
+ // On multi-group Windows (group_base >= 0) distribute the arena's threads across processor
+ // groups; max_threads_per_core is dropped so tbbbind does not confine it to one group.
+ if (group_base >= 0) {
+ _taskArena.reset(new custom::task_arena{custom::task_arena::constraints{}
+ .set_max_concurrency(concurrency)
+ .set_processor_group_base(group_base)});
+ } else {
+ _taskArena.reset(new custom::task_arena{custom::task_arena::constraints{}
+ .set_max_concurrency(concurrency)
+ .set_max_threads_per_core(max_threads_per_core)});
+ }
} else if (_stream_type == STREAM_WITH_NUMA_ID) {
// Numa node id has used different mapping methods in TBBBind since oneTBB 2021.4.0
# if USE_TBBBIND_2_5
@@ -183,10 +192,18 @@ struct CPUStreamsExecutor::Impl {
real_numa_node_id = _numaNodeId;
}
# endif
- _taskArena.reset(new custom::task_arena{custom::task_arena::constraints{}
- .set_numa_id(real_numa_node_id)
- .set_max_concurrency(concurrency)
- .set_max_threads_per_core(max_threads_per_core)});
+ // On multi-group Windows (group_base >= 0) distribute the arena's threads across processor
+ // groups; max_threads_per_core is dropped so tbbbind does not confine it to one group.
+ if (group_base >= 0) {
+ _taskArena.reset(new custom::task_arena{custom::task_arena::constraints{}
+ .set_max_concurrency(concurrency)
+ .set_processor_group_base(group_base)});
+ } else {
+ _taskArena.reset(new custom::task_arena{custom::task_arena::constraints{}
+ .set_numa_id(real_numa_node_id)
+ .set_max_concurrency(concurrency)
+ .set_max_threads_per_core(max_threads_per_core)});
+ }
} else if (_stream_type == STREAM_WITH_CORE_TYPE) {
// sys_core_types = [LPECore, Ecore, Pcore]
const auto sys_core_types = custom::info::core_types();
diff --git a/src/inference/src/dev/threading/cpu_streams_executor_internal.cpp b/src/inference/src/dev/threading/cpu_streams_executor_internal.cpp
index 4fa892f8215..70ed00a2763 100644
--- a/src/inference/src/dev/threading/cpu_streams_executor_internal.cpp
+++ b/src/inference/src/dev/threading/cpu_streams_executor_internal.cpp
@@ -10,6 +10,13 @@
#include "openvino/runtime/system_conf.hpp"
#include "openvino/runtime/threading/cpu_streams_info.hpp"
+#if defined(_WIN32)
+# ifndef NOMINMAX
+# define NOMINMAX
+# endif
+# include <windows.h>
+#endif
+
namespace ov {
namespace threading {
@@ -269,5 +276,26 @@ void update_proc_type_table(const std::vector<std::vector<int>> _cpu_mapping_tab
}
}
+int get_thread_processor_group(int stream_id, int thread_index, int group_count) {
+ if (group_count <= 1) {
+ return -1;
+ }
+ // Spread an arena's worker threads across groups by slot index, offset per stream so single-thread
+ // streams still fan out. numa id is deliberately not used as a group id (Windows does not match them).
+ const int base = stream_id > 0 ? stream_id : 0;
+ const int slot = thread_index > 0 ? thread_index : 0;
+ return (base + slot) % group_count;
+}
+
+int get_num_processor_groups() {
+#if defined(_WIN32)
+ // GetActiveProcessorGroupCount() returns 0 on failure; normalize to a single group so callers can
+ // rely on the count being at least 1 (matching get_thread_processor_group's group_count <= 1 path).
+ return std::max<int>(1, static_cast<int>(GetActiveProcessorGroupCount()));
+#else
+ return 1;
+#endif
+}
+
} // namespace threading
} // namespace ov
diff --git a/src/inference/src/dev/threading/parallel_custom_arena.cpp b/src/inference/src/dev/threading/parallel_custom_arena.cpp
index 7a37fc852cd..19c53134bd1 100644
--- a/src/inference/src/dev/threading/parallel_custom_arena.cpp
+++ b/src/inference/src/dev/threading/parallel_custom_arena.cpp
@@ -6,6 +6,7 @@
#include <cstring>
#include "dev/threading/itt.hpp"
+#include "openvino/runtime/threading/cpu_streams_executor_internal.hpp"
#if OV_THREAD == OV_THREAD_TBB || OV_THREAD == OV_THREAD_TBB_AUTO || OV_THREAD == OV_THREAD_TBB_ADAPTIVE
@@ -214,7 +215,9 @@ void binding_observer::on_scheduler_exit(bool) {
static binding_oberver_ptr construct_binding_observer(tbb::task_arena& ta, int num_slots, const constraints& c) {
binding_oberver_ptr observer{};
- if (detail::is_binding_environment_valid() &&
+ // A stream requesting processor-group distribution uses group_affinity_observer instead; the two
+ // are mutually exclusive so the tbbbind binding must not also attach to the same arena.
+ if (c.processor_group_base < 0 && detail::is_binding_environment_valid() &&
((c.core_type >= 0 && info::core_types().size() > 1) || (c.numa_id >= 0 && info::numa_nodes().size() > 1) ||
c.max_threads_per_core > 0)) {
observer.reset(new binding_observer{ta, num_slots, c});
@@ -224,6 +227,90 @@ static binding_oberver_ptr construct_binding_observer(tbb::task_arena& ta, int n
}
# endif // USE_TBBBIND_2_5
+
+# if defined(_WIN32)
+// Per-worker-thread LIFO stack of saved group affinities so nested group arenas restore correctly:
+// every on_scheduler_entry pushes exactly one frame and every on_scheduler_exit pops exactly one.
+struct saved_group_affinity {
+ GROUP_AFFINITY affinity;
+ bool valid;
+};
+static thread_local std::vector<saved_group_affinity> g_prev_group_affinity_stack;
+
+// Authoritative active-processor affinity of every processor group, queried once. Windows does not
+// guarantee the active processors form a contiguous low-bit range, so masks cannot be reconstructed.
+static const std::vector<GROUP_AFFINITY>& get_group_affinities() {
+ static const std::vector<GROUP_AFFINITY> affinities = [] {
+ std::vector<GROUP_AFFINITY> result;
+ DWORD len = 0;
+ if (GetLogicalProcessorInformationEx(RelationGroup, nullptr, &len) ||
+ GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+ return result;
+ }
+ std::vector<char> buffer(len);
+ auto* info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data());
+ if (!GetLogicalProcessorInformationEx(RelationGroup, info, &len) || info->Relationship != RelationGroup) {
+ return result;
+ }
+ for (WORD group = 0; group < info->Group.ActiveGroupCount; ++group) {
+ GROUP_AFFINITY affinity{};
+ affinity.Group = group;
+ affinity.Mask = info->Group.GroupInfo[group].ActiveProcessorMask;
+ result.push_back(affinity);
+ }
+ return result;
+ }();
+ return affinities;
+}
+
+group_affinity_observer::group_affinity_observer(tbb::task_arena& ta, int group_base)
+ : tbb::task_scheduler_observer(ta),
+ my_group_base(group_base) {
+ my_valid = group_base >= 0 && get_group_affinities().size() > 1;
+}
+
+void group_affinity_observer::on_scheduler_entry(bool) {
+ const auto& affinities = get_group_affinities();
+ const int slot = tbb::this_task_arena::current_thread_index();
+ const int group =
+ ov::threading::get_thread_processor_group(my_group_base, slot, static_cast<int>(affinities.size()));
+ // Always push exactly one frame so on_scheduler_exit stays balanced; mark it invalid when the
+ // affinity could not be applied, so exit skips the (impossible) restore for that frame.
+ GROUP_AFFINITY previous_affinity{};
+ if (group >= 0) {
+ GROUP_AFFINITY group_affinity = affinities[group];
+ if (SetThreadGroupAffinity(GetCurrentThread(), &group_affinity, &previous_affinity)) {
+ g_prev_group_affinity_stack.push_back({previous_affinity, true});
+ return;
+ }
+ }
+ g_prev_group_affinity_stack.push_back({GROUP_AFFINITY{}, false});
+}
+
+void group_affinity_observer::on_scheduler_exit(bool) {
+ if (g_prev_group_affinity_stack.empty()) {
+ return;
+ }
+ const saved_group_affinity saved = g_prev_group_affinity_stack.back();
+ g_prev_group_affinity_stack.pop_back();
+ if (saved.valid) {
+ GROUP_AFFINITY affinity = saved.affinity;
+ SetThreadGroupAffinity(GetCurrentThread(), &affinity, NULL);
+ }
+}
+
+static group_affinity_observer_ptr construct_group_affinity_observer(tbb::task_arena& ta, const constraints& c) {
+ group_affinity_observer_ptr observer{};
+ if (c.processor_group_base >= 0 && GetActiveProcessorGroupCount() > 1) {
+ group_affinity_observer_ptr candidate{new group_affinity_observer{ta, c.processor_group_base}};
+ if (candidate->valid()) {
+ candidate->observe(true);
+ observer = std::move(candidate);
+ }
+ }
+ return observer;
+}
+# endif // _WIN32
} // namespace detail
task_arena::task_arena(int max_concurrency_, unsigned reserved_for_masters)
@@ -266,6 +353,14 @@ task_arena::task_arena(const task_arena& s)
{
}
+void task_arena::init_group_affinity_observer() {
+# if defined(_WIN32)
+ std::call_once(my_group_observer_state, [this] {
+ my_group_affinity_observer = detail::construct_group_affinity_observer(my_task_arena, my_constraints);
+ });
+# endif
+}
+
void task_arena::initialize() {
my_task_arena.initialize();
# if USE_TBBBIND_2_5
@@ -275,6 +370,7 @@ void task_arena::initialize() {
detail::construct_binding_observer(my_task_arena, my_task_arena.max_concurrency(), my_constraints);
});
# endif
+ init_group_affinity_observer();
}
void task_arena::initialize(int max_concurrency_, unsigned reserved_for_masters) {
@@ -286,6 +382,7 @@ void task_arena::initialize(int max_concurrency_, unsigned reserved_for_masters)
detail::construct_binding_observer(my_task_arena, my_task_arena.max_concurrency(), my_constraints);
});
# endif
+ init_group_affinity_observer();
}
void task_arena::initialize(constraints constraints_, unsigned reserved_for_masters) {
@@ -302,6 +399,7 @@ void task_arena::initialize(constraints constraints_, unsigned reserved_for_mast
# else
my_task_arena.initialize(my_constraints.max_concurrency, reserved_for_masters);
# endif
+ init_group_affinity_observer();
}
task_arena::operator tbb::task_arena&() {
diff --git a/src/inference/src/dev/threading/parallel_custom_arena.hpp b/src/inference/src/dev/threading/parallel_custom_arena.hpp
index ae9d37bda1e..dd29d3fbecd 100644
--- a/src/inference/src/dev/threading/parallel_custom_arena.hpp
+++ b/src/inference/src/dev/threading/parallel_custom_arena.hpp
@@ -80,6 +80,10 @@ struct constraints {
max_threads_per_core = threads_number;
return *this;
}
+ constraints& set_processor_group_base(int base) {
+ processor_group_base = base;
+ return *this;
+ }
numa_node_id numa_id = tbb::task_arena::automatic;
int max_concurrency = tbb::task_arena::automatic;
@@ -87,9 +91,12 @@ struct constraints {
// Upper 4 bits reserved for format marker (single vs multiple core types)
static constexpr size_t core_type_id_bits = sizeof(core_type_id) * CHAR_BIT - 4;
int max_threads_per_core = tbb::task_arena::automatic;
+ // Per-stream offset for soft (non-pinning) distribution of the arena's threads across Windows
+ // processor groups (>64 logical processors). Negative means no group policy.
+ int processor_group_base = -1;
};
-#if USE_TBBBIND_2_5
+# if USE_TBBBIND_2_5
class binding_handler;
class binding_observer : public tbb::task_scheduler_observer {
binding_handler* my_binding_handler;
@@ -110,16 +117,52 @@ struct binding_observer_deleter {
};
using binding_oberver_ptr = std::unique_ptr<binding_observer, binding_observer_deleter>;
-#endif
+# endif
+
+# if defined(_WIN32)
+// Distributes an arena's worker threads across Windows processor groups without core pinning: each
+// entering thread is soft-bound (full active group mask) to group (base + slot) % group_count, and its
+// previous group affinity is restored on scheduler exit. This lets a single stream span multiple groups.
+class group_affinity_observer : public tbb::task_scheduler_observer {
+ // Per-stream group offset stored as a plain type so this header stays free of <windows.h>.
+ int my_group_base = -1;
+ bool my_valid = false;
+
+public:
+ group_affinity_observer(tbb::task_arena& ta, int group_base);
+
+ bool valid() const {
+ return my_valid;
+ }
+
+ void on_scheduler_entry(bool) override;
+ void on_scheduler_exit(bool) override;
+};
+
+struct group_affinity_observer_deleter {
+ void operator()(group_affinity_observer* observer) const {
+ observer->observe(false);
+ delete observer;
+ }
+};
+
+using group_affinity_observer_ptr = std::unique_ptr<group_affinity_observer, group_affinity_observer_deleter>;
+# endif
} // namespace detail
class task_arena {
tbb::task_arena my_task_arena;
std::once_flag my_initialization_state;
detail::constraints my_constraints;
-#if USE_TBBBIND_2_5
+# if USE_TBBBIND_2_5
detail::binding_oberver_ptr my_binding_observer;
-#endif
+# endif
+# if defined(_WIN32)
+ detail::group_affinity_observer_ptr my_group_affinity_observer;
+ std::once_flag my_group_observer_state;
+# endif
+
+ void init_group_affinity_observer();
public:
using constraints = detail::constraints;
diff --git a/src/inference/tests/unit/cpu_stream_info_test.cpp b/src/inference/tests/unit/cpu_stream_info_test.cpp
index fbb1b137cba..e3284501f5d 100644
--- a/src/inference/tests/unit/cpu_stream_info_test.cpp
+++ b/src/inference/tests/unit/cpu_stream_info_test.cpp
@@ -638,4 +638,79 @@ INSTANTIATE_TEST_SUITE_P(CpuStreamType,
_1sockets_32cores_all_proc_bind_subset_of_core_kinds,
_1sockets_32cores_all_proc_skip_binding_all_core_kinds));
#endif
+
+TEST(StreamProcessorGroupId, DistributesArenaThreadsAcrossGroups) {
+ // Within one stream, consecutive worker slots fan out across processor groups.
+ EXPECT_EQ(get_thread_processor_group(0, 0, 6), 0);
+ EXPECT_EQ(get_thread_processor_group(0, 1, 6), 1);
+ EXPECT_EQ(get_thread_processor_group(0, 5, 6), 5);
+ EXPECT_EQ(get_thread_processor_group(0, 6, 6), 0);
+}
+
+TEST(StreamProcessorGroupId, OffsetsStreamsSoSingleThreadStreamsFanOut) {
+ // With one thread per stream (slot 0), streams still spread across groups via the stream offset.
+ EXPECT_EQ(get_thread_processor_group(0, 0, 6), 0);
+ EXPECT_EQ(get_thread_processor_group(1, 0, 6), 1);
+ EXPECT_EQ(get_thread_processor_group(7, 0, 6), 1);
+}
+
+TEST(StreamProcessorGroupId, HandlesNegativeInputs) {
+ // Negative stream id / thread index degrade to 0 rather than an out-of-range index.
+ EXPECT_EQ(get_thread_processor_group(-1, -1, 6), 0);
+}
+
+TEST(StreamProcessorGroupId, ReturnsNoGroupOnSingleOrInvalidGroupCount) {
+ // A single group (or invalid count) means no distribution is needed: no group policy is applied.
+ EXPECT_EQ(get_thread_processor_group(7, 3, 1), -1);
+ EXPECT_EQ(get_thread_processor_group(7, 3, 0), -1);
+ EXPECT_EQ(get_thread_processor_group(7, 3, -1), -1);
+}
+
+TEST(StreamProcessorGroupId, ManySingleThreadStreamsFillAllGroups) {
+ // 288 concurrency-1 streams (slot 0) must spread evenly across 6 groups.
+ const int group_count = 6;
+ const int stream_count = 288;
+ std::vector<int> per_group(group_count, 0);
+ for (int stream_id = 0; stream_id < stream_count; ++stream_id) {
+ per_group[get_thread_processor_group(stream_id, 0, group_count)]++;
+ }
+ for (int group = 0; group < group_count; ++group) {
+ EXPECT_EQ(per_group[group], stream_count / group_count);
+ }
+}
+
+TEST(StreamProcessorGroupId, FewLargeStreamsFillAllGroups) {
+ // The reported case: 2 streams of 144 threads each must fill all 6 groups (48 threads per group),
+ // not just 2 groups. Every (stream, slot) worker thread is mapped to a group.
+ const int group_count = 6;
+ const int streams = 2;
+ const int threads_per_stream = 144;
+ std::vector<int> per_group(group_count, 0);
+ for (int stream_id = 0; stream_id < streams; ++stream_id) {
+ for (int slot = 0; slot < threads_per_stream; ++slot) {
+ per_group[get_thread_processor_group(stream_id, slot, group_count)]++;
+ }
+ }
+ for (int group = 0; group < group_count; ++group) {
+ EXPECT_EQ(per_group[group], streams * threads_per_stream / group_count);
+ }
+}
+
+TEST(StreamProcessorGroupId, NumProcessorGroupsIsAtLeastOne) {
+ // Always at least one group; on non-Windows platforms there is exactly one.
+ EXPECT_GE(get_num_processor_groups(), 1);
+#if !defined(_WIN32)
+ EXPECT_EQ(get_num_processor_groups(), 1);
+#endif
+}
+
+TEST(StreamProcessorGroupId, NoDistributionWhenSystemReportsSingleGroup) {
+ // End-to-end guard: on a single-group system (e.g. non-Windows CI) threads get no group policy,
+ // so behavior is unchanged regardless of stream index or slot.
+ if (get_num_processor_groups() <= 1) {
+ for (int i = 0; i < 16; ++i) {
+ EXPECT_EQ(get_thread_processor_group(i, i, get_num_processor_groups()), -1);
+ }
+ }
+}
} // namespace