Skip to content

Commit 603dcfb

Browse files
committed
Address feedback
1 parent bfb3dac commit 603dcfb

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

source/adapters/opencl/enqueue.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill(
211211
auto DeleteCallback = [](cl_event, cl_int, void *pUserData) {
212212
delete[] static_cast<uint64_t *>(pUserData);
213213
};
214-
CL_RETURN_ON_FAILURE(
215-
clSetEventCallback(WriteEvent, CL_COMPLETE, DeleteCallback, HostBuffer));
214+
ClErr =
215+
clSetEventCallback(WriteEvent, CL_COMPLETE, DeleteCallback, HostBuffer);
216+
if (ClErr != CL_SUCCESS) {
217+
// We can attempt to recover gracefully by attempting to wait for the write
218+
// to finish and deleting the host buffer.
219+
clWaitForEvents(1, &WriteEvent);
220+
delete[] HostBuffer;
221+
clReleaseEvent(WriteEvent);
222+
CL_RETURN_ON_FAILURE(ClErr);
223+
}
216224

217225
if (phEvent) {
218226
*phEvent = cl_adapter::cast<ur_event_handle_t>(WriteEvent);

source/adapters/opencl/usm.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,39 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill(
257257
&CopyEvent));
258258

259259
struct DeleteCallbackInfo {
260+
DeleteCallbackInfo(clMemBlockingFreeINTEL_fn USMFree, cl_context CLContext,
261+
void *HostBuffer)
262+
: USMFree(USMFree), CLContext(CLContext), HostBuffer(HostBuffer) {
263+
clRetainContext(CLContext);
264+
}
265+
~DeleteCallbackInfo() {
266+
USMFree(CLContext, HostBuffer);
267+
clReleaseContext(CLContext);
268+
}
269+
DeleteCallbackInfo(const DeleteCallbackInfo &) = delete;
270+
DeleteCallbackInfo &operator=(const DeleteCallbackInfo &) = delete;
271+
260272
clMemBlockingFreeINTEL_fn USMFree;
261273
cl_context CLContext;
262274
void *HostBuffer;
263-
void execute() {
264-
USMFree(CLContext, HostBuffer);
265-
delete this;
266-
}
267275
};
268276

269-
auto Info = new DeleteCallbackInfo{USMFree, CLContext, HostBuffer};
277+
auto Info = new DeleteCallbackInfo(USMFree, CLContext, HostBuffer);
270278

271279
auto DeleteCallback = [](cl_event, cl_int, void *pUserData) {
272-
static_cast<DeleteCallbackInfo *>(pUserData)->execute();
280+
auto Info = static_cast<DeleteCallbackInfo *>(pUserData);
281+
delete Info;
273282
};
274283

275-
CL_RETURN_ON_FAILURE(
276-
clSetEventCallback(CopyEvent, CL_COMPLETE, DeleteCallback, Info));
277-
284+
ClErr = clSetEventCallback(CopyEvent, CL_COMPLETE, DeleteCallback, Info);
285+
if (ClErr != CL_SUCCESS) {
286+
// We can attempt to recover gracefully by attempting to wait for the copy
287+
// to finish and deleting the info struct here.
288+
clWaitForEvents(1, &CopyEvent);
289+
delete Info;
290+
clReleaseEvent(CopyEvent);
291+
CL_RETURN_ON_FAILURE(ClErr);
292+
}
278293
if (phEvent) {
279294
*phEvent = cl_adapter::cast<ur_event_handle_t>(CopyEvent);
280295
} else {
@@ -426,7 +441,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
426441
return RetVal;
427442
}
428443

429-
std::vector<cl_event> Events;
444+
std::vector<cl_event> Events(height);
430445
for (size_t HeightIndex = 0; HeightIndex < height; HeightIndex++) {
431446
cl_event Event = nullptr;
432447
auto ClResult =
@@ -435,7 +450,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
435450
static_cast<const uint8_t *>(pSrc) + srcPitch * HeightIndex,
436451
width, numEventsInWaitList,
437452
cl_adapter::cast<const cl_event *>(phEventWaitList), &Event);
438-
Events.push_back(Event);
453+
Events[HeightIndex] = Event;
439454
if (ClResult != CL_SUCCESS) {
440455
for (const auto &E : Events) {
441456
clReleaseEvent(E);
@@ -453,7 +468,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D(
453468
Events.data(), cl_adapter::cast<cl_event *>(phEvent));
454469
}
455470
for (const auto &E : Events) {
456-
clReleaseEvent(E);
471+
CL_RETURN_ON_FAILURE(clReleaseEvent(E));
457472
}
458473
CL_RETURN_ON_FAILURE(ClResult)
459474
return UR_RESULT_SUCCESS;

0 commit comments

Comments
 (0)