typedefs.h
1 
5 #pragma once
6 
7 #include <array>
8 #include <atomic>
9 #include <cstddef>
10 #include <cstring>
11 #include <functional>
12 #include <limits>
13 #include <memory>
14 #include <optional>
15 #include <stdexcept>
16 #include <string>
17 #include <string_view>
18 #include <unordered_map>
19 #include <vector>
20 
21 #include <ucp/api/ucp.h>
22 
23 namespace ucxx {
24 
25 class Buffer;
26 class Request;
27 class RequestAm;
28 
36 typedef enum {
37  UCXX_LOG_LEVEL_FATAL, /* Immediate termination */
38  UCXX_LOG_LEVEL_ERROR, /* Error is returned to the user */
39  UCXX_LOG_LEVEL_WARN, /* Something's wrong, but we continue */
40  UCXX_LOG_LEVEL_DIAG, /* Diagnostics, silent adjustments or internal error handling */
41  UCXX_LOG_LEVEL_INFO, /* Information */
42  UCXX_LOG_LEVEL_DEBUG, /* Low-volume debugging */
43  UCXX_LOG_LEVEL_TRACE, /* High-volume debugging */
44  UCXX_LOG_LEVEL_TRACE_REQ, /* Every send/receive request */
45  UCXX_LOG_LEVEL_TRACE_DATA, /* Data sent/received on the transport */
46  UCXX_LOG_LEVEL_TRACE_ASYNC, /* Asynchronous progress engine */
47  UCXX_LOG_LEVEL_TRACE_FUNC, /* Function calls */
48  UCXX_LOG_LEVEL_TRACE_POLL, /* Polling functions */
49  UCXX_LOG_LEVEL_LAST, /* Last level barrier, not an actual level */
50  UCXX_LOG_LEVEL_PRINT /* Temporary output */
52 
58 enum class TransferDirection { Send = 0, Receive };
59 
66 enum Tag : ucp_tag_t {};
67 
74 enum TagMask : ucp_tag_t {};
75 
81 static constexpr TagMask TagMaskFull{std::numeric_limits<std::underlying_type_t<TagMask>>::max()};
82 
89 typedef std::unordered_map<std::string, std::string> ConfigMap;
90 
97 typedef std::function<void(ucs_status_t, std::shared_ptr<void>)> RequestCallbackUserFunction;
98 
105 typedef std::shared_ptr<void> RequestCallbackUserData;
106 
114 
122 
129 typedef std::function<std::shared_ptr<Buffer>(size_t)> AmAllocatorType;
130 
138 typedef std::function<void(std::shared_ptr<Request>, ucp_ep_h)> AmReceiverCallbackType;
139 
143 static constexpr size_t AmReceiverCallbackOwnerMaxLen = 63;
144 
148 static constexpr size_t AmReceiverCallbackOwnerStorageSize = AmReceiverCallbackOwnerMaxLen + 1;
149 
164  public:
167 
169  AmReceiverCallbackOwnerType(const char* s) // NOLINT(runtime/explicit)
170  {
171  if (s == nullptr) return;
172  const size_t len = std::strlen(s);
173  if (len > AmReceiverCallbackOwnerMaxLen)
174  throw std::invalid_argument(
175  "AmReceiverCallbackOwnerType: owner name exceeds "
176  "maximum length of " +
177  std::to_string(AmReceiverCallbackOwnerMaxLen) + " characters");
178  std::memcpy(_data.data(), s, len);
179  }
180 
182  AmReceiverCallbackOwnerType(const std::string& s) // NOLINT(runtime/explicit)
183  : AmReceiverCallbackOwnerType(s.c_str())
184  {
185  }
186 
188  [[nodiscard]] const char* data() const noexcept { return _data.data(); }
189 
191  [[nodiscard]] char* data() noexcept { return _data.data(); }
192 
194  static constexpr size_t storageSize() noexcept { return AmReceiverCallbackOwnerStorageSize; }
195 
197  bool operator==(const AmReceiverCallbackOwnerType& other) const noexcept
198  {
199  return _data == other._data;
200  }
201 
203  bool operator!=(const AmReceiverCallbackOwnerType& other) const noexcept
204  {
205  return !(*this == other);
206  }
207 
208  private:
209  std::array<char, AmReceiverCallbackOwnerStorageSize> _data{};
210 };
211 
217 typedef uint64_t AmReceiverCallbackIdType;
218 
225  public:
228 
229  AmReceiverCallbackInfo() = delete;
230 
238 };
239 
247  FallbackToHost = 0,
249 };
250 
257 struct AmSendParams {
258  uint32_t flags{UCP_AM_SEND_FLAG_REPLY};
259  ucp_datatype_t datatype{ucp_dt_make_contig(1)};
260  ucs_memory_type_t memoryType{UCS_MEMORY_TYPE_HOST};
263  std::optional<AmReceiverCallbackInfo> receiverCallbackInfo{
264  std::nullopt};
265  std::vector<std::byte> userHeader{};
274 
281  void setUserHeader(const void* data, size_t size)
282  {
283  if (size > 0 && data == nullptr)
284  throw std::invalid_argument(
285  "AmSendParams::setUserHeader received null data with non-zero size");
286  userHeader.resize(size);
287  if (size > 0) memcpy(userHeader.data(), data, size);
288  }
289 
295  void setUserHeader(std::string_view data) { setUserHeader(data.data(), data.size()); }
296 };
297 
304 typedef const std::string SerializedRemoteKey;
305 
314  size_t operator()(const AmReceiverCallbackOwnerType& o) const noexcept
315  {
316  return std::hash<std::string_view>{}(
317  std::string_view(o.data(), AmReceiverCallbackOwnerStorageSize));
318  }
319 };
320 
321 } // namespace ucxx
Information of an Active Message receiver callback.
Definition: typedefs.h:224
AmReceiverCallbackOwnerType owner
The owner name of the callback.
Definition: typedefs.h:226
AmReceiverCallbackInfo(const AmReceiverCallbackOwnerType owner, AmReceiverCallbackIdType id)
Construct an AmReceiverCallbackInfo object.
AmReceiverCallbackIdType id
The unique identifier of the callback.
Definition: typedefs.h:227
Active Message receiver callback owner name (fixed-size).
Definition: typedefs.h:163
char * data() noexcept
Mutable pointer to the raw fixed-size storage (for deserialization).
Definition: typedefs.h:191
const char * data() const noexcept
Pointer to the raw fixed-size storage.
Definition: typedefs.h:188
bool operator!=(const AmReceiverCallbackOwnerType &other) const noexcept
Inequality comparison.
Definition: typedefs.h:203
AmReceiverCallbackOwnerType()=default
Construct an empty (all-zero) owner name.
AmReceiverCallbackOwnerType(const char *s)
Construct from a null-terminated C string. Throws if length exceeds the limit.
Definition: typedefs.h:169
bool operator==(const AmReceiverCallbackOwnerType &other) const noexcept
Equality comparison.
Definition: typedefs.h:197
static constexpr size_t storageSize() noexcept
The fixed storage size that is always sent on the wire.
Definition: typedefs.h:194
AmReceiverCallbackOwnerType(const std::string &s)
Construct from a std::string. Throws if length exceeds the limit.
Definition: typedefs.h:182
Definition: address.h:16
std::function< void(ucs_status_t, std::shared_ptr< void >)> RequestCallbackUserFunction
A user-defined function to execute as part of a ucxx::Request callback.
Definition: typedefs.h:97
std::shared_ptr< void > RequestCallbackUserData
Data for the user-defined function provided to the ucxx::Request callback.
Definition: typedefs.h:105
std::unordered_map< std::string, std::string > ConfigMap
A UCP configuration map.
Definition: typedefs.h:89
RequestCallbackUserData EndpointCloseCallbackUserData
Data for the user-defined function provided to endpoint close callback.
Definition: typedefs.h:121
TransferDirection
The direction of a UCXX transfer.
Definition: typedefs.h:58
std::function< std::shared_ptr< Buffer >size_t)> AmAllocatorType
Custom Active Message allocator type.
Definition: typedefs.h:129
AmSendMemoryTypePolicy
Policy used to allocate receive buffers for Active Messages.
Definition: typedefs.h:246
@ FallbackToHost
If no allocator exists for memory type, fallback to host memory.
@ ErrorOnUnsupported
If no allocator exists for memory type, fail with unsupported error.
const std::string SerializedRemoteKey
Serialized form of a remote key.
Definition: typedefs.h:304
ucxx_log_level_t
Available logging levels.
Definition: typedefs.h:36
uint64_t AmReceiverCallbackIdType
Active Message receiver callback identifier.
Definition: typedefs.h:217
RequestCallbackUserFunction EndpointCloseCallbackUserFunction
A user-defined function to execute after an endpoint closes.
Definition: typedefs.h:113
TagMask
Strong type for a UCP tag mask.
Definition: typedefs.h:74
Tag
Strong type for a UCP tag.
Definition: typedefs.h:66
std::function< void(std::shared_ptr< Request >, ucp_ep_h)> AmReceiverCallbackType
Active Message receiver callback.
Definition: typedefs.h:138
Hash functor for AmReceiverCallbackOwnerType.
Definition: typedefs.h:312
size_t operator()(const AmReceiverCallbackOwnerType &o) const noexcept
Compute hash of an AmReceiverCallbackOwnerType.
Definition: typedefs.h:314
Parameters controlling Active Message send behavior.
Definition: typedefs.h:257
std::optional< AmReceiverCallbackInfo > receiverCallbackInfo
Optional receiver callback metadata.
Definition: typedefs.h:263
ucp_datatype_t datatype
Datatype used by ucp_am_send_nbx.
Definition: typedefs.h:259
AmSendMemoryTypePolicy memoryTypePolicy
Receiver allocation policy.
Definition: typedefs.h:261
void setUserHeader(std::string_view data)
Convenience overload to set user header from string-like views.
Definition: typedefs.h:295
std::vector< std::byte > userHeader
Definition: typedefs.h:265
void setUserHeader(const void *data, size_t size)
Set opaque user header bytes from raw pointer.
Definition: typedefs.h:281
ucs_memory_type_t memoryType
Sender memory type hint.
Definition: typedefs.h:260
uint32_t flags
UCP AM send flags.
Definition: typedefs.h:258