Skip to content

Commit

Permalink
[SYCL] Make host device inaccessible through SYCL API (#6685)
Browse files Browse the repository at this point in the history
This commit removes the host device from the device list and as such the
host device will no longer be available in user code. The following
changes are a result of this:
* Device filters using 'host' as either backend or device type will
cause a warning at runtime. Since there is no host device selectable for
these filters, the resulting device list will not contain a host
   device.
* is_host() on SYCL objects has been deprecated. Any use of them
internally on a host device should cause an assertion to fail.
* host_selector deprecation message has been changed to better reflect
that there is no alternative.

Signed-off-by: Larsen, Steffen <steffen.larsen@intel.com>
  • Loading branch information
steffenlarsen authored Sep 20, 2022
1 parent ed7cb4b commit 5b13d5b
Show file tree
Hide file tree
Showing 53 changed files with 304 additions and 692 deletions.
2 changes: 1 addition & 1 deletion sycl/include/sycl/backend_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {

enum class backend : char {
host = 0,
host __SYCL2020_DEPRECATED("'host' backend is no longer supported") = 0,
opencl = 1,
ext_oneapi_level_zero = 2,
level_zero __SYCL2020_DEPRECATED("use 'ext_oneapi_level_zero' instead") =
Expand Down
2 changes: 2 additions & 0 deletions sycl/include/sycl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class __SYCL_EXPORT context {
/// Checks if this context is a SYCL host context.
///
/// \return true if this context is a SYCL host context.
__SYCL2020_DEPRECATED(
"is_host() is deprecated as the host device is no longer supported.")
bool is_host() const;

/// Returns the backend associated with this context.
Expand Down
1 change: 0 additions & 1 deletion sycl/include/sycl/detail/device_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class device_filter_list {
bool backendCompatible(backend Backend);
bool deviceTypeCompatible(info::device_type DeviceType);
bool deviceNumberCompatible(int DeviceNum);
bool containsHost();
friend std::ostream &operator<<(std::ostream &Out,
const device_filter_list &List);
};
Expand Down
2 changes: 2 additions & 0 deletions sycl/include/sycl/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class __SYCL_EXPORT device {
/// Check if device is a host device
///
/// \return true if SYCL device is a host device
__SYCL2020_DEPRECATED(
"is_host() is deprecated as the host device is no longer supported.")
bool is_host() const;

/// Check if device is a CPU device
Expand Down
2 changes: 1 addition & 1 deletion sycl/include/sycl/device_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ __SYCL2020_DEPRECATED("Use the callable sycl::accelerator_selector_v instead.")
///
/// \ingroup sycl_api_dev_sel
class __SYCL_EXPORT
__SYCL2020_DEPRECATED("Use a callable function instead.") host_selector
__SYCL2020_DEPRECATED("Host device is no longer supported.") host_selector
: public device_selector {
public:
int operator()(const device &dev) const override;
Expand Down
2 changes: 2 additions & 0 deletions sycl/include/sycl/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class __SYCL_EXPORT event {
/// Checks if this event is a SYCL host event.
///
/// \return true if this event is a SYCL host event.
__SYCL2020_DEPRECATED(
"is_host() is deprecated as the host device is no longer supported.")
bool is_host() const;

/// Return the list of events that this event waits for.
Expand Down
18 changes: 0 additions & 18 deletions sycl/include/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,6 @@ class __SYCL_EXPORT handler {

~handler() = default;

bool is_host() { return MIsHost; }

#ifdef __SYCL_DEVICE_ONLY__
// In device compilation accessor isn't inherited from AccessorBaseHost, so
// can't detect by it. Since we don't expect it to be ever called in device
Expand Down Expand Up @@ -543,18 +541,6 @@ class __SYCL_EXPORT handler {
sizeof(sampler), ArgIndex);
}

void verifyKernelInvoc(const kernel &Kernel) {
if (is_host()) {
throw invalid_object_error(
"This kernel invocation method cannot be used on the host",
PI_ERROR_INVALID_DEVICE);
}
if (Kernel.is_host()) {
throw invalid_object_error("Invalid kernel type, OpenCL expected",
PI_ERROR_INVALID_KERNEL);
}
}

/* The kernel passed to StoreLambda can take an id, an item or an nd_item as
* its argument. Since esimd plugin directly invokes the kernel (doesn’t use
* piKernelSetArg), the kernel argument type must be known to the plugin.
Expand Down Expand Up @@ -1066,7 +1052,6 @@ class __SYCL_EXPORT handler {
template <int Dims>
void parallel_for_impl(range<Dims> NumWorkItems, kernel Kernel) {
throwIfActionIsCreated();
verifyKernelInvoc(Kernel);
MKernel = detail::getSyclObjImpl(std::move(Kernel));
detail::checkValueRange<Dims>(NumWorkItems);
MNDRDesc.set(std::move(NumWorkItems));
Expand Down Expand Up @@ -1877,7 +1862,6 @@ class __SYCL_EXPORT handler {
/// \param Kernel is a SYCL kernel object.
void single_task(kernel Kernel) {
throwIfActionIsCreated();
verifyKernelInvoc(Kernel);
// Ignore any set kernel bundles and use the one associated with the kernel
setHandlerKernelBundle(Kernel);
// No need to check if range is out of INT_MAX limits as it's compile-time
Expand Down Expand Up @@ -1914,7 +1898,6 @@ class __SYCL_EXPORT handler {
void parallel_for(range<Dims> NumWorkItems, id<Dims> WorkItemOffset,
kernel Kernel) {
throwIfActionIsCreated();
verifyKernelInvoc(Kernel);
MKernel = detail::getSyclObjImpl(std::move(Kernel));
detail::checkValueRange<Dims>(NumWorkItems, WorkItemOffset);
MNDRDesc.set(std::move(NumWorkItems), std::move(WorkItemOffset));
Expand All @@ -1933,7 +1916,6 @@ class __SYCL_EXPORT handler {
/// \param Kernel is a SYCL kernel function.
template <int Dims> void parallel_for(nd_range<Dims> NDRange, kernel Kernel) {
throwIfActionIsCreated();
verifyKernelInvoc(Kernel);
MKernel = detail::getSyclObjImpl(std::move(Kernel));
detail::checkValueRange<Dims>(NDRange);
MNDRDesc.set(std::move(NDRange));
Expand Down
2 changes: 2 additions & 0 deletions sycl/include/sycl/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class __SYCL_EXPORT kernel {
/// Check if the associated SYCL context is a SYCL host context.
///
/// \return true if this SYCL kernel is a host kernel.
__SYCL2020_DEPRECATED(
"is_host() is deprecated as the host device is no longer supported.")
bool is_host() const;

/// Get the context that this kernel is defined for.
Expand Down
2 changes: 2 additions & 0 deletions sycl/include/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class __SYCL_EXPORT platform {
/// Checks if this SYCL platform is a host platform.
///
/// \return true if this SYCL platform is a host platform.
__SYCL2020_DEPRECATED(
"is_host() is deprecated as the host device is no longer supported.")
bool is_host() const;

/// Returns all SYCL devices associated with this platform.
Expand Down
87 changes: 39 additions & 48 deletions sycl/include/sycl/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ class __SYCL_EXPORT queue {
device get_device() const;

/// \return true if this queue is a SYCL host queue.
__SYCL2020_DEPRECATED(
"is_host() is deprecated as the host device is no longer supported.")
bool is_host() const;

/// Queries SYCL queue for information.
Expand All @@ -302,28 +304,24 @@ class __SYCL_EXPORT queue {
_CODELOCARG(&CodeLoc);

#if __SYCL_USE_FALLBACK_ASSERT
if (!is_host()) {
auto PostProcess = [this, &CodeLoc](bool IsKernel, bool KernelUsesAssert,
event &E) {
if (IsKernel && !device_has(aspect::ext_oneapi_native_assert) &&
KernelUsesAssert && !device_has(aspect::accelerator)) {
// __devicelib_assert_fail isn't supported by Device-side Runtime
// Linking against fallback impl of __devicelib_assert_fail is
// performed by program manager class
// Fallback assert isn't supported for FPGA
submitAssertCapture(*this, E, /* SecondaryQueue = */ nullptr,
CodeLoc);
}
};

auto Event = submit_impl_and_postprocess(CGF, CodeLoc, PostProcess);
return discard_or_return(Event);
} else
auto PostProcess = [this, &CodeLoc](bool IsKernel, bool KernelUsesAssert,
event &E) {
if (IsKernel && !device_has(aspect::ext_oneapi_native_assert) &&
KernelUsesAssert && !device_has(aspect::accelerator)) {
// __devicelib_assert_fail isn't supported by Device-side Runtime
// Linking against fallback impl of __devicelib_assert_fail is
// performed by program manager class
// Fallback assert isn't supported for FPGA
submitAssertCapture(*this, E, /* SecondaryQueue = */ nullptr, CodeLoc);
}
};

auto Event = submit_impl_and_postprocess(CGF, CodeLoc, PostProcess);
return discard_or_return(Event);
#else
auto Event = submit_impl(CGF, CodeLoc);
return discard_or_return(Event);
#endif // __SYCL_USE_FALLBACK_ASSERT
{
auto Event = submit_impl(CGF, CodeLoc);
return discard_or_return(Event);
}
}

/// Submits a command group function object to the queue, in order to be
Expand All @@ -342,34 +340,27 @@ class __SYCL_EXPORT queue {
_CODELOCARG(&CodeLoc);

#if __SYCL_USE_FALLBACK_ASSERT
if (!is_host()) {
auto PostProcess = [this, &SecondaryQueue, &CodeLoc](
bool IsKernel, bool KernelUsesAssert, event &E) {
if (IsKernel && !device_has(aspect::ext_oneapi_native_assert) &&
KernelUsesAssert && !device_has(aspect::accelerator)) {
// Only secondary queues on devices need to be added to the assert
// capture.
// TODO: Handle case where primary queue is host but the secondary
// queue is not.
queue *DeviceSecondaryQueue =
SecondaryQueue.is_host() ? nullptr : &SecondaryQueue;
// __devicelib_assert_fail isn't supported by Device-side Runtime
// Linking against fallback impl of __devicelib_assert_fail is
// performed by program manager class
// Fallback assert isn't supported for FPGA
submitAssertCapture(*this, E, DeviceSecondaryQueue, CodeLoc);
}
};

auto Event = submit_impl_and_postprocess(CGF, SecondaryQueue, CodeLoc,
PostProcess);
return discard_or_return(Event);
} else
auto PostProcess = [this, &SecondaryQueue, &CodeLoc](
bool IsKernel, bool KernelUsesAssert, event &E) {
if (IsKernel && !device_has(aspect::ext_oneapi_native_assert) &&
KernelUsesAssert && !device_has(aspect::accelerator)) {
// Only secondary queues on devices need to be added to the assert
// capture.
// __devicelib_assert_fail isn't supported by Device-side Runtime
// Linking against fallback impl of __devicelib_assert_fail is
// performed by program manager class
// Fallback assert isn't supported for FPGA
submitAssertCapture(*this, E, &SecondaryQueue, CodeLoc);
}
};

auto Event =
submit_impl_and_postprocess(CGF, SecondaryQueue, CodeLoc, PostProcess);
return discard_or_return(Event);
#else
auto Event = submit_impl(CGF, SecondaryQueue, CodeLoc);
return discard_or_return(Event);
#endif // __SYCL_USE_FALLBACK_ASSERT
{
auto Event = submit_impl(CGF, SecondaryQueue, CodeLoc);
return discard_or_return(Event);
}
}

/// Prevents any commands submitted afterward to this queue from executing
Expand Down
13 changes: 9 additions & 4 deletions sycl/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ context::context(const std::vector<device> &DeviceList,
PI_ERROR_INVALID_VALUE);
}
auto NonHostDeviceIter = std::find_if_not(
DeviceList.begin(), DeviceList.end(),
[&](const device &CurrentDevice) { return CurrentDevice.is_host(); });
DeviceList.begin(), DeviceList.end(), [&](const device &CurrentDevice) {
return detail::getSyclObjImpl(CurrentDevice)->is_host();
});
if (NonHostDeviceIter == DeviceList.end())
impl = std::make_shared<detail::context_impl>(DeviceList[0], AsyncHandler,
PropList);
Expand All @@ -70,7 +71,7 @@ context::context(const std::vector<device> &DeviceList,
if (std::any_of(DeviceList.begin(), DeviceList.end(),
[&](const device &CurrentDevice) {
return (
CurrentDevice.is_host() ||
detail::getSyclObjImpl(CurrentDevice)->is_host() ||
(detail::getSyclObjImpl(CurrentDevice.get_platform())
->getHandleRef() != NonHostPlatform));
}))
Expand Down Expand Up @@ -122,7 +123,11 @@ context::get_info() const {

cl_context context::get() const { return impl->get(); }

bool context::is_host() const { return impl->is_host(); }
bool context::is_host() const {
bool IsHost = impl->is_host();
assert(!IsHost && "context::is_host should not be called in implementation.");
return IsHost;
}

backend context::get_backend() const noexcept { return getImplBackend(impl); }

Expand Down
10 changes: 3 additions & 7 deletions sycl/source/detail/backend_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
//===----------------------------------------------------------------------===//

#pragma once
#include <cassert>
#include <sycl/backend_types.hpp>

namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {
namespace detail {

template <class T> backend getImplBackend(const T &Impl) {
backend Result;
if (Impl->is_host())
Result = backend::host;
else
Result = Impl->getPlugin().getBackend();

return Result;
assert(!Impl->is_host() && "Cannot get the backend for host.");
return Impl->getPlugin().getBackend();
}

} // namespace detail
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace detail {
context_impl::context_impl(const device &Device, async_handler AsyncHandler,
const property_list &PropList)
: MAsyncHandler(AsyncHandler), MDevices(1, Device), MContext(nullptr),
MPlatform(), MPropList(PropList), MHostContext(Device.is_host()),
MPlatform(), MPropList(PropList),
MHostContext(detail::getSyclObjImpl(Device)->is_host()),
MSupportBufferLocationByDevices(NotChecked) {
MKernelProgramCache.setContextPtr(this);
}
Expand Down
27 changes: 12 additions & 15 deletions sycl/source/detail/device_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ device_filter::device_filter(const std::string &FilterString) {
else {
Backend = It->second;
TripleValueID++;

if (Backend == backend::host)
std::cerr << "WARNING: The 'host' backend type is no longer supported in "
"device filter."
<< std::endl;
}

// Handle the optional 2nd field of the filter - device type.
Expand All @@ -77,6 +82,11 @@ device_filter::device_filter(const std::string &FilterString) {
else {
DeviceType = Iter->second;
TripleValueID++;

if (DeviceType == info::device_type::host)
std::cerr << "WARNING: The 'host' device type is no longer supported "
"in device filter."
<< std::endl;
}
}

Expand All @@ -91,8 +101,8 @@ device_filter::device_filter(const std::string &FilterString) {
std::string Message =
std::string("Invalid device filter: ") + FilterString +
"\nPossible backend values are "
"{host,opencl,level_zero,cuda,hip,esimd_emulator,*}.\n"
"Possible device types are {host,cpu,gpu,acc,*}.\n"
"{opencl,level_zero,cuda,hip,esimd_emulator,*}.\n"
"Possible device types are {cpu,gpu,acc,*}.\n"
"Device number should be an non-negative integer.\n";
throw sycl::invalid_parameter_error(Message, PI_ERROR_INVALID_VALUE);
}
Expand Down Expand Up @@ -157,19 +167,6 @@ bool device_filter_list::deviceNumberCompatible(int DeviceNum) {
return false;
}

bool device_filter_list::containsHost() {
for (const device_filter &Filter : FilterList) {
if (Filter.Backend == backend::host || Filter.Backend == backend::all)
if (Filter.DeviceType == info::device_type::host ||
Filter.DeviceType == info::device_type::all)
// SYCL RT never creates more than one HOST device.
// All device numbers other than 0 are rejected.
if (!Filter.HasDeviceNum || Filter.DeviceNum == 0)
return true;
}
return false;
}

} // namespace detail
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
} // namespace sycl
Loading

0 comments on commit 5b13d5b

Please sign in to comment.