1- // ==---- ze_util.hpp ---------------------------------*- C++ -*----------------==//
1+ // ==---- ze_utils.hpp ---------------------------------*- C++
2+ // -*----------------==//
23//
34// Copyright (C) Intel Corporation
45// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1112
1213#ifdef ONEAPI_BACKEND_LEVEL_ZERO_EXT
1314#if defined(__linux__)
14- #include " level_zero/ze_api.h"
15- #include " sycl/ext/oneapi/backend/level_zero.hpp"
15+ #include < level_zero/ze_api.h>
16+ #include < sycl/ext/oneapi/backend/level_zero.hpp>
1617#include < sycl/sycl.hpp>
1718namespace dpct {
1819namespace experimental {
1920
21+ namespace detail {
2022// / System call number definitions for kernel compatibility.
2123// / SYS_pidfd_open: Process file descriptor opener (requires kernel 5.6+).
2224// / SYS_pidfd_getfd: Cross-process FD fetcher system call.
@@ -28,56 +30,46 @@ namespace experimental {
2830#define SYS_pidfd_getfd 438
2931#endif
3032
31- // / Process id and IPC memory handle structure for cross-process sharing.
33+ // / Covert remote fd to the local fd through IPC handle extension.
34+ // / \param [in] ipc_ext_handle The extension of the IPC handle
35+ // / \returns Local process file descriptor
36+ template <class T > int convert_fd_from_handle (T ipc_ext_handle) {
37+ int pidfd = syscall (SYS_pidfd_open, ipc_ext_handle.pid , 0 );
38+ if (pidfd < 0 )
39+ return -1 ;
40+ return syscall (SYS_pidfd_getfd, pidfd, *(int *)ipc_ext_handle.handle .data , 0 );
41+ }
42+
43+ } // namespace detail
44+
3245struct ipc_mem_handle_ext_t {
3346 pid_t pid;
3447 ze_ipc_mem_handle_t handle;
3548};
3649
3750// / Acquires IPC handle for shared memory region.
3851// / \param [in] ptr Pointer to shared memory region
39- // / \param [out] phipc Output IPC handle
52+ // / \param [out] handle_ptr Output IPC handle
4053// / \returns Level Zero operation status code
41- ze_result_t get_mem_ipc_handle (const void *ptr,
42- ipc_mem_handle_ext_t *ipc_ext_handle ) {
43- ipc_ext_handle ->pid = getpid ();
54+ inline ze_result_t get_mem_ipc_handle (const void *ptr,
55+ ipc_mem_handle_ext_t *handle_ptr ) {
56+ handle_ptr ->pid = getpid ();
4457 return zeMemGetIpcHandle (
4558 sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
4659 dpct::get_current_device ().get_context ()),
47- ptr, &ipc_ext_handle->handle );
48- }
49-
50- // / Releases resources associated with IPC handle.
51- // / \param [in] ptr Pointer to shared memory region
52- // / \returns Level Zero operation status code
53- ze_result_t close_mem_ipc_handle (const void *ptr) {
54- if (ptr == nullptr ) {
55- return ZE_RESULT_SUCCESS ;
56- }
57- return zeMemCloseIpcHandle (
58- sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
59- dpct::get_current_device ().get_context ()),
60- (char *)ptr);
61- }
62-
63- // / Covert remote fd to the local fd through the IPC handle extension.
64- // / \param [in] ipc_ext_handle The extension of the IPC handle
65- // / \returns Local process file descriptor
66- template <class T > int convert_fd_pidfd_from_handle (T ipc_ext_handle) {
67- int pidfd = syscall (SYS_pidfd_open, ipc_ext_handle.pid , 0 );
68- int fd;
69- memcpy (&fd, (void *)&ipc_ext_handle.handle .data , sizeof (int ));
70- return syscall (SYS_pidfd_getfd, pidfd, fd, 0 );
60+ ptr, &handle_ptr->handle );
7161}
7262
7363// / Maps remote IPC memory to local address space.
7464// / \param [in] ipc_ext_handle The extension of the IPC handle
7565// / \param [out] ptr Mapped memory pointer in local process
7666// / \returns Level Zero operation status code
77- ze_result_t open_mem_ipc_handle (ipc_mem_handle_ext_t ipc_ext_handle,
78- void **ptr) {
79- int newfd = convert_fd_pidfd_from_handle (ipc_ext_handle);
80- memcpy (&ipc_ext_handle.handle .data , &newfd, sizeof (newfd));
67+ inline ze_result_t open_mem_ipc_handle (ipc_mem_handle_ext_t ipc_ext_handle,
68+ void **ptr) {
69+ int newfd = detail::convert_fd_from_handle (ipc_ext_handle);
70+ if (newfd < 0 )
71+ throw std::runtime_error (" Cannot convert fd from handle" );
72+ *((int *)ipc_ext_handle.handle .data ) = newfd;
8173 return zeMemOpenIpcHandle (
8274 sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
8375 dpct::get_current_device ().get_context ()),
0 commit comments