Skip to content

Commit

Permalink
Testing adding handles to opencl
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Mar 26, 2024
1 parent f381d5c commit ea513e3
Show file tree
Hide file tree
Showing 38 changed files with 356 additions and 420 deletions.
2 changes: 2 additions & 0 deletions sycl/include/sycl/detail/pi.def
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ _PI_API(piSamplerCreate)
_PI_API(piSamplerGetInfo)
_PI_API(piSamplerRetain)
_PI_API(piSamplerRelease)
_PI_API(piextSamplerGetNativeHandle)
_PI_API(piextSamplerCreateWithNativeHandle)
// Queue commands
_PI_API(piEnqueueKernelLaunch)
_PI_API(piextEnqueueCooperativeKernelLaunch)
Expand Down
7 changes: 7 additions & 0 deletions sycl/include/sycl/detail/pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,13 @@ __SYCL_EXPORT pi_result piSamplerRetain(pi_sampler sampler);

__SYCL_EXPORT pi_result piSamplerRelease(pi_sampler sampler);

__SYCL_EXPORT pi_result piextSamplerCreateWithNativeHandle(
pi_native_handle nativeHandle, pi_context context,
const bool ownNativeHandle, pi_sampler *sampler);

__SYCL_EXPORT pi_result
piextSamplerGetNativeHandle(pi_sampler sampler, pi_native_handle *nativeHandle);

//
// Queue Commands
//
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList,
const pi_event *EventWaitList,
pi_event *OutEvent) {
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/hip/pi_hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList,
const pi_event *EventWaitList,
pi_event *OutEvent) {
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

//
// Queue Commands
//
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/native_cpu/pi_native_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

//
// Queue Commands
//
Expand Down
13 changes: 13 additions & 0 deletions sycl/plugins/opencl/pi_opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,19 @@ pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
const bool ownNativeHandle,
pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

pi_result piEnqueueEventsWait(pi_queue Queue, pi_uint32 NumEventsInWaitList,
const pi_event *EventWaitList,
pi_event *OutEvent) {
Expand Down
16 changes: 8 additions & 8 deletions sycl/plugins/unified_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ if(SYCL_PI_UR_USE_FETCH_CONTENT)
CACHE PATH "Path to external '${name}' adapter source dir" FORCE)
endfunction()

set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git")
# commit 1f4b703d8a136d49e4e98ca5f017727f3aaa41dc
# Merge: 6883118 a187fbb
# Author: Piotr Balcer <piotr.balcer@intel.com>
# Date: Mon Mar 25 11:22:45 2024 +0100
# Merge pull request #1471 from pbalcer/remove-coverage
# [CI] remove coverage workflow
set(UNIFIED_RUNTIME_TAG 1f4b703d8a136d49e4e98ca5f017727f3aaa41dc)
set(UNIFIED_RUNTIME_REPO "https://github.com/omarahmed1111/unified-runtime.git")
# commit 6513abc404979fa109d64500bf899e632d511291
# Merge: 09be0881 6d586094
# Author: Kenneth Benzie (Benie) <k.benzie@codeplay.com>
# Date: Thu Mar 14 22:38:53 2024 +0000
# Merge pull request #1410 from kbenzie/benie/cmake-external-adapter-source-dirs
# [CMake] Support external adapter source dirs
set(UNIFIED_RUNTIME_TAG 820779f6ede3291227581c2b3f8acaf71a91ee0c)

if(SYCL_PI_UR_OVERRIDE_FETCH_CONTENT_REPO)
set(UNIFIED_RUNTIME_REPO "${SYCL_PI_UR_OVERRIDE_FETCH_CONTENT_REPO}")
Expand Down
32 changes: 32 additions & 0 deletions sycl/plugins/unified_runtime/pi2ur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4471,6 +4471,38 @@ inline pi_result piSamplerRelease(pi_sampler Sampler) {
return PI_SUCCESS;
}

pi_result piextSamplerGetNativeHandle(pi_sampler sampler,
pi_native_handle *nativeHandle) {
auto UrSampler = reinterpret_cast<ur_sampler_handle_t>(sampler);

ur_native_handle_t UrNativeHandle{};
HANDLE_ERRORS(urSamplerGetNativeHandle(UrSampler, &UrNativeHandle));

*nativeHandle = reinterpret_cast<pi_native_handle>(UrNativeHandle);

return PI_SUCCESS;
}

pi_result piextSamplerCreateWithNativeHandle(pi_native_handle nativeHandle,
pi_context context,
bool ownNativeHandle,
pi_sampler *sampler) {
ur_native_handle_t UrNativeSampler =
reinterpret_cast<ur_native_handle_t>(nativeHandle);

ur_context_handle_t UrContext =
reinterpret_cast<ur_context_handle_t>(context);

ur_sampler_handle_t *URSampler =
reinterpret_cast<ur_sampler_handle_t *>(sampler);
ur_sampler_native_properties_t Properties{};
Properties.isNativeHandleOwned = ownNativeHandle;
HANDLE_ERRORS(urSamplerCreateWithNativeHandle(UrNativeSampler, UrContext,
&Properties, URSampler));

return PI_SUCCESS;
}

// Sampler
///////////////////////////////////////////////////////////////////////////////

Expand Down
14 changes: 14 additions & 0 deletions sycl/plugins/unified_runtime/pi_unified_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,18 @@ __SYCL_EXPORT pi_result piSamplerRetain(pi_sampler Sampler) {
__SYCL_EXPORT pi_result piSamplerRelease(pi_sampler Sampler) {
return pi2ur::piSamplerRelease(Sampler);
}
/* clang-format off */
__SYCL_EXPORT pi_result piextSamplerGetNativeHandle(
pi_sampler sampler, pi_native_handle *nativeHandle) {
return pi2ur::piextSamplerGetNativeHandle(sampler, nativeHandle);
}
/* clang-format on */
__SYCL_EXPORT pi_result piextSamplerCreateWithNativeHandle(
pi_native_handle nativeHandle, pi_context context,
const bool ownNativeHandle, pi_sampler *sampler) {
return pi2ur::piextSamplerCreateWithNativeHandle(nativeHandle, context,
ownNativeHandle, sampler);
}

__SYCL_EXPORT pi_result piMemImageGetInfo(pi_mem Image, pi_image_info ParamName,
size_t ParamValueSize,
Expand Down Expand Up @@ -1517,6 +1529,8 @@ __SYCL_EXPORT pi_result piPluginInit(pi_plugin *PluginInit) {
_PI_API(piSamplerGetInfo)
_PI_API(piSamplerRetain)
_PI_API(piSamplerRelease)
_PI_API(piextSamplerCreateWithNativeHandle)
_PI_API(piextSamplerGetNativeHandle)

// Peer to Peer
_PI_API(piextEnablePeerAccess)
Expand Down
3 changes: 3 additions & 0 deletions sycl/source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ function(add_sycl_rt_library LIB_NAME LIB_OBJ_NAME)
target_link_libraries(${LIB_OBJ_NAME}
PRIVATE OpenCL-Headers
)
target_link_libraries(${LIB_NAME}
PRIVATE OpenCL::OpenCL
)

if(SYCL_ENABLE_KERNEL_FUSION)
target_link_libraries(${LIB_NAME} PRIVATE sycl-fusion)
Expand Down
24 changes: 23 additions & 1 deletion sycl/source/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ __SYCL_EXPORT device make_device(pi_native_handle NativeHandle,
__SYCL_EXPORT context make_context(pi_native_handle NativeHandle,
const async_handler &Handler,
backend Backend) {
if (Backend == backend::opencl) {
sycl::context Context(detail::pi::cast<cl_context>(NativeHandle), Handler);
return Context;
}

const auto &Plugin = getPlugin(Backend);

pi::PiContext PiContext = nullptr;
Expand Down Expand Up @@ -127,7 +132,11 @@ __SYCL_EXPORT queue make_queue(pi_native_handle NativeHandle,
make_error_code(errc::invalid),
"Queue create using make_queue cannot have compute_index property.");
}

if (Backend == backend::opencl) {
sycl::queue Queue(detail::pi::cast<cl_command_queue>(NativeHandle), Context,
Handler);
return Queue;
}
// Create PI queue first.
pi::PiQueue PiQueue = nullptr;
Plugin->call<PiApiKind::piextQueueCreateWithNativeHandle>(
Expand Down Expand Up @@ -276,6 +285,19 @@ kernel make_kernel(const context &TargetContext,
*KernelBundle.begin();
const auto &DeviceImageImpl = getSyclObjImpl(DeviceImage);
PiProgram = DeviceImageImpl->get_program_ref();
} else if (Backend == backend::opencl) {
cl_kernel CLKernel = detail::pi::cast<cl_kernel>(NativeHandle);
cl_program CLProgram;
size_t Ret = clGetKernelInfo(CLKernel, CL_KERNEL_PROGRAM, sizeof(CLProgram),
&CLProgram, nullptr);
if (Ret != CL_SUCCESS) {
throw runtime_error(
"Failed to retrieve program associated with the kernel",
PI_ERROR_INVALID_KERNEL);
}
Plugin->call<detail::PiApiKind::piextProgramCreateWithNativeHandle>(
detail::pi::cast<pi_native_handle>(CLProgram),
ContextImpl->getHandleRef(), false, &PiProgram);
}

// Create PI kernel first.
Expand Down
30 changes: 27 additions & 3 deletions sycl/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,35 @@ context::context(const std::vector<device> &DeviceList,
PropList);
}
}

context::context(cl_context ClContext, async_handler AsyncHandler) {
const auto &Plugin = sycl::detail::pi::getPlugin<backend::opencl>();
impl = std::make_shared<detail::context_impl>(
detail::pi::cast<sycl::detail::pi::PiContext>(ClContext), AsyncHandler,
Plugin);
uint32_t DeviceCount = 0;
size_t Ret = clGetContextInfo(ClContext, CL_CONTEXT_NUM_DEVICES,
sizeof(DeviceCount), &DeviceCount, nullptr);
if (Ret != CL_SUCCESS) {
throw runtime_error(
"Failed to retrieve device count associated with the context",
PI_ERROR_INVALID_CONTEXT);
}
std::vector<cl_device_id> CLDevices(DeviceCount);
Ret = clGetContextInfo(ClContext, CL_CONTEXT_DEVICES, sizeof(CLDevices),
CLDevices.data(), nullptr);
if (Ret != CL_SUCCESS) {
throw runtime_error(
"Failed to retrieve devices associated with the context",
PI_ERROR_INVALID_CONTEXT);
}
std::vector<sycl::detail::pi::PiDevice> Devices(DeviceCount);
for (uint32_t i = 0; i < DeviceCount; i++) {
Plugin->call<detail::PiApiKind::piextDeviceCreateWithNativeHandle>(
detail::pi::cast<pi_native_handle>(CLDevices[i]), nullptr, &Devices[i]);
}
sycl::detail::pi::PiContext Context;
Plugin->call<detail::PiApiKind::piextContextCreateWithNativeHandle>(
detail::pi::cast<pi_native_handle>(ClContext), DeviceCount,
Devices.data(), false, &Context);
impl = std::make_shared<detail::context_impl>(Context, AsyncHandler, Plugin);
}

template <typename Param>
Expand Down
5 changes: 4 additions & 1 deletion sycl/source/detail/buffer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ void buffer_impl::addInteropObject(
const PluginPtr &Plugin = getPlugin();
Plugin->call<PiApiKind::piMemRetain>(
pi::cast<sycl::detail::pi::PiMem>(MInteropMemObject));
Handles.push_back(pi::cast<pi_native_handle>(MInteropMemObject));
pi_native_handle Handle;
Plugin->call<PiApiKind::piextMemGetNativeHandle>(MInteropMemObject,
nullptr, &Handle);
Handles.push_back(Handle);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions sycl/source/detail/context_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ cl_context context_impl::get() const {
"This instance of context doesn't support OpenCL interoperability.",
PI_ERROR_INVALID_CONTEXT);
}
// TODO catch an exception and put it to list of asynchronous exceptions
getPlugin()->call<PiApiKind::piContextRetain>(MContext);
return pi::cast<cl_context>(MContext);
// TODO catch an exception and put it to list of asynchronous exceptions
pi_native_handle NativeContext;
getPlugin()->call<PiApiKind::piextContextGetNativeHandle>(MContext,
&NativeContext);
return pi::cast<cl_context>(NativeContext);
}

bool context_impl::is_host() const { return MHostContext; }
Expand Down
10 changes: 0 additions & 10 deletions sycl/source/detail/kernel_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ kernel_impl::kernel_impl(sycl::detail::pi::PiKernel Kernel,
MCreatedFromSource(IsCreatedFromSource),
MKernelBundleImpl(std::move(KernelBundleImpl)),
MKernelArgMaskPtr{ArgMask} {

sycl::detail::pi::PiContext Context = nullptr;
// Using the plugin from the passed ContextImpl
getPlugin()->call<PiApiKind::piKernelGetInfo>(
MKernel, PI_KERNEL_INFO_CONTEXT, sizeof(Context), &Context, nullptr);
if (ContextImpl->getHandleRef() != Context)
throw sycl::invalid_parameter_error(
"Input context must be the same as the context of cl_kernel",
PI_ERROR_INVALID_CONTEXT);

MIsInterop = MProgramImpl->isInterop();
}

Expand Down
6 changes: 5 additions & 1 deletion sycl/source/detail/kernel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ class kernel_impl {
PI_ERROR_INVALID_KERNEL);
}
getPlugin()->call<PiApiKind::piKernelRetain>(MKernel);
return pi::cast<cl_kernel>(MKernel);
// TODO catch an exception and put it to list of asynchronous exceptions
pi_native_handle NativeKernel;
getPlugin()->call<PiApiKind::piextKernelGetNativeHandle>(MKernel,
&NativeKernel);
return pi::cast<cl_kernel>(NativeKernel);
}

/// Check if the associated SYCL context is a SYCL host context.
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/platform_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class platform_impl {
"This instance of platform doesn't support OpenCL interoperability.",
PI_ERROR_INVALID_PLATFORM);
}
return pi::cast<cl_platform_id>(MPlatform);
return pi::cast<cl_platform_id>(getNative());
}

/// Returns raw underlying plug-in platform handle.
Expand Down
Loading

0 comments on commit ea513e3

Please sign in to comment.