Skip to content

[SYCL] Fix exception duplication for copy back command #14622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,12 +1722,19 @@ ur_result_t MemCpyCommandHost::enqueueImp() {
}

flushCrossQueueDeps(EventImpls, MWorkerQueue);
MemoryManager::copy(
MSrcAllocaCmd->getSYCLMemObj(), MSrcAllocaCmd->getMemAllocation(),
MSrcQueue, MSrcReq.MDims, MSrcReq.MMemoryRange, MSrcReq.MAccessRange,
MSrcReq.MOffset, MSrcReq.MElemSize, *MDstPtr, MQueue, MDstReq.MDims,
MDstReq.MMemoryRange, MDstReq.MAccessRange, MDstReq.MOffset,
MDstReq.MElemSize, std::move(RawEvents), MEvent->getHandleRef(), MEvent);

try {
MemoryManager::copy(
MSrcAllocaCmd->getSYCLMemObj(), MSrcAllocaCmd->getMemAllocation(),
MSrcQueue, MSrcReq.MDims, MSrcReq.MMemoryRange, MSrcReq.MAccessRange,
MSrcReq.MOffset, MSrcReq.MElemSize, *MDstPtr, MQueue, MDstReq.MDims,
MDstReq.MMemoryRange, MDstReq.MAccessRange, MDstReq.MOffset,
MDstReq.MElemSize, std::move(RawEvents), MEvent->getHandleRef(),
MEvent);
} catch (sycl::exception &e) {
return static_cast<ur_result_t>(get_ur_error(e));
}

return UR_RESULT_SUCCESS;
}

Expand Down
25 changes: 19 additions & 6 deletions sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,40 @@ EventImplPtr Scheduler::addCopyBack(Requirement *Req) {
}

std::vector<Command *> ToCleanUp;
// EnqueueCommand will try to enqueue dependencies (previous operations on the
// buffer). If any dep kernel failed it would be reported as sync exception or
// async exception on host task completion and enqueue attempt.
// No need to report those failures again in copy back submission. So report
// only copy back auxiliary and main command failures.
bool CopyBackCmdsFailed = false;
try {
ReadLockT Lock = acquireReadLock();
EnqueueResultT Res;
bool Enqueued;
bool Enqueued = false;

for (Command *Cmd : AuxiliaryCmds) {
Enqueued = GraphProcessor::enqueueCommand(Cmd, Lock, Res, ToCleanUp, Cmd);
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult)
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult) {
CopyBackCmdsFailed |= Res.MCmd == Cmd;
throw exception(make_error_code(errc::runtime),
"Enqueue process failed.");
}
}

Enqueued =
GraphProcessor::enqueueCommand(NewCmd, Lock, Res, ToCleanUp, NewCmd);
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult)
if (!Enqueued && EnqueueResultT::SyclEnqueueFailed == Res.MResult) {
CopyBackCmdsFailed |= Res.MCmd == NewCmd;
throw exception(make_error_code(errc::runtime),
"Enqueue process failed.");
}
} catch (...) {
auto WorkerQueue = NewCmd->getEvent()->getWorkerQueue();
assert(WorkerQueue && "WorkerQueue for CopyBack command must be not null");
WorkerQueue->reportAsyncException(std::current_exception());
if (CopyBackCmdsFailed) {
auto WorkerQueue = NewCmd->getEvent()->getWorkerQueue();
assert(WorkerQueue &&
"WorkerQueue for CopyBack command must be not null");
WorkerQueue->reportAsyncException(std::current_exception());
}
}
EventImplPtr NewEvent = NewCmd->getEvent();
cleanupCommands(ToCleanUp);
Expand Down
44 changes: 44 additions & 0 deletions sycl/unittests/scheduler/FailedCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SchedulerTest.hpp"
#include "SchedulerTestUtils.hpp"

#include <helpers/TestKernel.hpp>
#include <helpers/UrMock.hpp>

using namespace sycl;
Expand Down Expand Up @@ -42,3 +43,46 @@ TEST_F(SchedulerTest, FailedDependency) {
ASSERT_EQ(MDep.MEnqueueStatus, detail::EnqueueResultT::SyclEnqueueFailed)
<< "MDep should be marked as failed\n";
}

void RunWithFailedCommandsAndCheck(bool SyncExceptionExpected,
int AsyncExceptionCountExpected) {
platform Plt = sycl::platform();
int ExceptionListSize = 0;
sycl::async_handler AsyncHandler =
[&ExceptionListSize](sycl::exception_list ExceptionList) {
ExceptionListSize = ExceptionList.size();
};
bool ExceptionThrown = false;
queue Queue(context(Plt), default_selector_v, AsyncHandler);
{
int initVal = 0;
sycl::buffer<int, 1> Buf(&initVal, 1);
try {
Queue.submit([&](sycl::handler &CGH) {
Buf.get_access<sycl::access::mode::write>(CGH);
CGH.single_task<TestKernel<1>>([]() {});
});
} catch (...) {
ExceptionThrown = true;
}
}
EXPECT_EQ(ExceptionThrown, SyncExceptionExpected);
Queue.wait_and_throw();
EXPECT_EQ(ExceptionListSize, AsyncExceptionCountExpected);
}

ur_result_t failingUrCall(void *) { return UR_RESULT_ERROR_UNKNOWN; }

TEST_F(SchedulerTest, FailedKernelException) {
unittest::UrMock<> Mock;
mock::getCallbacks().set_before_callback("urEnqueueKernelLaunch",
&failingUrCall);
RunWithFailedCommandsAndCheck(true, 0);
}

TEST_F(SchedulerTest, FailedCopyBackException) {
unittest::UrMock<> Mock;
mock::getCallbacks().set_before_callback("urEnqueueMemBufferRead",
&failingUrCall);
RunWithFailedCommandsAndCheck(false, 1);
}
Loading