-
Notifications
You must be signed in to change notification settings - Fork 94
[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
base: SYCLomatic
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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+). | ||
|
@@ -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 | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Positive FeedbackNegative Feedback |
||
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 | ||
|
||
|
There was a problem hiding this comment.
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.
Copilot uses AI. Check for mistakes.