okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- ze_utils.hpp ---------------------------------*- C++ 2//-*----------------==// 3// 4// Copyright (C) Intel Corporation 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// See https://llvm.org/LICENSE.txt for license information. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef __DPCT_ZE_UTILS_HPP__ 11#define __DPCT_ZE_UTILS_HPP__ 12 13#ifdef ONEAPI_BACKEND_LEVEL_ZERO_EXT 14#if defined(__linux__ ) 15#include <level_zero/ze_api.h> 16#include <sycl/ext/oneapi/backend/level_zero.hpp> 17#include <sycl/sycl.hpp> 18namespace dpct { 19namespace experimental { 20 21struct ipc_mem_handle_ext_t { 22pid_t pid ; 23ze_ipc_mem_handle_t handle ; 24}; 25 26namespace detail { 27 28#ifndef _SYS_pidfd_open 29#define _SYS_pidfd_open 434 // syscall number for pidfd_open 30#endif 31 32#ifndef _SYS_pidfd_getfd 33#define _SYS_pidfd_getfd 438 // syscall number for pidfd_getfd 34#endif 35 36inline int get_fd_of_peer_process (ipc_mem_handle_ext_t ext_handle ) { 37int pidfd = syscall (_SYS_pidfd_open ,ext_handle .pid , 380 );// obtain a file descriptor that refers to a 39// process(requires kernel 5.6+). 40if (pidfd < 0 ) 41return -1 ; 42return syscall (_SYS_pidfd_getfd ,pidfd ,* (int * )ext_handle .handle .data , 430 );// obtain a duplicate of another process's file 44// descriptor(requires kernel 5.6+). 45} 46 47}// namespace detail 48 49/// Creates an IPC memory handle for the specified allocation. 50/// \param [in] ptr Pointer to the device memory allocation 51/// \param [out] ext_handle_ptr IPC memory handle extension 52inline ze_result_t get_mem_ipc_handle (const void * ptr , 53ipc_mem_handle_ext_t * ext_handle_ptr ) { 54ext_handle_ptr -> pid = getpid (); 55return zeMemGetIpcHandle ( 56 sycl::get_native < sycl::backend::ext_oneapi_level_zero > ( 57 dpct::get_current_device ().get_context ()), 58ptr ,& ext_handle_ptr -> handle ); 59} 60 61/// Opens an IPC memory handle to retrieve a device pointer. 62/// \param [in] ext_handle IPC memory handle extension 63/// \param [out] pptr Pointer to device allocation in this process 64inline ze_result_t open_mem_ipc_handle (ipc_mem_handle_ext_t ext_handle , 65void ** pptr ) { 66int fd = detail::get_fd_of_peer_process (ext_handle ); 67if (fd < 0 ) 68throw std::runtime_error ("Cannot get file descriptor of peer process." ); 69* ((int * )ext_handle .handle .data )= fd ; 70 71return zeMemOpenIpcHandle ( 72 sycl::get_native < sycl::backend::ext_oneapi_level_zero > ( 73 dpct::get_current_device ().get_context ()), 74 sycl::get_native < sycl::backend::ext_oneapi_level_zero > ( 75 (sycl::device )dpct::get_current_device ()), 76ext_handle .handle ,0u ,pptr ); 77} 78 79}// namespace experimental 80}// namespace dpct 81 82#endif // __linux__ 83#endif // ONEAPI_BACKEND_LEVEL_ZERO_EXT 84 85#endif // ! __DPCT_ZE_UTILS_HPP__