Skip to content

Commit acf286f

Browse files
committed
Change IPC API (SYCL)
Signed-off-by: Larsen, Steffen <[email protected]>
1 parent ce651ce commit acf286f

File tree

7 files changed

+112
-335
lines changed

7 files changed

+112
-335
lines changed

sycl/doc/extensions/experimental/sycl_ext_oneapi_inter_process_communication.asciidoc

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ when the kernel is submitted to the queue.
6666

6767
This extension adds the ability for SYCL programs to share device USM memory
6868
allocations between processes. This is done by the allocating process creating
69-
a new `ipc_memory` object and transferring the "handle data" to the other
70-
processes. The other processes can use the handle data to recreate the
71-
`ipc_memory` object and get a pointer to the corresponding device USM memory.
69+
a new IPC memory handle through the new free frunctions and transferring the
70+
returned handle data to the other processes. The other processes can use the
71+
handle data to retrieve the corresponding device USM memory.
7272

7373

7474
== Specification
@@ -83,10 +83,6 @@ the implementation supports this feature, or applications can test the macro's
8383
value to determine which of the extension's features the implementation
8484
supports.
8585

86-
_And follow the text with a table like this *unless the extension is
87-
"experimental"*. Note that your table may have more than one row if it
88-
has multiple versions._
89-
9086
[%header,cols="1,5"]
9187
|===
9288
|Value
@@ -100,29 +96,22 @@ has multiple versions._
10096
=== Inter-process communicable memory
10197

10298

103-
This extension adds the new `ipc_memory` class. This new class adheres to the
104-
common reference semantics described in
105-
https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#sec:reference-semantics[Section 4.5.2.]
106-
in the SYCL 2020 specification.
99+
This extension adds new free functions under the `ipc_memory` experimental
100+
namespace.
107101

108102
```
109-
namespace sycl::ext::oneapi::experimental {
110-
using ipc_memory_handle_data_t = span<char, sycl::dynamic_extent>;
103+
namespace sycl::ext::oneapi::experimental::ipc_memory {
111104

112-
class ipc_memory {
113-
public:
114-
ipc_memory(void *ptr, sycl::context &ctx);
105+
using handle_data_t = std::vector<char>;
115106

116-
void put();
107+
handle_data_t get(void *ptr, const sycl::context &ctx);
117108

118-
static void *open(ipc_memory_handle_data_t ipc_memory_handle_data,
119-
const sycl::context &ctx, const sycl::device &dev);
120-
static void close(void *ptr, const sycl::context &ctx);
109+
void put(handle_data_t &handle_data, const sycl::context &ctx);
121110

122-
ipc_memory_handle_data_t get_handle_data() const;
111+
static void *open(handle_data_t handle_data, const sycl::context &ctx,
112+
const sycl::device &dev);
123113

124-
void *get_ptr() const;
125-
};
114+
static void close(void *ptr, const sycl::context &ctx);
126115

127116
}
128117
```
@@ -134,53 +123,45 @@ a|
134123
a!
135124
[source]
136125
----
137-
ipc_memory(void *ptr, const sycl::context &ctx)
126+
get(void *ptr, const sycl::context &ctx)
138127
----
139128
!====
140129

141-
_Effects:_ Constructs an IPC memory object in `ctx` from a pointer `ptr` to
142-
device USM memory.
143-
If `ptr` is not pointing to device USM memory, the behaviors of this constructor
144-
and any resulting objects are undefined.
130+
_Returns:_ A `handle_data_t` object containing the data of the IPC memory handle
131+
in `ctx` from a pointer `ptr` to device USM memory.
132+
Calling this function with a `ptr` that does not point to device USM memory, the
133+
behaviors is undefined.
145134

146135
!====
147136
a!
148137
[source]
149138
----
150-
void put()
139+
void put(handle_data_t &handle_data, const sycl::context &ctx)
151140
----
152141
!====
153142

154143
_Effects:_ Instructs the underlying IPC memory resources to be returned to
155-
the backend. This is not required to be called before the `ipc_memory` object
156-
dies. Freeing the device USM memory used when constructing this instance of
157-
`ipc_memory` will return the underlying IPC memory resources.
144+
the backend. Freeing the device USM memory used when the handle data was created
145+
through a call to `get()` will have the same effect as calling this function,
146+
so a direct call to this function is not strictly required.
158147
Calling this function after `sycl::free()` has been called on the device USM
159-
memory used when constructing this instance of `ipc_memory` will result in
160-
undefined behavior.
161-
162-
_Throws:_ A `sycl::exception` with `errc::invalid` if `ipc_memory::put()` has
163-
previously been called on this instance of `ipc_memory`.
148+
memory used when the handle data was created through a call to `get()` will
149+
result in undefined behavior.
164150

165151
!====
166152
a!
167153
[source]
168154
----
169-
static void *open(ipc_memory_handle_data_t ipc_memory_handle_data,
170-
const sycl::context &ctx, const sycl::device &dev)
155+
static void *open(handle_data_t &handle_data, const sycl::context &ctx,
156+
const sycl::device &dev)
171157
----
172158
!====
173159

174160
_Effects:_ Returns a pointer to the same device USM memory as the device USM
175-
memory associated with the `ipc_memory` object that the handle data originated
176-
from.
177-
The `ipc_memory` object that the handle data originated from is allowed to be
178-
from another process on the host system.
179-
If the `ipc_memory` object that the handle data originated from has been
180-
destroyed, the behaviors of this constructor and any resulting objects are
181-
undefined.
182-
If the device USM memory the original `ipc_memory` object was created with was
183-
not originally allocated on `dev`, the behaviors of this function is undefined.
161+
memory associated with `handle_data`.
162+
The handle data is allowed to be from another process on the host system.
163+
If the handle data has been destroyed, calling this function results in
164+
undefined behavior.
184165

185166
!====
186167
a!
@@ -191,35 +172,7 @@ static void close(void *ptr, const sycl::context &ctx)
191172
!====
192173

193174
_Effects:_ Closes a device USM pointer previously returned by a call to
194-
`ipc_memory::open()`.
195-
196-
!====
197-
a!
198-
[source]
199-
----
200-
ipc_memory_handle_data_t get_handle_data() const
201-
----
202-
!====
203-
204-
_Returns:_ The handle data of the `ipc_memory` object.
205-
Utilizing the handle data returned by this API after the `ipc_memory` object has
206-
been destroyed results in undefined behavior.
207-
208-
_Throws:_ A `sycl::exception` with `errc::invalid` if `ipc_memory::put()` has
209-
previously been called on this instance of `ipc_memory`.
210-
211-
!====
212-
a!
213-
[source]
214-
----
215-
void *get_ptr() const
216-
----
217-
!====
218-
219-
_Returns:_ A pointer to device USM memory corresponding to the pointer used to
220-
construct the original `ipc_memory` object.
221-
Accessing the pointer returned by this API after the `ipc_memory` object has
222-
been destroyed results in undefined behavior.
175+
`open()`.
223176

224177
|====
225178

sycl/include/sycl/ext/oneapi/experimental/ipc_memory.hpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,19 @@ inline namespace _V1 {
2121
class context;
2222
class device;
2323

24-
namespace detail {
25-
class ipc_memory_impl;
26-
}
24+
namespace ext::oneapi::experimental::ipc_memory {
2725

28-
namespace ext::oneapi::experimental {
29-
using ipc_memory_handle_data_t = span<char, sycl::dynamic_extent>;
26+
using handle_data_t = std::vector<char>;
3027

31-
class __SYCL_EXPORT ipc_memory
32-
: public sycl::detail::OwnerLessBase<ipc_memory> {
33-
public:
34-
ipc_memory(void *Ptr, const sycl::context &Ctx);
28+
__SYCL_EXPORT handle_data_t get(void *Ptr, const sycl::context &Ctx);
3529

36-
void put();
30+
__SYCL_EXPORT void put(handle_data_t &HandleData, const sycl::context &Ctx);
3731

38-
static void *open(ipc_memory_handle_data_t IPCMemoryHandleData,
39-
const sycl::context &Ctx, const sycl::device &Dev);
40-
static void close(void *Ptr, const sycl::context &Ctx);
32+
__SYCL_EXPORT void *open(handle_data_t &HandleData, const sycl::context &Ctx,
33+
const sycl::device &Dev);
4134

42-
ipc_memory_handle_data_t get_handle_data() const;
35+
__SYCL_EXPORT void close(void *Ptr, const sycl::context &Ctx);
4336

44-
void *get_ptr() const;
45-
46-
private:
47-
ipc_memory(std::shared_ptr<sycl::detail::ipc_memory_impl> IPCMemImpl)
48-
: impl{IPCMemImpl} {}
49-
50-
std::shared_ptr<sycl::detail::ipc_memory_impl> impl;
51-
52-
template <class Obj>
53-
friend const decltype(Obj::impl) &
54-
sycl::detail::getSyclObjImpl(const Obj &SyclObject);
55-
56-
template <class T>
57-
friend T sycl::detail::createSyclObjFromImpl(
58-
std::add_rvalue_reference_t<decltype(T::impl)> ImplObj);
59-
template <class T>
60-
friend T sycl::detail::createSyclObjFromImpl(
61-
std::add_lvalue_reference_t<const decltype(T::impl)> ImplObj);
62-
};
63-
} // namespace ext::oneapi::experimental
37+
} // namespace ext::oneapi::experimental::ipc_memory
6438
} // namespace _V1
6539
} // namespace sycl

sycl/source/detail/ipc_memory_impl.hpp

Lines changed: 0 additions & 90 deletions
This file was deleted.

sycl/source/ipc_memory.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,59 @@
88

99
#include <detail/adapter_impl.hpp>
1010
#include <detail/context_impl.hpp>
11-
#include <detail/ipc_memory_impl.hpp>
1211
#include <sycl/context.hpp>
1312
#include <sycl/ext/oneapi/experimental/ipc_memory.hpp>
1413

1514
namespace sycl {
1615
inline namespace _V1 {
17-
namespace ext::oneapi::experimental {
16+
namespace ext::oneapi::experimental::ipc_memory {
1817

19-
ipc_memory::ipc_memory(void *Ptr, const sycl::context &Ctx)
20-
: impl(detail::ipc_memory_impl::create(Ptr, Ctx)) {}
18+
__SYCL_EXPORT handle_data_t get(void *Ptr, const sycl::context &Ctx) {
19+
auto CtxImpl = sycl::detail::getSyclObjImpl(Ctx);
20+
sycl::detail::adapter_impl &Adapter = CtxImpl->getAdapter();
21+
22+
size_t HandleSize = 0;
23+
Adapter.call<sycl::detail::UrApiKind::urIPCGetMemHandleExp>(
24+
CtxImpl->getHandleRef(), Ptr, nullptr, &HandleSize);
2125

22-
void ipc_memory::put() { impl->put(); }
26+
handle_data_t Res(HandleSize);
27+
Adapter.call<sycl::detail::UrApiKind::urIPCGetMemHandleExp>(
28+
CtxImpl->getHandleRef(), Ptr, Res.data(), nullptr);
29+
return Res;
30+
}
2331

24-
void *ipc_memory::open(ipc_memory_handle_data_t IPCMemoryHandleData,
25-
const sycl::context &Ctx, const sycl::device &Dev) {
32+
__SYCL_EXPORT void put(handle_data_t &HandleData, const sycl::context &Ctx) {
33+
auto CtxImpl = sycl::detail::getSyclObjImpl(Ctx);
34+
sycl::detail::adapter_impl &Adapter = CtxImpl->getAdapter();
35+
Adapter.call<sycl::detail::UrApiKind::urIPCPutMemHandleExp>(
36+
CtxImpl->getHandleRef(), HandleData.data());
37+
}
38+
39+
__SYCL_EXPORT void *open(handle_data_t &HandleData, const sycl::context &Ctx,
40+
const sycl::device &Dev) {
2641
auto CtxImpl = sycl::detail::getSyclObjImpl(Ctx);
2742
sycl::detail::adapter_impl &Adapter = CtxImpl->getAdapter();
2843

2944
void *Ptr = nullptr;
3045
ur_result_t UrRes =
3146
Adapter.call_nocheck<sycl::detail::UrApiKind::urIPCOpenMemHandleExp>(
3247
CtxImpl->getHandleRef(), getSyclObjImpl(Dev)->getHandleRef(),
33-
IPCMemoryHandleData.data(), IPCMemoryHandleData.size(), &Ptr);
48+
HandleData.data(), HandleData.size(), &Ptr);
3449
if (UrRes == UR_RESULT_ERROR_INVALID_VALUE)
3550
throw sycl::exception(sycl::make_error_code(errc::invalid),
36-
"IPCMemoryHandleData data size does not correspond "
51+
"HandleData data size does not correspond "
3752
"to the target platform's IPC memory handle size.");
3853
Adapter.checkUrResult(UrRes);
3954
return Ptr;
4055
}
4156

42-
void ipc_memory::close(void *Ptr, const sycl::context &Ctx) {
57+
__SYCL_EXPORT void close(void *Ptr, const sycl::context &Ctx) {
4358
auto CtxImpl = sycl::detail::getSyclObjImpl(Ctx);
4459
sycl::detail::adapter_impl &Adapter = CtxImpl->getAdapter();
4560
Adapter.call<sycl::detail::UrApiKind::urIPCCloseMemHandleExp>(
4661
CtxImpl->getHandleRef(), Ptr);
4762
}
4863

49-
ipc_memory_handle_data_t ipc_memory::get_handle_data() const {
50-
return impl->get_handle_data();
51-
}
52-
53-
void *ipc_memory::get_ptr() const { return impl->get_ptr(); }
54-
55-
} // namespace ext::oneapi::experimental
64+
} // namespace ext::oneapi::experimental::ipc_memory
5665
} // namespace _V1
5766
} // namespace sycl

0 commit comments

Comments
 (0)