LCI v2.0.0-dev
For Asynchronous Multithreaded Communication
Loading...
Searching...
No Matches
lci.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 The LCI Project Authors
2// SPDX-License-Identifier: NCSA
3
4#ifndef LCI_API_LCI_HPP
5#define LCI_API_LCI_HPP
6
7#include <memory>
8#include <stdexcept>
9#include <vector>
10#include <string>
11#include <cstring>
12#include <cstdint>
13#include <functional>
14
15#include "lci_config.hpp"
16
21
26
31
36
41
46
52
57
62namespace lci
63{
64extern const char* DEFAULT_NAME;
65
72
82
83// mimic std::optional as we don't want to force c++17 for now
84template <typename T>
85struct option_t {
86 option_t() : m_value(), m_is_set(false) {}
87 option_t(T value_) : m_value(value_), m_is_set(true) {}
88 option_t(T value_, bool is_set_)
89 : m_value(value_), m_is_set(is_set_) {} // set default value
90 T get_value_or(T default_value) const
91 {
92 return m_is_set ? m_value : default_value;
93 }
94 bool get_set_value(T* value) const
95 {
96 if (m_is_set) {
97 *value = this->m_value;
98 return true;
99 }
100 return false;
101 }
102 T get_value() const { return m_value; }
103 bool is_set() const { return m_is_set; }
104 operator T() const { return m_value; }
107};
108} // namespace lci
109
110#include "lci_binding_pre.hpp"
111
112namespace lci
113{
124class peer_failure_error : public std::runtime_error
125{
126 public:
127 peer_failure_error(int failed_rank, const std::string& message)
128 : std::runtime_error(message), failed_rank_(failed_rank)
129 {
130 }
131
135 int failed_rank() const noexcept { return failed_rank_; }
136
137 private:
138 int failed_rank_;
139};
140
179
185const char* get_errorcode_str(errorcode_t errorcode);
186
193struct error_t {
200 error_t(errorcode_t errorcode_) : errorcode(errorcode_) {}
209 bool is_done() const
210 {
213 }
214
218 bool is_posted() const
219 {
222 }
223
227 bool is_retry() const
228 {
231 }
232
236 const char* get_str() const { return lci::get_errorcode_str(errorcode); }
237};
238
251
258
269
276
287
294
305
312
318using net_imm_data_t = uint32_t;
319
336
341const mr_t MR_HOST = mr_t();
342
347const mr_t MR_DEVICE = mr_t(reinterpret_cast<void*>(0x1));
348
354const mr_t MR_UNKNOWN = mr_t(reinterpret_cast<void*>(0x2));
355
356inline bool mr_t::is_empty() const
357{
358 return reinterpret_cast<uintptr_t>(p_impl) < 3;
359}
360
367struct rmr_t {
368 uintptr_t base;
369 uintptr_t mr_base;
370 uint64_t opaque_rkey;
371 rmr_t() : base(0), mr_base(0), opaque_rkey(0) {}
372 bool is_empty() const
373 {
374 return base == 0 && mr_base == 0 && opaque_rkey == 0;
375 }
376};
377
383
388using tag_t = uint64_t;
389
394enum class direction_t {
397};
398
405using rcomp_t = uint32_t;
406
411const int ANY_SOURCE = -1;
412
417const tag_t ANY_TAG = static_cast<tag_t>(-1);
418
424enum class matching_entry_type_t : unsigned {
425 send = 0,
426 recv = 1,
427};
428
433enum class matching_policy_t : unsigned {
434 none = 0,
438 max = 4,
439};
440
444using matching_entry_key_t = uint64_t;
450
463
469 virtual void* allocate(size_t size) = 0;
470 virtual void deallocate(void* ptr) = 0;
471 virtual ~allocator_base_t() = default;
472};
473
475 void* allocate(size_t size) { return malloc(size); }
476 void deallocate(void* ptr) { free(ptr); }
477};
478extern allocator_default_t g_allocator_default;
479
484struct status_t {
486 int rank = -1;
487 void* buffer = nullptr;
488 size_t size = 0;
490 void* user_context = nullptr;
491 status_t() = default;
492 status_t(errorcode_t error_) : error(error_) {}
493 explicit status_t(void* user_context_)
494 : error(errorcode_t::done), user_context(user_context_)
495 {
496 }
500 bool is_done() const { return error.is_done(); }
501 bool is_posted() const { return error.is_posted(); }
502 bool is_retry() const { return error.is_retry(); }
503 error_t get_error() const { return error; }
504 int get_rank() const { return rank; }
505 void* get_buffer() const { return buffer; }
506 size_t get_size() const { return size; }
507 tag_t get_tag() const { return tag; }
508 void* get_user_context() const { return user_context; }
509};
510
516const comp_t COMP_NULL = comp_t(reinterpret_cast<comp_impl_t*>(0x0));
517
523 comp_t(reinterpret_cast<comp_impl_t*>(0x0));
524
529const comp_t COMP_NULL_RETRY = comp_t(reinterpret_cast<comp_impl_t*>(0x1));
530
536 comp_t(reinterpret_cast<comp_impl_t*>(0x1));
537
538inline bool comp_t::is_empty() const
539{
540 return reinterpret_cast<uintptr_t>(p_impl) <= 1;
541}
542
550{
551 public:
554 {
555 attr.comp_type = attr_comp_type_t::custom;
556 attr.zero_copy_am = false;
557 attr.name = DEFAULT_NAME;
558 attr.user_context = nullptr;
559 }
560 comp_impl_t(const attr_t& attr_) : attr(attr_) {}
561 virtual ~comp_impl_t() = default;
562 virtual void signal(status_t) = 0;
564};
565
570using comp_handler_t = void (*)(status_t status);
571
577using reduce_op_t = void (*)(const void* left, const void* right, void* dst,
578 size_t n);
583using graph_node_t = void*;
584
589const graph_node_t GRAPH_START = reinterpret_cast<graph_node_t>(0x1);
590const graph_node_t GRAPH_END = reinterpret_cast<graph_node_t>(0x2);
591
597using graph_node_run_cb_t = status_t (*)(void* value);
598
606
612using graph_node_free_cb_t = void (*)(void* value);
613
618using graph_edge_run_cb_t = void (*)(status_t status, void* src_value,
619 void* dst_value);
620
621} // namespace lci
622
623#include "lci_binding_post.hpp"
624
625namespace lci
626{
669
670/***********************************************************************
671 * Overloading graph_add_node for functor
672 **********************************************************************/
673#if __cplusplus >= 201703L
674template <typename T>
675status_t graph_execute_op_fn(void* value)
676{
677 auto op = static_cast<T*>(value);
678 using result_t = std::invoke_result_t<T>;
679
680 if constexpr (std::is_same_v<result_t, status_t>) {
681 status_t result = (*op)();
682 return result;
683 } else if constexpr (std::is_same_v<result_t, errorcode_t>) {
684 errorcode_t result = (*op)();
685 return result;
686 } else {
687 (*op)();
688 return errorcode_t::done;
689 }
690}
691#else
692// Specialization for status_t return type
693template <typename T>
694typename std::enable_if<
695 std::is_same<typename std::result_of<T()>::type, status_t>::value,
696 status_t>::type
698{
699 auto op = static_cast<T*>(value);
700 status_t result = (*op)();
701 return result;
702}
703
704// Specialization for errorcode_t return type
705template <typename T>
706typename std::enable_if<
707 std::is_same<typename std::result_of<T()>::type, errorcode_t>::value,
708 status_t>::type
710{
711 auto op = static_cast<T*>(value);
712 errorcode_t result = (*op)();
713 return result;
714}
715
716// Specialization for all other return types
717template <typename T>
718typename std::enable_if<
719 !std::is_same<typename std::result_of<T()>::type, status_t>::value &&
720 !std::is_same<typename std::result_of<T()>::type, errorcode_t>::value,
721 status_t>::type
723{
724 auto op = static_cast<T*>(value);
725 (*op)();
726 return errorcode_t::done;
727}
728#endif
729
730template <typename T>
731void graph_free_op_fn(void* value)
732{
733 auto op = static_cast<T*>(value);
734 delete op;
735}
736
745template <typename T>
747{
749 T* fn = new T(op);
751 auto ret = graph_add_node_x(graph, wrapper)
752 .value(reinterpret_cast<void*>(fn))
753 .free_cb(free_cb)();
754 fn->user_context(ret);
755 return ret;
756}
757
758} // namespace lci
759
760#endif // LCI_API_LCI_HPP
Completion object implementation base type.
Definition lci.hpp:550
comp_impl_t(const attr_t &attr_)
Definition lci.hpp:560
virtual ~comp_impl_t()=default
comp_impl_t()
Definition lci.hpp:553
comp_attr_t attr
Definition lci.hpp:563
comp_attr_t attr_t
Definition lci.hpp:552
virtual void signal(status_t)=0
The actual implementation for RESOURCE comp.
Definition lci_binding_pre.hpp:352
bool is_empty() const
Definition lci.hpp:538
comp_impl_t * p_impl
Definition lci_binding_pre.hpp:364
The actual implementation for graph_add_node.
Definition lci_binding_post.hpp:1509
graph_add_node_x && free_cb(graph_node_free_cb_t free_cb_in)
Definition lci_binding_post.hpp:1525
graph_add_node_x && value(void *value_in)
Definition lci_binding_post.hpp:1524
The actual implementation for RESOURCE mr.
Definition lci_binding_pre.hpp:510
mr_impl_t * p_impl
Definition lci_binding_pre.hpp:517
bool is_empty() const
Definition lci.hpp:356
int failed_rank() const noexcept
Get the rank of the peer associated with the failed operation.
Definition lci.hpp:135
peer_failure_error(int failed_rank, const std::string &message)
Definition lci.hpp:127
graph_node_t graph_add_node_op(comp_t graph, const T &op)
Add a functor as a node to the completion graph.
Definition lci.hpp:746
broadcast_algorithm_t
The type of broadcast algorithm.
Definition lci.hpp:263
errorcode_t
The actual error code for LCI API functions.
Definition lci.hpp:152
void(*)(status_t status, void *src_value, void *dst_value) graph_edge_run_cb_t
The function signature for a edge funciton in the completion graph.
Definition lci.hpp:618
void set_g_default_attr(const global_attr_t &attr)
Set the default global attributes for LCI.
const rmr_t RMR_NULL
The NULL value of rkey_t.
Definition lci.hpp:382
uint64_t matching_entry_key_t
The type of matching engine entry key.
Definition lci.hpp:444
const mr_t MR_DEVICE
A special mr_t value for device memory.
Definition lci.hpp:347
const int ANY_SOURCE
Special rank value for any-source receive.
Definition lci.hpp:411
void global_initialize()
Global initialization for LCI, including rank_me, rank_n, and g_default_attr.
status_t(*)(void *value) graph_node_run_cb_t
The function signature for a node function in the completion graph.
Definition lci.hpp:597
net_opcode_t
The Type of network communication operation codes.
Definition lci.hpp:243
const mr_t MR_HOST
A special mr_t value for host memory.
Definition lci.hpp:341
direction_t
The enum class of comunication direction.
Definition lci.hpp:394
void(*)(void *value) graph_node_free_cb_t
The function signature for a callback that will be triggered when the node was freed.
Definition lci.hpp:612
void(*)(const void *left, const void *right, void *dst, size_t n) reduce_op_t
The user-defined reduction operation.
Definition lci.hpp:577
int get_rank_n()
Get the number of ranks in the current application/job.
matching_policy_t
Enum class for matching policy.
Definition lci.hpp:433
void * graph_node_t
The node type for the completion graph.
Definition lci.hpp:583
const tag_t ANY_TAG
Special tag value for any-tag receive.
Definition lci.hpp:417
const graph_node_run_cb_t GRAPH_NODE_DUMMY_CB
A dummy callback function for a graph node.
Definition lci.hpp:605
allreduce_algorithm_t
The type of allreduce algorithm.
Definition lci.hpp:299
int get_rank_me()
Get the rank of the current process.
reduce_scatter_algorithm_t
The type of reduce scatter algorithm.
Definition lci.hpp:281
const comp_t COMP_NULL_EXPECT_DONE
Deprecated. Same as COMP_NULL.
Definition lci.hpp:522
const mr_t MR_UNKNOWN
A special mr_t value for unknown memory. LCI will detect the memory type automatically.
Definition lci.hpp:354
void * matching_entry_val_t
The type of matching engine entry value.
Definition lci.hpp:449
void global_finalize()
Global finalization for LCI.
matching_entry_type_t
The type of matching entry.
Definition lci.hpp:424
uint32_t rcomp_t
The type of remote completion handler.
Definition lci.hpp:405
void(*)(status_t status) comp_handler_t
Function Signature for completion handler.
Definition lci.hpp:570
const graph_node_t GRAPH_START
The start node of the completion graph.
Definition lci.hpp:589
const comp_t COMP_NULL
Special completion object setting allow_posted and allow_retry to false.
Definition lci.hpp:516
const comp_t COMP_NULL_EXPECT_DONE_OR_RETRY
Deprecated. Same as COMP_NULL_RETRY.
Definition lci.hpp:535
uint64_t tag_t
The type of tag.
Definition lci.hpp:388
global_attr_t get_g_default_attr()
Get the default global attributes for LCI. The default global attributes contain the default attribut...
comp_semantic_t
The enum class of completion semantic.
Definition lci.hpp:459
uint32_t net_imm_data_t
The type of network-layer immediate data field.
Definition lci.hpp:318
const comp_t COMP_NULL_RETRY
Special completion object setting allow_posted to false.
Definition lci.hpp:529
@ ring
Definition lci.hpp:267
@ direct
Definition lci.hpp:265
@ tree
Definition lci.hpp:266
@ retry
Definition lci.hpp:165
@ retry_nopacket
Definition lci.hpp:169
@ done_max
Definition lci.hpp:157
@ posted_backlog
Definition lci.hpp:161
@ done
Definition lci.hpp:154
@ retry_min
Definition lci.hpp:164
@ done_min
Definition lci.hpp:153
@ retry_lock
Definition lci.hpp:168
@ posted_min
Definition lci.hpp:158
@ retry_max
Definition lci.hpp:175
@ posted
Definition lci.hpp:159
@ retry_backlog
Definition lci.hpp:173
@ retry_init
Definition lci.hpp:167
@ done_backlog
Definition lci.hpp:155
@ retry_nomem
Definition lci.hpp:171
@ fatal
Definition lci.hpp:176
@ posted_max
Definition lci.hpp:163
@ READ
Definition lci.hpp:248
@ SEND
Definition lci.hpp:244
@ REMOTE_WRITE
Definition lci.hpp:247
@ RECV
Definition lci.hpp:245
@ ERROR
Definition lci.hpp:249
@ WRITE
Definition lci.hpp:246
@ IN
Definition lci.hpp:396
@ OUT
Definition lci.hpp:395
@ max
Definition lci.hpp:438
@ rank_tag
Definition lci.hpp:437
@ tag_only
Definition lci.hpp:436
@ rank_only
Definition lci.hpp:435
@ send
Definition lci.hpp:425
@ recv
Definition lci.hpp:426
@ network
Definition lci.hpp:461
@ memory
Definition lci.hpp:460
All LCI API functions and classes are defined in this namespace.
const char * get_allreduce_algorithm_str(broadcast_algorithm_t algorithm)
Get the string representation of a collective algorithm.
attr_backend_t
Definition lci.hpp:66
@ none
Definition lci.hpp:67
@ ibv
Definition lci.hpp:68
@ ofi
Definition lci.hpp:69
@ ucx
Definition lci.hpp:70
const char * DEFAULT_NAME
const graph_node_t GRAPH_END
Definition lci.hpp:590
const char * get_reduce_scatter_algorithm_str(broadcast_algorithm_t algorithm)
Get the string representation of a collective algorithm.
allocator_default_t g_allocator_default
@ custom
Definition lci_binding_pre.hpp:45
@ graph
Definition lci_binding_pre.hpp:44
const char * get_errorcode_str(errorcode_t errorcode)
Get the string representation of an error code.
void graph_free_op_fn(void *value)
Definition lci.hpp:731
attr_net_lock_mode_t
Definition lci.hpp:73
@ LCI_NET_LOCK_MR
Definition lci.hpp:77
@ LCI_NET_TRYLOCK_POLL
Definition lci.hpp:76
@ LCI_NET_TRYLOCK_SEND
Definition lci.hpp:74
@ LCI_NET_LOCK_ALL
Definition lci.hpp:78
@ LCI_NET_TRYLOCK_RECV
Definition lci.hpp:75
@ LCI_NET_TRYLOCK_MAX
Definition lci.hpp:80
const char * get_net_opcode_str(net_opcode_t opcode)
Get the string representation of a network operation code.
std::enable_if< std::is_same< typenamestd::result_of< T()>::type, status_t >::value, status_t >::type graph_execute_op_fn(void *value)
Definition lci.hpp:697
const char * get_broadcast_algorithm_str(broadcast_algorithm_t algorithm)
Get the string representation of a collective algorithm.
The user-defined allocator type.
Definition lci.hpp:468
virtual ~allocator_base_t()=default
virtual void deallocate(void *ptr)=0
virtual void * allocate(size_t size)=0
Definition lci.hpp:474
void * allocate(size_t size)
Definition lci.hpp:475
void deallocate(void *ptr)
Definition lci.hpp:476
Definition lci_binding_pre.hpp:102
Wrapper class for error code.
Definition lci.hpp:193
bool is_done() const
Check if the error code is in the done category.
Definition lci.hpp:209
error_t()
Definition lci.hpp:195
bool is_posted() const
Check if the error code is in the posted category.
Definition lci.hpp:218
const char * get_str() const
Get the string representation of the error code.
Definition lci.hpp:236
errorcode_t errorcode
Definition lci.hpp:194
void reset_retry()
Reset the error code to retry.
Definition lci.hpp:204
error_t(errorcode_t errorcode_)
Construct an error_t object with a specific error code.
Definition lci.hpp:200
bool is_retry() const
Check if the error code is in the retry category.
Definition lci.hpp:227
Definition lci_binding_pre.hpp:160
The struct for network status.
Definition lci.hpp:329
net_imm_data_t imm_data
Definition lci.hpp:334
int rank
Definition lci.hpp:331
void * user_context
Definition lci.hpp:332
net_opcode_t opcode
Definition lci.hpp:330
size_t length
Definition lci.hpp:333
bool m_is_set
Definition lci.hpp:106
option_t()
Definition lci.hpp:86
T get_value() const
Definition lci.hpp:102
T get_value_or(T default_value) const
Definition lci.hpp:90
bool get_set_value(T *value) const
Definition lci.hpp:94
T m_value
Definition lci.hpp:105
option_t(T value_, bool is_set_)
Definition lci.hpp:88
bool is_set() const
Definition lci.hpp:103
option_t(T value_)
Definition lci.hpp:87
The type of remote memory region.
Definition lci.hpp:367
uintptr_t mr_base
Definition lci.hpp:369
uintptr_t base
Definition lci.hpp:368
bool is_empty() const
Definition lci.hpp:372
uint64_t opaque_rkey
Definition lci.hpp:370
rmr_t()
Definition lci.hpp:371
The type of the completion desciptor for a posted communication.
Definition lci.hpp:484
tag_t get_tag() const
Definition lci.hpp:507
status_t(errorcode_t error_)
Definition lci.hpp:492
status_t(void *user_context_)
Definition lci.hpp:493
error_t get_error() const
Definition lci.hpp:503
void set_retry()
Definition lci.hpp:499
void * get_user_context() const
Definition lci.hpp:508
error_t error
Definition lci.hpp:485
void set_done()
Definition lci.hpp:497
tag_t tag
Definition lci.hpp:489
size_t size
Definition lci.hpp:488
void set_posted()
Definition lci.hpp:498
bool is_done() const
Definition lci.hpp:500
void * buffer
Definition lci.hpp:487
void * user_context
Definition lci.hpp:490
bool is_posted() const
Definition lci.hpp:501
status_t()=default
bool is_retry() const
Definition lci.hpp:502
int get_rank() const
Definition lci.hpp:504
void * get_buffer() const
Definition lci.hpp:505
int rank
Definition lci.hpp:486
size_t get_size() const
Definition lci.hpp:506