Skip to content

feat(shmem): bind ranks to GPU-local CPUs by default#401

Open
jhchouuu wants to merge 1 commit into
mainfrom
jiahzhou/numa-cpu-affinity
Open

feat(shmem): bind ranks to GPU-local CPUs by default#401
jhchouuu wants to merge 1 commit into
mainfrom
jiahzhou/numa-cpu-affinity

Conversation

@jhchouuu

Copy link
Copy Markdown
Collaborator

What

The shmem/EP path never pinned its rank thread. Kernel-launch, CQ-polling and staging-copy work — plus any later-spawned threads (e.g. IO workers, which inherit the parent's affinity) — could float onto a CPU on a NUMA node remote from the rank's GPU and NIC, adding cross-socket hops on the host side of every transfer.

This pins each rank to the CPUs local to its GPU, on by default, following NCCL.

How

At the top of ShmemInit, read the GPU's local CPU set (PCI BDF → sysfs local_cpulist), intersect it with the current cpuset, and pin the calling thread to the result:

  • Intersect, don't overwrite — an outer numactl / torchrun binding or a cgroup CPU limit is respected; we only narrow within what's already allowed.
  • Skip if the intersection is empty — never strand a rank with zero CPUs.
  • Opt-out: MORI_IGNORE_CPU_AFFINITY=1 leaves placement entirely to the caller.
  • Unlike NCCL (a sub-library that restores the caller's affinity after init), mori keeps the binding for the rank's lifetime, since a rank is dedicated to one GPU.

Single bind site

All init entries (ShmemMpiInit, ShmemInit, ShmemTorchProcessGroupInit, the socket-bootstrap path) funnel through ShmemInit(BootstrapNetwork*), and it runs before any buffer allocation or worker-thread spawn — so buffers land on the local node and children inherit the affinity.

Per the existing SPMT contract, the caller has already hipSetDevice()'d its GPU before ShmemInit, so the calling thread's current device is the rank's GPU in both layouts — one-rank-per-GPU and SPMT (single process, one init thread per GPU). No build-time special-casing is needed: SPMT and non-SPMT use the same code path, each thread pinning to its own GPU's NUMA node.

Scope

shmem/EP only. IO / UMBP go through RdmaContext directly (not ShmemInit) and have their own affinity handling, so they are unaffected.

Testing

  • Builds clean in both normal and MORI_MULTITHREAD_SUPPORT=ON (SPMT) configs.
  • Default: each rank logs thread bound to N CPUs local to GPU X (numa node Y), pinned to the correct NUMA node; new threads inherit it.
  • MORI_IGNORE_CPU_AFFINITY=1: no binding applied.
  • With an outer --bind-to core the intersection correctly collapses to the caller-provided CPU (existing binding respected).

Env vars

Var Default Effect
MORI_IGNORE_CPU_AFFINITY unset (binding on) Set to 1 to disable GPU-local CPU pinning.

The shmem/EP path never pinned its rank thread, so kernel-launch / CQ-polling /
staging-copy work (and later-spawned threads such as IO workers) could float onto
a CPU on a NUMA node remote from the rank's GPU and NIC.

Following NCCL: read the GPU's local CPU set (PCI BDF -> sysfs local_cpulist),
intersect it with the current cpuset (so cgroup limits and any outer numactl/
torchrun binding are respected), and pin the calling thread to it; skip if the
intersection is empty. Enabled by default; set MORI_IGNORE_CPU_AFFINITY=1 to opt
out. New threads inherit the affinity. Unlike NCCL (a sub-library that restores
the caller's affinity after init), mori keeps it for the rank lifetime since a
rank is dedicated to one GPU.

Bind at a single site: the top of ShmemInit, which every init entry funnels
through and which runs before any buffer allocation or worker-thread spawn (so
buffers land on the local node and children inherit the affinity). Per the SPMT
contract the caller has already hipSetDevice()'d its GPU before ShmemInit, so the
calling thread's current device is the rank's GPU in both one-rank-per-GPU and
SPMT (one process, one such thread per GPU) layouts -- no build-time special
casing needed. The helper (application/utils/cpu_affinity.hpp) is
thread-local-once and reads the current device, so re-init is a no-op.
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.

1 participant