Resources#

All resources which are specific to a computing environment like host or device are contained within, and managed by, raft::resources. This design simplifies the APIs and eases user burden by making the APIs opaque by default but allowing customization based on user preference.

Vocabulary#

#include <raft/core/resource/resource_types.hpp>

namespace raft::resource

enum resource_type#

Resource types can apply to any resource and don’t have to be host- or device-specific.

Values:

enumerator CUBLAS_HANDLE#
enumerator CUSOLVER_DN_HANDLE#
enumerator CUSOLVER_SP_HANDLE#
enumerator CUSPARSE_HANDLE#
enumerator CUDA_STREAM_VIEW#
enumerator CUDA_STREAM_POOL#
enumerator CUDA_STREAM_SYNC_EVENT#
enumerator COMMUNICATOR#
enumerator SUB_COMMUNICATOR#
enumerator DEVICE_PROPERTIES#
enumerator DEVICE_ID#
enumerator STREAM_VIEW#
enumerator THRUST_POLICY#
enumerator WORKSPACE_RESOURCE#
enumerator CUBLASLT_HANDLE#
enumerator CUSTOM#
enumerator LARGE_WORKSPACE_RESOURCE#
enumerator NCCL_COMM#
enumerator ROOT_RANK#
enumerator MULTI_GPU#
enumerator PINNED_MEMORY_RESOURCE#
enumerator MANAGED_MEMORY_RESOURCE#
enumerator DRY_RUN_FLAG#
enumerator LAST_KEY#
class resource#
#include <resource_types.hpp>

A resource constructs and contains an instance of some pre-determined object type and facades that object behind a common API.

Subclassed by raft::resource::comms_resource, raft::resource::cublas_resource, raft::resource::cublaslt_resource, raft::resource::cuda_event_resource, raft::resource::cuda_stream_pool_resource, raft::resource::cuda_stream_resource, raft::resource::cusolver_dn_resource, raft::resource::cusolver_sp_resource, raft::resource::cusparse_resource, raft::resource::custom_resource, raft::resource::device_id_resource, raft::resource::device_memory_resource, raft::resource::device_properties_resource, raft::resource::dry_run_flag_resource, raft::resource::limiting_memory_resource, raft::resource::managed_memory_resource, raft::resource::multi_gpu_resource, raft::resource::nccl_comm_resource, raft::resource::pinned_memory_resource, raft::resource::root_rank_resource, raft::resource::stream_view_resource, raft::resource::sub_comms_resource, raft::resource::thrust_policy_resource

class resource_factory#
#include <resource_types.hpp>

A resource factory knows how to construct an instance of a specific raft::resource::resource.

Subclassed by raft::resource::comms_resource_factory, raft::resource::cublas_resource_factory, raft::resource::cublaslt_resource_factory, raft::resource::cuda_stream_pool_resource_factory, raft::resource::cuda_stream_resource_factory, raft::resource::cusolver_dn_resource_factory, raft::resource::cusolver_sp_resource_factory, raft::resource::cusparse_resource_factory, raft::resource::custom_resource_factory, raft::resource::device_id_resource_factory, raft::resource::device_properties_resource_factory, raft::resource::dry_run_flag_resource_factory, raft::resource::large_workspace_resource_factory, raft::resource::managed_memory_resource_factory, raft::resource::multi_gpu_resource_factory, raft::resource::nccl_comm_resource_factory, raft::resource::pinned_memory_resource_factory, raft::resource::root_rank_resource_factory, raft::resource::stream_view_resource_factory, raft::resource::sub_comms_resource_factory, raft::resource::thrust_policy_resource_factory, raft::resource::workspace_resource_factory

Public Functions

virtual resource_type get_resource_type() = 0#

Return the resource_type associated with the current factory.

Returns:

resource_type corresponding to the current factory

virtual resource *make_resource() = 0#

Construct an instance of the factory’s underlying resource.

Returns:

resource instance

struct resource_cell#
#include <resource_types.hpp>

Shared cell holding a factory and a lazily-created resource.

Multiple raft::resources handles can share a cell via shared_ptr. Lazy initialization stores the concrete resource atomically, so all handles sharing the cell see the update. Explicit set replaces the shared_ptr<resource_cell> in the local handle, isolating the change.

Device Resources#

raft::device_resources is a convenience over using raft::resources directly. It provides accessor methods to retrieve resources such as the CUDA stream, stream pool, and handles to the various CUDA math libraries like cuBLAS and cuSOLVER.

#include <raft/core/device_resources.hpp>

namespace raft::core

class device_resources : public raft::resources#

Main resource container object that stores all necessary resources used for calling necessary device functions, cuda kernels and/or libraries.

Subclassed by raft::device_resources_snmg

Public Functions

inline device_resources(
cuda::stream_ref stream_view = cuda::stream_ref{cudaStreamPerThread},
std::shared_ptr<rmm::cuda_stream_pool> stream_pool = {nullptr},
std::optional<raft::mr::device_resource> workspace_resource = std::nullopt,
std::optional<std::size_t> allocation_limit = std::nullopt
)#

Construct a resources instance with a stream view and stream pool.

Parameters:
  • stream_view – [in] the default stream (which has the default per-thread stream if unspecified)

  • stream_pool – [in] the stream pool used (which has default of nullptr if unspecified)

  • workspace_resource – [in] an optional resource used by some functions for allocating temporary workspaces.

  • allocation_limit – [in] the total amount of memory in bytes available to the temporary workspace resources.

inline virtual ~device_resources()#

Destroys all held-up resources

inline void sync_stream(
cuda::stream_ref stream,
std::source_location location = std::source_location::current()
) const#

synchronize a stream on the current container

Parameters:
  • stream – [in] stream to synchronize

  • location – [in] the call site to blame for the errors; leave at its default

inline void sync_stream(
std::source_location location = std::source_location::current()
) const#

synchronize main stream on the current container

Parameters:

location – [in] the call site to blame for the errors; leave at its default

inline cuda::stream_ref get_stream() const#

returns main stream on the current container

inline bool is_stream_pool_initialized() const#

returns whether stream pool was initialized on the current container

inline const rmm::cuda_stream_pool &get_stream_pool() const#

returns stream pool on the current container

inline cuda::stream_ref get_stream_from_stream_pool() const#

return stream from pool

inline cuda::stream_ref get_stream_from_stream_pool(
std::size_t stream_idx
) const#

return stream from pool at index

inline cuda::stream_ref get_next_usable_stream() const#

return stream from pool if size > 0, else main stream on current container

inline cuda::stream_ref get_next_usable_stream(
std::size_t stream_idx
) const#

return stream from pool at index if size > 0, else main stream on current container

Parameters:

stream_idx – [in] the required index of the stream in the stream pool if available

inline void sync_stream_pool() const#

synchronize the stream pool on the current container

inline void sync_stream_pool(
const std::vector<std::size_t> stream_indices
) const#

synchronize subset of stream pool

Parameters:

stream_indices – [in] the indices of the streams in the stream pool to synchronize

inline void wait_stream_pool_on_stream() const#

ask stream pool to wait on last event in main stream

Device Resources Manager#

While raft::device_resources provides a convenient way to access device-related resources for a sequence of RAFT calls, it is sometimes useful to be able to limit those resources across an entire application. For instance, in highly multi-threaded applications, it can be helpful to limit the total number of streams rather than relying on the default stream per thread. raft::device_resources_manager offers a way to access raft::device_resources instances that draw from a limited pool of underlying device resources.

#include <raft/core/device_resources_manager.hpp>

namespace raft::core

Warning

doxygenclass: Cannot find class “raft::device_resources_manager” in doxygen xml output for project “RAFT” from directory: ../../cpp/doxygen/_xml/

SNMG Device Resources#

The raft::device_resources_snmg provides a convenient way to configure a SNMG (single-node multi-GPU) clique for MG algorithms. It initiates device-related resources for a set of devices. Calling nccl-related functions in raft/core/resource/nccl_comm.hpp will initialize nccl comm on each device resource. GPUs can be addressed and exchanges be made over multiple threads for performance.

#include <raft/core/device_resources_snmg.hpp>

namespace raft::core

class device_resources_snmg : public raft::device_resources#

SNMG (single-node multi-GPU) resource container object that tracks resources for each GPU. Note the device_resources_snmg object can also be used as a classic device_resources object. The associated resources will be the ones of the GPU used during object instantiation and a GPU switch operation will be ordered during the retrieval of said resources.

The device_resources_snmg class is intended to be used in a single process to manage several GPUs.

Public Functions

inline device_resources_snmg()#

Construct a SNMG resources instance with all available GPUs.

inline device_resources_snmg(const std::vector<int> &device_ids)#

Construct a SNMG resources instance with a subset of available GPUs.

Parameters:

device_ids – [in] List of device IDs to be used

inline device_resources_snmg(const device_resources_snmg &world)#

SNMG resources instance copy constructor.

Parameters:

world – [in] A SNMG resources instance

inline void set_memory_pool(int percent_of_free_memory)#

Set a memory pool on all GPUs of the multi-gpu world.

inline void set_stream_pool(std::size_t num_streams)#

Set a CUDA stream pool on all GPUs of the multi-GPU world.

Parameters:

num_streams – [in] Number of CUDA streams in each device’s pool

inline virtual bool has_resource_factory(
resource::resource_type resource_type
) const override#

Returns true if a resource_factory has been registered for the given resource_type, false otherwise.

Parameters:

resource_type – resource type to check

Returns:

true if resource_factory is registered for the given resource_type

Resource Functions#

Comms#

#include <raft/core/resource/comms.hpp>

namespace raft::resource

inline bool comms_initialized(resources const &res)#
inline comms::comms_t const &get_comms(resources const &res)#
inline void set_comms(
resources &res,
std::shared_ptr<comms::comms_t> communicator
)#

cuBLAS Handle#

#include <raft/core/resource/cublas_handle.hpp>

namespace raft::resource

inline cublasHandle_t get_cublas_handle(resources const &res)#

Load a cublasHandle_t from raft res if it exists, otherwise add it and return it.

Parameters:

res – [in] the raft resources object

Returns:

cublas handle

cuBLASLt Handle#

#include <raft/core/resource/cublaslt_handle.hpp>

namespace raft::resource

inline cublaslt_resource const &get_cublaslt_resource(
resources const &res
)#

Load the cuBLASLt resource from raft res if it exists, otherwise add it and return it.

Parameters:

res – [in] the raft resources object

Returns:

cuBLASLt resource

inline cublasLtHandle_t get_cublaslt_handle(resources const &res)#

Load a cublasLtHandle_t from raft res if it exists, otherwise add it and return it.

Parameters:

res – [in] the raft resources object

Returns:

cublasLt handle

inline std::size_t get_cublaslt_workspace_size(resources const &res)#

Get the maximum workspace size (in bytes) the cuBLASLt matmul wrappers may request from the workspace resource for a single call.

Parameters:

res – [in] the raft resources object

Returns:

workspace budget in bytes

inline void set_cublaslt_workspace_size(
resources &res,
std::size_t workspace_size
)#

Create a new cuBLASLt handle in the raft resources object with the given workspace budget.

The budget is the maximum workspace size (in bytes) the cuBLASLt matmul wrappers may allocate from the workspace resource for a single call; it is further capped by the workspace resource allocation limit. A larger budget lets cuBLASLt pick faster algorithms (e.g. split-K for skinny matrices); zero disables the workspace.

Parameters:
  • res – [in] the raft resources object

  • workspace_size – [in] workspace budget in bytes

CUDA Stream#

#include <raft/core/resource/cuda_stream.hpp>

namespace raft::resource

inline cuda::stream_ref get_cuda_stream(resources const &res)#

Load a cuda::stream_ref from a resources instance (and populate it on the res if needed).

Parameters:

res – raft res object for managing resources

Returns:

inline void set_cuda_stream(
resources &res,
cuda::stream_ref stream_view
)#

Set a cuda::stream_ref on a resources instance.

Parameters:
  • res – [in] raft resources object for managing resources

  • stream_view – [in] cuda stream reference

inline void sync_stream(
const resources &res,
cuda::stream_ref stream,
std::source_location location = std::source_location::current()
)#

synchronize a specific stream

Parameters:
  • res – [in] the raft resources object

  • stream – [in] stream to synchronize

  • location – [in] the call site to blame for the errors; leave at its default unless synchronizing on behalf of a caller, in which case forward the caller’s location.

inline void sync_stream(
const resources &res,
std::source_location location = std::source_location::current()
)#

synchronize main stream on the resources instance

Parameters:
  • res – [in] the raft resources object

  • location – [in] the call site to blame for the errors; leave at its default unless synchronizing on behalf of a caller, in which case forward the caller’s location.

CUDA Stream Pool#

#include <raft/core/resource/cuda_stream_pool.hpp>

namespace raft::resource

inline const rmm::cuda_stream_pool &get_cuda_stream_pool(
const resources &res
)#

Load a cuda_stream_pool, and create a new one if it doesn’t already exist

Parameters:

res – raft res object for managing resources

Returns:

inline void set_cuda_stream_pool(
resources &res,
std::shared_ptr<rmm::cuda_stream_pool> stream_pool
)#

Explicitly set a stream pool on the current res. Note that this will overwrite an existing stream pool on the res.

Parameters:
  • res –

  • stream_pool –

inline std::size_t get_stream_pool_size(const resources &res)#
inline cuda::stream_ref get_stream_from_stream_pool(
const resources &res
)#

return stream from pool

inline cuda::stream_ref get_stream_from_stream_pool(
const resources &res,
std::size_t stream_idx
)#

return stream from pool at index

inline cuda::stream_ref get_next_usable_stream(const resources &res)#

return stream from pool if size > 0, else main stream on res

inline cuda::stream_ref get_next_usable_stream(
const resources &res,
std::size_t stream_idx
)#

return stream from pool at index if size > 0, else main stream on res

Parameters:
  • res – [in] the raft resources object

  • stream_idx – [in] the required index of the stream in the stream pool if available

inline void sync_stream_pool(const resources &res)#

synchronize the stream pool on the res

Parameters:

res – [in] the raft resources object

inline void sync_stream_pool(
const resources &res,
const std::vector<std::size_t> stream_indices
)#

synchronize subset of stream pool

Parameters:
  • res – [in] the raft resources object

  • stream_indices – [in] the indices of the streams in the stream pool to synchronize

inline void wait_stream_pool_on_stream(const resources &res)#

ask stream pool to wait on last event in main stream

Parameters:

res – [in] the raft resources object

cuSolverDn Handle#

#include <raft/core/resource/cusolver_dn_handle.hpp> namespace raft::resource

inline cusolverDnHandle_t get_cusolver_dn_handle(
resources const &res
)#

Load a cusolverSpres_t from raft res if it exists, otherwise add it and return it.

Parameters:

res – [in] the raft resources object

Returns:

cusolver dn handle

class cusolver_dn_resource_factory : public raft::resource::resource_factory#
#include <cusolver_dn_handle.hpp>

Factory that knows how to construct a specific raft::resource to populate the res_t.

Public Functions

inline virtual resource_type get_resource_type() override#

Return the resource_type associated with the current factory.

Returns:

resource_type corresponding to the current factory

inline virtual resource *make_resource() override#

Construct an instance of the factory’s underlying resource.

Returns:

resource instance

cuSolverSp Handle#

#include <raft/core/resource/cusolver_sp_handle.hpp>

namespace raft::resource

inline cusolverSpHandle_t get_cusolver_sp_handle(
resources const &res
)#

Load a cusolverSpres_t from raft res if it exists, otherwise add it and return it.

Parameters:

res – [in] the raft resources object

Returns:

cusolver sp handle

cuSparse Handle#

#include <raft/core/resource/cusparse_handle.hpp>

namespace raft::resource

inline cusparseHandle_t get_cusparse_handle(resources const &res)#

Load a cusparseres_t from raft res if it exists, otherwise add it and return it.

Parameters:

res – [in] the raft resources object

Returns:

cusparse handle

Device ID#

#include <raft/core/resource/device_id.hpp>

namespace raft::resource

inline int get_device_id(resources const &res)#

Load a device id from a res (and populate it on the res if needed).

Parameters:

res – raft res object for managing resources

Returns:

device id

Device Memory Resource#

#include <raft/core/resource/device_memory_resource.hpp>

namespace raft::resource

inline explicit device_memory_resource(raft::mr::device_resource ar)#
~device_memory_resource() override = default#
inline virtual void *override get_resource ()
inline limiting_memory_resource(
raft::mr::device_resource ar,
std::size_t allocation_limit,
std::optional<std::size_t> alignment
)#
inline virtual void *override get_resource ()
~limiting_memory_resource() override = default
inline large_workspace_resource_factory()#
inline explicit large_workspace_resource_factory(
raft::mr::device_resource mr
)#
inline virtual resource_type get_resource_type() override#

Return the resource_type associated with the current factory.

Returns:

resource_type corresponding to the current factory

inline virtual resource *override make_resource ()

Construct an instance of the factory’s underlying resource.

Returns:

resource instance

inline explicit workspace_resource_factory(
raft::mr::device_resource mr = raft::mr::device_resource{rmm::mr::get_current_device_resource_ref()},
std::optional<std::size_t> allocation_limit = std::nullopt,
std::optional<std::size_t> alignment = std::nullopt
)#
inline virtual resource_type get_resource_type() override

Return the resource_type associated with the current factory.

Returns:

resource_type corresponding to the current factory

inline virtual resource *override make_resource ()

Construct an instance of the factory’s underlying resource.

Returns:

resource instance

static inline raft::mr::device_resource default_pool_resource(
std::size_t limit
)#

Construct a sensible default pool memory resource.

inline rmm::mr::limiting_resource_adaptor *get_workspace_resource(
resources const &res
)#

Load a temp workspace resource from a resources instance (and populate it on the res if needed).

Prefer get_workspace_resource_ref() for allocations and get_workspace_{total,used,free}_bytes() for accounting queries.

Parameters:

res – raft resources object for managing resources

Returns:

pointer to the workspace limiting_resource_adaptor

inline rmm::device_async_resource_ref get_workspace_resource_ref(
resources const &res
)#

Get the workspace as a non-owning device_async_resource_ref.

Parameters:

res – raft resources object for managing resources

Returns:

non-owning reference to the workspace device memory resource

inline size_t get_workspace_total_bytes(resources const &res)#

Get the total size of the workspace resource.

Parameters:

res – raft resources object for managing resources

Returns:

total allocation limit in bytes

inline size_t get_workspace_used_bytes(resources const &res)#

Get the already allocated size of the workspace resource.

Parameters:

res – raft resources object for managing resources

Returns:

currently allocated bytes

inline size_t get_workspace_free_bytes(resources const &res)#

Get the available size of the workspace resource.

Parameters:

res – raft resources object for managing resources

Returns:

free bytes (total limit minus allocated)

inline void set_workspace_resource(
resources &res,
raft::mr::device_resource mr,
std::optional<std::size_t> allocation_limit = std::nullopt,
std::optional<std::size_t> alignment = std::nullopt
)#

Set the workspace resource.

Parameters:
  • res – raft resources object for managing resources

  • mr – device memory resource

  • allocation_limit – the total amount of memory in bytes available to the temporary workspace resources.

  • alignment – optional alignment requirements passed to allocations

inline void set_workspace_to_pool_resource(
resources &res,
std::optional<std::size_t> allocation_limit = std::nullopt
)#

Set the temporary workspace resource to a pool on top of the global memory resource (rmm::mr::get_current_device_resource_ref()).

Parameters:
  • res – raft resources object for managing resources

  • allocation_limit – the total amount of memory in bytes available to the temporary workspace resources; if not provided, a last used or default limit is used.

inline void set_workspace_to_global_resource(
resources &res,
std::optional<std::size_t> allocation_limit = std::nullopt
)#

Set the temporary workspace resource the same as the global memory resource (rmm::mr::get_current_device_resource_ref()).

Note, the workspace resource is always limited; the limit here defines how much of the global memory resource can be consumed by the workspace allocations.

Parameters:
  • res – raft resources object for managing resources

  • allocation_limit – the total amount of memory in bytes available to the temporary workspace resources.

inline rmm::device_async_resource_ref get_large_workspace_resource_ref(
resources const &res
)#

Get the large workspace as a non-owning device_async_resource_ref.

Parameters:

res – raft resources object for managing resources

Returns:

non-owning reference to the large workspace device memory resource

inline void set_large_workspace_resource(
resources &res,
raft::mr::device_resource mr
)#

Set the large workspace resource.

Parameters:
  • res – raft resources object for managing resources

  • mr – device memory resource

class device_memory_resource : public raft::resource::resource#
#include <device_memory_resource.hpp>
class limiting_memory_resource : public raft::resource::resource#
#include <device_memory_resource.hpp>
class large_workspace_resource_factory : public raft::resource::resource_factory#
#include <device_memory_resource.hpp>

Factory that knows how to construct a specific raft::resource to populate the resources instance.

class workspace_resource_factory : public raft::resource::resource_factory#
#include <device_memory_resource.hpp>

Factory that knows how to construct a specific raft::resource to populate the resources instance.

Memory Statistics Resources#

#include <raft/core/memory_stats_resources.hpp>

namespace raft

struct memory_stats#

Snapshot of memory usage across the six tracked resource types.

Returned by accessor methods on dry_run_resources and memory_stats_resources (e.g. get_bytes_peak(), get_bytes_current()).

Public Functions

inline constexpr std::size_t total() const#

Sum of all memory stats across the six tracked categories.

The three resource wrapper classes (dry_run_resources, memory_stats_resources, memory_tracking_resources) guarantee that every category is tracked by its own independent adaptor: each wrapper force-initializes all resources, captures their upstream refs before replacing the global device resource, and wraps those originals. Workspace and large-workspace allocations therefore bypass the device-global tracking adaptor and are counted exactly once, making this sum an accurate total when used with stats produced by any of the three wrappers.

class memory_stats_resources : public raft::resources#

Resources handle that wraps all reachable memory resources with statistics adaptors to track actual allocation usage.

Inherits from raft::resources, so it can be passed anywhere a raft::resources& is expected. On construction the handle:

  • Materializes all tracked resource types (host, device, pinned, managed, workspace, large_workspace).

  • Takes a snapshot of the original resources to keep them alive.

  • Wraps each with statistics_adaptor.

  • Replaces global host and device resources with tracked versions.

On destruction the handle restores global resources.

Memory Tracking Resources#

#include <raft/core/memory_tracking_resources.hpp>

namespace raft

class memory_tracking_resources : public raft::resources#

A resources handle that wraps all reachable memory resources with allocation-tracking adaptors and logs CSV statistics from a background thread.

Inherits from raft::resources, so it can be passed anywhere a raft::resources& is expected. On construction the handle:

  • Materializes all tracked resource types (host, device, pinned, managed, workspace, large_workspace).

  • Takes a snapshot of the original resources to keep them alive.

  • Wraps each with statistics_adaptor + notifying_adaptor.

  • Replaces global host and device resources with tracked versions.

  • Starts a background CSV reporter.

On destruction the handle stops the reporter and restores the global host and device resources.

Public Functions

inline memory_tracking_resources(
const resources &existing,
std::ostream &out,
duration sample_interval = std::chrono::milliseconds{10}
)#

Construct from an existing resources handle, logging to an ostream.

Parameters:
  • existing – Resources to shallow-copy and wrap with tracking.

  • out – Output stream for CSV rows (must outlive this object).

  • sample_interval – Minimum time between successive CSV samples.

inline memory_tracking_resources(
const resources &existing,
const std::string &file_path,
duration sample_interval = std::chrono::milliseconds{10}
)#

Construct from an existing resources handle, logging to a file.

Parameters:
  • existing – Resources to shallow-copy and wrap with tracking.

  • file_path – Path to the output CSV file (created/truncated).

  • sample_interval – Minimum time between successive CSV samples.

inline explicit memory_tracking_resources(
std::ostream &out,
duration sample_interval = std::chrono::milliseconds{10}
)#

Construct from scratch (default resources), logging to an ostream.

Parameters:
  • out – Output stream for CSV rows (must outlive this object).

  • sample_interval – Minimum time between successive CSV samples.

inline explicit memory_tracking_resources(
const std::string &file_path,
duration sample_interval = std::chrono::milliseconds{10}
)#

Construct from scratch (default resources), logging to a file.

Parameters:
  • file_path – Path to the output CSV file (created/truncated).

  • sample_interval – Minimum time between successive CSV samples.

inline raft::mr::resource_monitor &report() noexcept#

Access the underlying CSV reporter (e.g. to read stats).

Dry-Run Resources#

#include <raft/core/dry_run_resources.hpp>

namespace raft

class dry_run_resources : public raft::resources#

Resources handle that wraps all reachable memory resources with dry-run adaptors and tracks peak allocation usage.

Inherits from raft::resources, so it can be passed anywhere a raft::resources& is expected. On construction the handle:

  • If dry-run mode is already active, does nothing (no-op).

  • Materializes all tracked resource types (host, device, pinned, managed, workspace, large_workspace).

  • Takes a snapshot of the original resources to keep them alive.

  • Wraps each with dry_run_resource.

  • Replaces global host and device resources with dry-run versions.

  • Sets the dry-run flag.

On destruction the handle resets the flag and restores global resources. Composable with memory_tracking_resources in either order.

Warning

This class swaps process-global memory resources (the default host resource and the current device resource). Only one dry-run region may be active per process at a time. A process-global swap cannot be made fully thread-safe: two overlapping dry-run regions on different threads could corrupt each other’s global state. Nested dry-run on the same handle chain is safe (it becomes a no-op because the dry-run flag is already set).

namespace raft::util

template<typename Action, typename ...Args>
raft::memory_stats raft::util::dry_run_execute(
const raft::resources &res,
Action &&action,
Args&&... args
)#

Execute an action in dry-run mode and return peak memory usage.

Creates an independent copy of the resources handle with all memory resources replaced by dry-run versions, executes the action, and returns peak usage stats.

The action receives the dry-run resources handle (as const raft::resources&) and can check the dry-run flag via raft::resource::get_dry_run_flag(res) to skip kernel execution.

raft::resources res;
auto stats = raft::util::dry_run_execute(res, [](const raft::resources& r) {
  my_algorithm(r);
});
std::cout << "Peak workspace: " << stats.device_workspace << " bytes\n";

Template Parameters:
  • Action – A callable with signature void(const raft::resources&, Args…).

  • Args – Additional argument types to forward to the action.

Parameters:
  • res – The raft resources handle.

  • action – The action to execute in dry-run mode.

  • args – Additional arguments to forward to the action.

Returns:

memory_stats with peak memory usage from the dry run.

Device Properties#

#include <raft/core/resource/device_properties.hpp>

namespace raft::resource

inline cudaDeviceProp &get_device_properties(resources const &res)#

Load a cudaDeviceProp from a res (and populate it on the res if needed).

Parameters:

res – raft res object for managing resources

Returns:

populated cuda device properties instance

class device_properties_resource_factory : public raft::resource::resource_factory#
#include <device_properties.hpp>

Factory that knows how to construct a specific raft::resource to populate the res_t.

Public Functions

inline virtual resource_type get_resource_type() override#

Return the resource_type associated with the current factory.

Returns:

resource_type corresponding to the current factory

inline virtual resource *make_resource() override#

Construct an instance of the factory’s underlying resource.

Returns:

resource instance

Sub Communicators#

#include <raft/core/resource/sub_comms.hpp>

namespace raft::resource

inline const comms::comms_t &get_subcomm(
const resources &res,
std::string key
)#
inline void set_subcomm(
resources const &res,
std::string key,
std::shared_ptr<comms::comms_t> subcomm
)#

Thrust Exec Policy#

#include <raft/core/resource/thrust_policy.hpp>

namespace raft::resource

inline rmm::exec_policy_nosync &get_thrust_policy(
resources const &res
)#

Load a thrust policy from a res (and populate it on the res if needed).

Parameters:

res – raft res object for managing resources

Returns:

thrust execution policy

Custom runtime-shared resources#

A custom resource is an arbitrary default-constructible C++ class. The consumer of the API can keep such a resource in the raft::resources handle. For example, consider a function that is expected to be called repeatedly and involves a costly kernel configuration. One can cache the kernel configuration in a custom resource. The cost of accessing it is one hashmap lookup.

#include <raft/core/resource/custom_resource.hpp>

namespace raft::resource

template<typename ResourceT>
ResourceT *get_custom_resource(
resources const &res
)#

Get the custom default-constructible resource if it exists, create it otherwise.

Note: in contrast to the other, hard-coded resources, there’s no information about the custom resources at compile time. Hence, custom resources are kept in a hashmap and looked-up at runtime. This leads to slightly slower access times.

Template Parameters:

ResourceT – the type of the resource; it must be complete and default-constructible.

Parameters:

res – [in] the raft resources object

Returns:

a pointer to the custom resource.