Context#
2026-03-31
5 min read time
The ucxx::Context class encapsulates a UCP context (ucp_context_h). It is the
top-level object in the UCXX hierarchy and must be created before any other UCXX objects.
Contexts are created via the ucxx::createContext() factory function.
#include <ucxx/context.h>
namespace ucxx
-
class Context : public ucxx::Component#
Component encapsulating the UCP context.
The UCP layer provides a handle to access its context in form of
ucp_context_hobject, this class encapsulates that object and provides methods to simplify its handling.Public Functions
- ~Context(
ucxx::Contextdestructor
- ConfigMap getConfig(
Get the context configuration.
The context configuration is a
ConfigMapcontaining entries of the UCX variables that were set upon creation of the UCP context. Only those variables known to UCP can be acquired.// context is `std::shared_ptr<ucxx::Context>` auto contextConfig = context->getConfig();
- Returns:
A
ConfigMapcorresponding to the context’s configuration.
- ucp_context_h getHandle(
Get the underlying
ucp_context_hhandle.Lifetime of the
ucp_context_hhandle is managed by theucxx::Contextobject and its ownership is non-transferrable. Once theucxx::Contextis destroyed the handle is not valid anymore, it is the user’s responsibility to ensure the owner’s lifetime while using the handle.// context is `std::shared_ptr<ucxx::Context>` ucp_context_h contextHandle = context->getHandle();
- Returns:
The underlying
ucp_context_hhandle
- std::string getInfo(
Get information from UCP context.
Get information from UCP context, including memory domains, transport resources, and other useful information. This method is a wrapper to
ucp_context_print_info.// context is `std::shared_ptr<ucxx::Context>` auto contextInfo = context->getInfo();
- Returns:
String containing context information
- uint64_t getFeatureFlags(
Get feature flags that were used to construct the UCP context.
Get feature flags that were used to construct the UCP context, this has the same value that was specified by the user when creating the
ucxx::Contextobject.// context is `std::shared_ptr<ucxx::Context>` uint64_t contextFeatureFlags= context->getFeatureFlags();
- Returns:
Feature flags for this context
- bool hasCudaSupport(
Query whether CUDA (or ROCm) support is available.
Query whether the UCP context has CUDA support available. This is a done through a combination of verifying whether CUDA/HIP memory support is available and
UCX_TLSallows CUDA to be enabled, essentiallyUCX_TLSmust explicitly be one of the following:Exactly
all;Contain a field starting with
cuda/rocm;Start with
^(disable all listed transports) and NOT contain a field named eithercudaorcuda_copy(rocmorrocm_copyon AMD).
- Returns:
Whether CUDA support is available.
- std::shared_ptr<Worker> createWorker(
- const bool enableDelayedSubmission = false,
- const bool enableFuture = false
Create a new
ucxx::Worker.Create a new
ucxx::Workeras a child of the currentucxx::Context. Theucxx::Contextwill retain ownership of theucxx::Workerand will not be destroyed until allucxx::Workerobjects are destroyed first.// context is `std::shared_ptr<ucxx::Context>` auto worker = context->createWorker(true);
- Parameters:
enableDelayedSubmission – [in] whether the worker should delay transfer requests to the worker thread.
enableFuture – [in] if
true, notifies the future associated with eachucxx::Request, currently used only byucxx::python::Worker.
- Returns:
Shared pointer to the
ucxx::Workerobject.
- std::shared_ptr<MemoryHandle> createMemoryHandle(
- const size_t size,
- void *buffer,
- const ucs_memory_type_t memoryType = UCS_MEMORY_TYPE_HOST
Create a new
std::shared_ptr<ucxx::memoryHandle>.Create a new
std::shared_ptr<ucxx::MemoryHandle>as a child of the currentucxx::Context. Theucxx::Contextwill retain ownership of the underlyingucxx::MemoryHandleand will not be destroyed until allucxx::MemoryHandleobjects are destroyed first.The allocation requires a
sizeand abuffer. The actual size of the allocation may be larger than requested, and can later be found calling thegetSize()method. Thebufferprovided may be either anullptr, in which case UCP will allocate a new memory region for it, or an already existing allocation, in which case UCP will only map it for RMA and it’s the caller’s responsibility to keepbufferalive until this object is destroyed.// `context` is `std::shared_ptr<ucxx::Context>` // Allocate a 128-byte buffer with UCP. auto memoryHandle = context->createMemoryHandle(128, nullptr); // Map an existing 128-byte buffer with UCP. size_t allocationSize = 128; auto buffer = new uint8_t[allocationSize]; auto memoryHandleFromBuffer = context->createMemoryHandle( allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) );
- Throws:
- Parameters:
size – [in] the minimum size of the memory allocation.
buffer – [in] the pointer to an existing allocation or
nullptrto allocate a new memory region.memoryType – [in] the type of memory the handle points to.
- Returns:
The
shared_ptr<ucxx::MemoryHandle>object
Public Static Attributes
-
static constexpr uint64_t defaultFeatureFlags = UCP_FEATURE_TAG | UCP_FEATURE_WAKEUP | UCP_FEATURE_STREAM | UCP_FEATURE_AM | UCP_FEATURE_RMA#
Suggested default context feature flags to use.
Friends
- friend std::shared_ptr<Context> createContext(
- ConfigMap ucxConfig,
- const uint64_t featureFlags
Constructor of
shared_ptr<ucxx::Context>.The constructor for a
shared_ptr<ucxx::Context>object. The default constructor is made private to ensure all UCXX objects are shared pointers for correct lifetime management.auto context = ucxx::createContext({}, UCP_FEATURE_WAKEUP | UCP_FEATURE_TAG);
- Parameters:
ucxConfig – [in] configurations overriding
UCX_*defaults and environment variables.featureFlags – [in] feature flags to be used at UCP context construction time.
- Returns:
The
shared_ptr<ucxx::Context>object
Factory Functions#
#include <ucxx/constructors.h>
- std::shared_ptr<Context> ucxx::createContext(
- const ConfigMap ucxConfig,
- const uint64_t featureFlags
The constructor for a
shared_ptr<ucxx::Context>object. The default constructor is made private to ensure all UCXX objects are shared pointers for correct lifetime management.auto context = ucxx::createContext({}, UCP_FEATURE_WAKEUP | UCP_FEATURE_TAG);
- Parameters:
ucxConfig – [in] configurations overriding
UCX_*defaults and environment variables.featureFlags – [in] feature flags to be used at UCP context construction time.
- Returns:
The
shared_ptr<ucxx::Context>object