|
| 1 | +// Copyright (c) 2026 Hartmut Kaiser |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSL-1.0 |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <hpx/config.hpp> |
| 10 | + |
| 11 | +#if defined(HPX_HAVE_NETWORKING) && defined(HPX_HAVE_DEVICE_CONDUIT_UCX) |
| 12 | +#include <hpx/modules/functional.hpp> |
| 13 | +#include <hpx/modules/futures.hpp> |
| 14 | +#include <hpx/modules/parcelset.hpp> |
| 15 | +#include <hpx/modules/runtime_configuration.hpp> |
| 16 | +#include <hpx/modules/serialization.hpp> |
| 17 | +#include <hpx/modules/synchronization.hpp> |
| 18 | + |
| 19 | +#include <atomic> |
| 20 | +#include <cstddef> |
| 21 | +#include <cstdint> |
| 22 | +#include <exception> |
| 23 | +#include <functional> |
| 24 | +#include <map> |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +// the UCX handle types (ucp_context_h etc. are pointers to these) |
| 29 | +struct ucp_context; |
| 30 | +struct ucp_worker; |
| 31 | +struct ucp_ep; |
| 32 | + |
| 33 | +namespace hpx::parcelset::device_conduits { |
| 34 | + |
| 35 | + /// A device conduit moving the zero-copy chunks of device memory of |
| 36 | + /// parcels with UCX tagged messages: a context and a worker of its own |
| 37 | + /// per locality (also next to a UCX based MPI library, which uses the |
| 38 | + /// same UCX library), one endpoint per destination, created on the first |
| 39 | + /// message to it from the destination's worker address, which an action |
| 40 | + /// fetches. The UCX tag of a chunk is made of the sending locality and |
| 41 | + /// the descriptor's tag. |
| 42 | + class HPX_EXPORT ucx_conduit : public device_conduit_impl<ucx_conduit> |
| 43 | + { |
| 44 | + public: |
| 45 | + explicit ucx_conduit(util::runtime_configuration const& cfg); |
| 46 | + ~ucx_conduit() override; |
| 47 | + |
| 48 | + ucx_conduit(ucx_conduit const&) = delete; |
| 49 | + ucx_conduit& operator=(ucx_conduit const&) = delete; |
| 50 | + |
| 51 | + [[nodiscard]] static std::string name_impl() |
| 52 | + { |
| 53 | + return "ucx"; |
| 54 | + } |
| 55 | + |
| 56 | + static void config_defaults(std::vector<std::string>& ini); |
| 57 | + |
| 58 | + [[nodiscard]] bool can_reach_impl(std::uint32_t) const noexcept |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + void send_impl(std::uint32_t locality_id, |
| 64 | + serialization::detail::device_chunk_descriptor const& d, |
| 65 | + void const* p, std::size_t size, |
| 66 | + hpx::move_only_function<void(std::exception_ptr)> sent); |
| 67 | + |
| 68 | + void recv_impl(serialization::detail::device_chunk_descriptor const& d, |
| 69 | + void* p, std::size_t size, |
| 70 | + std::function<void(std::exception_ptr)> done); |
| 71 | + |
| 72 | + bool background_work_impl(); |
| 73 | + |
| 74 | + /// Closes the endpoint to a locality that has left, after the sends |
| 75 | + /// in flight to it have finished (or failed, with |
| 76 | + /// hpx.parcel.conduit.ucx.error_handling 'peer'), or that was |
| 77 | + /// disconnected ('forced'), cancelling them (with 'peer'; with 'none' |
| 78 | + /// UCX allows only to flush the endpoint). |
| 79 | + void locality_removed_impl( |
| 80 | + std::uint32_t locality_id, bool forced) noexcept; |
| 81 | + |
| 82 | + /// The address of this locality's worker (what the action returns). |
| 83 | + [[nodiscard]] std::vector<char> const& worker_address() const noexcept |
| 84 | + { |
| 85 | + return address_; |
| 86 | + } |
| 87 | + |
| 88 | + private: |
| 89 | + // an endpoint, and which one of those to the same locality (a |
| 90 | + // removed locality's endpoint is closed, a later message to that |
| 91 | + // locality creates a new one) |
| 92 | + struct endpoint_entry |
| 93 | + { |
| 94 | + hpx::shared_future<ucp_ep*> ep; |
| 95 | + std::uint64_t generation = 0; |
| 96 | + }; |
| 97 | + |
| 98 | + // the endpoint to 'locality_id', fetches the address and creates it |
| 99 | + // the first time (on an HPX thread) |
| 100 | + endpoint_entry endpoint(std::uint32_t locality_id); |
| 101 | + |
| 102 | + // starts closing an endpoint that is no longer in endpoints_ (or at |
| 103 | + // destruction), without holding endpoints_mtx_: UCX may complete the |
| 104 | + // sends on it right away, calling their callbacks |
| 105 | + void close_endpoint(ucp_ep* ep, std::uint32_t locality_id, bool force); |
| 106 | + |
| 107 | + // makes sure the OS thread has a CUDA context for the GPU of the |
| 108 | + // memory at 'p' (if it is device memory), remembers the GPU for |
| 109 | + // progress |
| 110 | + void note_device(void const* p) noexcept; |
| 111 | + |
| 112 | + // sends unless the endpoint was closed since (its locality was |
| 113 | + // removed) |
| 114 | + void start_send(std::uint32_t locality_id, std::uint64_t generation, |
| 115 | + ucp_ep* ep, std::uint64_t tag, void const* p, std::size_t size, |
| 116 | + hpx::move_only_function<void(std::exception_ptr)> sent); |
| 117 | + |
| 118 | + ucp_context* context_ = nullptr; |
| 119 | + ucp_worker* worker_ = nullptr; |
| 120 | + std::vector<char> address_; |
| 121 | + |
| 122 | + // UCP_ERR_HANDLING_MODE_PEER for the endpoints |
| 123 | + // (hpx.parcel.conduit.ucx.error_handling) |
| 124 | + bool peer_errors_ = true; |
| 125 | + |
| 126 | + hpx::spinlock endpoints_mtx_; |
| 127 | + std::map<std::uint32_t, endpoint_entry> endpoints_; |
| 128 | + std::uint64_t generation_ = 0; |
| 129 | + |
| 130 | + // the requests closing the endpoints of removed localities |
| 131 | + // (ucs_status_ptr_t, guarded by endpoints_mtx_) |
| 132 | + std::vector<void*> closing_; |
| 133 | + |
| 134 | + // the GPU of the last device memory sent or received: progress makes |
| 135 | + // its context current where none is (with several GPUs per locality, |
| 136 | + // the context of the last one) |
| 137 | + std::atomic<int> device_ = -1; |
| 138 | + |
| 139 | + // one thread at a time makes progress |
| 140 | + hpx::spinlock progress_mtx_; |
| 141 | + }; |
| 142 | +} // namespace hpx::parcelset::device_conduits |
| 143 | + |
| 144 | +#endif |
0 commit comments