Skip to content

[SYCL][NFC] Pass adapter by ref in ur::getAdapter and event:getAdapter #19202

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

Draft
wants to merge 2 commits into
base: sycl
Choose a base branch
from
Draft
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
58 changes: 29 additions & 29 deletions sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace sycl {
inline namespace _V1 {
namespace detail {

static const AdapterPtr &getAdapter(backend Backend) {
static const adapter_impl &getAdapter(backend Backend) {
switch (Backend) {
case backend::opencl:
return ur::getAdapter<backend::opencl>();
Expand Down Expand Up @@ -71,24 +71,24 @@ backend convertUrBackend(ur_backend_t UrBackend) {
}

platform make_platform(ur_native_handle_t NativeHandle, backend Backend) {
const auto &Adapter = getAdapter(Backend);
const adapter_impl &Adapter = getAdapter(Backend);

// Create UR platform first.
ur_platform_handle_t UrPlatform = nullptr;
Adapter->call<UrApiKind::urPlatformCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrPlatform);
Adapter.call<UrApiKind::urPlatformCreateWithNativeHandle>(
NativeHandle, Adapter.getUrAdapter(), nullptr, &UrPlatform);

return detail::createSyclObjFromImpl<platform>(
platform_impl::getOrMakePlatformImpl(UrPlatform, Adapter));
}

__SYCL_EXPORT device make_device(ur_native_handle_t NativeHandle,
backend Backend) {
const auto &Adapter = getAdapter(Backend);
const adapter_impl &Adapter = getAdapter(Backend);

ur_device_handle_t UrDevice = nullptr;
Adapter->call<UrApiKind::urDeviceCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrDevice);
Adapter.call<UrApiKind::urDeviceCreateWithNativeHandle>(
NativeHandle, Adapter.getUrAdapter(), nullptr, &UrDevice);

// Construct the SYCL device from UR device.
return detail::createSyclObjFromImpl<device>(
Expand All @@ -100,7 +100,7 @@ __SYCL_EXPORT context make_context(ur_native_handle_t NativeHandle,
const async_handler &Handler,
backend Backend, bool KeepOwnership,
const std::vector<device> &DeviceList) {
const auto &Adapter = getAdapter(Backend);
const adapter_impl &Adapter = getAdapter(Backend);

ur_context_handle_t UrContext = nullptr;
ur_context_native_properties_t Properties{};
Expand All @@ -110,8 +110,8 @@ __SYCL_EXPORT context make_context(ur_native_handle_t NativeHandle,
for (const auto &Dev : DeviceList) {
DeviceHandles.push_back(detail::getSyclObjImpl(Dev)->getHandleRef());
}
Adapter->call<UrApiKind::urContextCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), DeviceHandles.size(),
Adapter.call<UrApiKind::urContextCreateWithNativeHandle>(
NativeHandle, Adapter.getUrAdapter(), DeviceHandles.size(),
DeviceHandles.data(), &Properties, &UrContext);
// Construct the SYCL context from UR context.
return detail::createSyclObjFromImpl<context>(context_impl::create(
Expand All @@ -125,7 +125,7 @@ __SYCL_EXPORT queue make_queue(ur_native_handle_t NativeHandle,
const async_handler &Handler, backend Backend) {
ur_device_handle_t UrDevice =
Device ? getSyclObjImpl(*Device)->getHandleRef() : nullptr;
const auto &Adapter = getAdapter(Backend);
const adapter_impl &Adapter = getAdapter(Backend);
context_impl &ContextImpl = *getSyclObjImpl(Context);

if (PropList.has_property<ext::intel::property::queue::compute_index>()) {
Expand Down Expand Up @@ -155,7 +155,7 @@ __SYCL_EXPORT queue make_queue(ur_native_handle_t NativeHandle,
// Create UR queue first.
ur_queue_handle_t UrQueue = nullptr;

Adapter->call<UrApiKind::urQueueCreateWithNativeHandle>(
Adapter.call<UrApiKind::urQueueCreateWithNativeHandle>(
NativeHandle, ContextImpl.getHandleRef(), UrDevice, &NativeProperties,
&UrQueue);
// Construct the SYCL queue from UR queue.
Expand All @@ -171,15 +171,15 @@ __SYCL_EXPORT event make_event(ur_native_handle_t NativeHandle,
__SYCL_EXPORT event make_event(ur_native_handle_t NativeHandle,
const context &Context, bool KeepOwnership,
backend Backend) {
const auto &Adapter = getAdapter(Backend);
const adapter_impl &Adapter = getAdapter(Backend);
const auto &ContextImpl = getSyclObjImpl(Context);

ur_event_handle_t UrEvent = nullptr;
ur_event_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_EVENT_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = !KeepOwnership;

Adapter->call<UrApiKind::urEventCreateWithNativeHandle>(
Adapter.call<UrApiKind::urEventCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &Properties, &UrEvent);
event Event = detail::createSyclObjFromImpl<event>(
event_impl::create_from_handle(UrEvent, Context));
Expand All @@ -193,15 +193,15 @@ std::shared_ptr<detail::kernel_bundle_impl>
make_kernel_bundle(ur_native_handle_t NativeHandle,
const context &TargetContext, bool KeepOwnership,
bundle_state State, backend Backend) {
const auto &Adapter = getAdapter(Backend);
const adapter_impl &Adapter = getAdapter(Backend);
const auto &ContextImpl = getSyclObjImpl(TargetContext);

ur_program_handle_t UrProgram = nullptr;
ur_program_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_PROGRAM_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = !KeepOwnership;

Adapter->call<UrApiKind::urProgramCreateWithNativeHandle>(
Adapter.call<UrApiKind::urProgramCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), &Properties, &UrProgram);
if (UrProgram == nullptr)
throw sycl::exception(
Expand All @@ -214,39 +214,39 @@ make_kernel_bundle(ur_native_handle_t NativeHandle,
std::vector<ur_device_handle_t> ProgramDevices;
uint32_t NumDevices = 0;

Adapter->call<UrApiKind::urProgramGetInfo>(
Adapter.call<UrApiKind::urProgramGetInfo>(
UrProgram, UR_PROGRAM_INFO_NUM_DEVICES, sizeof(NumDevices), &NumDevices,
nullptr);
ProgramDevices.resize(NumDevices);
Adapter->call<UrApiKind::urProgramGetInfo>(
Adapter.call<UrApiKind::urProgramGetInfo>(
UrProgram, UR_PROGRAM_INFO_DEVICES,
sizeof(ur_device_handle_t) * NumDevices, ProgramDevices.data(), nullptr);

for (auto &Dev : ProgramDevices) {
ur_program_binary_type_t BinaryType;
Adapter->call<UrApiKind::urProgramGetBuildInfo>(
Adapter.call<UrApiKind::urProgramGetBuildInfo>(
UrProgram, Dev, UR_PROGRAM_BUILD_INFO_BINARY_TYPE,
sizeof(ur_program_binary_type_t), &BinaryType, nullptr);
switch (BinaryType) {
case (UR_PROGRAM_BINARY_TYPE_NONE):
if (State == bundle_state::object) {
auto Res = Adapter->call_nocheck<UrApiKind::urProgramCompileExp>(
auto Res = Adapter.call_nocheck<UrApiKind::urProgramCompileExp>(
UrProgram, 1, &Dev, nullptr);
if (Res == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) {
Res = Adapter->call_nocheck<UrApiKind::urProgramCompile>(
Res = Adapter.call_nocheck<UrApiKind::urProgramCompile>(
ContextImpl->getHandleRef(), UrProgram, nullptr);
}
Adapter->checkUrResult<errc::build>(Res);
Adapter.checkUrResult<errc::build>(Res);
}

else if (State == bundle_state::executable) {
auto Res = Adapter->call_nocheck<UrApiKind::urProgramBuildExp>(
auto Res = Adapter.call_nocheck<UrApiKind::urProgramBuildExp>(
UrProgram, 1, &Dev, nullptr);
if (Res == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) {
Res = Adapter->call_nocheck<UrApiKind::urProgramBuild>(
Res = Adapter.call_nocheck<UrApiKind::urProgramBuild>(
ContextImpl->getHandleRef(), UrProgram, nullptr);
}
Adapter->checkUrResult<errc::build>(Res);
Adapter.checkUrResult<errc::build>(Res);
}

break;
Expand All @@ -259,15 +259,15 @@ make_kernel_bundle(ur_native_handle_t NativeHandle,
detail::codeToString(UR_RESULT_ERROR_INVALID_VALUE));
if (State == bundle_state::executable) {
ur_program_handle_t UrLinkedProgram = nullptr;
auto Res = Adapter->call_nocheck<UrApiKind::urProgramLinkExp>(
auto Res = Adapter.call_nocheck<UrApiKind::urProgramLinkExp>(
ContextImpl->getHandleRef(), 1, &Dev, 1, &UrProgram, nullptr,
&UrLinkedProgram);
if (Res == UR_RESULT_ERROR_UNSUPPORTED_FEATURE) {
Res = Adapter->call_nocheck<UrApiKind::urProgramLink>(
Res = Adapter.call_nocheck<UrApiKind::urProgramLink>(
ContextImpl->getHandleRef(), 1, &UrProgram, nullptr,
&UrLinkedProgram);
}
Adapter->checkUrResult<errc::build>(Res);
Adapter.checkUrResult<errc::build>(Res);
if (UrLinkedProgram != nullptr) {
UrProgram = UrLinkedProgram;
}
Expand Down Expand Up @@ -351,7 +351,7 @@ kernel make_kernel(const context &TargetContext,
ur_kernel_native_properties_t Properties{};
Properties.stype = UR_STRUCTURE_TYPE_KERNEL_NATIVE_PROPERTIES;
Properties.isNativeHandleOwned = !KeepOwnership;
Adapter->call<UrApiKind::urKernelCreateWithNativeHandle>(
Adapter.call<UrApiKind::urKernelCreateWithNativeHandle>(
NativeHandle, ContextImpl->getHandleRef(), UrProgram, &Properties,
&UrKernel);

Expand Down
7 changes: 4 additions & 3 deletions sycl/source/backend/level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ using namespace sycl::detail;

__SYCL_EXPORT device make_device(const platform &Platform,
ur_native_handle_t NativeHandle) {
const auto &Adapter = ur::getAdapter<backend::ext_oneapi_level_zero>();
const adapter_impl &Adapter =
ur::getAdapter<backend::ext_oneapi_level_zero>();
// Create UR device first.
ur_device_handle_t UrDevice;
Adapter->call<UrApiKind::urDeviceCreateWithNativeHandle>(
NativeHandle, Adapter->getUrAdapter(), nullptr, &UrDevice);
Adapter.call<UrApiKind::urDeviceCreateWithNativeHandle>(
NativeHandle, Adapter.getUrAdapter(), nullptr, &UrDevice);

return detail::createSyclObjFromImpl<device>(
getSyclObjImpl(Platform)->getOrMakeDeviceImpl(UrDevice));
Expand Down
8 changes: 5 additions & 3 deletions sycl/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include <detail/adapter_impl.hpp>
#include <detail/backend_impl.hpp>
#include <detail/context_impl.hpp>
#include <detail/ur.hpp>
Expand Down Expand Up @@ -72,13 +73,14 @@ context::context(const std::vector<device> &DeviceList,
impl = detail::context_impl::create(DeviceList, AsyncHandler, PropList);
}
context::context(cl_context ClContext, async_handler AsyncHandler) {
const auto &Adapter = sycl::detail::ur::getAdapter<backend::opencl>();
const detail::adapter_impl &Adapter =
sycl::detail::ur::getAdapter<backend::opencl>();

ur_context_handle_t hContext = nullptr;
ur_native_handle_t nativeHandle =
reinterpret_cast<ur_native_handle_t>(ClContext);
Adapter->call<detail::UrApiKind::urContextCreateWithNativeHandle>(
nativeHandle, Adapter->getUrAdapter(), 0, nullptr, nullptr, &hContext);
Adapter.call<detail::UrApiKind::urContextCreateWithNativeHandle>(
nativeHandle, Adapter.getUrAdapter(), 0, nullptr, nullptr, &hContext);

impl = detail::context_impl::create(hContext, AsyncHandler, Adapter);
}
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/adapter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class adapter_impl {
return UrPlatforms;
}

ur_adapter_handle_t getUrAdapter() { return MAdapter; }
ur_adapter_handle_t getUrAdapter() const { return MAdapter; }

/// Calls the UR Api, traces the call, and returns the result.
///
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/allowlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ void applyAllowList(std::vector<ur_device_handle_t> &UrDevices,
// Get platform's backend and put it to DeviceDesc
DeviceDescT DeviceDesc;
platform_impl &PlatformImpl =
platform_impl::getOrMakePlatformImpl(UrPlatform, Adapter);
platform_impl::getOrMakePlatformImpl(UrPlatform, *Adapter);
backend Backend = PlatformImpl.getBackend();

for (const auto &SyclBe : getSyclBeMap()) {
Expand Down
8 changes: 4 additions & 4 deletions sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ context_impl::context_impl(const std::vector<sycl::device> Devices,

context_impl::context_impl(ur_context_handle_t UrContext,
async_handler AsyncHandler,
const AdapterPtr &Adapter,
const adapter_impl &Adapter,
const std::vector<sycl::device> &DeviceList,
bool OwnedByRuntime, private_tag)
: MOwnedByRuntime(OwnedByRuntime), MAsyncHandler(AsyncHandler),
Expand All @@ -74,12 +74,12 @@ context_impl::context_impl(ur_context_handle_t UrContext,
std::vector<ur_device_handle_t> DeviceIds;
uint32_t DevicesNum = 0;
// TODO catch an exception and put it to list of asynchronous exceptions
Adapter->call<UrApiKind::urContextGetInfo>(
Adapter.call<UrApiKind::urContextGetInfo>(
MContext, UR_CONTEXT_INFO_NUM_DEVICES, sizeof(DevicesNum), &DevicesNum,
nullptr);
DeviceIds.resize(DevicesNum);
// TODO catch an exception and put it to list of asynchronous exceptions
Adapter->call<UrApiKind::urContextGetInfo>(
Adapter.call<UrApiKind::urContextGetInfo>(
MContext, UR_CONTEXT_INFO_DEVICES,
sizeof(ur_device_handle_t) * DevicesNum, &DeviceIds[0], nullptr);

Expand Down Expand Up @@ -366,7 +366,7 @@ context_impl::initializeDeviceGlobals(ur_program_handle_t NativePrg,
InitEventsRef.begin(), InitEventsRef.end(),
[&Adapter](const ur_event_handle_t &Event) {
return get_event_info<info::event::command_execution_status>(
Event, Adapter) == info::event_command_status::complete;
Event, *Adapter) == info::event_command_status::complete;
});
// Release the removed events.
for (auto EventIt = NewEnd; EventIt != InitEventsRef.end(); ++EventIt)
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ class context_impl : public std::enable_shared_from_this<context_impl> {
/// \param OwnedByRuntime is the flag if ownership is kept by user or
/// transferred to runtime
context_impl(ur_context_handle_t UrContext, async_handler AsyncHandler,
const AdapterPtr &Adapter,
const adapter_impl &Adapter,
const std::vector<sycl::device> &DeviceList, bool OwnedByRuntime,
private_tag);

context_impl(ur_context_handle_t UrContext, async_handler AsyncHandler,
const AdapterPtr &Adapter, private_tag tag)
const adapter_impl &Adapter, private_tag tag)
: context_impl(UrContext, AsyncHandler, Adapter,
std::vector<sycl::device>{},
/*OwnedByRuntime*/ true, tag) {}
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/device_global_map_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ OwnedUrEvent DeviceGlobalUSMMem::getInitEvent(const AdapterPtr &Adapter) {
// If there is a init event we can remove it if it is done.
if (MInitEvent.has_value()) {
if (get_event_info<info::event::command_execution_status>(
*MInitEvent, Adapter) == info::event_command_status::complete) {
*MInitEvent, *Adapter) == info::event_command_status::complete) {
Adapter->call<UrApiKind::urEventRelease>(*MInitEvent);
MInitEvent = {};
return OwnedUrEvent(Adapter);
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/device_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ class device_impl : public std::enable_shared_from_this<device_impl> {
CASE(info::device::platform) {
return createSyclObjFromImpl<platform>(
platform_impl::getOrMakePlatformImpl(
get_info_impl<UR_DEVICE_INFO_PLATFORM>(), getAdapter()));
get_info_impl<UR_DEVICE_INFO_PLATFORM>(), *getAdapter()));
}

CASE(info::device::profile) {
Expand Down
Loading
Loading