Skip to content

Commit b65cdd9

Browse files
committed
update
Signed-off-by: Chen, Sheng S <sheng.s.chen@intel.com>
1 parent cf20d74 commit b65cdd9

3 files changed

Lines changed: 41 additions & 47 deletions

File tree

clang/lib/DPCT/RulesLang/APINamesMisc.inc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,25 @@ ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
148148
CALL(MapNames::getDpctNamespace() +
149149
"experimental::get_mem_ipc_handle",
150150
ARG(1), ARG(0))),
151-
UNSUPPORT_FACTORY_ENTRY("cudaIpcGetMemHandle",
152-
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
153-
ARG("cudaIpcGetMemHandle"),
154-
ARG("--use-experimental-features=level_zero")))))
151+
UNSUPPORT_FACTORY_ENTRY(
152+
"cudaIpcGetMemHandle", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
153+
ARG("cudaIpcGetMemHandle"),
154+
ARG("--use-experimental-features=level_zero")))))
155155

156156
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
157157
HelperFeatureEnum::device_ext,
158158
CONDITIONAL_FACTORY_ENTRY(
159159
clang::dpct::UseExtL0IPC,
160160
CALL_FACTORY_ENTRY("cudaIpcCloseMemHandle",
161-
CALL(MapNames::getDpctNamespace() +
162-
"experimental::close_mem_ipc_handle",
161+
CALL("zeMemCloseIpcHandle",
162+
CALL("sycl::get_native<sycl::backend::ext_oneapi_level_zero>", MEMBER_CALL(CALL(MapNames::getDpctNamespace() +
163+
"get_current_device"),
164+
false, "get_context")),
163165
ARG(0))),
164-
UNSUPPORT_FACTORY_ENTRY("cudaIpcCloseMemHandle",
165-
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
166-
ARG("cudaIpcCloseMemHandle"),
167-
ARG("--use-experimental-features=level_zero")))))
166+
UNSUPPORT_FACTORY_ENTRY(
167+
"cudaIpcCloseMemHandle", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
168+
ARG("cudaIpcCloseMemHandle"),
169+
ARG("--use-experimental-features=level_zero")))))
168170

169171
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
170172
HelperFeatureEnum::device_ext,

clang/runtime/dpct-rt/include/dpct/ze_utils.hpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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
@@ -11,12 +12,13 @@
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>
1718
namespace dpct {
1819
namespace 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+
3245
struct 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()),

clang/test/dpct/IPC/share_mem_exp_option.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int parentProcess(char *app)
150150
}
151151
}
152152
std::cout << "verified Pass.\n";
153-
// CHECK: dpct::experimental::close_mem_ipc_handle(ptr);
153+
// CHECK: zeMemCloseIpcHandle(sycl::get_native<sycl::backend::ext_oneapi_level_zero>(dpct::get_current_device().get_context()), ptr);
154154
cudaIpcCloseMemHandle(ptr);
155155
return 0;
156156
}

0 commit comments

Comments
 (0)