-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[OpenMP] Basic BumpAllocator for (AMD)GPUs #69806
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,44 @@ | ||
//===-------- Allocator.h - OpenMP memory allocator interface ---- 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 OMPTARGET_ALLOCATOR_H | ||
#define OMPTARGET_ALLOCATOR_H | ||
|
||
#include "Types.h" | ||
|
||
// Forward declaration. | ||
struct KernelEnvironmentTy; | ||
|
||
#pragma omp begin declare target device_type(nohost) | ||
|
||
namespace ompx { | ||
|
||
namespace allocator { | ||
|
||
static uint64_t constexpr ALIGNMENT = 16; | ||
|
||
/// Initialize the allocator according to \p KernelEnvironment | ||
void init(bool IsSPMD, KernelEnvironmentTy &KernelEnvironment); | ||
|
||
/// Allocate \p Size bytes. | ||
[[gnu::alloc_size(1), gnu::assume_aligned(ALIGNMENT), gnu::malloc]] void * | ||
alloc(uint64_t Size); | ||
|
||
/// Free the allocation pointed to by \p Ptr. | ||
void free(void *Ptr); | ||
|
||
} // namespace allocator | ||
|
||
} // namespace ompx | ||
|
||
#pragma omp end declare target | ||
|
||
#endif |
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,80 @@ | ||
//===------ State.cpp - OpenMP State & ICV interface ------------- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Allocator.h" | ||
#include "Configuration.h" | ||
#include "Environment.h" | ||
#include "Mapping.h" | ||
#include "Synchronization.h" | ||
#include "Types.h" | ||
#include "Utils.h" | ||
|
||
using namespace ompx; | ||
|
||
#pragma omp begin declare target device_type(nohost) | ||
|
||
[[gnu::used, gnu::retain, gnu::weak, | ||
gnu::visibility( | ||
"protected")]] DeviceMemoryPoolTy __omp_rtl_device_memory_pool; | ||
[[gnu::used, gnu::retain, gnu::weak, | ||
gnu::visibility("protected")]] DeviceMemoryPoolTrackingTy | ||
__omp_rtl_device_memory_pool_tracker; | ||
|
||
/// Stateless bump allocator that uses the __omp_rtl_device_memory_pool | ||
/// directly. | ||
struct BumpAllocatorTy final { | ||
|
||
void *alloc(uint64_t Size) { | ||
Size = utils::roundUp(Size, uint64_t(allocator::ALIGNMENT)); | ||
|
||
if (config::isDebugMode(DeviceDebugKind::AllocationTracker)) { | ||
atomic::add(&__omp_rtl_device_memory_pool_tracker.NumAllocations, 1, | ||
atomic::seq_cst); | ||
atomic::add(&__omp_rtl_device_memory_pool_tracker.AllocationTotal, Size, | ||
atomic::seq_cst); | ||
atomic::min(&__omp_rtl_device_memory_pool_tracker.AllocationMin, Size, | ||
atomic::seq_cst); | ||
atomic::max(&__omp_rtl_device_memory_pool_tracker.AllocationMax, Size, | ||
atomic::seq_cst); | ||
} | ||
|
||
uint64_t *Data = | ||
reinterpret_cast<uint64_t *>(&__omp_rtl_device_memory_pool.Ptr); | ||
uint64_t End = | ||
reinterpret_cast<uint64_t>(Data) + __omp_rtl_device_memory_pool.Size; | ||
|
||
uint64_t OldData = atomic::add(Data, Size, atomic::seq_cst); | ||
if (OldData + Size > End) | ||
__builtin_trap(); | ||
|
||
return reinterpret_cast<void *>(OldData); | ||
} | ||
|
||
void free(void *) {} | ||
}; | ||
|
||
BumpAllocatorTy BumpAllocator; | ||
|
||
/// allocator namespace implementation | ||
/// | ||
///{ | ||
|
||
void allocator::init(bool IsSPMD, KernelEnvironmentTy &KernelEnvironment) { | ||
// TODO: Check KernelEnvironment for an allocator choice as soon as we have | ||
// more than one. | ||
} | ||
|
||
void *allocator::alloc(uint64_t Size) { return BumpAllocator.alloc(Size); } | ||
|
||
void allocator::free(void *Ptr) { BumpAllocator.free(Ptr); } | ||
|
||
///} | ||
|
||
#pragma omp end declare target |
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
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
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.
Why not just return
nullptr
? Isn't that the expect failure mode for amalloc
call?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.
This allocator is not a production allocator. Right now, we run into too many problems. And honestly, nobody checks for nullptr.
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.
True, but I figured the inevitable dereference of the nullptr would be a good trap that's usually more easily connected with
malloc
.Uh oh!
There was an error while loading. Please reload this page.
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.
Based on my experience it's not always easy to expose a dereference
nullptr
on a GPU, or to put another way, sometimes even dereference a nullptr will not trigger a trap. Given what we have right now in this allocator, it is better to just trap if we don't have enough memory.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.
I guess I'm just averse to traps because they currently deadlock on my machine. I still haven't tracked down the cause of that however.
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.
That is arguably not true, especially for "implicit"/"hidden" mallocs. People that run on AMD complain often about tests not running, or only running with O1/2/3 but don't connect it to the malloc. The problem with
*nullptr
is that it's UB. So we don't necessarily do it at all. I have seen many tests "pass" even though the smart stack was full but the fallback to malloc was removed and somehow writing some shared memory worked out. But that also leads to spurious fails. Let's crash loud and fast, not via silent corruption.