feat(cpu): add hybrid architecture awareness and --pool-affinity for P/E-core CPUs

- Add detect_cpu_topology() for Windows (GetLogicalProcessorInformationEx) and Linux (/sys/devices/system/cpu/cpu*/cpu_capacity)
- Identify hybrid P/E-core topology and SMT sibling structures
- Prioritize primary P-cores for worker allocation, reserving core 0 for host thread
- Default pool_workers to (p_cores - 1) on hybrid CPUs when unspecified
- Add --pool-affinity auto|p-cores|all CLI option
- Maintain 100% backward compatibility on non-hybrid CPUs and default configs
This commit is contained in:
Pravesh Khatana authored and Niko1221 committed 2026-10-01 08:47:28 +02:00
1 parent 029e10d9d9
commit 69743d3a5c
3 files changed
+261 -35

No files matched your search

+33 -2
View File
@@ -63,13 +63,35 @@ struct ExpertJobMulti {
const void* nact[MAXT] = {};
};
/// One logical processor per PHYSICAL core, so a worker is never scheduled onto an SMT sibling of another
/// worker. On the 6-core/12-thread machine this project measures on, `hardware_concurrency()/2` workers on
/// logical processors 0..5 would put every worker on a sibling pair and halve the useful bandwidth - which is
/// exactly the kind of error that shows up as "the CPU path is slower than the model says" with no clue why.
/// How worker threads are allocated across physical/logical CPU cores.
enum class PoolAffinity {
Auto, ///< Hybrid: prioritize physical P-cores, then SMT, then E-cores (defaults to P-core count)
PCores, ///< Restrict workers strictly to Performance cores and their SMT siblings
All, ///< Legacy behavior: linear physical core order without hybrid distinction
};
struct CpuTopology {
bool is_hybrid = false;
int p_cores = 0; ///< Physical performance cores
int p_threads = 0; ///< Total logical threads on performance cores
int e_cores = 0; ///< Efficient cores
std::vector<int> worker_cores; ///< Ordered logical core IDs for workers (excluding host if skip_first)
int host_core = -1; ///< Logical core reserved for host thread
};
CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity = PoolAffinity::Auto);
/// One logical processor per PHYSICAL core, so a worker is never scheduled onto an SMT sibling of another
/// worker. On the 6-core/12-thread machine this project measures on, `hardware_concurrency()/2` workers on
/// logical processors 0..5 would put every worker on a sibling pair and halve the useful bandwidth - which is
/// exactly the kind of error that shows up as "the CPU path is slower than the model says" with no clue why.
///
/// `skip_first` drops the first core, which P2.S3 reserves for the host loop.
std::vector<int> physical_cores(bool skip_first);
std::vector<int> physical_cores(bool skip_first, PoolAffinity affinity = PoolAffinity::Auto);
/// **THE RESERVATION IS A FICTION UNLESS THE HOST IS ACTUALLY PUT THERE.**
///
@@ -101,7 +123,8 @@ public:
/// With `host_works`, `run()` claims jobs itself instead of spinning on `done_`, and the pool is six
/// threads on six cores. `false` is the A/B arm and exists so the change is measurable rather than
/// asserted - the counter it moves is `pool phases ... drain`, which is host-side and needs no profiler.
explicit ExpertPool(int n_workers = 0, bool pin = true, bool host_works = true);
explicit ExpertPool(int n_workers = 0, bool pin = true, bool host_works = true,
PoolAffinity affinity = PoolAffinity::Auto);
/// The watchdog's view of the pool (issue #31): the batch, the counters, every thread's state.
void diag(std::FILE* f) const;
~ExpertPool();
@@ -113,6 +136,12 @@ public:
/// is on" is only true if the engine says which adaptation it took.
bool host_works() const { return host_works_; }
bool is_hybrid() const { return topo_.is_hybrid; }
int p_cores() const { return topo_.p_cores; }
int p_threads() const { return topo_.p_threads; }
int e_cores() const { return topo_.e_cores; }
PoolAffinity affinity() const { return affinity_; }
/// Publish `n` jobs, then block until every one has been claimed AND every worker has parked.
/// `jobs` must outlive the call (it does, and the workers never touch it afterwards).
void run(ExpertJob* jobs, int n);
@@ -239,6 +268,8 @@ private:
};
const NativeFmt* nfmt_ = nullptr;
std::vector<SplitBufMulti> split_multi_;
PoolAffinity affinity_ = PoolAffinity::Auto;
CpuTopology topo_;
};
} // namespace strata::kernels::cpu