Skip to content

Commit b514a43

Browse files
authored
[SYCL] Implement sycl_khr_work_item_queries extension (#18519)
Implements the extension defined in KhronosGroup/SYCL-Docs#682. --------- Signed-off-by: Michael Aziz <[email protected]>
1 parent 8ea381f commit b514a43

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- work_item_queries.hpp --- KHR work item queries extension ---------===//
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+
#pragma once
9+
10+
#ifdef __DPCPP_ENABLE_UNFINISHED_KHR_EXTENSIONS
11+
12+
#include <sycl/ext/oneapi/free_function_queries.hpp>
13+
14+
namespace sycl {
15+
inline namespace _V1 {
16+
namespace khr {
17+
18+
template <int Dimensions> nd_item<Dimensions> this_nd_item() {
19+
return ext::oneapi::experimental::this_nd_item<Dimensions>();
20+
}
21+
22+
template <int Dimensions> group<Dimensions> this_group() {
23+
return ext::oneapi::this_work_item::get_work_group<Dimensions>();
24+
}
25+
26+
inline sub_group this_sub_group() {
27+
return ext::oneapi::this_work_item::get_sub_group();
28+
}
29+
30+
} // namespace khr
31+
} // namespace _V1
32+
} // namespace sycl
33+
34+
#endif

sycl/include/sycl/sycl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@
124124
#include <sycl/ext/oneapi/weak_object.hpp>
125125
#include <sycl/khr/free_function_commands.hpp>
126126
#include <sycl/khr/group_interface.hpp>
127+
#include <sycl/khr/work_item_queries.hpp>

sycl/source/feature_test.hpp.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ inline namespace _V1 {
123123
#define SYCL_KHR_DEFAULT_CONTEXT 1
124124
#define SYCL_EXT_INTEL_EVENT_MODE 1
125125

126+
// Unfinished KHR extensions. These extensions are only available if the
127+
// __DPCPP_ENABLE_UNFINISHED_KHR_EXTENSIONS macro is defined.
128+
#ifdef __DPCPP_ENABLE_UNFINISHED_KHR_EXTENSIONS
129+
#define SYCL_KHR_WORK_ITEM_QUERIES 1
130+
#endif
131+
126132
#ifndef __has_include
127133
#define __has_include(x) 0
128134
#endif
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
//===- work_item_queries.cpp - KHR work item queries test -----------------===//
5+
//
6+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7+
// See https://llvm.org/LICENSE.txt for license information.
8+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#define __DPCPP_ENABLE_UNFINISHED_KHR_EXTENSIONS
13+
14+
#include <cassert>
15+
#include <iostream>
16+
#include <sycl/detail/core.hpp>
17+
#include <sycl/khr/work_item_queries.hpp>
18+
19+
template <size_t... Dims> static int check_this_nd_item_api() {
20+
// Define the kernel ranges.
21+
constexpr int Dimensions = sizeof...(Dims);
22+
const sycl::range<Dimensions> local_range{Dims...};
23+
const sycl::range<Dimensions> global_range = local_range;
24+
const sycl::nd_range<Dimensions> nd_range{global_range, local_range};
25+
// Launch an ND-range kernel.
26+
sycl::queue q;
27+
sycl::buffer<bool, Dimensions> results{global_range};
28+
q.submit([&](sycl::handler &cgh) {
29+
sycl::accessor acc{results, cgh, sycl::write_only};
30+
cgh.parallel_for(nd_range, [=](sycl::nd_item<Dimensions> it) {
31+
// Compare it to this_nd_item<Dimensions>().
32+
acc[it.get_global_id()] = (it == sycl::khr::this_nd_item<Dimensions>());
33+
});
34+
});
35+
// Check the test results.
36+
sycl::host_accessor acc{results};
37+
for (const auto &result : acc) {
38+
if (!result) {
39+
std::cerr << "check_this_nd_item_api failed for dimensionality "
40+
<< Dimensions << ".\n";
41+
return 1;
42+
}
43+
}
44+
return 0;
45+
}
46+
47+
template <size_t... Dims> static int check_this_group_api() {
48+
// Define the kernel ranges.
49+
constexpr int Dimensions = sizeof...(Dims);
50+
const sycl::range<Dimensions> local_range{Dims...};
51+
const sycl::range<Dimensions> global_range = local_range;
52+
const sycl::nd_range<Dimensions> nd_range{global_range, local_range};
53+
// Launch an ND-range kernel.
54+
sycl::queue q;
55+
sycl::buffer<bool, Dimensions> results{global_range};
56+
q.submit([&](sycl::handler &cgh) {
57+
sycl::accessor acc{results, cgh, sycl::write_only};
58+
cgh.parallel_for(nd_range, [=](sycl::nd_item<Dimensions> it) {
59+
// Compare it.get_group() to this_group<Dimensions>().
60+
acc[it.get_global_id()] =
61+
(it.get_group() == sycl::khr::this_group<Dimensions>());
62+
});
63+
});
64+
// Check the test results.
65+
sycl::host_accessor acc{results};
66+
for (const auto &result : acc) {
67+
if (!result) {
68+
std::cerr << "check_this_group_api failed for dimensionality "
69+
<< Dimensions << ".\n";
70+
return 1;
71+
}
72+
}
73+
return 0;
74+
}
75+
76+
template <size_t... Dims> static int check_this_sub_group_api() {
77+
// Define the kernel ranges.
78+
constexpr int Dimensions = sizeof...(Dims);
79+
const sycl::range<Dimensions> local_range{Dims...};
80+
const sycl::range<Dimensions> global_range = local_range;
81+
const sycl::nd_range<Dimensions> nd_range{global_range, local_range};
82+
// Launch an ND-range kernel.
83+
sycl::queue q;
84+
sycl::buffer<bool, Dimensions> results{global_range};
85+
q.submit([&](sycl::handler &cgh) {
86+
sycl::accessor acc{results, cgh, sycl::write_only};
87+
cgh.parallel_for(nd_range, [=](sycl::nd_item<Dimensions> it) {
88+
// Compare it.get_sub_group() to this_sub_group().
89+
acc[it.get_global_id()] =
90+
(it.get_sub_group() == sycl::khr::this_sub_group());
91+
});
92+
});
93+
// Check the test results.
94+
sycl::host_accessor acc{results};
95+
for (const auto &result : acc) {
96+
if (!result) {
97+
std::cerr << "check_this_sub_group_api failed for dimensionality "
98+
<< Dimensions << ".\n";
99+
return 1;
100+
}
101+
}
102+
return 0;
103+
}
104+
105+
int main() {
106+
int failed = 0;
107+
// nd_item
108+
failed += check_this_nd_item_api<2>();
109+
failed += check_this_nd_item_api<2, 3>();
110+
failed += check_this_nd_item_api<2, 3, 4>();
111+
// group
112+
failed += check_this_group_api<2>();
113+
failed += check_this_group_api<2, 3>();
114+
failed += check_this_group_api<2, 3, 4>();
115+
// sub_group
116+
failed += check_this_sub_group_api<2>();
117+
failed += check_this_sub_group_api<2, 3>();
118+
failed += check_this_sub_group_api<2, 3, 4>();
119+
return failed;
120+
}

0 commit comments

Comments
 (0)