Fixing NUMA checks (set_mempolicy)#303
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to improve robustness of NUMA-related configuration by adding early validation that selected NUMA nodes can allocate memory under the current process policy, and it changes how CPU HSA agents are collected (moving from an allocation-based discovery to iterating HSA agents).
Changes:
- Add NUMA mempolicy/cpuset-based checks during
MemDevicevalidation to fail early when a CPU/closest-CPU NUMA node cannot allocate memory. - Rework CPU HSA agent discovery in
System::CollectTopology()to usehsa_iterate_agents()andHSA_AGENT_INFO_NODEinstead of allocating CPU memory and querying pointer ownership. - Minor refactor in
MEM_CPU_CLOSESTvalidation to store the closest NUMA index in a local variable.
Comments suppressed due to low confidence (3)
src/header/TransferBench.hpp:1880
- Same as the CPU path above: this condition is checking whether the closest NUMA node is permitted by the current process memory policy (numa_get_mems_allowed), not whether the CPU node is "equipped". Please adjust the error message to explicitly reference mempolicy/cpuset restrictions to avoid misleading users.
if (GetRank() == memDevice.memRank && !numa_bitmask_isbitset(numa_get_mems_allowed(), actualNumaIdx))
return {ERR_FATAL, "CPU %d on rank %d is not equipped to be able to allocate memory", actualNumaIdx, memDevice.memRank};
src/header/TransferBench.hpp:7784
- CollectTopology now calls hsa_shut_down() immediately after hsa_iterate_agents(), but the function continues to call HSA APIs afterward (hsa_amd_pointer_info below, and hsa_agent_get_info inside GetRankTopology which is called later in this same function). Shutting HSA down here is very likely to break those subsequent calls; please avoid calling hsa_shut_down in CollectTopology (or ensure HSA is initialized for the remainder of the program) and propagate/handle hsa_init/hsa_iterate_agents errors via the existing ErrResult/logging instead of printf.
hsa_init();
std::map<int, hsa_agent_t> cpuAgentMap;
hsa_status_t s = hsa_iterate_agents(cpuAgentCallback, &cpuAgentMap);
if (s != HSA_STATUS_SUCCESS) {
const char *errString = NULL;
hsa_status_string(s, &errString);
printf("FAIL %s\n",errString );
}
hsa_shut_down();
src/header/TransferBench.hpp:7793
- cpuAgents is resized and then only conditionally filled from cpuAgentMap; any missing entry leaves a default-initialized hsa_agent_t (handle==0). Later code assumes cpuAgents entries are valid (e.g., closest-CPU detection and DMA engine validation), so this can lead to incorrect topology or failures. Please either (a) build cpuAgents only for nodes you can actually map (and update the reported CPU executor count accordingly), or (b) detect missing mappings and return/record a fatal error instead of silently leaving invalid agents.
cpuAgents.clear();
int numCpus = numa_num_configured_nodes();
cpuAgents.resize(numCpus);
for (int i = 0; i < numCpus; i++) {
if (cpuAgentMap.count(i)) {
cpuAgents[i] = cpuAgentMap[i];
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
1 task
nileshnegi
approved these changes
May 15, 2026
nileshnegi
added a commit
that referenced
this pull request
May 19, 2026
Restore the Linux-NUMA-indexed cpuAgents[] table that the GPU-to-closest-CPU lookup in CollectTopology() depends on. After #303, CPU agents were enumerated via hsa_iterate_agents + HSA_AGENT_INFO_NODE and stored at the HSA/KFD topology node id, which is not guaranteed to equal the Linux NUMA index (e.g. on systems where GPU agents are enumerated before CPU agents in the KFD topology). When the two diverged, cpuAgents[i] stayed default-initialised, the closestCpuAgent lookup never matched, and GetClosestCpuNumaToGpu() returned -1 for every GPU. That broke the topology display (all GPUs reported with no closest NUMA) and produced malformed transfer descriptors like "R0C-1" in any preset that consumes this info. Switch back to the allocation-based enumeration that preserves cpuAgents[i] == "agent owning Linux NUMA i" while still tolerating NUMA nodes restricted by the process's cpuset / mempolicy: AllocateMemory(MEM_CPU) already rejects such nodes since #303, so we skip them on ERR_FATAL and leave the slot zero-initialised. Restricted NUMAs cannot service transfers anyway, so a zero-handle entry is the correct sentinel and harmless to the handle comparison performed by the closest-CPU lookup. Also drop the explicit hsa_init() / hsa_shut_down() around the iteration: HIP already manages HSA lifetime for this process and tearing it down between topology collection and the rest of the run is unnecessary. Verified on 8 x gfx950, 2 NUMA nodes: - ./TransferBench now prints the expected "NUMA 00 -> GPUs 0 1 2 3" / "NUMA 01 -> GPUs 4 5 6 7" mapping that pre-#303 (e8edacf) produced. Co-authored-by: Claude <claude@anthropic.com>
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Some NUMA nodes might not be able to allocate memory.
This PR adds early checks for this, and also changes how hsa_agents are collected