Skip to content

Commit cd93d8f

Browse files
[SYCL] Implement SYCL 2020 default async_handler behavior (#7162)
SYCL 2020 specifies that if no async_handler is given to either the queue nor the context a default async_handler will be used. This default handler must report the problems reported and call `std::terminate` or similar. This commit adds an implementation of the default async_handler. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent b21e74e commit cd93d8f

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

sycl/include/sycl/exception_list.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <sycl/detail/defines.hpp>
1414
#include <sycl/detail/export.hpp>
15+
#include <sycl/detail/iostream_proxy.hpp>
1516
#include <sycl/stl.hpp>
1617

1718
#include <cstddef>
@@ -52,5 +53,23 @@ class __SYCL_EXPORT exception_list {
5253

5354
using async_handler = std::function<void(sycl::exception_list)>;
5455

56+
namespace detail {
57+
// Default implementation of async_handler used by queue and context when no
58+
// user-defined async_handler is specified.
59+
inline void defaultAsyncHandler(exception_list Exceptions) {
60+
std::cerr << "Default async_handler caught exceptions:";
61+
for (auto &EIt : Exceptions) {
62+
try {
63+
if (EIt) {
64+
std::rethrow_exception(EIt);
65+
}
66+
} catch (const std::exception &E) {
67+
std::cerr << "\n\t" << E.what();
68+
}
69+
}
70+
std::cerr << std::endl;
71+
std::terminate();
72+
}
73+
} // namespace detail
5574
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
5675
} // namespace sycl

sycl/include/sycl/queue.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class __SYCL_EXPORT queue {
9090
///
9191
/// \param PropList is a list of properties for queue construction.
9292
explicit queue(const property_list &PropList = {})
93-
: queue(default_selector(), async_handler{}, PropList) {}
93+
: queue(default_selector(), detail::defaultAsyncHandler, PropList) {}
9494

9595
/// Constructs a SYCL queue instance with an async_handler using the device
9696
/// returned by an instance of default_selector.
@@ -125,8 +125,8 @@ class __SYCL_EXPORT queue {
125125
detail::EnableIfSYCL2020DeviceSelectorInvocable<DeviceSelector>>
126126
explicit queue(const DeviceSelector &deviceSelector,
127127
const property_list &PropList = {})
128-
: queue(detail::select_device(deviceSelector), async_handler{},
129-
PropList) {}
128+
: queue(detail::select_device(deviceSelector),
129+
detail::defaultAsyncHandler, PropList) {}
130130

131131
/// Constructs a SYCL queue instance using the device identified by the
132132
/// device selector provided.
@@ -171,7 +171,8 @@ class __SYCL_EXPORT queue {
171171
"use SYCL 2020 device selectors instead.")
172172
queue(const device_selector &DeviceSelector,
173173
const property_list &PropList = {})
174-
: queue(DeviceSelector.select_device(), async_handler{}, PropList) {}
174+
: queue(DeviceSelector.select_device(), detail::defaultAsyncHandler,
175+
PropList) {}
175176

176177
/// Constructs a SYCL queue instance with an async_handler using the device
177178
/// returned by the DeviceSelector provided.
@@ -190,7 +191,7 @@ class __SYCL_EXPORT queue {
190191
/// \param SyclDevice is an instance of SYCL device.
191192
/// \param PropList is a list of properties for queue construction.
192193
explicit queue(const device &SyclDevice, const property_list &PropList = {})
193-
: queue(SyclDevice, async_handler{}, PropList) {}
194+
: queue(SyclDevice, detail::defaultAsyncHandler, PropList) {}
194195

195196
/// Constructs a SYCL queue instance with an async_handler using the device
196197
/// provided.

sycl/source/backend/level_zero.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ __SYCL_EXPORT context make_context(const std::vector<device> &DeviceList,
5757
NativeHandle, DeviceHandles.size(), DeviceHandles.data(), !KeepOwnership,
5858
&PiContext);
5959
// Construct the SYCL context from PI context.
60-
return detail::createSyclObjFromImpl<context>(
61-
std::make_shared<context_impl>(PiContext, async_handler{}, Plugin));
60+
return detail::createSyclObjFromImpl<context>(std::make_shared<context_impl>(
61+
PiContext, detail::defaultAsyncHandler, Plugin));
6262
}
6363

6464
//----------------------------------------------------------------------------

sycl/source/backend/opencl.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ __SYCL_EXPORT device make_device(pi_native_handle NativeHandle) {
3636
//----------------------------------------------------------------------------
3737
// Implementation of opencl::make<context>
3838
__SYCL_EXPORT context make_context(pi_native_handle NativeHandle) {
39-
return detail::make_context(NativeHandle, async_handler{}, backend::opencl);
39+
return detail::make_context(NativeHandle, detail::defaultAsyncHandler,
40+
backend::opencl);
4041
}
4142

4243
//----------------------------------------------------------------------------

sycl/source/context.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace sycl {
2828
__SYCL_INLINE_VER_NAMESPACE(_V1) {
29+
2930
context::context(const property_list &PropList)
3031
: context(default_selector().select_device(), PropList) {}
3132

@@ -49,7 +50,7 @@ context::context(const platform &Platform, async_handler AsyncHandler,
4950

5051
context::context(const std::vector<device> &DeviceList,
5152
const property_list &PropList)
52-
: context(DeviceList, async_handler{}, PropList) {}
53+
: context(DeviceList, detail::defaultAsyncHandler, PropList) {}
5354

5455
context::context(const std::vector<device> &DeviceList,
5556
async_handler AsyncHandler, const property_list &PropList) {

sycl/source/detail/queue_impl.hpp

-4
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ class queue_impl {
230230
try {
231231
return submit_impl(CGF, Self, Self, SecondQueue, Loc, PostProcess);
232232
} catch (...) {
233-
{
234-
std::lock_guard<std::mutex> Lock(MMutex);
235-
MExceptions.PushBack(std::current_exception());
236-
}
237233
return SecondQueue->submit_impl(CGF, SecondQueue, Self, SecondQueue, Loc,
238234
PostProcess);
239235
}

0 commit comments

Comments
 (0)