Skip to content

Commit fa09fb1

Browse files
committed
Use atomics to avoid a race condition
1 parent 72e4e39 commit fa09fb1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

csrc/ops.cu

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212

1313
#if BNB_HIP
1414
#include <hip/hip_runtime.h>
15+
#include <atomic>
1516

1617
// NOTE: This queries device 0 once and caches the result. On mixed RDNA+CDNA
1718
// systems (warp size 32 vs 64) this will return the wrong value for whichever
1819
// device doesn't match device 0.
1920
static int bnb_host_warp_size() {
20-
static int warp_size = 0;
21-
if (warp_size == 0)
22-
(void)hipDeviceGetAttribute(&warp_size, hipDeviceAttributeWarpSize, 0);
23-
return warp_size;
21+
static std::atomic<int> warp_size{0};
22+
int ws = warp_size.load(std::memory_order_relaxed);
23+
if (ws == 0) {
24+
(void)hipDeviceGetAttribute(&ws, hipDeviceAttributeWarpSize, 0);
25+
warp_size.store(ws, std::memory_order_relaxed);
26+
}
27+
return ws;
2428
}
2529
#else
2630
static constexpr int bnb_host_warp_size() { return 32; }

0 commit comments

Comments
 (0)