Skip to content

Commit 26fb1aa

Browse files
committed
[libc] Perform bitfield zero initialization wave-parallel
Summary: We need to set the bitfield memory to zero because the system does not guarantee zeroed out memory. Even if fresh pages are zero, the system allows re-use so we would need a `kfd` level API to skip this step. Because we can't this patch updates the logic to perform the zero initialization wave-parallel. This reduces the amount of time it takes to allocate a fresh by up to a tenth. This has the unfortunate side effect that the control flow is more convoluted and we waste some extra registers, but it's worth it to reduce the slab allocation latency.
1 parent f1575de commit 26fb1aa

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

libc/src/__support/GPU/allocator.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,19 @@ struct Slab {
157157
Header *header = reinterpret_cast<Header *>(memory);
158158
header->chunk_size = chunk_size;
159159
header->global_index = global_index;
160+
}
160161

161-
// This memset is expensive and likely not necessary for the current 'kfd'
162-
// driver. Until zeroed pages are exposed by the API we must be careful.
163-
__builtin_memset(get_bitfield(), 0, bitfield_bytes(chunk_size));
162+
// Set the necessary bitfield bytes to zero in parallel using many lanes. This
163+
// must be called before the bitfield can be accessed safely, memory is not
164+
// guaranteed to be zero initialized in the current implementation.
165+
void initialize(uint64_t uniform) {
166+
uint64_t mask = gpu::get_lane_mask();
167+
uint32_t *bitfield = get_bitfield();
168+
uint32_t workers = cpp::popcount(uniform);
169+
uint32_t words = (bitfield_bytes(get_chunk_size()) + sizeof(uint32_t) - 1) /
170+
sizeof(uint32_t);
171+
for (uint32_t i = impl::lane_count(mask & uniform); i < words; i += workers)
172+
bitfield[i] = 0;
164173
}
165174

166175
// Get the number of chunks that can theoretically fit inside this slab.
@@ -354,14 +363,7 @@ struct GuardPtr {
354363
void *raw = impl::rpc_allocate(sizeof(Slab));
355364
if (!raw)
356365
return nullptr;
357-
Slab *mem = new (raw) Slab(cpp::forward<Args>(args)...);
358-
359-
cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
360-
ptr.store(mem, cpp::MemoryOrder::RELAXED);
361-
cpp::atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
362-
if (!ref.acquire(n, count))
363-
ref.reset(n, count);
364-
return mem;
366+
return new (raw) Slab(cpp::forward<Args>(args)...);
365367
}
366368

367369
if (!expected || expected == reinterpret_cast<Slab *>(SENTINEL))
@@ -374,6 +376,16 @@ struct GuardPtr {
374376
return ptr.load(cpp::MemoryOrder::RELAXED);
375377
}
376378

379+
// Finalize the associated memory and signal that it is ready to use by
380+
// resetting the counter.
381+
void finalize(Slab *mem, uint32_t n, uint64_t &count) {
382+
cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
383+
ptr.store(mem, cpp::MemoryOrder::RELAXED);
384+
cpp::atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
385+
if (!ref.acquire(n, count))
386+
ref.reset(n, count);
387+
}
388+
377389
public:
378390
// Attempt to lock access to the pointer, potentially creating it if empty.
379391
// The uniform mask represents which lanes share the same pointer. For each
@@ -392,6 +404,14 @@ struct GuardPtr {
392404
if (!result)
393405
return nullptr;
394406

407+
// We defer storing the newly allocated slab until now so that we can use
408+
// multiple lanes to initialize it and release it for use.
409+
if (count == cpp::numeric_limits<uint64_t>::max()) {
410+
result->initialize(uniform);
411+
if (gpu::get_lane_id() == uint32_t(cpp::countr_zero(uniform)))
412+
finalize(result, cpp::popcount(uniform), count);
413+
}
414+
395415
if (count != cpp::numeric_limits<uint64_t>::max())
396416
count = count - cpp::popcount(uniform) + impl::lane_count(uniform) + 1;
397417

0 commit comments

Comments
 (0)