Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions cpp/benchmarks/bench_comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,7 @@ int main(int argc, char** argv) {
set_current_rmm_resource(args.rmm_mr);

rmm::device_async_resource_ref mr = cudf::get_current_device_resource_ref();
BufferResource br{
mr,
PinnedMemoryResource::Disabled,
{},
std::chrono::milliseconds{1},
std::make_shared<rmm::cuda_stream_pool>(
16, rmm::cuda_stream::flags::non_blocking
),
Comment thread
nirandaperera marked this conversation as resolved.
Resolved
stats
};
BufferResource br{stats, mr};

// Print benchmark/hardware info.
{
Expand Down
4 changes: 2 additions & 2 deletions cpp/benchmarks/bench_partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void BM_PartitionAndPack(benchmark::State& state) {

// Create a pool memory resource with 50% of GPU memory
rmm::mr::pool_memory_resource pool_mr{rmm::mr::cuda_memory_resource{}, pool_size};
rapidsmpf::BufferResource br{pool_mr};
rapidsmpf::BufferResource br{rapidsmpf::Statistics::disabled(), pool_mr};

// Create input table
auto table = create_int_table(num_rows, stream);
Expand Down Expand Up @@ -111,7 +111,7 @@ static void BM_PartitionAndPackCurrentImpl(benchmark::State& state) {

// Create a pool memory resource with 50% of GPU memory
rmm::mr::pool_memory_resource pool_mr{rmm::mr::cuda_memory_resource{}, pool_size};
rapidsmpf::BufferResource br{pool_mr};
rapidsmpf::BufferResource br{rapidsmpf::Statistics::disabled(), pool_mr};

// Create input table
auto table = create_int_table(num_rows, stream);
Expand Down
8 changes: 2 additions & 6 deletions cpp/benchmarks/bench_shuffle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,11 @@ int main(int argc, char** argv) {
// We're only going to measure the last run, so disable initially.
stats->disable();
rapidsmpf::BufferResource br{
stats,
stat_enabled_mr,
args.pinned_mem_disable ? rapidsmpf::PinnedMemoryResource::Disabled
: rapidsmpf::PinnedMemoryResource::make_if_available(),
std::move(memory_limits),
std::chrono::milliseconds{1},
std::make_shared<rmm::cuda_stream_pool>(
16, rmm::cuda_stream::flags::non_blocking
),
stats
std::move(memory_limits)
};

std::shared_ptr<rapidsmpf::Communicator> comm;
Expand Down
20 changes: 6 additions & 14 deletions cpp/benchmarks/streaming/bench_streaming_shuffle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ int main(int argc, char** argv) {

// Initialize configuration options from environment variables.
rapidsmpf::config::Options options{rapidsmpf::config::get_environment_variables()};
auto progress_thread = std::make_shared<rapidsmpf::ProgressThread>();
auto stats = rapidsmpf::Statistics::from_options(options);
auto progress_thread = std::make_shared<rapidsmpf::ProgressThread>(stats);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the existing behavior, I think unintentionally, from statistics being enabled by default to disabled by default. As is, users now require specifying RAPIDSMPF_STATISTICS=1, which I think is undesirable.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all benchmarks should have enabled statistics. So yes, I think you should create() the stats. Rather than from_options()ing them


std::shared_ptr<rapidsmpf::Communicator> comm;
if (args.comm_type == "mpi") {
Expand Down Expand Up @@ -364,20 +365,11 @@ int main(int argc, char** argv) {
memory_limits[rapidsmpf::MemoryType::DEVICE] = args.device_mem_limit_mb << 20;
}

auto stats = rapidsmpf::Statistics::create();

auto pinned_mr = args.pinned_mem_disable
? rapidsmpf::PinnedMemoryResource::Disabled
: rapidsmpf::PinnedMemoryResource::make_if_available();
auto br = std::make_shared<rapidsmpf::BufferResource>(
stat_enabled_mr,
pinned_mr,
std::move(memory_limits),
std::nullopt,
std::make_shared<rmm::cuda_stream_pool>(
16, rmm::cuda_stream::flags::non_blocking
),
stats
stats, stat_enabled_mr, pinned_mr, std::move(memory_limits)
Comment thread
nirandaperera marked this conversation as resolved.
Resolved
);

auto& log = *comm->logger();
Expand Down Expand Up @@ -411,7 +403,7 @@ int main(int argc, char** argv) {
for (std::uint64_t i = 0; i < total_num_runs; ++i) {
// Clear statistics before the last run so only the final run is reported.
if (i == total_num_runs - 1) {
ctx->statistics()->clear();
stats->clear();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the ctx not advertise a statistics object any more? That seems wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but its the same as stats in this scope

double const elapsed = run(ctx, comm, args, stream).count();
std::stringstream ss;
Expand Down Expand Up @@ -460,13 +452,13 @@ int main(int argc, char** argv) {

auto statistics = ctx->statistics();
Comment thread
nirandaperera marked this conversation as resolved.
Resolved
if (args.enable_memory_profiler) {
log.print(statistics->report({
log.print(stats->report({
.mr = stat_enabled_mr,
.pinned_mr = pinned_mr,
.header = "Statistics (of the last run):",
}));
} else {
log.print(statistics->report({.header = "Statistics (of the last run):"}));
log.print(stats->report({.header = "Statistics (of the last run):"}));
}

if (!use_bootstrap) {
Expand Down
4 changes: 2 additions & 2 deletions cpp/benchmarks/streaming/ndsh/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ create_context(
);

auto br = std::make_shared<BufferResource>(
statistics,
std::move(mr),
arguments.no_pinned_host_memory ? PinnedMemoryResource::Disabled
: PinnedMemoryResource::make_if_available(),
std::move(memory_limits),
arguments.periodic_spill,
std::make_shared<rmm::cuda_stream_pool>(
arguments.num_streams, rmm::cuda_stream::flags::non_blocking
),
statistics
)
);
auto environment = config::get_environment_variables();
environment["NUM_STREAMING_THREADS"] =
Expand Down
2 changes: 1 addition & 1 deletion cpp/examples/example_shuffle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char** argv) {
// We will use the same stream, memory, and buffer resource throughout the example.
rmm::cuda_stream_view stream = cudf::get_default_stream();
rmm::device_async_resource_ref mr = cudf::get_current_device_resource_ref();
rapidsmpf::BufferResource br{mr};
rapidsmpf::BufferResource br{stats, mr};

// As input data, we use a helper function from the benchmark suite. It creates a
// random cudf table with 2 columns and 100 rows. In this example, each MPI rank
Expand Down
4 changes: 3 additions & 1 deletion cpp/include/rapidsmpf/bootstrap/ucxx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ namespace bootstrap {
* @throws std::runtime_error if initialization fails.
*
* @code
* auto progress = std::make_shared<rapidsmpf::ProgressThread>();
* auto progress = std::make_shared<rapidsmpf::ProgressThread>(
* rapidsmpf::Statistics::disabled()
* );
* auto comm = rapidsmpf::bootstrap::create_ucxx_comm(progress);
* comm->logger().print("Hello from rank " + std::to_string(comm->rank()));
* @endcode
Expand Down
32 changes: 31 additions & 1 deletion cpp/include/rapidsmpf/communicator/communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <rapidsmpf/error.hpp>
#include <rapidsmpf/memory/buffer.hpp>
#include <rapidsmpf/progress_thread.hpp>
#include <rapidsmpf/statistics.hpp>

/**
* @namespace rapidsmpf
Expand Down Expand Up @@ -403,7 +404,21 @@ class Communicator {
};

protected:
Communicator() = default;
/**
* @brief Construct the base Communicator with a statistics instance.
*
* Derived classes typically forward `progress_thread->statistics()` here so
* that the communicator and its progress thread share the same `Statistics`
* instance.
*
* @param statistics The statistics instance to associate with this
* communicator. Must not be null.
*
* @throws std::invalid_argument If @p statistics is null.
*/
explicit Communicator(std::shared_ptr<Statistics> statistics);

std::shared_ptr<Statistics> statistics_; ///< Statistics instance. Never null.

public:
virtual ~Communicator() noexcept = default;
Expand Down Expand Up @@ -619,13 +634,28 @@ class Communicator {
[[nodiscard]] virtual std::shared_ptr<ProgressThread> const&
progress_thread() const = 0;

/**
* @brief Retrieves the statistics instance associated with this communicator.
*
* Satisfies the `StatisticsProvider` concept. Returns the instance supplied
* to the protected `Communicator` constructor by the concrete implementation
* (typically the same instance owned by `progress_thread()`).
*
* @return Shared pointer to the statistics instance (never null).
*/
[[nodiscard]] std::shared_ptr<Statistics> statistics() const noexcept {
return statistics_;
}

/**
* @brief Provides a string representation of the communicator.
* @return A string describing the communicator.
*/
[[nodiscard]] virtual std::string str() const = 0;
};

static_assert(StatisticsProvider<Communicator>);

/// @brief Whether RapidsMPF was built with the UCXX Communicator.
#ifdef RAPIDSMPF_HAVE_UCXX
constexpr bool COMM_HAVE_UCXX = true;
Expand Down
1 change: 1 addition & 0 deletions cpp/include/rapidsmpf/communicator/mpi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,6 @@ class MPI final : public Communicator {
std::shared_ptr<ProgressThread> progress_thread_;
};

static_assert(StatisticsProvider<MPI>);

} // namespace rapidsmpf
1 change: 1 addition & 0 deletions cpp/include/rapidsmpf/communicator/single.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,6 @@ class Single final : public Communicator {
std::shared_ptr<ProgressThread> progress_thread_;
};

static_assert(StatisticsProvider<Single>);

} // namespace rapidsmpf
2 changes: 2 additions & 0 deletions cpp/include/rapidsmpf/communicator/ucxx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,6 @@ class UCXX final : public Communicator {

} // namespace ucxx

static_assert(StatisticsProvider<ucxx::UCXX>);

} // namespace rapidsmpf
12 changes: 7 additions & 5 deletions cpp/include/rapidsmpf/memory/buffer_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class BufferResource {
* If pinned-host memory is disabled, available pinned-host memory is always reported
* as zero regardless of the configured limit.
*
* @param statistics The statistics instance to use. Pass `Statistics::disabled()`
* to opt out of statistics collection.
* @param device_mr The RMM device memory resource used for device allocations.
* @param pinned_mr The pinned host memory resource used for `MemoryType::PINNED_HOST`
* allocations. If disabled, pinned host allocations are unavailable regardless of
Expand All @@ -80,16 +82,15 @@ class BufferResource {
* periodic spill checking is performed.
* @param stream_pool Pool of CUDA streams used throughout RapidsMPF for operations
* that do not take an explicit CUDA stream.
* @param statistics The statistics instance to use (disabled by default).
*/
BufferResource(
std::shared_ptr<Statistics> statistics,
cuda::mr::any_resource<cuda::mr::device_accessible> device_mr,
std::optional<PinnedMemoryResource> pinned_mr = PinnedMemoryResource::Disabled,
std::unordered_map<MemoryType, std::int64_t> memory_limits = {},
std::optional<Duration> periodic_spill_check = std::chrono::milliseconds{1},
std::shared_ptr<rmm::cuda_stream_pool> stream_pool = std::make_shared<
rmm::cuda_stream_pool>(16, rmm::cuda_stream::flags::non_blocking),
std::shared_ptr<Statistics> statistics = Statistics::disabled()
rmm::cuda_stream_pool>(16, rmm::cuda_stream::flags::non_blocking)
);

/**
Expand All @@ -101,15 +102,16 @@ class BufferResource {
*
* @param mr A device-accessible RMM memory resource.
* @param options Configuration options.
* @param statistics The statistics instance to use (disabled by default).
* @param statistics The statistics instance to use. Pass `Statistics::disabled()`
* to opt out of statistics collection.
*
* @return A shared pointer to a BufferResource instance configured according to the
* options.
*/
static std::shared_ptr<BufferResource> from_options(
cuda::mr::any_resource<cuda::mr::device_accessible> mr,
config::Options options,
std::shared_ptr<Statistics> statistics = Statistics::disabled()
std::shared_ptr<Statistics> statistics
);

~BufferResource() noexcept = default;
Expand Down
5 changes: 3 additions & 2 deletions cpp/include/rapidsmpf/progress_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ class ProgressThread {
/**
* @brief Construct a new progress thread that can handle multiple functions.
*
* @param statistics The statistics instance to use (disabled by default).
* @param statistics The statistics instance to use. Pass `Statistics::disabled()`
* to opt out of statistics collection.
* @param sleep The duration to sleep between each progress loop iteration.
* If 0, the thread yields execution instead of sleeping. Anecdotally, a 1 us
* sleep time (the default) is sufficient to avoid starvation and get smooth
* progress.
*/
ProgressThread(
std::shared_ptr<Statistics> statistics = Statistics::disabled(),
std::shared_ptr<Statistics> statistics,
Duration sleep = std::chrono::microseconds{1}
);

Expand Down
18 changes: 18 additions & 0 deletions cpp/include/rapidsmpf/statistics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,24 @@ class Statistics : public std::enable_shared_from_this<Statistics> {
*/
static std::shared_ptr<Statistics> from_options(config::Options options);

/**
* @brief Resets the current state of this instance based on @p new_options.
*
* @warning Live `MemoryRecorder` instances (constructed before this call
* but not yet destroyed) are *not* tracked. Their destructors will run
* after the reset and publish their scope into the freshly cleared
* `memory_records_` map, mixing pre-reset measurements into the new
* session. Likewise, a `StreamOrderedTiming` whose stop callback is
* already mid-execution at reset time may still record one stale stat
* (its global-map entry was extracted before `cancel_inflight_timings`
* could remove it). Callers that need clean boundaries must quiesce
* recorders and synchronise the relevant CUDA streams before calling.
*
* @param new_options Configuration options whose `"statistics"` field
* controls the new enabled state.
*/
void reset_from_options(config::Options new_options);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you need this for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wence- In cudf-polars engines, there is a _reset_worker(...options) method. There, we are only resetting buffer resource, context, etc with a new stats object. Then, after calling _reset_worker, we would have multiple instances of statistics in the engine ([commuicator, progress thread] vs [buffer resource, context])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why do we need to reset the statistics in this setup? When is _reset_worker called? Is it only for tests? For every query, something else?

What do we actually need to achieve here? It seems like we want to zero the statistics object that we have around no?

@nirandaperera nirandaperera May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking into two options.

  1. Introduce set_stats for communicator and progress thread and reset the new stats object
  2. reset the stats object in-place (clear + enable/disable based on new options)

I think 1. is the safer option.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the mechanism for how to reset stats. I am asking a different question: why do we want to reset stats?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, _reset is only called in the tests (it is a "hack" to workaround that booting a dask or ray cluster is expensive), and I do not think that we have to tie ourselves in knots to publicly support that api also being able to reset statistics. Note that the docstring of _reset already says that it doesn't reset and reinitialize with all the options.

I think we have enough rope with .enable() and .clear() on the stats object, without introducing a new thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay. Let's do that.

@nirandaperera nirandaperera May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt clear() was insufficient because it only clears the stats, but not the memory records and report entries. So, we can have a scenario where we create a new Buffer resource with new rmm resource adapter, but stats contains old memory records from a previous buffer resource.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, but that problem can be fixed (separately) by clearing memory records

~Statistics() noexcept;

// `Statistics` is owned exclusively through `std::shared_ptr`. Use
Expand Down
6 changes: 3 additions & 3 deletions cpp/include/rapidsmpf/streaming/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class Context {
* @param logger The logger to use.
* @param options Configuration options used to initialize the Context and its
* components.
* @param statistics The statistics instance to use (disabled by default).
*
* @param statistics The statistics instance to use. Pass `Statistics::disabled()`
* to opt out of statistics collection.
* @return A fully initialized Context.
*
* @throws std::invalid_argument If an option value is invalid.
Expand All @@ -105,7 +105,7 @@ class Context {
RmmResourceAdaptor mr,
std::shared_ptr<Communicator::Logger> logger,
config::Options options,
std::shared_ptr<Statistics> statistics = Statistics::disabled()
std::shared_ptr<Statistics> statistics
);

// No copy constructor and assignment operator.
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/coll/allreduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ AllReduce::AllReduce(
out_buffer_->rebind_stream(in_buffer_->stream());
// Note: after this copy, we must check out_buffer's write event before receiving into
// in_buffer. See StartPreRemainder in the event loop.
// TODO: make Communicator statistics-aware, and pass its statistics instance here.
buffer_copy(Statistics::disabled(), *out_buffer_, *in_buffer_, in_buffer_->size);
buffer_copy(comm_->statistics(), *out_buffer_, *in_buffer_, in_buffer_->size);

auto const rank = comm_->rank();
if (rank < 2 * non_pow2_remainder_) {
Expand Down
14 changes: 14 additions & 0 deletions cpp/src/communicator/communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdexcept>
#include <utility>

#include <rapidsmpf/communicator/communicator.hpp>
#include <rapidsmpf/error.hpp>
#include <rapidsmpf/utils/string.hpp>

namespace rapidsmpf {

Communicator::Communicator(std::shared_ptr<Statistics> statistics)
: statistics_{std::move(statistics)} {
RAPIDSMPF_EXPECTS(
statistics_ != nullptr,
"Communicator statistics cannot be null",
std::invalid_argument
);
}

Comment thread
nirandaperera marked this conversation as resolved.
Resolved
namespace {
Communicator::Logger::LOG_LEVEL level_from_string(std::string const& str) {
if (str.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/communicator/mpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ MPI::MPI(
config::Options options,
std::shared_ptr<ProgressThread> progress_thread
)
: comm_{comm},
: Communicator{progress_thread->statistics()},
comm_{comm},
rank_{[comm]() {
int r;
RAPIDSMPF_MPI(MPI_Comm_rank(comm, &r));
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/communicator/single.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace rapidsmpf {


Single::Single(config::Options options, std::shared_ptr<ProgressThread> progress_thread)
: logger_{std::make_shared<Logger>(0, std::move(options))},
: Communicator{progress_thread->statistics()},
logger_{std::make_shared<Logger>(0, std::move(options))},
progress_thread_{std::move(progress_thread)} {}

std::unique_ptr<Communicator::Future> Single::send(
Expand Down
Loading
Loading