DPC++ Runtime
Runtime libraries for oneAPI DPC++
barrier.hpp
Go to the documentation of this file.
1 //==--- barrier.hpp - SYCL_ONEAPI_BARRIER ---------------------------------==//
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/__spirv/spirv_ops.hpp> // for __clc_BarrierInitialize
12 #include <sycl/detail/pi.h> // for PI_ERROR_INVALID_DEVICE
13 #include <sycl/exception.hpp> // for runtime_error
14 
15 #include <stdint.h> // for int32_t, int64_t, uint32_t, uint64_t
16 
17 #define SYCL_EXT_ONEAPI_CUDA_ASYNC_BARRIER 1
18 
19 namespace sycl {
20 inline namespace _V1 {
21 namespace ext {
22 namespace oneapi {
23 namespace experimental {
24 namespace cuda {
25 
26 class barrier {
27  int64_t state;
28 
29 public:
31 
32  // barriers cannot be moved or copied
33  barrier(const barrier &other) = delete;
34  barrier(barrier &&other) noexcept = delete;
35  barrier &operator=(const barrier &other) = delete;
36  barrier &operator=(barrier &&other) noexcept = delete;
37 
38  void initialize(uint32_t expected_count) {
39 #ifdef __SYCL_DEVICE_ONLY__
40  __clc_BarrierInitialize(&state, expected_count);
41 #else
42  (void)state;
43  (void)expected_count;
44  throw runtime_error("Barrier is not supported on host device.",
45  PI_ERROR_INVALID_DEVICE);
46 #endif
47  }
48 
49  void invalidate() {
50 #ifdef __SYCL_DEVICE_ONLY__
51  __clc_BarrierInvalidate(&state);
52 #else
53  throw runtime_error("Barrier is not supported on host device.",
54  PI_ERROR_INVALID_DEVICE);
55 #endif
56  }
57 
59 #ifdef __SYCL_DEVICE_ONLY__
60  return __clc_BarrierArrive(&state);
61 #else
62  throw runtime_error("Barrier is not supported on host device.",
63  PI_ERROR_INVALID_DEVICE);
64 #endif
65  }
66 
68 #ifdef __SYCL_DEVICE_ONLY__
69  return __clc_BarrierArriveAndDrop(&state);
70 #else
71  throw runtime_error("Barrier is not supported on host device.",
72  PI_ERROR_INVALID_DEVICE);
73 #endif
74  }
75 
77 #ifdef __SYCL_DEVICE_ONLY__
78  return __clc_BarrierArriveNoComplete(&state, count);
79 #else
80  (void)count;
81  throw runtime_error("Barrier is not supported on host device.",
82  PI_ERROR_INVALID_DEVICE);
83 #endif
84  }
85 
87 #ifdef __SYCL_DEVICE_ONLY__
88  return __clc_BarrierArriveAndDropNoComplete(&state, count);
89 #else
90  (void)count;
91  throw runtime_error("Barrier is not supported on host device.",
92  PI_ERROR_INVALID_DEVICE);
93 #endif
94  }
95 
97 #ifdef __SYCL_DEVICE_ONLY__
98  __clc_BarrierCopyAsyncArrive(&state);
99 #else
100  throw runtime_error("Barrier is not supported on host device.",
101  PI_ERROR_INVALID_DEVICE);
102 #endif
103  }
104 
106 #ifdef __SYCL_DEVICE_ONLY__
107  __clc_BarrierCopyAsyncArriveNoInc(&state);
108 #else
109  throw runtime_error("Barrier is not supported on host device.",
110  PI_ERROR_INVALID_DEVICE);
111 #endif
112  }
113 
114  void wait(arrival_token arrival) {
115 #ifdef __SYCL_DEVICE_ONLY__
116  __clc_BarrierWait(&state, arrival);
117 #else
118  (void)arrival;
119  throw runtime_error("Barrier is not supported on host device.",
120  PI_ERROR_INVALID_DEVICE);
121 #endif
122  }
123 
124  bool test_wait(arrival_token arrival) {
125 #ifdef __SYCL_DEVICE_ONLY__
126  return __clc_BarrierTestWait(&state, arrival);
127 #else
128  (void)arrival;
129  throw runtime_error("Barrier is not supported on host device.",
130  PI_ERROR_INVALID_DEVICE);
131 #endif
132  }
133 
135 #ifdef __SYCL_DEVICE_ONLY__
136  __clc_BarrierArriveAndWait(&state);
137 #else
138  throw runtime_error("Barrier is not supported on host device.",
139  PI_ERROR_INVALID_DEVICE);
140 #endif
141  }
142 
143 // On Windows certain headers define macros min/max
144 #pragma push_macro("max")
145 #ifdef max
146 #undef max
147 #endif
148  static constexpr uint64_t max() { return (1 << 20) - 1; }
149 #pragma pop_macro("max")
150 };
151 } // namespace cuda
152 } // namespace experimental
153 } // namespace oneapi
154 } // namespace ext
155 } // namespace _V1
156 } // namespace sycl
barrier & operator=(const barrier &other)=delete
arrival_token arrive_and_drop_no_complete(int32_t count)
Definition: barrier.hpp:86
void initialize(uint32_t expected_count)
Definition: barrier.hpp:38
barrier & operator=(barrier &&other) noexcept=delete
arrival_token arrive_no_complete(int32_t count)
Definition: barrier.hpp:76
barrier(barrier &&other) noexcept=delete
conditional< sizeof(long)==8, long, long long >::type int64_t
Definition: kernel_desc.hpp:35
Definition: access.hpp:18
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324