Skip to content

Commit 8c3cd09

Browse files
authored
Merge pull request #8166 from hkaiser/ucx-conduit
Add a device conduit based on UCX
2 parents 9165643 + ccd4e25 commit 8c3cd09

24 files changed

Lines changed: 1731 additions & 34 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ unset(HPX_ENABLED_MODULES CACHE)
138138
unset(HPX_CORE_ENABLED_MODULES CACHE)
139139
unset(HPX_FULL_ENABLED_MODULES CACHE)
140140
unset(HPX_STATIC_PARCELPORT_PLUGINS CACHE)
141+
unset(HPX_STATIC_DEVICE_CONDUIT_PLUGINS CACHE)
141142

142143
# ##############################################################################
143144
# Set up dummy compiler flags targets
@@ -1527,6 +1528,19 @@ if(HPX_WITH_NETWORKING)
15271528
if(HPX_WITH_PARCELPORT_TCP)
15281529
hpx_add_config_define(HPX_HAVE_PARCELPORT_TCP)
15291530
endif()
1531+
1532+
# device conduits move device memory between localities below the parcelports
1533+
# (hpx::parcelset::device_conduit)
1534+
hpx_option(
1535+
HPX_WITH_DEVICE_CONDUIT_UCX
1536+
BOOL
1537+
"Enable the UCX based device conduit (uses the UCX of the MPI library if it is UCX based; set UCX_ROOT otherwise)."
1538+
OFF
1539+
CATEGORY "Parcelport"
1540+
)
1541+
if(HPX_WITH_DEVICE_CONDUIT_UCX)
1542+
hpx_add_config_define(HPX_HAVE_DEVICE_CONDUIT_UCX)
1543+
endif()
15301544
hpx_option(
15311545
HPX_WITH_PARCELPORT_COUNTERS BOOL
15321546
"Enable performance counters reporting parcelport statistics." OFF
@@ -2852,6 +2866,11 @@ set(HPX_STATIC_PARCELPORT_PLUGINS
28522866
""
28532867
CACHE INTERNAL "" FORCE
28542868
)
2869+
# collect names of the device conduits compiled into HPX
2870+
set(HPX_STATIC_DEVICE_CONDUIT_PLUGINS
2871+
""
2872+
CACHE INTERNAL "" FORCE
2873+
)
28552874

28562875
# Locate Python before adding libraries. Module tests and examples are
28572876
# registered from libs/ via add_hpx_test, which prefixes hpxrun.py with

‎cmake/HPX_SetupUCX.cmake‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
# Finds UCX for the UCX device conduit (PkgConfig::UCX). It must be the UCX
8+
# library of the MPI library if that is UCX based (one process can't use two
9+
# different UCX libraries): look next to MPI (MPI_HOME) first, then UCX_ROOT,
10+
# then the default pkg-config paths. If MPI's UCX is installed elsewhere,
11+
# UCX_ROOT must name it. HPX_UCX_ROOT remembers the UCX that was found (for
12+
# projects using an installed HPX).
13+
macro(hpx_setup_ucx)
14+
if(NOT TARGET PkgConfig::UCX)
15+
find_package(PkgConfig REQUIRED)
16+
17+
set(_hpx_ucx_pkgconfig_dirs)
18+
foreach(_hpx_ucx_root ${HPX_UCX_ROOT} ${MPI_HOME} $ENV{MPI_HOME}
19+
${UCX_ROOT} $ENV{UCX_ROOT}
20+
)
21+
list(APPEND _hpx_ucx_pkgconfig_dirs "${_hpx_ucx_root}/lib/pkgconfig"
22+
"${_hpx_ucx_root}/lib64/pkgconfig"
23+
)
24+
endforeach()
25+
26+
set(_hpx_pkg_config_path "$ENV{PKG_CONFIG_PATH}")
27+
string(REPLACE ";" ":" _hpx_ucx_pkgconfig_path "${_hpx_ucx_pkgconfig_dirs}")
28+
set(ENV{PKG_CONFIG_PATH}
29+
"${_hpx_ucx_pkgconfig_path}:${_hpx_pkg_config_path}"
30+
)
31+
pkg_check_modules(UCX REQUIRED IMPORTED_TARGET GLOBAL ucx)
32+
set(ENV{PKG_CONFIG_PATH} "${_hpx_pkg_config_path}")
33+
34+
set(HPX_UCX_ROOT
35+
"${UCX_PREFIX}"
36+
CACHE INTERNAL "" FORCE
37+
)
38+
endif()
39+
endmacro()

‎cmake/templates/HPXConfig.cmake.in‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ if(HPX_WITH_NETWORKING AND HPX_WITH_PARCELPORT_LCI)
7373
endif()
7474
endif()
7575

76+
# UCX (device conduit)
77+
if(HPX_WITH_NETWORKING AND HPX_WITH_DEVICE_CONDUIT_UCX)
78+
set(HPX_UCX_ROOT "@HPX_UCX_ROOT@")
79+
include(HPX_SetupUCX)
80+
hpx_setup_ucx()
81+
endif()
82+
7683
# LCW
7784
if(HPX_WITH_NETWORKING AND HPX_WITH_PARCELPORT_LCW)
7885
if(HPX_WITH_FETCH_LCW)

‎libs/full/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(_hpx_full_modules
2323
components
2424
components_base
2525
compute
26+
device_conduit_ucx
2627
distribution_policies
2728
execution_distributed
2829
executors_distributed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
if(NOT (HPX_WITH_NETWORKING AND HPX_WITH_DEVICE_CONDUIT_UCX))
8+
return()
9+
endif()
10+
11+
# the UCX of the MPI library if that is UCX based
12+
include(HPX_SetupUCX)
13+
hpx_setup_ucx()
14+
hpx_info(" UCX ${UCX_VERSION} for the device conduit: ${HPX_UCX_ROOT}")
15+
16+
set(device_conduit_ucx_headers hpx/device_conduit_ucx/ucx_conduit.hpp)
17+
set(device_conduit_ucx_sources ucx_conduit.cpp)
18+
19+
# the CUDA driver API (the CUDA context of the calling OS thread)
20+
set(device_conduit_ucx_dependencies hpx_core PkgConfig::UCX)
21+
if(HPX_WITH_CUDA)
22+
list(APPEND device_conduit_ucx_dependencies CUDA::cuda_driver)
23+
endif()
24+
25+
include(HPX_AddModule)
26+
add_hpx_module(
27+
full device_conduit_ucx
28+
GLOBAL_HEADER_GEN OFF
29+
SOURCES ${device_conduit_ucx_sources}
30+
HEADERS ${device_conduit_ucx_headers}
31+
DEPENDENCIES ${device_conduit_ucx_dependencies}
32+
MODULE_DEPENDENCIES
33+
hpx_actions
34+
hpx_actions_base
35+
hpx_async_distributed
36+
hpx_naming_base
37+
hpx_parcelset
38+
hpx_parcelset_base
39+
hpx_plugin_factories
40+
CMAKE_SUBDIRS tests
41+
)
42+
43+
set(HPX_STATIC_DEVICE_CONDUIT_PLUGINS
44+
${HPX_STATIC_DEVICE_CONDUIT_PLUGINS} ucx
45+
CACHE INTERNAL "" FORCE
46+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
..
2+
Copyright (c) 2026 The STE||AR-Group
3+
4+
SPDX-License-Identifier: BSL-1.0
5+
Distributed under the Boost Software License, Version 1.0. (See accompanying
6+
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
8+
.. _modules_device_conduit_ucx:
9+
10+
==================
11+
device_conduit_ucx
12+
==================
13+
14+
A device conduit (``hpx::parcelset::device_conduit``) based on UCX: it moves
15+
the zero-copy chunks of device memory of parcels between localities with UCX
16+
tagged messages, below the parcelport, which sends the rest of the parcels.
17+
UCX chooses the path: CUDA or ROCm IPC within a node, GPUDirect RDMA between
18+
nodes, staging through host memory where neither is available. UCX registers
19+
CUDA memory with the CUDA context current on the calling thread: the conduit
20+
makes the primary context of the memory's GPU current on threads that have
21+
none.
22+
23+
Enable it with ``HPX_WITH_DEVICE_CONDUIT_UCX=ON`` and select it at runtime
24+
with ``hpx.parcel.device_conduit=ucx``. It looks for UCX next to the MPI
25+
library (``MPI_HOME``, if it is set) first, then in ``UCX_ROOT``. One process
26+
can use only one UCX library: if the MPI library is UCX based and its UCX is
27+
installed elsewhere (or ``MPI_HOME`` is not set because MPI was found through
28+
its compiler wrappers), set ``UCX_ROOT`` to that UCX installation. The UCX that
29+
was found is remembered in the |cmake|_ cache: to switch to another one,
30+
configure a new build directory.
31+
32+
The conduit reads its UCX configuration from the ``UCX_`` environment
33+
variables, like the MPI library, and from those with the prefix
34+
``HPX_DEVICE_CONDUIT_UCX_``, which take precedence and apply to the conduit
35+
only. The prefixed variables cover the protocol settings (e.g.
36+
``HPX_DEVICE_CONDUIT_UCX_TLS``, ``HPX_DEVICE_CONDUIT_UCX_RNDV_THRESH``); the
37+
settings of the memory domains and transports are read by UCX from the
38+
``UCX_`` variables only (e.g. ``UCX_ROCM_COPY_DMABUF=yes``, which GPUDirect
39+
RDMA with ROCm needs where it is available only through DMA-BUF). The
40+
configuration entry
41+
``hpx.parcel.conduit.ucx.rndv_thresh`` sets the size from which UCX uses its
42+
rendezvous protocol (default 16384 bytes) unless
43+
``HPX_DEVICE_CONDUIT_UCX_RNDV_THRESH`` is set.
44+
45+
The configuration entry ``hpx.parcel.conduit.ucx.error_handling`` (or
46+
``HPX_PARCEL_CONDUIT_UCX_ERROR_HANDLING``) sets the error handling of the
47+
endpoints, the same on all localities. With ``peer`` (the default), a locality
48+
that fails or leaves fails the transfers to it instead of hanging them, and the
49+
endpoints are closed at shutdown without waiting for the peers. UCX then does
50+
not use its pipelined protocol for staging device memory through host memory,
51+
which it needs where the network cannot access device memory (no GPUDirect
52+
RDMA, e.g. ConnectX-3 adapters): there, transfers between nodes are several
53+
times slower (4.7x for 8 MiB on V100 nodes with ConnectX-3). With ``none``,
54+
they are as fast as with a UCX based MPI library, but a locality leaving while
55+
transfers to or from it are in flight may hang the others, and closing the
56+
endpoints at shutdown waits for the peers. Use ``none`` only where no locality
57+
leaves before the others (no localities connecting and disconnecting
58+
dynamically).
59+
60+
See the :ref:`API reference <modules_device_conduit_ucx_api>` of this module
61+
for more details.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)