108 lines · cpp
1//===------- Offload API tests - olLaunchHostFunction ---------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "../common/Fixtures.hpp"10#include <OffloadAPI.h>11#include <gtest/gtest.h>12#include <thread>13 14struct olLaunchHostFunctionTest : OffloadQueueTest {};15OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olLaunchHostFunctionTest);16 17struct olLaunchHostFunctionKernelTest : OffloadKernelTest {};18OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olLaunchHostFunctionKernelTest);19 20TEST_P(olLaunchHostFunctionTest, Success) {21 ASSERT_SUCCESS(olLaunchHostFunction(Queue, [](void *) {}, nullptr));22}23 24TEST_P(olLaunchHostFunctionTest, SuccessSequence) {25 uint32_t Buff[16] = {1, 1};26 27 for (auto BuffPtr = &Buff[2]; BuffPtr != &Buff[16]; BuffPtr++) {28 ASSERT_SUCCESS(olLaunchHostFunction(29 Queue,30 [](void *BuffPtr) {31 uint32_t *AsU32 = reinterpret_cast<uint32_t *>(BuffPtr);32 AsU32[0] = AsU32[-1] + AsU32[-2];33 },34 BuffPtr));35 }36 37 ASSERT_SUCCESS(olSyncQueue(Queue));38 39 for (uint32_t i = 2; i < 16; i++) {40 ASSERT_EQ(Buff[i], Buff[i - 1] + Buff[i - 2]);41 }42}43 44TEST_P(olLaunchHostFunctionKernelTest, SuccessBlocking) {45 // Verify that a host kernel can block execution - A host task is created that46 // 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(olLaunchHostFunction(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(olDestroyQueue(Queue));96 ASSERT_SUCCESS(olMemFree(Mem));97}98 99TEST_P(olLaunchHostFunctionTest, InvalidNullCallback) {100 ASSERT_ERROR(OL_ERRC_INVALID_NULL_POINTER,101 olLaunchHostFunction(Queue, nullptr, nullptr));102}103 104TEST_P(olLaunchHostFunctionTest, InvalidNullQueue) {105 ASSERT_ERROR(OL_ERRC_INVALID_NULL_HANDLE,106 olLaunchHostFunction(nullptr, [](void *) {}, nullptr));107}108