@@ -20,16 +20,24 @@ namespace experimental {
2020
2121namespace detail {
2222
23- // / Covert remote fd to the local fd through IPC handle extension.
24- // / \param [in] ipc_ext_handle The extension of the IPC handle
25- // / \returns Local process file descriptor
26- template <class T > int convert_fd_from_handle (T ipc_ext_handle) {
27- int pidfd = syscall (434 , ipc_ext_handle.pid ,
23+ #ifndef _SYS_pidfd_open
24+ #define _SYS_pidfd_open 434 // syscall number for pidfd_open
25+ #endif
26+
27+ #ifndef _SYS_pidfd_getfd
28+ #define _SYS_pidfd_getfd 438 // syscall number for pidfd_getfd
29+ #endif
30+
31+ // / Obtain a duplicate of another process's file descriptor.
32+ // / \param [in] ext_handle IPC memory handle extension
33+ // / \returns obtained file descriptor
34+ template <class T > int get_fd_of_peer_process (T ext_handle) {
35+ int pidfd = syscall (_SYS_pidfd_open, ext_handle.pid ,
2836 0 ); // obtain a file descriptor that refers to a
2937 // process(requires kernel 5.6+).
3038 if (pidfd < 0 )
3139 return -1 ;
32- return syscall (438 , pidfd, *(int *)ipc_ext_handle .handle .data ,
40+ return syscall (_SYS_pidfd_getfd , pidfd, *(int *)ext_handle .handle .data ,
3341 0 ); // obtain a duplicate of another process's file
3442 // descriptor(requires kernel 5.6+).
3543}
@@ -41,35 +49,37 @@ struct ipc_mem_handle_ext_t {
4149 ze_ipc_mem_handle_t handle;
4250};
4351
44- // / Acquires IPC handle for shared memory region.
45- // / \param [in] ptr Pointer to shared memory region
46- // / \param [out] handle_ptr Output IPC handle
47- // / \returns Level Zero operation status code
48- inline ze_result_t get_mem_ipc_handle (const void *ptr,
49- ipc_mem_handle_ext_t *handle_ptr) {
50- handle_ptr->pid = getpid ();
51- return zeMemGetIpcHandle (
52- sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
53- dpct::get_current_device ().get_context ()),
54- ptr, &handle_ptr->handle );
52+ // / Creates an IPC memory handle for the specified allocation.
53+ // / \param [in] ptr Pointer to the device memory allocation
54+ // / \param [out] ext_handle_ptr IPC memory handle extension
55+ inline void get_mem_ipc_handle (const void *ptr,
56+ ipc_mem_handle_ext_t *ext_handle_ptr) {
57+ ext_handle_ptr->pid = getpid ();
58+ auto ret =
59+ zeMemGetIpcHandle (sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
60+ dpct::get_current_device ().get_context ()),
61+ ptr, &ext_handle_ptr->handle );
62+ if (ret != ZE_RESULT_SUCCESS )
63+ throw std::runtime_error (" The zeMemGetIpcHandle execution failed." );
5564}
5665
57- // / Maps remote IPC memory to local address space .
58- // / \param [in] ipc_ext_handle The extension of the IPC handle
59- // / \param [out] ptr Mapped memory pointer in local process
66+ // / Opens an IPC memory handle to retrieve a device pointer .
67+ // / \param [in] ext_handle IPC memory handle extension
68+ // / \param [out] pptr Pointer to device allocation in this process
6069// / \returns Level Zero operation status code
61- inline ze_result_t open_mem_ipc_handle (ipc_mem_handle_ext_t ipc_ext_handle,
62- void **ptr) {
63- int newfd = detail::convert_fd_from_handle (ipc_ext_handle);
64- if (newfd < 0 )
65- throw std::runtime_error (" Cannot convert fd from handle" );
66- *((int *)ipc_ext_handle.handle .data ) = newfd;
67- return zeMemOpenIpcHandle (
68- sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
69- dpct::get_current_device ().get_context ()),
70- sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
71- (sycl::device)dpct::get_current_device ()),
72- ipc_ext_handle.handle , 0u , ptr);
70+ inline void open_mem_ipc_handle (ipc_mem_handle_ext_t ext_handle, void **pptr) {
71+ int fd = detail::get_fd_of_peer_process (ext_handle);
72+ if (fd < 0 )
73+ throw std::runtime_error (" Cannot get file descriptor of peer process." );
74+ *((int *)ext_handle.handle .data ) = fd;
75+ auto ret =
76+ zeMemOpenIpcHandle (sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
77+ dpct::get_current_device ().get_context ()),
78+ sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
79+ (sycl::device)dpct::get_current_device ()),
80+ ext_handle.handle , 0u , pptr);
81+ if (ret != ZE_RESULT_SUCCESS )
82+ throw std::runtime_error (" The zeMemOpenIpcHandle execution failed." );
7383}
7484
7585} // namespace experimental
0 commit comments