-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[Offload] olLaunchHostFunction
#152482
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
Open
RossBrunton
wants to merge
5
commits into
llvm:main
Choose a base branch
from
RossBrunton:hostcallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Offload] olLaunchHostFunction
#152482
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c90bf7
[Offload] `olEnqueueHostCallback`
RossBrunton a430d79
Add kernel blocking test
RossBrunton ae99dec
Rename to olLaunchHostFunction
RossBrunton 0059552
Delete queue
RossBrunton 8d6efb3
Clean docs and renamed enqueueHostCallback
RossBrunton 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
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
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
107 changes: 107 additions & 0 deletions
107
offload/unittests/OffloadAPI/queue/olLaunchHostFunction.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,107 @@ | ||
//===------- Offload API tests - olLaunchHostFunction ---------------------===// | ||
// | ||
// 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 "../common/Fixtures.hpp" | ||
#include <OffloadAPI.h> | ||
#include <gtest/gtest.h> | ||
#include <thread> | ||
|
||
struct olLaunchHostFunctionTest : OffloadQueueTest {}; | ||
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olLaunchHostFunctionTest); | ||
|
||
struct olLaunchHostFunctionKernelTest : OffloadKernelTest {}; | ||
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olLaunchHostFunctionKernelTest); | ||
|
||
TEST_P(olLaunchHostFunctionTest, Success) { | ||
ASSERT_SUCCESS(olLaunchHostFunction(Queue, [](void *) {}, nullptr)); | ||
} | ||
|
||
TEST_P(olLaunchHostFunctionTest, SuccessSequence) { | ||
uint32_t Buff[16] = {1, 1}; | ||
|
||
for (auto BuffPtr = &Buff[2]; BuffPtr != &Buff[16]; BuffPtr++) { | ||
ASSERT_SUCCESS(olLaunchHostFunction( | ||
Queue, | ||
[](void *BuffPtr) { | ||
uint32_t *AsU32 = reinterpret_cast<uint32_t *>(BuffPtr); | ||
AsU32[0] = AsU32[-1] + AsU32[-2]; | ||
}, | ||
BuffPtr)); | ||
} | ||
|
||
ASSERT_SUCCESS(olSyncQueue(Queue)); | ||
|
||
for (uint32_t i = 2; i < 16; i++) { | ||
ASSERT_EQ(Buff[i], Buff[i - 1] + Buff[i - 2]); | ||
} | ||
} | ||
|
||
TEST_P(olLaunchHostFunctionKernelTest, SuccessBlocking) { | ||
// Verify that a host kernel can block execution - A host task is created that | ||
// only resolves when Block is set to false. | ||
ol_kernel_launch_size_args_t LaunchArgs; | ||
LaunchArgs.Dimensions = 1; | ||
LaunchArgs.GroupSize = {64, 1, 1}; | ||
LaunchArgs.NumGroups = {1, 1, 1}; | ||
LaunchArgs.DynSharedMemory = 0; | ||
|
||
ol_queue_handle_t Queue; | ||
ASSERT_SUCCESS(olCreateQueue(Device, &Queue)); | ||
|
||
void *Mem; | ||
ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, | ||
LaunchArgs.GroupSize.x * sizeof(uint32_t), &Mem)); | ||
|
||
uint32_t *Data = (uint32_t *)Mem; | ||
for (uint32_t i = 0; i < 64; i++) { | ||
Data[i] = 0; | ||
} | ||
|
||
volatile bool Block = true; | ||
ASSERT_SUCCESS(olLaunchHostFunction( | ||
Queue, | ||
[](void *Ptr) { | ||
volatile bool *Block = | ||
reinterpret_cast<volatile bool *>(reinterpret_cast<bool *>(Ptr)); | ||
|
||
while (*Block) | ||
std::this_thread::yield(); | ||
}, | ||
const_cast<bool *>(&Block))); | ||
|
||
struct { | ||
void *Mem; | ||
} Args{Mem}; | ||
ASSERT_SUCCESS( | ||
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs)); | ||
|
||
std::this_thread::sleep_for(std::chrono::milliseconds(500)); | ||
for (uint32_t i = 0; i < 64; i++) { | ||
ASSERT_EQ(Data[i], 0); | ||
} | ||
|
||
Block = false; | ||
ASSERT_SUCCESS(olSyncQueue(Queue)); | ||
|
||
for (uint32_t i = 0; i < 64; i++) { | ||
ASSERT_EQ(Data[i], i); | ||
} | ||
|
||
ASSERT_SUCCESS(olDestroyQueue(Queue)); | ||
ASSERT_SUCCESS(olMemFree(Mem)); | ||
} | ||
|
||
TEST_P(olLaunchHostFunctionTest, InvalidNullCallback) { | ||
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER, | ||
olLaunchHostFunction(Queue, nullptr, nullptr)); | ||
} | ||
|
||
TEST_P(olLaunchHostFunctionTest, InvalidNullQueue) { | ||
ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE, | ||
olLaunchHostFunction(nullptr, [](void *) {}, nullptr)); | ||
} |
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.
Is it worth making a pool for these?
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 thought we already had callback handling in the HSA signal handler. It's called
schedCallback
.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.
schedCallback enqueues tasks to be completed synchronously once the queue has finished all of its tasks (e.g. as part of
synchronise
). They don't participate in the input/output signal dependency resolution, which is what we need forolLaunchFunction
.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.
... Although, why don't we replace schedCallback with olLaunchHostFunction? And maybe only use one thread per queue rather than per host function.
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 would introduce
std::thread
overhead, right?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.
Yes, unless we had one thread per queue that could be sent work through a pipe?
But I think that's probably future work, are you okay with thread overhead just for olLaunchHostFunction?
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'd leave the
schedCallback
as they are, being executed by the thread that synchronizes/queries the stream, so no overhead. Making another thread execute these callbacks adds unnecessary inter-thread synchronization overhead in my opinion.Efficiently implementing a per-stream thread that handles the host callbacks of that stream can be tricky: (1) the thread will need to sleep as much time as possible to avoid disturbing the other threads; (2) the synchronization mechanism between this thread and the ones enqueuing callbacks; (3) possible on-demand thread creation to avoid creating a thread for each stream when not needed.