Skip to content

threads: a mask that leaves the L3 groups unequal pins every allowed cpu before any shares one - #824

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

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

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

CPU 마스크가 L3 그룹의 크기를 서로 다르게 남길 때, 스레드 풀이 허용된 모든 cpu에 한 명씩 먼저 배치하도록 고칩니다.

What and why

The host thread pool places participant t on CCD t % ccds at slot t / ccds of that CCD's cpu list. It assumes every L3 group has the same number of cpus, and Shape::cpu_for clamps a slot past a group's length to the group's last cpu. When the process's cpu mask leaves the groups unequal, several participants land on one cpu while other allowed cpus stay idle. The pool prints no oversubscription line, because the thread count never exceeded the cpu count.

Take a mask of [0, 4, 5, 6, 7] over two groups of four cores. Before this change the five participants pinned to [0, 4, 0, 5, 0]: three on cpu 0, and cpus 6 and 7 unused. On a two-CCD x86 host with 32 cpus, taskset -c 0,8-15,24-31 gave nine participants the pins [0, 8, 0, 9, 0, 10, 0, 11, 0], which is five distinct cpus.

ccd_slot in crates/threads/src/topology.rs now takes a Spread: either Even(n), groups of equal size, or Uneven(&groups). Round r hands one participant, in CCD order, to each CCD that holds more than r cpus. No cpu takes a second participant while another is free. Participants past the cpu count go around every CCD and share that CCD's last slot, as before. Groups of equal size keep the closed form (t % n, t / n), so a machine without a restricting mask pins as it did.

CcdMap now holds a Spread<'static> instead of a CCD count, and ccd_of, rank_of and first_lane read the same rule as the pins, so the map agrees with where participants sit. Over unequal groups, first_lane asks ccd_slot where the next participant goes and derives the counts before each CCD from that place, so the round logic exists once. The pool builds its map in one place, Shape::ccd_map. CcdMap is still Copy and allocates nothing. There is no new option or environment variable.

One public signature changes: Pool::ccd_map(&self) becomes Pool::ccd_map(&'static self), because the map borrows the topology of the pool, which is leaked. Every caller already goes through threads::pool(), and cargo check --workspace --all-targets passed on an earlier head of this branch that had the same signature.

Release notes: Fixed: a process whose cpu mask leaves the L3 groups unequal now gets one thread pool participant on each allowed cpu, instead of several participants sharing a cpu while others stay idle.

Code shape

  • No second copy. ccd_slot and Spread in topology.rs are the one owner of the placement rule. The pool pins by them and CcdMap answers by them, and the map construction that lived in Pool::ccd_map moved into Shape::ccd_map.
  • Model code holds only architecture facts. The change is in the threads crate. No model file changed.
  • Dead code is gone. The old ccd_slot(t, nccd) and the ccds field of CcdMap are replaced, not kept beside the new ones.
  • 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 -p bloomery-threads --check passes, and cargo clippy is clean for the threads crate on this head.

Duplication found or removed (path:line): none left. A first version counted the participants of a CCD in a second function that repeated the round walk of ccd_slot. That function is deleted, and the duplicate scan reports no candidates.

What I ran

The head is 3180c990b13d1eea5ae2a4590a924b09676ed8d2, one commit on main 869eeb70641dcb7a19be3294de62f4aa3e669f18. The tests ran on x86 Linux. The results below come from the head's tree, or from an earlier head of this branch where I say so.

The unit test unequal_groups_take_every_cpu_before_one_is_shared in topology.rs covers the mask above, an explicit thread count past the cpu count, and three groups of 1, 4 and 2 cpus with thread counts 1 to 10. For those it checks that the CcdMap follows the pins and that each CCD's width equals the participants placed on it. On the base the unit test fails:

assertion `left == right` failed: a participant on each allowed cpu
  left: [0, 4, 0, 5, 0]
 right: [0, 4, 5, 6, 7]

On this head, cargo test -p bloomery-threads --lib --test pool exits 0 and the pool tests report 8 passed; 0 failed; 2 ignored, the two ignored being hardware tests. To check that the test pins the new count, I dropped the g < next && term of the partial-round count: the test then panics and the run exits 101.

The hw_ccd_map_matches_the_pins integration test (#[ignore], it pins the calling thread and reads the real topology) also asserts that no cpu holds two participants while the topology has a cpu for each. I ran it under taskset -c 0,8-15,24-31, which leaves one cpu in the first L3 group and sixteen in the second. On the base the hardware test fails:

hw_ccd_map: pins=[0, 8, 0, 9, 0, 10, 0, 11, 0] groups=[[0], [8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31]]
assertion `left == right` failed: a cpu holds two participants
  left: 5
 right: 9

With the fix, on the same mask, on an earlier head of this branch (before the last refactor of first_lane and a rebase):

hw_ccd_map: pins=[0, 8, 9, 10, 11, 12, 13, 14, 15] groups=[[0], [8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31]]
hw_ccd_map: threads=9 ccds=2 widths=[1, 8] l3_bytes=67108864
test hw_ccd_map_matches_the_pins ... ok

cargo fmt -p bloomery-threads --check passes on that earlier head, and so does cargo check --workspace --all-targets. The existing tests ccd_map_places_participants_by_the_pinning_rule and ccd_spans_follow_the_participants_per_ccd cover the equal-group arithmetic and are unchanged.

Not run on this head: the hardware test, cargo check --workspace --all-targets, just contrib-check, and the GB10 and Apple Silicon hosts. I did not time anything, because only the cpu placement of the host pool changes, not the GPU or decode path. A host with a single L3 group cannot produce unequal groups, so the hardware test skips there.

Hardware

x86 Linux, 32 cpus, 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 초안 리뷰는 제가 직접 했습니다.

…cpu before any shares one

The pool placed participant t on CCD t % ccds at slot t / ccds and clamped a
slot past the CCD's cpus to its last one. A mask that leaves the groups of
unequal size, [0,4,5,6,7] on two groups of four cores, then pinned five
participants to [0,4,0,5,0]: three on cpu 0, cpus 6 and 7 idle, and no
oversubscription line because the thread count did not pass the cpu count.

ccd_slot now hands round r one participant, in CCD order, to each CCD holding
more than r cpus, so no cpu takes a second participant while another is free;
past the cpus each round goes over every CCD and shares its last slot, as
before. Groups of equal counts keep the closed form, so an unconstrained
machine pins as it did. CcdMap reads the same spread (Shape::ccd_map), so its
CCD, rank, width and lanes match the pins.
@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: the new placement rule and CCD-map counts agree for partial rounds and oversubscription.

Non-blocking

⚠ Measure the host dispatch cost: crates/threads/src/topology.rs:435. Unequal-group lookups now walk the topology. Seat::of at ops.rs:4662 invokes ccd_slot five times per participant; every span invokes it twice.
Seat setup and spans alone contribute 5 × T × D + 2 × S topology-walking calls/token [derived], where T is participants, D is CCD dispatches/token and S is span calls/token. The work depends on fixed placement rather than token data.
Attach current-head gate-threads output and timed A/B under the same uneven mask, as required for dispatch changes by AGENTS.md's Measuring contract. Resolve fixed placement once if the measured cost matters; checking now establishes the cost before more callers depend on it.

Checked

Duplicates: dryer's ccd_of/rank_of pair selects different tuple components; extracting their short bounds checks would add elements.
Complexity: crapper flagged no functions.
Silent failure: the new max().unwrap_or(0) cannot see empty groups through Shape::of, which rejects an empty mask and removes empty CCDs.
Tests: static mutation checks show assertions catch old pin arithmetic, removal of g < next at topology.rs:441, and removal of either pin-state guard at lib.rs:293.
Arithmetic cross-check: an independent model checked 1,364 shapes (1–5 groups, each of size 1–4) and 34,588 thread counts (1 through twice each shape's CPU count plus 2), confirming ranks, prefixes and lane bijections. This is not execution of the Rust implementation.
Gates: no thresholds, tolerances or lint levels moved; the body supplies FAIL-first output for the unequal-mask defect.
Escape catalog: no patterns matched.

Notes

Rust and hardware tests were not run during validation. The body's hardware pass predates the final map refactor; it contains no timed A/B or qualifying GPU gate evidence.

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

Filed outside this PR: #825

@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 af50bf7 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.

1 participant