Skip to content

[SYCLomaitc] Migrate 2 CUDA IPC code to level zero APIs. #2818

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

Open
wants to merge 3 commits into
base: SYCLomatic
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions clang/lib/DPCT/RuleInfra/MapNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ void MapNames::setExplicitNamespaceMap(
{"cudaIpcMemHandle_t",
std::make_shared<TypeNameRule>(getDpctNamespace() +
"experimental::ipc_mem_handle_ext_t")},
{"cudaIpcEventHandle_t",
std::make_shared<TypeNameRule>(
getDpctNamespace() + "experimental::ipc_event_pool_handle_ext_t")},
{"char1", std::make_shared<TypeNameRule>("int8_t")},
{"char2", std::make_shared<TypeNameRule>(getClNamespace() + "char2")},
{"char3", std::make_shared<TypeNameRule>(getClNamespace() + "char3")},
Expand Down
26 changes: 26 additions & 0 deletions clang/lib/DPCT/RulesLang/APINamesMisc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,29 @@ ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
ARG("cudaIpcOpenMemHandle"),
ARG("--use-experimental-features=level_zero")))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CONDITIONAL_FACTORY_ENTRY(
clang::dpct::UseExtLevelZero,
CALL_FACTORY_ENTRY("cudaIpcOpenEventHandle",
CALL(MapNames::getDpctNamespace() +
"experimental::open_event_pool_ipc_handle",
ARG(0), ARG(1))),
UNSUPPORT_FACTORY_ENTRY(
"cudaIpcOpenEventHandle", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
ARG("cudaIpcOpenEventHandle"),
ARG("--use-experimental-features=level_zero")))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CONDITIONAL_FACTORY_ENTRY(
clang::dpct::UseExtLevelZero,
CALL_FACTORY_ENTRY("cudaIpcGetEventHandle",
CALL(MapNames::getDpctNamespace() +
"experimental::get_event_pool_ipc_handle",
ARG(1), ARG(0))),
UNSUPPORT_FACTORY_ENTRY(
"cudaIpcGetEventHandle", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
ARG("cudaIpcGetEventHandle"),
ARG("--use-experimental-features=level_zero")))))
10 changes: 9 additions & 1 deletion clang/lib/DPCT/RulesLang/RulesLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ void TypeInDeclRule::registerMatcher(MatchFinder &MF) {
"cudaGraphicsRegisterFlags", "cudaExternalMemoryHandleType",
"cudaExternalSemaphoreHandleType", "CUstreamCallback",
"cudaHostFn_t", "__nv_half2", "__nv_half", "cudaGraphNodeType",
"CUsurfref", "CUdevice_P2PAttribute", "cudaIpcMemHandle_t"))))))
"CUsurfref", "CUdevice_P2PAttribute", "cudaIpcMemHandle_t",
"cudaIpcEventHandle_t"))))))
.bind("cudaTypeDef"),
this);

Expand Down Expand Up @@ -935,6 +936,13 @@ void TypeInDeclRule::runRule(const MatchFinder::MatchResult &Result) {
return;
}
}
if (CanonicalTypeStr == "cudaIpcEventHandle_st") {
if (!DpctGlobalInfo::useExtLevelZero()) {
report(TL->getBeginLoc(), Diagnostics::TRY_EXPERIMENTAL_FEATURE, false,
"cudaIpcMemHandle_t", "--use-experimental-features=level_zero");
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The report message within this block uses 'cudaIpcMemHandle_t' instead of 'cudaIpcEventHandle_t', which may confuse users expecting the correct type name. Please update the string to 'cudaIpcEventHandle_t' to reflect the correct experimental feature.

Suggested change
"cudaIpcMemHandle_t", "--use-experimental-features=level_zero");
"cudaIpcEventHandle_t", "--use-experimental-features=level_zero");

Copilot uses AI. Check for mistakes.

return;
}
}

if (CanonicalTypeStr == "cudaGraphExecUpdateResult") {
report(TL->getBeginLoc(), Diagnostics::API_NOT_MIGRATED, false,
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/DPCT/SrcAPI/APINames.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1658,9 +1658,9 @@ ENTRY(cuDeviceGetPCIBusId, cuDeviceGetPCIBusId, false, NO_FLAG, P4, "comment")
ENTRY(cuDeviceRegisterAsyncNotification, cuDeviceRegisterAsyncNotification, false, NO_FLAG, P4, "comment")
ENTRY(cuDeviceUnregisterAsyncNotification, cuDeviceUnregisterAsyncNotification, false, NO_FLAG, P4, "comment")
ENTRY(cuIpcCloseMemHandle, cuIpcCloseMemHandle, false, NO_FLAG, P4, "comment")
ENTRY(cuIpcGetEventHandle, cuIpcGetEventHandle, false, NO_FLAG, P4, "comment")
ENTRY(cuIpcGetEventHandle, cuIpcGetEventHandle, true, NO_FLAG, P4, "DPCT1119")
ENTRY(cuIpcGetMemHandle, cuIpcGetMemHandle, false, NO_FLAG, P4, "comment")
ENTRY(cuIpcOpenEventHandle, cuIpcOpenEventHandle, false, NO_FLAG, P4, "comment")
ENTRY(cuIpcOpenEventHandle, cuIpcOpenEventHandle, true, NO_FLAG, P4, "DPCT1119")
ENTRY(cuIpcOpenMemHandle, cuIpcOpenMemHandle, false, NO_FLAG, P4, "comment")
ENTRY(cuMemAlloc, cuMemAlloc_v2, true, NO_FLAG, P4, "Successful")
ENTRY(cuMemAllocHost, cuMemAllocHost_v2, true, NO_FLAG, P4, "comment")
Expand Down
66 changes: 65 additions & 1 deletion clang/runtime/dpct-rt/include/dpct/ze_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ struct ipc_mem_handle_ext_t {
ze_ipc_mem_handle_t handle;
};

struct ipc_event_pool_handle_ext_t {
pid_t pid;
ze_ipc_event_pool_handle_t handle;
};

namespace detail {

#ifndef _SYS_pidfd_open
Expand All @@ -33,7 +38,7 @@ namespace detail {
#define _SYS_pidfd_getfd 438 // syscall number for pidfd_getfd
#endif

inline int get_fd_of_peer_process(ipc_mem_handle_ext_t ext_handle) {
template <class T> inline int get_fd_of_peer_process(T ext_handle) {
int pidfd = syscall(_SYS_pidfd_open, ext_handle.pid,
0); // obtain a file descriptor that refers to a
// process(requires kernel 5.6+).
Expand All @@ -43,6 +48,35 @@ inline int get_fd_of_peer_process(ipc_mem_handle_ext_t ext_handle) {
0); // obtain a duplicate of another process's file
// descriptor(requires kernel 5.6+).
}
constexpr ze_event_pool_desc_t default_event_pool_desc = {
.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC,
.pNext = nullptr,
.flags = ZE_EVENT_POOL_FLAG_IPC | ZE_EVENT_POOL_FLAG_HOST_VISIBLE,
.count = 1};

constexpr ze_event_desc_t default_event_desc = {
.stype = ZE_STRUCTURE_TYPE_EVENT_DESC, .index = 0, .signal = 0, .wait = 0};

ze_event_pool_handle_t create_event_in_pool(sycl::event *event) {
ze_event_pool_handle_t h_event_pool = {};
auto context = dpct::get_current_device().get_context();

if (h_event_pool == nullptr) {
ze_device_handle_t device =
sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
(sycl::device)dpct::get_current_device());
zeEventPoolCreate(
sycl::get_native<sycl::backend::ext_oneapi_level_zero>(context),
&default_event_pool_desc, 1, &device, &h_event_pool);
}

ze_event_handle_t ze_event = {};
zeEventCreate(h_event_pool, &default_event_desc, &ze_event);
zeEventHostReset(ze_event);
*event = sycl::make_event<sycl::backend::ext_oneapi_level_zero>(
{ze_event, sycl::ext::oneapi::level_zero::ownership::keep}, context);
return h_event_pool;
}

} // namespace detail

Expand Down Expand Up @@ -76,6 +110,36 @@ inline ze_result_t open_mem_ipc_handle(ipc_mem_handle_ext_t ext_handle,
ext_handle.handle, 0u, pptr);
}

/// Gets an IPC event pool handle for the specified event handle that can be shared with another process.
/// \param [in] ext_handle IPC memory handle extension
/// \param [out] phipc Returned IPC event handle
ze_result_t get_event_pool_ipc_handle(sycl::event *event,
ipc_event_pool_handle_ext_t *phipc) {
phipc->pid = getpid();
ze_event_pool_handle_t h_event_pool = detail::create_event_in_pool(event);
return zeEventPoolGetIpcHandle(h_event_pool, &phipc->handle);
}

ze_result_t open_event_pool_ipc_handle(sycl::event **event,
ipc_event_pool_handle_ext_t hipc) {
ze_context_handle_t ze_context =
sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
dpct::get_current_device().get_context());
int fd = detail::get_fd_of_peer_process(hipc);
if (fd < 0)
throw std::runtime_error("Cannot get file descriptor of peer process.");
*((int *)hipc.handle.data) = detail::get_fd_of_peer_process(hipc);
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting hipc.handle.data to an int pointer directly might lead to issues if the underlying type of handle.data is not guaranteed to store an integer file descriptor. Consider using a safer method, like memcpy or a static_cast/reinterpret_cast with appropriate validation.

Copilot uses AI. Check for mistakes.

ze_event_handle_t ze_event = {};
ze_event_pool_handle_t h_event_pool_t;
auto ret = zeEventPoolOpenIpcHandle(ze_context, hipc.handle, &h_event_pool_t);
zeEventCreate(h_event_pool_t, &detail::default_event_desc, &ze_event);
zeEventHostSignal(ze_event);
*event =
new sycl::event(sycl::make_event<sycl::backend::ext_oneapi_level_zero>(
{ze_event, sycl::ext::oneapi::level_zero::ownership::keep},
dpct::get_current_device().get_context()));
return ret;
}
} // namespace experimental
} // namespace dpct

Expand Down
Loading
Loading