UCX++
memory.h
1#pragma once
2
3#include <cstdint>
4#include <memory>
5
6#include "ucxpp/awaitable.h"
7#include "ucxpp/context.h"
8
9#include "ucxpp/detail/noncopyable.h"
10
11namespace ucxpp {
12
13class endpoint;
14
20 friend class local_memory_handle;
21 void *buffer_;
22 size_t length_;
23 packed_memory_rkey(void *buffer, size_t length);
24
25public:
27 void *get_buffer();
28 void const *get_buffer() const;
29 size_t get_length() const;
31};
32
38 std::shared_ptr<context> ctx_;
39 ucp_mem_h mem_;
40
41public:
48 local_memory_handle(std::shared_ptr<context> ctx, ucp_mem_h mem);
49
56
65 static std::pair<void *, local_memory_handle>
66 allocate_mem(std::shared_ptr<context> ctx, size_t length);
67 static local_memory_handle register_mem(std::shared_ptr<context> ctx,
68 void *address, size_t length);
69
75 ucp_mem_h handle() const;
76
84
91};
92
99 std::shared_ptr<endpoint> endpoint_;
100 ucp_ep_h ep_;
101 ucp_rkey_h rkey_;
102
103public:
112 remote_memory_handle(std::shared_ptr<endpoint> endpoint,
113 void const *packed_rkey_buffer);
114
121
130 rma_put_awaitable put(void const *buffer, size_t length,
131 uint64_t remote_addr) const;
132
141 rma_get_awaitable get(void *buffer, size_t length,
142 uint64_t remote_addr) const;
143
148 rma_put_awaitable write(void const *buffer, size_t length,
149 uint64_t remote_addr) const;
150
155 rma_get_awaitable read(void *buffer, size_t length,
156 uint64_t remote_addr) const;
157
163 std::shared_ptr<endpoint> endpoint_ptr() const;
164
170 ucp_rkey_h handle() const;
171
183 template <class T>
184 rma_atomic_awaitable<T> atomic_fetch_add(uint64_t remote_addr, T const &delta,
185 T &old_value) const {
186 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_ADD, &delta, remote_addr,
187 rkey_, &old_value);
188 }
189
201 template <class T>
202 rma_atomic_awaitable<T> atomic_fetch_and(uint64_t remote_addr, T const &bits,
203 T &old_value) const {
204 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_AND, &bits, remote_addr,
205 rkey_, &old_value);
206 }
207
219 template <class T>
220 rma_atomic_awaitable<T> atomic_fetch_or(uint64_t remote_addr, T const &bits,
221 T &old_value) const {
222 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_OR, &bits, remote_addr,
223 rkey_, &old_value);
224 }
225
237 template <class T>
238 rma_atomic_awaitable<T> atomic_fetch_xor(uint64_t remote_addr, T const &bits,
239 T &old_value) const {
240 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_XOR, &bits, remote_addr,
241 rkey_, &old_value);
242 }
243
253 template <class T>
255 T const &delta) const {
256 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_ADD, &delta, remote_addr,
257 rkey_);
258 }
259
269 template <class T>
271 T const &bits) const {
272 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_AND, &bits, remote_addr,
273 rkey_);
274 }
275
284 template <class T>
285 rma_atomic_awaitable<T> atomic_or(uint64_t remote_addr, T const &bits) const {
286 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_OR, &bits, remote_addr,
287 rkey_);
288 }
289
300 template <class T>
302 T const &bits) const {
303 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_XOR, &bits, remote_addr,
304 rkey_);
305 }
306
318 template <class T>
319 rma_atomic_awaitable<T> atomic_swap(uint64_t remote_addr, T const &new_value,
320 T &old_value) const {
321 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_SWAP, &new_value,
322 remote_addr, rkey_, &old_value);
323 }
324
337 template <class T>
338 rma_atomic_awaitable<T> atomic_compare_swap(uint64_t raddr, T const &expected,
339 T &desired_and_old) const {
340 return rma_atomic_awaitable<T>(ep_, UCP_ATOMIC_OP_CSWAP, &expected, raddr,
341 rkey_, &desired_and_old);
342 }
343
350};
351
352} // namespace ucxpp
Abstraction for a UCX endpoint.
Definition: endpoint.h:27
Represents a registered local memory region.
Definition: memory.h:37
static std::pair< void *, local_memory_handle > allocate_mem(std::shared_ptr< context > ctx, size_t length)
Allocate and register a local memory region.
Definition: memory.cc:38
~local_memory_handle()
Destroy the local memory handle object. The memory region will be deregistered.
Definition: memory.cc:65
packed_memory_rkey pack_rkey() const
Pack the information needed for remote access to the memory region. It is intended to be sent to the ...
Definition: memory.cc:55
local_memory_handle(std::shared_ptr< context > ctx, ucp_mem_h mem)
Construct a new local memory handle object.
Definition: memory.cc:15
ucp_mem_h handle() const
Get the native UCX memory handle.
Definition: memory.cc:63
Definition: noncopyable.h:5
A serializable UCX memory handle ready to send to peer.
Definition: memory.h:19
Represents a remote memory region. Note that this does not contain the remote address....
Definition: memory.h:98
~remote_memory_handle()
Destroy the remote memory handle object and the associated rkey handle.
Definition: memory.cc:129
rma_atomic_awaitable< T > atomic_xor(uint64_t remote_addr, T const &bits) const
Atomically XOR a value to the remote memory region.
Definition: memory.h:301
rma_atomic_awaitable< T > atomic_fetch_and(uint64_t remote_addr, T const &bits, T &old_value) const
Atomically fetch and AND a value to the remote memory region.
Definition: memory.h:202
ucp_rkey_h handle() const
Get the native UCX rkey handle.
Definition: memory.cc:107
remote_memory_handle(std::shared_ptr< endpoint > endpoint, void const *packed_rkey_buffer)
Construct a new remote memory handle object. All subsequent remote memory access will happen on the g...
Definition: memory.cc:90
rma_get_awaitable get(void *buffer, size_t length, uint64_t remote_addr) const
Read from the remote memory region.
Definition: memory.cc:114
rma_atomic_awaitable< T > atomic_and(uint64_t remote_addr, T const &bits) const
Atomically AND a value in the remote memory region.
Definition: memory.h:270
rma_put_awaitable put(void const *buffer, size_t length, uint64_t remote_addr) const
Write to the remote memory region.
Definition: memory.cc:109
rma_put_awaitable write(void const *buffer, size_t length, uint64_t remote_addr) const
Definition: memory.cc:119
rma_get_awaitable read(void *buffer, size_t length, uint64_t remote_addr) const
Definition: memory.cc:124
std::shared_ptr< endpoint > endpoint_ptr() const
Get the memory region's endpoint object.
Definition: memory.cc:103
rma_atomic_awaitable< T > atomic_add(uint64_t remote_addr, T const &delta) const
Atomically add to a value in the remote memory region.
Definition: memory.h:254
rma_atomic_awaitable< T > atomic_fetch_or(uint64_t remote_addr, T const &bits, T &old_value) const
Atomically fetch and OR a value to the remote memory region.
Definition: memory.h:220
rma_atomic_awaitable< T > atomic_compare_swap(uint64_t raddr, T const &expected, T &desired_and_old) const
Atomically compare and swap a value in the remote memory region.
Definition: memory.h:338
rma_atomic_awaitable< T > atomic_swap(uint64_t remote_addr, T const &new_value, T &old_value) const
Atomically swap a value in the remote memory region.
Definition: memory.h:319
rma_atomic_awaitable< T > atomic_or(uint64_t remote_addr, T const &bits) const
Atomically OR to a value in the remote memory region.
Definition: memory.h:285
rma_atomic_awaitable< T > atomic_fetch_xor(uint64_t remote_addr, T const &bits, T &old_value) const
Atomically fetch and XOR a value to the remote memory region.
Definition: memory.h:238
rma_atomic_awaitable< T > atomic_fetch_add(uint64_t remote_addr, T const &delta, T &old_value) const
Atomically fetch and add a value to the remote memory region.
Definition: memory.h:184
Definition: awaitable.h:151
Definition: awaitable.h:128
Definition: awaitable.h:106