| 558 | } |
| 559 | |
| 560 | bool SessionLoopScratch::init(size_t parts_bytes_in, std::string& err) { |
| 561 | if (y_miss != nullptr || probe != nullptr) { |
| 562 | err = "SessionLoopScratch::init: already initialised"; |
| 563 | return false; |
| 564 | } |
| 565 | parts_bytes = parts_bytes_in; |
| 566 | // MAPPED as well as pinned: the token graph's handoff kernel reads it through its device pointer. |
| 567 | if (strata::core::host_alloc_guarded((void**) &y_miss, parts_bytes, cudaHostAllocMapped | cudaHostAllocPortable) != cudaSuccess) { |
| 568 | err = "SessionLoopScratch: cudaHostAlloc for the pool's staging failed"; |
| 569 | return false; |
| 570 | } |
| 571 | std::memset(y_miss, 0, parts_bytes); |
| 572 | if (cudaEventCreate(&probe) != cudaSuccess) { |
| 573 | err = "SessionLoopScratch: cudaEventCreate failed"; |
| 574 | free(); |
| 575 | return false; |
| 576 | } |
| 577 | // **PIN THE HOST ONCE, NOT ONCE PER TOKEN.** `ExpertPool` builds its workers from `physical_cores(true)`, |
| 578 | // which drops the first physical core so the host loop can spin without taking a worker's cycles - and |
| 579 | // nothing in the pool can pin the host, so if this does not happen the spin is free to land on a worker's |
| 580 | // core or its SMT sibling. The symptom is not an error: it is a CPU path at 26.9 GB/s where the same pool |
| 581 | // runs at 36.32. It was being done and undone on EVERY token, which is a syscall pair on the critical path |
| 582 | // for a property that wants to hold for the whole session. |
| 583 | // The host's core is the pool's reserved one: the first physical core, or the last with --host-core last (F12), |
| 584 | // or the first core's SMT sibling with --host-core sibling. |
| 585 | const int host_core = strata::kernels::cpu::planned_host_core(); |
| 586 | if (host_core >= 0) { |
| 587 | pinned_core = strata::kernels::cpu::pin_current_thread(host_core); |
| 588 | pinned = pinned_core.valid; |
| 589 | } else { |
| 590 | const std::vector<int> cores = strata::kernels::cpu::physical_cores(false); |
| 591 | if (!cores.empty()) { |
| 592 | pinned_core = strata::kernels::cpu::pin_current_thread(cores[0]); |
| 593 | pinned = pinned_core.valid; |
| 594 | } |
| 595 | } |
| 596 | strata::platform::name_this_thread("strata-host"); |
| 597 | strata::aux_cpus::note_owned_thread(); // the host: --aux-cpus leaves it where it is |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | void SessionLoopScratch::free() { |
| 602 | // Restores the caller's affinity on every path, including teardown: a library that silently leaves its |