Skip to content

linalg: reusable CPU cache-size detection module - #2349

Merged
kali merged 1 commit into
sonos:mainfrom
czoli1976:feature/linalg-cache-module
Jun 23, 2026
Merged

linalg: reusable CPU cache-size detection module#2349
kali merged 1 commit into
sonos:mainfrom
czoli1976:feature/linalg-cache-module

Conversation

@czoli1976

@czoli1976 czoli1976 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Factors the L2 cache-size probe that drives the single-thread MMM block budget out of frame/mmm/mod.rs into a small, reusable cache module, so other heuristics (im2col lowering thresholds, kernel dispatch, future K-blocking) can read the same memoised numbers instead of re-implementing a platform probe.

cache::cache_info() returns L1d / L2 / L3 (bytes; 0 = undetected) from one lazily-detected, process-cached probe: macOS/iOS via sysctlbyname (FFI, replacing the previous sysctl subprocess; hw.perflevel0.* preferred on hybrid Apple Silicon), Linux and Android via the /sys cache walk keyed off each entry's reported level+type, and Windows via wmic. block_budget_bytes now reads cache_info().l2; the existing L2 budget formula and the 256 KiB unknown-cache fallback are unchanged, so single-thread blocking behaviour is preserved. Honest 0-on-unknown semantics with *_or_default helpers for callers that want an arch-based guess.

Verified on Apple M-series (L1d 128 KiB, L2 12 MiB, L3 not exposed → reported 0): full tract-linalg suite green, cargo fmt --all clean, no new clippy findings.

Citations for the runtime cache-size detection driving blocking:

  • Eigen queryCacheSizes() (Eigen/src/Core/util/Memory.h) — runtime L1/L2/L3 via CPUID (x86) / sysctl-sysconf, ~0.5 ms once per process. Direct analog of this module. (ref)

  • glibc sysconf(_SC_LEVELx_CACHE_SIZE); x86 CPUID leaf 0x04/0x18; Linux sysfs cacheinfo (drivers/base/cacheinfo.c); ACPI PPTT (ARM cache topology).

  • hwloc (Portable Hardware Locality) — cross-platform topology incl. shared/last-level caches.

Note this PR does not make hwloc a dependency, but borrows the idea from it

The single-thread MMM block-budget probed L2 with detection logic inlined in
frame/mmm/mod.rs, reusable nowhere and limited to macOS/Linux L2. Move it into a
cache module that exposes L1d/L2/L3 through one memoised probe (macOS/iOS via
sysctlbyname, Linux/Android via /sys, Windows via wmic) and have the block
budget read it. The existing L2 budget is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@czoli1976

Copy link
Copy Markdown
Contributor Author

doing some cleanup: @kali : keep or close?

@kali

kali commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

I'm interested. It's an enabler for matmul sizing optims. So I say keep.

Don't read too much into my limited pace at looking and integrating. It's not lack of interest, it's limited bandwidth.

@kali
kali merged commit c16ba88 into sonos:main Jun 23, 2026
74 of 75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants