Skip to content

Commit 57d8254

Browse files
committed
Add kernel blocking test
1 parent d1b750e commit 57d8254

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

offload/unittests/OffloadAPI/queue/olEnqueueHostCallback.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
#include "../common/Fixtures.hpp"
1010
#include <OffloadAPI.h>
1111
#include <gtest/gtest.h>
12+
#include <thread>
1213

1314
struct olEnqueueHostCallbackTest : OffloadQueueTest {};
1415
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olEnqueueHostCallbackTest);
1516

17+
struct olEnqueueHostCallbackKernelTest : OffloadKernelTest {};
18+
OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olEnqueueHostCallbackKernelTest);
19+
1620
TEST_P(olEnqueueHostCallbackTest, Success) {
1721
ASSERT_SUCCESS(olEnqueueHostCallback(Queue, [](void *) {}, nullptr));
1822
}
@@ -37,6 +41,60 @@ TEST_P(olEnqueueHostCallbackTest, SuccessSequence) {
3741
}
3842
}
3943

44+
TEST_P(olEnqueueHostCallbackKernelTest, SuccessBlocking) {
45+
// Verify that a host kernel can block execution - A host task is created that
46+
// only resolves when Block is set to false.
47+
ol_kernel_launch_size_args_t LaunchArgs;
48+
LaunchArgs.Dimensions = 1;
49+
LaunchArgs.GroupSize = {64, 1, 1};
50+
LaunchArgs.NumGroups = {1, 1, 1};
51+
LaunchArgs.DynSharedMemory = 0;
52+
53+
ol_queue_handle_t Queue;
54+
ASSERT_SUCCESS(olCreateQueue(Device, &Queue));
55+
56+
void *Mem;
57+
ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED,
58+
LaunchArgs.GroupSize.x * sizeof(uint32_t), &Mem));
59+
60+
uint32_t *Data = (uint32_t *)Mem;
61+
for (uint32_t i = 0; i < 64; i++) {
62+
Data[i] = 0;
63+
}
64+
65+
volatile bool Block = true;
66+
ASSERT_SUCCESS(olEnqueueHostCallback(
67+
Queue,
68+
[](void *Ptr) {
69+
volatile bool *Block =
70+
reinterpret_cast<volatile bool *>(reinterpret_cast<bool *>(Ptr));
71+
72+
while (*Block)
73+
std::this_thread::yield();
74+
},
75+
const_cast<bool *>(&Block)));
76+
77+
struct {
78+
void *Mem;
79+
} Args{Mem};
80+
ASSERT_SUCCESS(
81+
olLaunchKernel(Queue, Device, Kernel, &Args, sizeof(Args), &LaunchArgs));
82+
83+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
84+
for (uint32_t i = 0; i < 64; i++) {
85+
ASSERT_EQ(Data[i], 0);
86+
}
87+
88+
Block = false;
89+
ASSERT_SUCCESS(olSyncQueue(Queue));
90+
91+
for (uint32_t i = 0; i < 64; i++) {
92+
ASSERT_EQ(Data[i], i);
93+
}
94+
95+
ASSERT_SUCCESS(olMemFree(Mem));
96+
}
97+
4098
TEST_P(olEnqueueHostCallbackTest, InvalidNullCallback) {
4199
ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,
42100
olEnqueueHostCallback(Queue, nullptr, nullptr));

0 commit comments

Comments
 (0)