-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[libc] Cache old slabs when allocating GPU memory #151866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
//===-- A lock-free data structure for a fixed capacity stack ---*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC___SUPPORT_GPU_FIXEDSTACK_H | ||
#define LLVM_LIBC_SRC___SUPPORT_GPU_FIXEDSTACK_H | ||
|
||
#include "src/__support/CPP/atomic.h" | ||
#include "src/__support/threads/sleep.h" | ||
|
||
#include <stdint.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
// A lock-free fixed size stack backed by an underlying array of data. It | ||
// supports push and pop operations in a completely lock-free manner. | ||
template <typename T, uint32_t CAPACITY> struct alignas(16) FixedStack { | ||
// The index is stored as a 20-bit value and cannot index into any more. | ||
static_assert(CAPACITY < 1024 * 1024, "Invalid buffer size"); | ||
|
||
// The head of the free and used stacks. Represents as a 20-bit index combined | ||
// with a 44-bit ABA tag that is updated in a single atomic operation. | ||
uint64_t free; | ||
uint64_t used; | ||
|
||
// The stack is a linked list of indices into the underlying data | ||
uint32_t next[CAPACITY]; | ||
T data[CAPACITY]; | ||
|
||
// Get the 20-bit index into the underlying array from the head. | ||
LIBC_INLINE static constexpr uint32_t get_node(uint64_t head) { | ||
return static_cast<uint32_t>(head & 0xfffff); | ||
} | ||
|
||
// Increment the old ABA tag and merge it into the new index. | ||
LIBC_INLINE static constexpr uint64_t make_head(uint64_t orig, | ||
uint32_t node) { | ||
return static_cast<uint64_t>(node) | (((orig >> 20ul) + 1ul) << 20ul); | ||
} | ||
|
||
// Attempts to pop data from the given stack by making it point to the next | ||
// node. We repeatedly attempt to write to the head using compare-and-swap, | ||
// expecting that it has not been changed by any other thread. | ||
LIBC_INLINE uint32_t pop_impl(cpp::AtomicRef<uint64_t> head) { | ||
uint64_t orig = head.load(cpp::MemoryOrder::RELAXED); | ||
|
||
for (;;) { | ||
if (get_node(orig) == CAPACITY) | ||
return CAPACITY; | ||
|
||
uint32_t node = | ||
cpp::AtomicRef(next[get_node(orig)]).load(cpp::MemoryOrder::RELAXED); | ||
if (head.compare_exchange_strong(orig, make_head(orig, node), | ||
cpp::MemoryOrder::ACQUIRE, | ||
cpp::MemoryOrder::RELAXED)) | ||
break; | ||
} | ||
return get_node(orig); | ||
} | ||
|
||
// Attempts to push data to the given stack by making it point to the new | ||
// node. We repeatedly attempt to write to the head using compare-and-swap, | ||
// expecting that it has not been changed by any other thread. | ||
LIBC_INLINE uint32_t push_impl(cpp::AtomicRef<uint64_t> head, uint32_t node) { | ||
uint64_t orig = head.load(cpp::MemoryOrder::RELAXED); | ||
for (;;) { | ||
next[node] = get_node(orig); | ||
if (head.compare_exchange_strong(orig, make_head(orig, node), | ||
cpp::MemoryOrder::RELEASE, | ||
cpp::MemoryOrder::RELAXED)) | ||
break; | ||
} | ||
return get_node(head.load(cpp::MemoryOrder::RELAXED)); | ||
} | ||
|
||
public: | ||
// Initialize the free stack to be full and the used stack to be empty. We use | ||
// the capacity of the stack as a sentinel value. | ||
LIBC_INLINE constexpr FixedStack() : free(0), used(CAPACITY), data{} { | ||
for (uint32_t i = 0; i < CAPACITY; ++i) | ||
next[i] = i + 1; | ||
} | ||
|
||
LIBC_INLINE bool push(const T &val) { | ||
uint32_t node = pop_impl(cpp::AtomicRef(free)); | ||
if (node == CAPACITY) | ||
return false; | ||
|
||
data[node] = val; | ||
push_impl(cpp::AtomicRef(used), node); | ||
return true; | ||
} | ||
|
||
LIBC_INLINE bool pop(T &val) { | ||
uint32_t node = pop_impl(cpp::AtomicRef(used)); | ||
if (node == CAPACITY) | ||
return false; | ||
|
||
val = data[node]; | ||
push_impl(cpp::AtomicRef(free), node); | ||
return true; | ||
} | ||
}; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_GPU_FIXEDSTACK_H |
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
44 changes: 44 additions & 0 deletions
44
libc/test/integration/src/__support/GPU/fixedstack_test.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//===-- Integration test for the lock-free stack --------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/GPU/fixedstack.h" | ||
#include "src/__support/GPU/utils.h" | ||
#include "test/IntegrationTest/test.h" | ||
|
||
using namespace LIBC_NAMESPACE; | ||
|
||
static FixedStack<uint32_t, 2048> global_stack; | ||
|
||
void run() { | ||
// We need enough space in the stack as threads in flight can temporarily | ||
// consume memory before they finish comitting it back to the stack. | ||
ASSERT_EQ(gpu::get_num_blocks() * gpu::get_num_threads(), 512); | ||
|
||
uint32_t val; | ||
uint32_t num_threads = static_cast<uint32_t>(gpu::get_num_threads()); | ||
for (int i = 0; i < 256; ++i) { | ||
EXPECT_TRUE(global_stack.push(UINT32_MAX)) | ||
EXPECT_TRUE(global_stack.pop(val)) | ||
ASSERT_TRUE(val < num_threads || val == UINT32_MAX); | ||
} | ||
|
||
EXPECT_TRUE(global_stack.push(static_cast<uint32_t>(gpu::get_thread_id()))); | ||
EXPECT_TRUE(global_stack.push(static_cast<uint32_t>(gpu::get_thread_id()))); | ||
EXPECT_TRUE(global_stack.pop(val)); | ||
ASSERT_TRUE(val < num_threads || val == UINT32_MAX); | ||
|
||
// Fill the rest of the stack with the default value. | ||
while (!global_stack.push(UINT32_MAX)) | ||
; | ||
} | ||
|
||
TEST_MAIN(int argc, char **argv, char **envp) { | ||
run(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we already have one of these? Don't want to copy/paste a header really, and if it's written from scratch for this commit the complexity of reviewing jumps a couple of orders of magnitude
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an old PR that never got merged #83026.