Skip to content

Commit 5272332

Browse files
authored
[SYCL] Refactoring of queue classes (#2205)
1. Aligned variables names 2. Replaced "take by value" to "take by reference" in several functions 3. Reduced scope of locks 4. Always use vector of queues instead of accessing one dedicated queue.
1 parent 8a23977 commit 5272332

File tree

4 files changed

+177
-182
lines changed

4 files changed

+177
-182
lines changed

sycl/include/CL/sycl/queue.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ class __SYCL_EXPORT queue {
145145
queue(cl_command_queue ClQueue, const context &SyclContext,
146146
const async_handler &AsyncHandler = {});
147147

148-
queue(const queue &rhs) = default;
148+
queue(const queue &RHS) = default;
149149

150-
queue(queue &&rhs) = default;
150+
queue(queue &&RHS) = default;
151151

152-
queue &operator=(const queue &rhs) = default;
152+
queue &operator=(const queue &RHS) = default;
153153

154-
queue &operator=(queue &&rhs) = default;
154+
queue &operator=(queue &&RHS) = default;
155155

156-
bool operator==(const queue &rhs) const { return impl == rhs.impl; }
156+
bool operator==(const queue &RHS) const { return impl == RHS.impl; }
157157

158-
bool operator!=(const queue &rhs) const { return !(*this == rhs); }
158+
bool operator!=(const queue &RHS) const { return !(*this == RHS); }
159159

160160
/// \return a valid instance of OpenCL queue, which is retained before being
161161
/// returned.
@@ -317,7 +317,7 @@ class __SYCL_EXPORT queue {
317317
/// \return a copy of the property of type PropertyT that the queue was
318318
/// constructed with. If the queue was not constructed with the PropertyT
319319
/// property, an invalid_object_error SYCL exception.
320-
template <typename propertyT> propertyT get_property() const;
320+
template <typename PropertyT> PropertyT get_property() const;
321321

322322
/// Fills the memory pointed by a USM pointer with the value specified.
323323
///
@@ -900,10 +900,10 @@ class __SYCL_EXPORT queue {
900900

901901
namespace std {
902902
template <> struct hash<cl::sycl::queue> {
903-
size_t operator()(const cl::sycl::queue &q) const {
903+
size_t operator()(const cl::sycl::queue &Q) const {
904904
return std::hash<
905905
cl::sycl::shared_ptr_class<cl::sycl::detail::queue_impl>>()(
906-
cl::sycl::detail::getSyclObjImpl(q));
906+
cl::sycl::detail::getSyclObjImpl(Q));
907907
}
908908
};
909909
} // namespace std

sycl/source/detail/queue_impl.cpp

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <detail/queue_impl.hpp>
1515

1616
#include <cstring>
17+
#include <utility>
1718

1819
#ifdef XPTI_ENABLE_INSTRUMENTATION
1920
#include "xpti_trace_framework.hpp"
@@ -27,7 +28,7 @@ template <> cl_uint queue_impl::get_info<info::queue::reference_count>() const {
2728
RT::PiResult result = PI_SUCCESS;
2829
if (!is_host())
2930
getPlugin().call<PiApiKind::piQueueGetInfo>(
30-
MCommandQueue, PI_QUEUE_INFO_REFERENCE_COUNT, sizeof(result), &result,
31+
MQueues[0], PI_QUEUE_INFO_REFERENCE_COUNT, sizeof(result), &result,
3132
nullptr);
3233
return result;
3334
}
@@ -40,78 +41,80 @@ template <> device queue_impl::get_info<info::queue::device>() const {
4041
return get_device();
4142
}
4243

43-
static event prepareUSMEvent(shared_ptr_class<detail::queue_impl> QueueImpl,
44-
RT::PiEvent NativeEvent) {
44+
static event
45+
prepareUSMEvent(const shared_ptr_class<detail::queue_impl> &QueueImpl,
46+
RT::PiEvent NativeEvent) {
4547
auto EventImpl = std::make_shared<detail::event_impl>(QueueImpl);
4648
EventImpl->getHandleRef() = NativeEvent;
4749
EventImpl->setContextImpl(detail::getSyclObjImpl(QueueImpl->get_context()));
4850
return detail::createSyclObjFromImpl<event>(EventImpl);
4951
}
5052

51-
event queue_impl::memset(shared_ptr_class<detail::queue_impl> Impl, void *Ptr,
52-
int Value, size_t Count) {
53-
context Context = get_context();
54-
RT::PiEvent NativeEvent = nullptr;
55-
MemoryManager::fill_usm(Ptr, Impl, Count, Value, /*DepEvents*/ {},
53+
event queue_impl::memset(const shared_ptr_class<detail::queue_impl> &Self,
54+
void *Ptr, int Value, size_t Count) {
55+
RT::PiEvent NativeEvent{};
56+
MemoryManager::fill_usm(Ptr, Self, Count, Value, /*DepEvents*/ {},
5657
NativeEvent);
5758

58-
if (Context.is_host())
59+
if (MContext->is_host())
5960
return event();
6061

61-
event ResEvent = prepareUSMEvent(Impl, NativeEvent);
62+
event ResEvent = prepareUSMEvent(Self, NativeEvent);
6263
addUSMEvent(ResEvent);
6364
return ResEvent;
6465
}
6566

66-
event queue_impl::memcpy(shared_ptr_class<detail::queue_impl> Impl, void *Dest,
67-
const void *Src, size_t Count) {
68-
context Context = get_context();
69-
RT::PiEvent NativeEvent = nullptr;
70-
MemoryManager::copy_usm(Src, Impl, Count, Dest, /*DepEvents*/ {},
67+
event queue_impl::memcpy(const shared_ptr_class<detail::queue_impl> &Self,
68+
void *Dest, const void *Src, size_t Count) {
69+
RT::PiEvent NativeEvent{};
70+
MemoryManager::copy_usm(Src, Self, Count, Dest, /*DepEvents*/ {},
7171
NativeEvent);
7272

73-
if (Context.is_host())
73+
if (MContext->is_host())
7474
return event();
7575

76-
event ResEvent = prepareUSMEvent(Impl, NativeEvent);
76+
event ResEvent = prepareUSMEvent(Self, NativeEvent);
7777
addUSMEvent(ResEvent);
7878
return ResEvent;
7979
}
8080

81-
event queue_impl::mem_advise(shared_ptr_class<detail::queue_impl> Impl,
81+
event queue_impl::mem_advise(const shared_ptr_class<detail::queue_impl> &Self,
8282
const void *Ptr, size_t Length,
8383
pi_mem_advice Advice) {
84-
context Context = get_context();
85-
if (Context.is_host()) {
84+
if (MContext->is_host()) {
8685
return event();
8786
}
8887

8988
// non-Host device
90-
RT::PiEvent NativeEvent = nullptr;
89+
RT::PiEvent NativeEvent{};
9190
const detail::plugin &Plugin = getPlugin();
9291
Plugin.call<PiApiKind::piextUSMEnqueueMemAdvise>(getHandleRef(), Ptr, Length,
9392
Advice, &NativeEvent);
9493

95-
event ResEvent = prepareUSMEvent(Impl, NativeEvent);
94+
event ResEvent = prepareUSMEvent(Self, NativeEvent);
9695
addUSMEvent(ResEvent);
9796
return ResEvent;
9897
}
9998

100-
void queue_impl::addEvent(event Event) {
99+
void queue_impl::addEvent(const event &Event) {
101100
std::weak_ptr<event_impl> EventWeakPtr{getSyclObjImpl(Event)};
102-
std::lock_guard<mutex_class> Guard(MMutex);
101+
std::lock_guard<mutex_class> Lock(MMutex);
103102
MEvents.push_back(std::move(EventWeakPtr));
104103
}
105104

106-
void queue_impl::addUSMEvent(event Event) {
107-
std::lock_guard<mutex_class> Guard(MMutex);
108-
MUSMEvents.push_back(std::move(Event));
105+
void queue_impl::addUSMEvent(const event &Event) {
106+
std::lock_guard<mutex_class> Lock(MMutex);
107+
MUSMEvents.push_back(Event);
109108
}
110109

111110
void *queue_impl::instrumentationProlog(const detail::code_location &CodeLoc,
112111
string_class &Name, int32_t StreamID,
113112
uint64_t &IId) {
114113
void *TraceEvent = nullptr;
114+
(void)CodeLoc;
115+
(void)Name;
116+
(void)StreamID;
117+
(void)IId;
115118
#ifdef XPTI_ENABLE_INSTRUMENTATION
116119
xpti::trace_event_data_t *WaitEvent = nullptr;
117120
if (!xptiTraceEnabled())
@@ -172,6 +175,10 @@ void *queue_impl::instrumentationProlog(const detail::code_location &CodeLoc,
172175

173176
void queue_impl::instrumentationEpilog(void *TelemetryEvent, string_class &Name,
174177
int32_t StreamID, uint64_t IId) {
178+
(void)TelemetryEvent;
179+
(void)Name;
180+
(void)StreamID;
181+
(void)IId;
175182
#ifdef XPTI_ENABLE_INSTRUMENTATION
176183
if (!(xptiTraceEnabled() && TelemetryEvent))
177184
return;
@@ -184,6 +191,7 @@ void queue_impl::instrumentationEpilog(void *TelemetryEvent, string_class &Name,
184191
}
185192

186193
void queue_impl::wait(const detail::code_location &CodeLoc) {
194+
(void)CodeLoc;
187195
#ifdef XPTI_ENABLE_INSTRUMENTATION
188196
void *TelemetryEvent = nullptr;
189197
uint64_t IId;
@@ -192,24 +200,20 @@ void queue_impl::wait(const detail::code_location &CodeLoc) {
192200
TelemetryEvent = instrumentationProlog(CodeLoc, Name, StreamID, IId);
193201
#endif
194202

195-
std::vector<std::shared_ptr<event_impl>> Events;
203+
vector_class<std::weak_ptr<event_impl>> Events;
204+
vector_class<event> USMEvents;
196205
{
197-
std::lock_guard<mutex_class> Guard(MMutex);
198-
for (std::weak_ptr<event_impl> &EventImplWeakPtr : MEvents)
199-
if (std::shared_ptr<event_impl> EventImplPtr = EventImplWeakPtr.lock())
200-
Events.push_back(EventImplPtr);
206+
std::lock_guard<mutex_class> Lock(MMutex);
207+
Events = std::move(MEvents);
208+
USMEvents = std::move(MUSMEvents);
201209
}
202210

203-
for (std::shared_ptr<event_impl> &Event : Events) {
204-
Event->wait(Event);
205-
}
211+
for (std::weak_ptr<event_impl> &EventImplWeakPtr : Events)
212+
if (std::shared_ptr<event_impl> EventImplPtr = EventImplWeakPtr.lock())
213+
EventImplPtr->wait(EventImplPtr);
206214

207-
for (event &Event : MUSMEvents) {
215+
for (event &Event : USMEvents)
208216
Event.wait();
209-
}
210-
211-
MEvents.clear();
212-
MUSMEvents.clear();
213217

214218
#ifdef XPTI_ENABLE_INSTRUMENTATION
215219
instrumentationEpilog(TelemetryEvent, Name, StreamID, IId);
@@ -222,9 +226,9 @@ void queue_impl::initHostTaskAndEventCallbackThreadPool() {
222226

223227
int Size = 1;
224228

225-
if (const char *val = std::getenv("SYCL_QUEUE_THREAD_POOL_SIZE"))
229+
if (const char *Val = std::getenv("SYCL_QUEUE_THREAD_POOL_SIZE"))
226230
try {
227-
Size = std::stoi(val);
231+
Size = std::stoi(Val);
228232
} catch (...) {
229233
throw invalid_parameter_error(
230234
"Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment variable",
@@ -241,9 +245,9 @@ void queue_impl::initHostTaskAndEventCallbackThreadPool() {
241245
}
242246

243247
pi_native_handle queue_impl::getNative() const {
244-
auto Plugin = getPlugin();
245-
pi_native_handle Handle;
246-
Plugin.call<PiApiKind::piextQueueGetNativeHandle>(MCommandQueue, &Handle);
248+
const detail::plugin &Plugin = getPlugin();
249+
pi_native_handle Handle{};
250+
Plugin.call<PiApiKind::piextQueueGetNativeHandle>(MQueues[0], &Handle);
247251
return Handle;
248252
}
249253

0 commit comments

Comments
 (0)