-
Notifications
You must be signed in to change notification settings - Fork 766
/
Copy pathqueue.cpp
259 lines (214 loc) · 8.03 KB
/
queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//===--------- queue.cpp - CUDA Adapter -----------------------------------===//
//
// Copyright (C) 2023 Intel Corporation
//
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "queue.hpp"
#include "common.hpp"
#include "context.hpp"
#include "event.hpp"
#include <cassert>
#include <cuda.h>
template <>
void cuda_stream_queue::computeStreamWaitForBarrierIfNeeded(CUstream Stream,
uint32_t StreamI) {
if (BarrierEvent && !ComputeAppliedBarrier[StreamI]) {
UR_CHECK_ERROR(cuStreamWaitEvent(Stream, BarrierEvent, 0));
ComputeAppliedBarrier[StreamI] = true;
}
}
template <>
void cuda_stream_queue::transferStreamWaitForBarrierIfNeeded(CUstream Stream,
uint32_t StreamI) {
if (BarrierEvent && !TransferAppliedBarrier[StreamI]) {
UR_CHECK_ERROR(cuStreamWaitEvent(Stream, BarrierEvent, 0));
TransferAppliedBarrier[StreamI] = true;
}
}
template <>
ur_queue_handle_t cuda_stream_queue::getEventQueue(const ur_event_handle_t e) {
return e->getQueue();
}
template <>
uint32_t
cuda_stream_queue::getEventComputeStreamToken(const ur_event_handle_t e) {
return e->getComputeStreamToken();
}
template <>
CUstream cuda_stream_queue::getEventStream(const ur_event_handle_t e) {
return e->getStream();
}
/// Creates a `ur_queue_handle_t` object on the CUDA backend.
/// Valid properties
/// * __SYCL_PI_CUDA_USE_DEFAULT_STREAM -> CU_STREAM_DEFAULT
/// * __SYCL_PI_CUDA_SYNC_WITH_DEFAULT -> CU_STREAM_NON_BLOCKING
UR_APIEXPORT ur_result_t UR_APICALL
urQueueCreate(ur_context_handle_t hContext, ur_device_handle_t hDevice,
const ur_queue_properties_t *pProps, ur_queue_handle_t *phQueue) {
try {
std::unique_ptr<ur_queue_handle_t_> Queue{nullptr};
if (std::find(hContext->getDevices().begin(), hContext->getDevices().end(),
hDevice) == hContext->getDevices().end()) {
*phQueue = nullptr;
return UR_RESULT_ERROR_INVALID_DEVICE;
}
unsigned int Flags = CU_STREAM_NON_BLOCKING;
ur_queue_flags_t URFlags = 0;
// '0' is the default priority, per CUDA Toolkit 12.2 and earlier
int Priority = 0;
bool IsOutOfOrder = false;
if (pProps && pProps->stype == UR_STRUCTURE_TYPE_QUEUE_PROPERTIES) {
URFlags = pProps->flags;
if (URFlags == UR_QUEUE_FLAG_USE_DEFAULT_STREAM) {
Flags = CU_STREAM_DEFAULT;
} else if (URFlags == UR_QUEUE_FLAG_SYNC_WITH_DEFAULT_STREAM) {
Flags = 0;
}
if (URFlags & UR_QUEUE_FLAG_OUT_OF_ORDER_EXEC_MODE_ENABLE) {
IsOutOfOrder = true;
}
if (URFlags & UR_QUEUE_FLAG_PRIORITY_HIGH) {
ScopedContext Active(hDevice);
UR_CHECK_ERROR(cuCtxGetStreamPriorityRange(nullptr, &Priority));
} else if (URFlags & UR_QUEUE_FLAG_PRIORITY_LOW) {
ScopedContext Active(hDevice);
UR_CHECK_ERROR(cuCtxGetStreamPriorityRange(&Priority, nullptr));
}
}
Queue = std::unique_ptr<ur_queue_handle_t_>(new ur_queue_handle_t_{
{IsOutOfOrder, hContext, hDevice, Flags, URFlags, Priority}});
*phQueue = Queue.release();
return UR_RESULT_SUCCESS;
} catch (ur_result_t Err) {
return Err;
} catch (std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}
}
UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) {
assert(hQueue->getReferenceCount() > 0);
hQueue->incrementReferenceCount();
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) {
if (hQueue->decrementReferenceCount() > 0) {
return UR_RESULT_SUCCESS;
}
try {
std::unique_ptr<ur_queue_handle_t_> Queue(hQueue);
if (!hQueue->backendHasOwnership())
return UR_RESULT_SUCCESS;
ScopedContext Active(hQueue->getDevice());
hQueue->forEachStream([](CUstream S) {
UR_CHECK_ERROR(cuStreamSynchronize(S));
UR_CHECK_ERROR(cuStreamDestroy(S));
});
if (hQueue->getHostSubmitTimeStream() != CUstream{0}) {
UR_CHECK_ERROR(cuStreamSynchronize(hQueue->getHostSubmitTimeStream()));
UR_CHECK_ERROR(cuStreamDestroy(hQueue->getHostSubmitTimeStream()));
}
return UR_RESULT_SUCCESS;
} catch (ur_result_t Err) {
return Err;
} catch (...) {
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
}
}
UR_APIEXPORT ur_result_t UR_APICALL urQueueFinish(ur_queue_handle_t hQueue) {
ur_result_t Result = UR_RESULT_SUCCESS;
try {
ScopedContext active(hQueue->getDevice());
hQueue->syncStreams</*ResetUsed=*/true>(
[](CUstream s) { UR_CHECK_ERROR(cuStreamSynchronize(s)); });
} catch (ur_result_t Err) {
Result = Err;
} catch (...) {
Result = UR_RESULT_ERROR_OUT_OF_RESOURCES;
}
return Result;
}
// There is no CUDA counterpart for queue flushing and we don't run into the
// same problem of having to flush cross-queue dependencies as some of the
// other plugins, so it can be left as no-op.
UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) {
std::ignore = hQueue;
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL
urQueueGetNativeHandle(ur_queue_handle_t hQueue, ur_queue_native_desc_t *pDesc,
ur_native_handle_t *phNativeQueue) {
std::ignore = pDesc;
ScopedContext Active(hQueue->getDevice());
*phNativeQueue =
reinterpret_cast<ur_native_handle_t>(hQueue->getNextComputeStream());
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
ur_native_handle_t hNativeQueue, ur_context_handle_t hContext,
ur_device_handle_t hDevice, const ur_queue_native_properties_t *pProperties,
ur_queue_handle_t *phQueue) {
if (!hDevice && hContext->getDevices().size() == 1)
hDevice = hContext->getDevices().front();
unsigned int CuFlags;
CUstream CuStream = reinterpret_cast<CUstream>(hNativeQueue);
UR_CHECK_ERROR(cuStreamGetFlags(CuStream, &CuFlags));
ur_queue_flags_t Flags = 0;
if (CuFlags == CU_STREAM_DEFAULT)
Flags = UR_QUEUE_FLAG_USE_DEFAULT_STREAM;
else if (CuFlags == CU_STREAM_NON_BLOCKING)
Flags = UR_QUEUE_FLAG_SYNC_WITH_DEFAULT_STREAM;
else
die("Unknown cuda stream");
auto isNativeHandleOwned =
pProperties ? pProperties->isNativeHandleOwned : false;
// Create queue from a native stream
*phQueue = new ur_queue_handle_t_{
{CuStream, hContext, hDevice, CuFlags, Flags, isNativeHandleOwned}};
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urQueueGetInfo(ur_queue_handle_t hQueue,
ur_queue_info_t propName,
size_t propValueSize,
void *pPropValue,
size_t *pPropSizeRet) {
UrReturnHelper ReturnValue(propValueSize, pPropValue, pPropSizeRet);
switch (propName) {
case UR_QUEUE_INFO_CONTEXT:
return ReturnValue(hQueue->Context);
case UR_QUEUE_INFO_DEVICE:
return ReturnValue(hQueue->Device);
case UR_QUEUE_INFO_REFERENCE_COUNT:
return ReturnValue(hQueue->getReferenceCount());
case UR_QUEUE_INFO_FLAGS:
return ReturnValue(hQueue->URFlags);
case UR_QUEUE_INFO_EMPTY: {
try {
bool IsReady = hQueue->allOf([](CUstream S) -> bool {
const CUresult Ret = cuStreamQuery(S);
if (Ret == CUDA_SUCCESS)
return true;
if (Ret == CUDA_ERROR_NOT_READY)
return false;
UR_CHECK_ERROR(Ret);
return false;
});
return ReturnValue(IsReady);
} catch (ur_result_t Err) {
return Err;
} catch (...) {
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
}
}
case UR_QUEUE_INFO_DEVICE_DEFAULT:
case UR_QUEUE_INFO_SIZE:
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
}