aboutsummaryrefslogtreecommitdiff
path: root/offload/unittests/OffloadAPI/queue/olLaunchHostFunction.cpp
blob: aa86750f6adf9b109b694f7d97280b4e1175b3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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));
}