Skip to content

Commit 19dcac7

Browse files
author
Alexander Batashev
authored
[SYCL] Implement SYCL2020 sub_group class (#3765)
1 parent 5a7acb2 commit 19dcac7

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

sycl/include/CL/sycl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <CL/sycl/sampler.hpp>
5353
#include <CL/sycl/specialization_id.hpp>
5454
#include <CL/sycl/stream.hpp>
55+
#include <CL/sycl/sub_group.hpp>
5556
#include <CL/sycl/types.hpp>
5657
#include <CL/sycl/usm.hpp>
5758
#include <CL/sycl/version.hpp>

sycl/include/CL/sycl/ONEAPI/sub_group.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <CL/sycl/detail/spirv.hpp>
1919
#include <CL/sycl/detail/type_traits.hpp>
2020
#include <CL/sycl/id.hpp>
21+
#include <CL/sycl/memory_enums.hpp>
2122
#include <CL/sycl/range.hpp>
2223
#include <CL/sycl/types.hpp>
2324

@@ -104,6 +105,8 @@ struct sub_group {
104105
using range_type = range<1>;
105106
using linear_id_type = uint32_t;
106107
static constexpr int dimensions = 1;
108+
static constexpr sycl::memory_scope fence_scope =
109+
sycl::memory_scope::sub_group;
107110

108111
/* --- common interface members --- */
109112

@@ -699,6 +702,33 @@ struct sub_group {
699702
#endif
700703
}
701704

705+
linear_id_type get_group_linear_range() const {
706+
#ifdef __SYCL_DEVICE_ONLY__
707+
return static_cast<linear_id_type>(get_group_range()[0]);
708+
#else
709+
throw runtime_error("Sub-groups are not supported on host device.",
710+
PI_INVALID_DEVICE);
711+
#endif
712+
}
713+
714+
linear_id_type get_local_linear_range() const {
715+
#ifdef __SYCL_DEVICE_ONLY__
716+
return static_cast<linear_id_type>(get_local_range()[0]);
717+
#else
718+
throw runtime_error("Sub-groups are not supported on host device.",
719+
PI_INVALID_DEVICE);
720+
#endif
721+
}
722+
723+
bool leader() const {
724+
#ifdef __SYCL_DEVICE_ONLY__
725+
return get_local_linear_id() == 0;
726+
#else
727+
throw runtime_error("Sub-groups are not supported on host device.",
728+
PI_INVALID_DEVICE);
729+
#endif
730+
}
731+
702732
protected:
703733
template <int dimensions> friend class cl::sycl::nd_item;
704734
friend sub_group this_sub_group();

sycl/include/CL/sycl/group.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <CL/sycl/device_event.hpp>
1818
#include <CL/sycl/h_item.hpp>
1919
#include <CL/sycl/id.hpp>
20+
#include <CL/sycl/memory_enums.hpp>
2021
#include <CL/sycl/pointers.hpp>
2122
#include <CL/sycl/range.hpp>
2223
#include <stdexcept>
@@ -431,5 +432,8 @@ template <int Dims> group<Dims> this_group() {
431432
#endif
432433
}
433434

435+
template <typename Group>
436+
void group_barrier(Group G, memory_scope FenceScope = Group::fence_scope);
437+
434438
} // namespace sycl
435439
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/include/CL/sycl/memory_enums.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//==-------------- memory_enums.hpp --- SYCL enums -------------------------==//
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+
#pragma once
10+
11+
#include <CL/sycl/ONEAPI/atomic_enums.hpp>
12+
13+
__SYCL_INLINE_NAMESPACE(cl) {
14+
namespace sycl {
15+
using ONEAPI::memory_scope;
16+
17+
#if __cplusplus >= 201703L
18+
inline constexpr auto memory_scope_work_item = memory_scope::work_item;
19+
inline constexpr auto memory_scope_sub_group = memory_scope::sub_group;
20+
inline constexpr auto memory_scope_work_group = memory_scope::work_group;
21+
inline constexpr auto memory_scope_device = memory_scope::device;
22+
inline constexpr auto memory_scope_system = memory_scope::system;
23+
#endif
24+
} // namespace sycl
25+
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/include/CL/sycl/nd_item.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#pragma once
1010

1111
#include <CL/__spirv/spirv_ops.hpp>
12-
#include <CL/sycl/ONEAPI/sub_group.hpp>
1312
#include <CL/sycl/access/access.hpp>
1413
#include <CL/sycl/detail/defines.hpp>
1514
#include <CL/sycl/detail/helpers.hpp>
@@ -18,6 +17,7 @@
1817
#include <CL/sycl/item.hpp>
1918
#include <CL/sycl/nd_range.hpp>
2019
#include <CL/sycl/range.hpp>
20+
#include <CL/sycl/sub_group.hpp>
2121

2222
#include <cstddef>
2323
#include <stdexcept>
@@ -67,7 +67,7 @@ template <int dimensions = 1> class nd_item {
6767

6868
group<dimensions> get_group() const { return Group; }
6969

70-
ONEAPI::sub_group get_sub_group() const { return ONEAPI::sub_group(); }
70+
sub_group get_sub_group() const { return sub_group(); }
7171

7272
size_t __SYCL_ALWAYS_INLINE get_group(int dimension) const {
7373
size_t Size = Group[dimension];

sycl/include/CL/sycl/sub_group.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//==----------- sub_group.hpp --- SYCL sub-group ---------------------------==//
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+
#pragma once
10+
11+
#include <CL/sycl/ONEAPI/sub_group.hpp>
12+
#include <CL/sycl/group.hpp>
13+
14+
__SYCL_INLINE_NAMESPACE(cl) {
15+
namespace sycl {
16+
using ONEAPI::sub_group;
17+
// TODO move the entire sub_group class implementation to this file once
18+
// breaking changes are allowed.
19+
20+
template <>
21+
inline void group_barrier<sub_group>(sub_group Group, memory_scope FenceScope) {
22+
(void)Group;
23+
(void)FenceScope;
24+
#ifdef __SYCL_DEVICE_ONLY__
25+
__spirv_ControlBarrier(__spv::Scope::Subgroup,
26+
detail::spirv::getScope(FenceScope),
27+
__spv::MemorySemanticsMask::AcquireRelease |
28+
__spv::MemorySemanticsMask::SubgroupMemory |
29+
__spv::MemorySemanticsMask::WorkgroupMemory |
30+
__spv::MemorySemanticsMask::CrossWorkgroupMemory);
31+
#else
32+
throw runtime_error("Sub-groups are not supported on host device.",
33+
PI_INVALID_DEVICE);
34+
#endif
35+
}
36+
} // namespace sycl
37+
} // __SYCL_INLINE_NAMESPACE(cl)

0 commit comments

Comments
 (0)