Skip to content

threads: the default pool takes every allowed physical core before an SMT sibling - #826

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-threads-smt
Oct 26, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-fix-threads-smt

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

CPU 마스크가 일부 코어의 SMT 형제만 남길 때도, 기본 스레드 풀이 허용된 물리 코어마다 한 명씩 먼저 배치하도록 고칩니다.

What and why

Fixes #825. The host thread pool's default thread count is the number of allowed physical cores, but the placement rule only looked at each CCD's total cpu count. When the process's cpu mask leaves two L3 groups with the same number of cpus but a different number of cores, the pool treated them as equal and went round robin. One core could then hold two participants, as a core and its SMT sibling, while another allowed core had none.

Take a mask of [0, 8, 4, 5] over two L3 groups of four cores, where cpu 8 is the SMT sibling of cpu 0. Each group keeps two cpus, but the first holds one core and the second holds two. Before this change the three participants pinned to [0, 4, 8]: cpus 0 and 8 share a core, and the core holding cpu 5 stayed idle. Now they pin to [0, 4, 5].

Shape in crates/threads/src/topology.rs now keeps each CCD's allowed core count next to its cpu list, and Spread::Uneven carries it. ccd_slot and Spread::first_lane hand out slots in two tiers: physical cores first, one participant per CCD that still has a core free in each round, then the SMT siblings in rounds the same way. Once every cpu holds a participant, the rest share each CCD's last slot, as before. The pins and CcdMap read the same Shape::spread(), so the map still agrees with where participants sit. There is no new option or environment variable.

CCDs with equal core counts and equal cpu counts keep the closed form (t % n, t / n), so a host with no restricting mask pins as before. Without SMT siblings the second tier is empty and the rule is the same as before this change.

Release notes: Fixed: a process whose cpu mask leaves L3 groups with different core counts now gets one thread pool participant on each allowed physical core before any core holds two.

Code shape

  • No second copy. ccd_slot, Spread and Shape::spread() in topology.rs stay the one owner of the placement rule. I changed that owner in place, and the pins and CcdMap both read it.
  • Model code holds only architecture facts. The change is in the threads crate. No model file changed.
  • Dead code is gone. The running kept_cores counter in Shape::of is replaced by a per-CCD list that it is summed from.
  • No silent failure. The change takes no new input. The one expect states an invariant: a participant in a round belongs to one of the CCDs open in that round.
  • Comments state what is true now. The comments describe the code as it is.
  • No relaxed gate. No band, tolerance or lint level moved.
  • cargo fmt and cargo clippy are clean on what I touched.

Duplication found or removed (path:line): none.

What I ran

The head is f329a9138fbeba6b7df86e1cabba3dc7afa4fb1e, one commit on main af50bf7874ca867912332ea45602060732477588. The hardware results below ran on an earlier head of this branch. The only change since then is a type alias inside a test that clippy asked for, so the product code is the same.

The new unit test every_allowed_core_takes_a_participant_before_a_sibling in topology.rs builds the mask above, an explicit thread count of four, and a topology without SMT siblings (groups of five and three one-cpu cores). The existing mapping test in topology.rs now also builds three masks that leave the groups unequal in cores or in cpus, and goes through every thread count up to past the cpu count. On the base the new test fails:

assertion `left == right` failed
  left: ([0, 4], 8)
 right: ([0, 4], 5)
test result: FAILED. 0 passed; 1 failed

On the earlier head, on an RTX 5090 machine with taskset set to the mask 0,16,8,9 (cpus 0 and 16 are one core, cpus 8 and 9 are two cores of the other L3 group), cargo test -p bloomery-threads passes (the lib tests report 18 passed, the pool tests 8 passed; 0 failed; 2 ignored) and cargo fmt -p bloomery-threads --check prints FMT-OK. The hw_ccd_map_matches_the_pins test (#[ignore], it pins the calling thread and reads the real topology) now also asserts that no core holds two participants while an allowed core is free. It passes under that mask and without a mask:

== masked
hw_ccd_map: pins=[0, 8, 9] groups=[[0, 16], [8, 9]]
hw_ccd_map: threads=3 ccds=2 widths=[1, 2] l3_bytes=67108864
test hw_ccd_map_matches_the_pins ... ok
== unconstrained
hw_ccd_map: pins=[0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15] groups=[[0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23], [8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31]]
hw_ccd_map: threads=16 ccds=2 widths=[8, 8] l3_bytes=67108864
test hw_ccd_map_matches_the_pins ... ok

cargo clippy for the threads crate is clean on this head (exit 0).

Not run: the hardware test on the base, the hardware test on this exact head, cargo check --workspace --all-targets (no public signature changed; Spread and Shape are pub(crate)), just contrib-check, and the GB10 and Apple Silicon hosts, which have no SMT siblings and are covered by the unit test without SMT. I did not time anything, because only the cpu placement of the host pool changes, not the GPU or decode path.

Hardware

NVIDIA GeForce RTX 5090, 595.99.02
Model name:                              AMD Ryzen 9 9950X 16-Core Processor
Thread(s) per core:                      2
Core(s) per socket:                      16

x86 Linux, two L3 groups (cpus 0-7 and 16-23 share one, cpus 8-15 and 24-31 the other). The change does not touch the GPU path.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

… SMT sibling

Shape::of kept each CCD's allowed cpus with the primaries first but the
placement rule read only the total cpu count per CCD. A mask of one core and
its sibling in one L3 group and two cores of another, [0,8,4,5] on two groups
of four two-cpu cores, gave groups [[0,8],[4,5]] of equal counts, so the
round robin pinned the three default participants to [0,4,8]: two on one
core while allowed cpu 5's core idled.

Shape now keeps each CCD's allowed core count, and ccd_slot hands out the
physical cores in rounds over the CCDs before any sibling, then the siblings
in rounds, then shares the last slot as before. CcdMap reads the same spread,
so its CCD, rank, width and lanes still match the pins. CCDs of equal core and
cpu counts keep the closed form, so an unconstrained machine pins as it did,
and a topology without SMT places exactly as before.

Fixes midagedev#825
@github-actions github-actions Bot added the host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset label Oct 26, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: No must-fix finding remains; masked host-tier performance needs measurement.

Non-blocking

⚠ Measure masked host-tier dispatch: crates/threads/src/topology.rs:426 changes equal-CPU-count masks with unequal core counts from closed-form placement to scanning.
crates/model/src/ops.rs:4662 calls ccd_of, first_lane, width (two more first_lane calls), and rank_of: up to 5 × T × D placement scans/token [derived from source calls, before compiler optimization], for T participants and D CCD-major row dispatches/token.
Provide a masked same-lease host-tier A/B and the owning gate-threads output. AGENTS.md requires a same-lease A/B for dispatch-path changes; the PR reports no timing. Measuring now separates this cost from later host-tier changes.

Checked

  • Duplicates: pins and CCD mapping share ccd_slot; dryer reported 0 pairs.
  • Complexity: crapper reported no changed function crossing its thresholds.
  • Silent failure: empty masks panic; oversubscribed participants share their CCD's last CPU slot. The new expect states the round-membership invariant.
  • Tests, source mutation analysis: dropping the core-count comparison at topology.rs:426 restores pins [0, 4, 8], failing the expected [0, 4, 5]. Changing >= to > at line 471 misclassifies the first sibling slot; the expanded mapping assertions cover that boundary.
  • Gates: no thresholds, bands, cases, or lint levels relaxed; the hardware test remains hw_ and ignored.
  • Escape catalog: no C1–C4 patterns match the diff.

Notes

Rust and hardware tests were not rerun during validation. Validation used source inspection.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 895b32d into midagedev:main Oct 26, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset

Projects

None yet

Development

Successfully merging this pull request may close these issues.

threads: default placement uses SMT while an allowed physical core stays idle

1 participant