|
| 1 | +//==-------------- RootGroup.cpp - root group extension test ---------------==// |
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <gtest/gtest.h> |
| 10 | +#include <sycl/ext/oneapi/experimental/root_group.hpp> |
| 11 | + |
| 12 | +// Include helpers for device image, kernel info, and Unified Runtime (UR) mocks |
| 13 | +#include "helpers/MockDeviceImage.hpp" |
| 14 | +#include "helpers/MockKernelInfo.hpp" |
| 15 | +#include "helpers/UrMock.hpp" |
| 16 | + |
| 17 | +// Define a mock kernel class with several operator() overloads for different |
| 18 | +// SYCL item types |
| 19 | +class QueryKernel { |
| 20 | +public: |
| 21 | + void operator()() const {} |
| 22 | + void operator()(sycl::item<1>) const {} |
| 23 | + void operator()(sycl::nd_item<1> Item) const {} |
| 24 | +}; |
| 25 | + |
| 26 | +// Specialize KernelInfo for QueryKernel to provide mock metadata for the kernel |
| 27 | +namespace sycl { |
| 28 | +inline namespace _V1 { |
| 29 | +namespace detail { |
| 30 | +template <> |
| 31 | +struct KernelInfo<QueryKernel> : public unittest::MockKernelInfoBase { |
| 32 | + static constexpr const char *getName() { return "QueryKernel"; } |
| 33 | + static constexpr int64_t getKernelSize() { return sizeof(QueryKernel); } |
| 34 | + static constexpr const char *getFileName() { return "QueryKernel.hpp"; } |
| 35 | + static constexpr const char *getFunctionName() { |
| 36 | + return "QueryKernelFunctionName"; |
| 37 | + } |
| 38 | + static constexpr unsigned getLineNumber() { return 1; } |
| 39 | + static constexpr unsigned getColumnNumber() { return 1; } |
| 40 | +}; |
| 41 | +} // namespace detail |
| 42 | +} // namespace _V1 |
| 43 | +} // namespace sycl |
| 44 | + |
| 45 | +// Test that querying max_num_work_groups with an invalid (zero) work-group size |
| 46 | +// throws the correct exception |
| 47 | +TEST(RootGroupTests, InvalidWorkGroupSize) { |
| 48 | + namespace syclex = sycl::ext::oneapi::experimental; |
| 49 | + |
| 50 | + // Create a mock device image containing the QueryKernel |
| 51 | + sycl::unittest::MockDeviceImage Img = |
| 52 | + sycl::unittest::generateDefaultImage({"QueryKernel"}); |
| 53 | + const sycl::unittest::MockDeviceImageArray<1> ImgArray{&Img}; |
| 54 | + const sycl::unittest::UrMock<> Mock; |
| 55 | + |
| 56 | + const sycl::queue q; |
| 57 | + // Get the kernel bundle and kernel object for QueryKernel |
| 58 | + const auto bundle = |
| 59 | + sycl::get_kernel_bundle<sycl::bundle_state::executable>(q.get_context()); |
| 60 | + const auto kernel = bundle.get_kernel<QueryKernel>(); |
| 61 | + try { |
| 62 | + // Attempt to query max_num_work_groups with a zero work-group size |
| 63 | + kernel.ext_oneapi_get_info< |
| 64 | + syclex::info::kernel_queue_specific::max_num_work_groups>(q, {0}, 0); |
| 65 | + FAIL() << "The ext_oneapi_get_info query should have thrown."; |
| 66 | + } catch (const sycl::exception &e) { |
| 67 | + // Check that the correct error code and message are returned |
| 68 | + EXPECT_EQ(e.code(), sycl::make_error_code(sycl::errc::invalid)); |
| 69 | + EXPECT_STREQ(e.what(), "The launch work-group size cannot be zero."); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Test that querying max_num_work_groups with a valid work-group size returns |
| 74 | +// the expected value |
| 75 | +TEST(RootGroupTests, ValidNumWorkGroupsQuery) { |
| 76 | + namespace syclex = sycl::ext::oneapi::experimental; |
| 77 | + |
| 78 | + // Create a mock device image containing the QueryKernel |
| 79 | + sycl::unittest::MockDeviceImage Img = |
| 80 | + sycl::unittest::generateDefaultImage({"QueryKernel"}); |
| 81 | + const sycl::unittest::MockDeviceImageArray<1> ImgArray{&Img}; |
| 82 | + const sycl::unittest::UrMock<> Mock; |
| 83 | + |
| 84 | + // Set up a mock callback to return a specific group count when queried |
| 85 | + constexpr std::size_t mock_group_count = 42; |
| 86 | + mock::getCallbacks().set_replace_callback( |
| 87 | + "urKernelSuggestMaxCooperativeGroupCountExp", [](void *pParams) { |
| 88 | + auto params = static_cast< |
| 89 | + ur_kernel_suggest_max_cooperative_group_count_exp_params_t *>( |
| 90 | + pParams); |
| 91 | + **params->ppGroupCountRet = mock_group_count; |
| 92 | + return UR_RESULT_SUCCESS; |
| 93 | + }); |
| 94 | + |
| 95 | + const sycl::queue q; |
| 96 | + // Get the kernel bundle and kernel object for QueryKernel |
| 97 | + const auto bundle = |
| 98 | + sycl::get_kernel_bundle<sycl::bundle_state::executable>(q.get_context()); |
| 99 | + const auto kernel = bundle.get_kernel<QueryKernel>(); |
| 100 | + // Query max_num_work_groups with a valid work-group size (1) |
| 101 | + const auto maxWGs = kernel.ext_oneapi_get_info< |
| 102 | + syclex::info::kernel_queue_specific::max_num_work_groups>(q, {1}, 0); |
| 103 | + // Check that the returned value matches the mock group count |
| 104 | + EXPECT_EQ(maxWGs, mock_group_count); |
| 105 | +} |
0 commit comments