Skip to content

Commit b729d1b

Browse files
nileshnegiclaude
andauthored
Fix memory allocation bug to use the correct hipDevice (#308)
- Not using hipSetDevice before allocating memory can use unintended deviceIdx when executing fabric-handle based transfers - Reset numa_set_preferred(-1) before ERR_FATAL early return in the non-POD_COMM_ENABLED path; without this the NUMA policy stays dirty for subsequent CPU allocations in the same process - Use memDevice.memIndex directly in the top-level hipSetDevice call instead of deviceIdx, which is NUMA-remapped for CPU types only; documents that the MEM_CPU_CLOSEST remapping does not apply to GPU - Remove now-redundant hipSetDevice inside the POD_COMM GPU memHandle branch; device was already set at the top of AllocateMemory - Guard CollectTopology GPU agent probe loop with hipSetDevice(i) so each AllocateMemory call targets the correct device --------- Co-authored-by: Claude <claude@anthropic.com>
1 parent 9b22b00 commit b729d1b

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/header/TransferBench.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,15 @@ namespace {
14961496
deviceIdx = GetClosestCpuNumaToGpu(memDevice.memIndex);
14971497
}
14981498

1499+
if (IsCpuMemType(memType)) {
1500+
// Set NUMA policy prior to call to hipHostMalloc
1501+
numa_set_preferred(deviceIdx);
1502+
} else if (IsGpuMemType(memType)) {
1503+
// Switch to the appropriate GPU
1504+
// IMP: if the remapping above changes, remember to modify this!
1505+
ERR_CHECK(hipSetDevice(deviceIdx));
1506+
}
1507+
14991508
// If memHandle is provided, allocate sharable memory
15001509
if (memHandle != NULL) {
15011510
#ifdef POD_COMM_ENABLED
@@ -1533,13 +1542,14 @@ namespace {
15331542
memset(*memPtr, 0, roundedUpBytes);
15341543
// Check that the allocated pages are actually on the correct NUMA node
15351544
ERR_CHECK(CheckPages((char*)*memPtr, roundedUpBytes, deviceIdx));
1545+
numa_set_preferred(-1);
15361546
} else if (IsGpuMemType(memType)) {
1537-
ERR_CHECK(hipSetDevice(memDevice.memIndex));
15381547
ERR_CHECK(hipMemset(*memPtr, 0, numBytes));
15391548
ERR_CHECK(hipDeviceSynchronize());
15401549
}
15411550
return ERR_NONE;
15421551
#else
1552+
if (IsCpuMemType(memType)) numa_set_preferred(-1);
15431553
return {ERR_FATAL, "Unable to allocate sharable memory if not compiled with pod communication support"};
15441554
#endif
15451555
} else {
@@ -1548,9 +1558,6 @@ namespace {
15481558

15491559
if (IsCpuMemType(memType)) {
15501560

1551-
// Set NUMA policy prior to call to hipHostMalloc
1552-
numa_set_preferred(deviceIdx);
1553-
15541561
// Allocate host-pinned memory (should respect NUMA mem policy)
15551562
int flags = 0;
15561563
#if !defined (__NVCC__)
@@ -1591,8 +1598,6 @@ namespace {
15911598
// Reset to default numa mem policy
15921599
numa_set_preferred(-1);
15931600
} else if (IsGpuMemType(memType)) {
1594-
// Switch to the appropriate GPU
1595-
ERR_CHECK(hipSetDevice(memDevice.memIndex));
15961601

15971602
if (memType == MEM_GPU) {
15981603
// Allocate GPU memory on appropriate device

0 commit comments

Comments
 (0)