diff --git a/cpp/libcudf_streaming/tests/CMakeLists.txt b/cpp/libcudf_streaming/tests/CMakeLists.txt index b8ff8a876f74..76bcbea20839 100644 --- a/cpp/libcudf_streaming/tests/CMakeLists.txt +++ b/cpp/libcudf_streaming/tests/CMakeLists.txt @@ -50,11 +50,9 @@ target_sources( streaming/test_partition.cpp streaming/test_cudf_utils.cpp streaming/test_shuffler.cpp - streaming/test_allgather.cpp streaming/test_leaf_actor.cpp test_partition.cpp test_shuffler.cpp - test_shuffler_many_streams.cpp ) add_executable(cudf_streaming_single_tests main/single.cpp) diff --git a/cpp/libcudf_streaming/tests/streaming/test_allgather.cpp b/cpp/libcudf_streaming/tests/streaming/test_allgather.cpp deleted file mode 100644 index 7617ce156eaa..000000000000 --- a/cpp/libcudf_streaming/tests/streaming/test_allgather.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/** - * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights - * reserved. SPDX-License-Identifier: Apache-2.0 - */ - -#include "base_streaming_fixture.hpp" - -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -using namespace rapidsmpf; - -class StreamingAllGather : public BaseStreamingFixture, - public ::testing::WithParamInterface> { - public: - void SetUp() override { BaseStreamingFixture::SetUpWithThreads(std::get<0>(GetParam())); } - - void TearDown() override { BaseStreamingFixture::TearDown(); } -}; - -INSTANTIATE_TEST_SUITE_P(StreamingAllGather, - StreamingAllGather, - ::testing::Combine(::testing::Values(1, 4), - ::testing::Values(MemoryType::HOST, - MemoryType::DEVICE)), - [](testing::TestParamInfo const& info) { - return "nthreads_" + std::to_string(std::get<0>(info.param)) + - "_mem_type_" + - (std::get<1>(info.param) == MemoryType::HOST ? "HOST" : "DEVICE"); - }); - -TEST_P(StreamingAllGather, basic) -{ - auto mem_type = std::get<1>(GetParam()); - auto comm = GlobalEnvironment->comm_; - auto allgather = streaming::AllGather(ctx, comm, OpID{0}); - - int size = comm->nranks(); - int rank = comm->rank(); - - constexpr int n_inserts{100}; - - coro::latch latch(n_inserts); - auto insert = [&](int insertion_id, std::uint64_t sequence) -> coro::task { - std::vector data(size); - for (int i = 0; i < size; i++) { - data[i] = (n_inserts * rank + insertion_id) * size + i; - } - - auto br = ctx->br(); - auto buf = br->make_buffer(br->stream_pool().get_stream(), - br->reserve_or_fail(data.size() * sizeof(int), mem_type)); - buf->write_access([&](std::byte* buf_data, rmm::cuda_stream_view stream) { - RAPIDSMPF_CUDA_TRY( - cuda_memcpy_async(buf_data, data.data(), data.size() * sizeof(int), stream)); - }); - auto meta = std::make_unique>(sizeof(int)); - std::memcpy(meta->data(), &size, sizeof(int)); - allgather.insert(sequence, PackedData{std::move(meta), std::move(buf)}); - latch.count_down(); - co_return; - }; - auto insert_finished = [&]() -> coro::task { - co_await latch; - allgather.insert_finished(); - }; - std::vector result(size * size * n_inserts); - - auto extract = [&]() -> coro::task { - auto data = co_await allgather.extract_all(streaming::AllGather::Ordered::YES); - std::size_t offset{0}; - for (auto& pd : data) { - RAPIDSMPF_EXPECTS(pd.metadata->size() == sizeof(int), "Invalid metadata buffer size"); - int msize; - std::memcpy(&msize, pd.metadata->data(), sizeof(int)); - RAPIDSMPF_EXPECTS(msize == size, "Corrupted metadata value"); - RAPIDSMPF_CUDA_TRY(cuda_memcpy_async( - result.data() + offset, pd.data->data(), pd.data->size, pd.data->stream())); - offset += msize; - pd.data->stream().synchronize(); - } - }; - - std::vector> pipeline{}; - // Insertions can all run concurrently. - for (int i = 0; i < n_inserts; i++) { - pipeline.push_back(ctx->executor()->schedule(insert(i, i))); - } - pipeline.push_back(ctx->executor()->schedule(insert_finished())); - pipeline.push_back(ctx->executor()->schedule(extract())); - streaming::run_actor_network(std::move(pipeline)); - std::vector expected(size * size * n_inserts); - std::iota(expected.begin(), expected.end(), 0); - EXPECT_EQ(expected, result); -} - -TEST_P(StreamingAllGather, streaming_actor) -{ - auto mem_type = std::get<1>(GetParam()); - - auto ch_in = ctx->create_channel(); - auto ch_out = ctx->create_channel(); - - auto comm = GlobalEnvironment->comm_; - int size = comm->nranks(); - int rank = comm->rank(); - - constexpr int n_inserts{100}; - std::vector input_messages; - for (int insertion_id = 0; insertion_id < n_inserts; insertion_id++) { - std::vector data(size); - for (int i = 0; i < size; i++) { - data[i] = (n_inserts * rank + insertion_id) * size + i; - } - - auto br = ctx->br(); - auto buf = br->make_buffer(br->stream_pool().get_stream(), - br->reserve_or_fail(data.size() * sizeof(int), mem_type)); - buf->write_access([&](std::byte* buf_data, rmm::cuda_stream_view stream) { - RAPIDSMPF_CUDA_TRY( - cuda_memcpy_async(buf_data, data.data(), data.size() * sizeof(int), stream)); - }); - auto meta = std::make_unique>(sizeof(int)); - std::memcpy(meta->data(), &size, sizeof(int)); - input_messages.emplace_back(streaming::to_message( - insertion_id, std::make_unique(std::move(meta), std::move(buf)))); - } - std::vector output_messages; - std::vector> pipeline{}; - pipeline.push_back(ctx->executor()->schedule( - streaming::actor::push_to_channel(ctx, ch_in, std::move(input_messages)))); - pipeline.push_back( - ctx->executor()->schedule(streaming::actor::allgather(ctx, comm, ch_in, ch_out, OpID{0}))); - pipeline.push_back( - ctx->executor()->schedule(streaming::actor::pull_from_channel(ctx, ch_out, output_messages))); - streaming::run_actor_network(std::move(pipeline)); - std::vector actual(size * size * n_inserts); - std::size_t offset{0}; - for (auto& msg : output_messages) { - auto pd = msg.release(); - RAPIDSMPF_EXPECTS(pd.metadata->size() == sizeof(int), "Invalid metadata buffer size"); - int msize; - std::memcpy(&msize, pd.metadata->data(), sizeof(int)); - RAPIDSMPF_EXPECTS(msize == size, "Corrupted metadata value"); - RAPIDSMPF_CUDA_TRY( - cuda_memcpy_async(actual.data() + offset, pd.data->data(), pd.data->size, pd.stream())); - offset += msize; - pd.stream().synchronize(); - } - std::vector expected(size * size * n_inserts); - std::iota(expected.begin(), expected.end(), 0); - EXPECT_EQ(expected, actual); -} diff --git a/cpp/libcudf_streaming/tests/streaming/test_leaf_actor.cpp b/cpp/libcudf_streaming/tests/streaming/test_leaf_actor.cpp index 4423ef1e863e..69843484769b 100644 --- a/cpp/libcudf_streaming/tests/streaming/test_leaf_actor.cpp +++ b/cpp/libcudf_streaming/tests/streaming/test_leaf_actor.cpp @@ -71,256 +71,3 @@ TEST_F(StreamingLeafTasks, PushAndPullChunks) expects[i].view()); } } - -namespace { -Actor shutdown(std::shared_ptr ctx, - std::shared_ptr ch, - std::vector&& tasks) -{ - ShutdownAtExit c{ch}; - coro_results(co_await coro::when_all(std::move(tasks))); - co_await ch->drain(ctx->executor()); -} - -Actor producer(std::shared_ptr ctx, - std::shared_ptr ch, - int val, - bool should_throw = false) -{ - co_await ctx->executor()->schedule(); - auto ticket = co_await ch->acquire(); - auto [_, receipt] = - co_await ticket.send(Message{0, std::make_unique(val), ContentDescription{}}); - if (should_throw) { throw std::runtime_error("Producer throws"); } - EXPECT_THROW(co_await ticket.send(Message{0, std::make_unique(val), ContentDescription{}}), - std::logic_error); - co_await receipt; - EXPECT_TRUE(receipt.is_ready()); -} - -Actor consumer(std::shared_ptr ctx, - std::shared_ptr ch, - std::atomic& result, - bool should_throw = false) -{ - ShutdownAtExit c{ch}; - co_await ctx->executor()->schedule(); - while (true) { - auto msg = co_await ch->receive(); - if (should_throw) { throw std::runtime_error("Consumer throws"); } - if (msg.empty()) { break; } - auto val = msg.release(); - result.fetch_add(val, std::memory_order_relaxed); - } -} -} // namespace - -TEST_F(StreamingLeafTasks, ThrottledAdaptor) -{ - auto ch = ctx->create_channel(); - auto throttle = std::make_shared(ch, 4); - std::vector producers; - std::vector consumers; - constexpr int n_producer{100}; - constexpr int n_consumer{3}; - for (int i = 0; i < n_producer; i++) { - producers.push_back(producer(ctx, throttle, i)); - } - consumers.push_back(shutdown(ctx, ch, std::move(producers))); - std::atomic result{0}; - for (int i = 0; i < n_consumer; i++) { - consumers.push_back(consumer(ctx, ch, result)); - } - run_actor_network(std::move(consumers)); - EXPECT_EQ(result, ((n_producer - 1) * n_producer) / 2); -} - -TEST_F(StreamingLeafTasks, ThrottledAdaptorThrowInProduce) -{ - auto ch = ctx->create_channel(); - auto throttle = std::make_shared(ch, 4); - std::vector producers; - std::vector consumers; - constexpr int n_producer{10}; - for (int i = 0; i < n_producer; i++) { - producers.push_back(producer(ctx, throttle, i, i == 2)); - } - consumers.push_back(shutdown(ctx, ch, std::move(producers))); - std::atomic result; - consumers.push_back(consumer(ctx, ch, result)); - EXPECT_THROW(run_actor_network(std::move(consumers)), std::runtime_error); -} - -TEST_F(StreamingLeafTasks, ThrottledAdaptorThrowInConsume) -{ - auto ch = ctx->create_channel(); - auto throttle = std::make_shared(ch, 4); - std::vector producers; - std::vector consumers; - constexpr int n_producer{100}; - constexpr int n_consumer{3}; - for (int i = 0; i < n_producer; i++) { - producers.push_back(producer(ctx, throttle, i)); - } - consumers.push_back(shutdown(ctx, ch, std::move(producers))); - std::atomic result; - for (int i = 0; i < n_consumer; i++) { - consumers.push_back(consumer(ctx, ch, result, i == 1)); - } - EXPECT_THROW(run_actor_network(std::move(consumers)), std::runtime_error); -} - -class StreamingThrottledAdaptor : public StreamingLeafTasks, - public ::testing::WithParamInterface {}; - -INSTANTIATE_TEST_SUITE_P(InvalidMaxTickets, StreamingThrottledAdaptor, ::testing::Values(-1, 0)); - -TEST_P(StreamingThrottledAdaptor, NonPositiveThrottleThrows) -{ - if (GlobalEnvironment->comm_->rank() != 0) { - // Test is independent of size of communicator. - GTEST_SKIP() << "Test only runs on rank zero"; - } - int max_tickets = GetParam(); - auto ch = ctx->create_channel(); - EXPECT_THROW(ThrottlingAdaptor(ch, max_tickets), std::logic_error); -} - -using StreamingBoundedQueue = StreamingLeafTasks; - -TEST_F(StreamingBoundedQueue, TicketUseOnce) -{ - if (GlobalEnvironment->comm_->rank() != 0) { - // Test is independent of size of communicator. - GTEST_SKIP() << "Test only runs on rank zero"; - } - - auto q = ctx->create_bounded_queue(2); - - auto producer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - auto ticket = co_await q->acquire(); - EXPECT_TRUE(ticket.has_value()); - auto sent = co_await ticket->send(Message{0, std::make_unique(0), ContentDescription{}}); - EXPECT_TRUE(sent); - EXPECT_THROW(co_await ticket->send(Message{1, std::make_unique(1), ContentDescription{}}), - std::logic_error); - }; - - coro::sync_wait(producer(q)); -} - -TEST_F(StreamingBoundedQueue, ShutdownStopsProducer) -{ - if (GlobalEnvironment->comm_->rank() != 0) { - // Test is independent of size of communicator. - GTEST_SKIP() << "Test only runs on rank zero"; - } - - auto q = ctx->create_bounded_queue(2); - - auto producer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - while (true) { - auto ticket = co_await q->acquire(); - // We're going to shutdown before the producer gets to go. - EXPECT_TRUE(!ticket.has_value()); - if (!ticket.has_value()) { break; } - } - }; - coro::sync_wait(q->shutdown()); - coro::sync_wait(producer(q)); -} - -TEST_F(StreamingBoundedQueue, ProducerThrows) -{ - if (GlobalEnvironment->comm_->rank() != 0) { - // Test is independent of size of communicator. - GTEST_SKIP() << "Test only runs on rank zero"; - } - - auto q = ctx->create_bounded_queue(2); - - auto producer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - throw std::runtime_error("Producer throws"); - }; - auto consumer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - auto [receipt, msg] = co_await q->receive(); - EXPECT_TRUE(msg.empty()); - }; - EXPECT_THROW(coro_results(coro::sync_wait(coro::when_all(consumer(q), producer(q)))), - std::runtime_error); -} - -TEST_F(StreamingBoundedQueue, ConsumerThrows) -{ - if (GlobalEnvironment->comm_->rank() != 0) { - // Test is independent of size of communicator. - GTEST_SKIP() << "Test only runs on rank zero"; - } - - auto q = ctx->create_bounded_queue(2); - - auto producer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - auto ticket = co_await q->acquire(); - EXPECT_TRUE(ticket.has_value()); - EXPECT_TRUE(co_await ticket->send(Message{0, std::make_unique(1), ContentDescription{}})); - while (true) { - auto ticket = co_await q->acquire(); - if (!ticket.has_value()) { break; } - } - }; - auto consumer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - auto [receipt, msg] = co_await q->receive(); - EXPECT_FALSE(msg.empty()); - EXPECT_EQ(msg.sequence_number(), 0); - EXPECT_EQ(msg.release(), 1); - co_await receipt; - throw std::runtime_error("Consumer throws"); - }; - EXPECT_THROW(coro_results(coro::sync_wait(coro::when_all(consumer(q), producer(q)))), - std::runtime_error); -} - -TEST_F(StreamingBoundedQueue, MultipleAcquire) -{ - if (GlobalEnvironment->comm_->rank() != 0) { - // Test is independent of size of communicator. - GTEST_SKIP() << "Test only runs on rank zero"; - } - - auto q = ctx->create_bounded_queue(2); - - auto producer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - int i = 0; - while (true) { - auto ticket = co_await q->acquire(); - if (!ticket.has_value()) { - EXPECT_EQ(i, 2); - break; - } - EXPECT_TRUE(ticket.has_value()); - EXPECT_TRUE(co_await ticket->send( - Message{static_cast(i), std::make_unique(i), ContentDescription{}})); - i++; - } - }; - auto consumer = [](std::shared_ptr q) -> coro::task { - auto shutdown = q->raii_shutdown(); - // Receiving, but not releasing the tickets - auto [_, msg] = co_await q->receive(); - EXPECT_FALSE(msg.empty()); - EXPECT_EQ(msg.sequence_number(), 0); - EXPECT_EQ(msg.release(), 0); - std::tie(std::ignore, msg) = co_await q->receive(); - EXPECT_FALSE(msg.empty()); - EXPECT_EQ(msg.sequence_number(), 1); - EXPECT_EQ(msg.release(), 1); - }; - coro_results(coro::sync_wait(coro::when_all(consumer(q), producer(q)))); -} diff --git a/cpp/libcudf_streaming/tests/streaming/test_partition.cpp b/cpp/libcudf_streaming/tests/streaming/test_partition.cpp index 945bb0364488..6dad58788bed 100644 --- a/cpp/libcudf_streaming/tests/streaming/test_partition.cpp +++ b/cpp/libcudf_streaming/tests/streaming/test_partition.cpp @@ -72,71 +72,3 @@ TEST_F(StreamingPartition, PackUnpackRoundTrip) sort_table(expects[i].view())); } } - -TEST_F(StreamingPartition, PartitionMapChunkToMessage) -{ - constexpr std::uint64_t seq = 42; - std::unordered_map data; - data.emplace(0, generate_packed_data(10, 0, stream, *br)); - data.emplace(1, generate_packed_data(10, 10, stream, *br)); - auto chunk = std::make_unique(std::move(data)); - - rapidsmpf::streaming::Message m = to_message(seq, std::move(chunk)); - EXPECT_FALSE(m.empty()); - EXPECT_TRUE(m.holds()); - EXPECT_TRUE(m.content_description().spillable()); - EXPECT_EQ(m.content_description().content_size(rapidsmpf::MemoryType::HOST), 0); - EXPECT_EQ(m.content_description().content_size(rapidsmpf::MemoryType::DEVICE), 80); - EXPECT_EQ(m.sequence_number(), seq); - - auto res = br->reserve_or_fail(m.copy_cost(), rapidsmpf::MemoryType::DEVICE); - rapidsmpf::streaming::Message m2 = m.copy(res); - EXPECT_EQ(res.size(), 0); - EXPECT_FALSE(m2.empty()); - EXPECT_TRUE(m2.holds()); - EXPECT_TRUE(m2.content_description().spillable()); - EXPECT_EQ(m2.content_description().content_size(rapidsmpf::MemoryType::HOST), 0); - EXPECT_EQ(m2.content_description().content_size(rapidsmpf::MemoryType::DEVICE), 80); - - auto chunk2 = m2.release(); - validate_packed_data(std::move(chunk2.data.at(0)), 10, 0, stream, *br); - validate_packed_data(std::move(chunk2.data.at(1)), 10, 10, stream, *br); -} - -TEST_F(StreamingPartition, PartitionMapChunkContentDescription) -{ - // Create a packed data, one in device and one in host memory. - std::unordered_map data; - auto pack1 = generate_packed_data(5, 0, stream, *br); - auto pack1_size = pack1.data->size; - auto pack2_size = pack1.data->size * 2; - auto res = br->reserve_or_fail(pack2_size, rapidsmpf::MemoryType::HOST); - auto pack2 = generate_packed_data(10, 0, stream, *br).copy(res); - data.emplace(0, std::move(pack1)); - data.emplace(1, std::move(pack2)); - - auto chunk = std::make_unique(std::move(data)); - auto cd = get_content_description(*chunk); - EXPECT_TRUE(cd.spillable()); - EXPECT_EQ(cd.content_size(rapidsmpf::MemoryType::DEVICE), pack1_size); - EXPECT_EQ(cd.content_size(rapidsmpf::MemoryType::HOST), pack2_size); -} - -TEST_F(StreamingPartition, PartitionVectorChunkContentDescription) -{ - // Create a packed data, one in device and one in host memory. - std::vector data; - auto pack1 = generate_packed_data(5, 0, stream, *br); - auto pack1_size = pack1.data->size; - auto pack2_size = pack1.data->size * 2; - auto res = br->reserve_or_fail(pack2_size, rapidsmpf::MemoryType::HOST); - auto pack2 = generate_packed_data(10, 0, stream, *br).copy(res); - data.push_back(std::move(pack1)); - data.push_back(std::move(pack2)); - - auto chunk = std::make_unique(std::move(data)); - auto cd = get_content_description(*chunk); - EXPECT_TRUE(cd.spillable()); - EXPECT_EQ(cd.content_size(rapidsmpf::MemoryType::DEVICE), pack1_size); - EXPECT_EQ(cd.content_size(rapidsmpf::MemoryType::HOST), pack2_size); -} diff --git a/cpp/libcudf_streaming/tests/streaming/test_shuffler.cpp b/cpp/libcudf_streaming/tests/streaming/test_shuffler.cpp index af2e87f61d00..f32c1dba6dc5 100644 --- a/cpp/libcudf_streaming/tests/streaming/test_shuffler.cpp +++ b/cpp/libcudf_streaming/tests/streaming/test_shuffler.cpp @@ -30,33 +30,7 @@ using cudf_streaming::streaming::TableChunk; namespace actor = rapidsmpf::streaming::actor; namespace cs_actor = cudf_streaming::streaming::actor; -class BaseStreamingShuffle : public BaseStreamingFixture {}; - -TEST_F(BaseStreamingShuffle, zero_owned_partitions_completes) -{ - auto comm = GlobalEnvironment->comm_; - if (comm->nranks() < 2) { - GTEST_SKIP() << "Need at least 2 ranks so that some rank owns 0 partitions"; - } - constexpr Rank owner = 0; - auto collapse = [](std::shared_ptr const&, - shuffler::PartID, - shuffler::PartID) -> Rank { return owner; }; - constexpr OpID op_id = 0; - constexpr shuffler::PartID total = 4; - auto shuffler = std::make_unique(ctx, comm, op_id, total, collapse); - - coro::sync_wait(shuffler->insert_finished()); - - auto local_pids = shuffler->local_partitions(); - if (comm->rank() == owner) { - EXPECT_EQ(local_pids.size(), total); - } else { - EXPECT_TRUE(local_pids.empty()); - } -} - -class StreamingShuffler : public BaseStreamingShuffle, public ::testing::WithParamInterface { +class StreamingShuffler : public BaseStreamingFixture, public ::testing::WithParamInterface { public: const unsigned int num_partitions = 10; const unsigned int num_rows = 1000; @@ -66,86 +40,9 @@ class StreamingShuffler : public BaseStreamingShuffle, public ::testing::WithPar const cudf::hash_id hash_function = cudf::hash_id::HASH_MURMUR3; const OpID op_id = 0; - void SetUp() override { BaseStreamingShuffle::SetUpWithThreads(GetParam()); } - - void TearDown() override { BaseStreamingShuffle::TearDown(); } - - void run_test(auto make_shuffler_actor_fn) - { - // Create the full input table and slice it into chunks. - cudf::table full_input_table = random_table_with_index(seed, num_rows, 0, 10); - std::vector input_chunks; - for (unsigned int i = 0; i < num_chunks; ++i) { - input_chunks.emplace_back( - to_message(i, - std::make_unique( - std::make_unique( - cudf::slice(full_input_table, - {static_cast(i * chunk_size), - static_cast((i + 1) * chunk_size)}, - stream) - .at(0), - stream, - ctx->br()->device_mr()), - stream))); - } - - // Create and run the streaming pipeline. - std::vector output_chunks; - { - std::vector actors; - auto ch1 = ctx->create_channel(); - actors.push_back(actor::push_to_channel(ctx, ch1, std::move(input_chunks))); - - auto ch2 = ctx->create_channel(); - actors.push_back( - cs_actor::partition_and_pack(ctx, ch1, ch2, {1}, num_partitions, hash_function, seed)); - - auto ch3 = ctx->create_channel(); - actors.emplace_back(make_shuffler_actor_fn(ch2, ch3)); - - auto ch4 = ctx->create_channel(); - actors.push_back(cs_actor::unpack_and_concat(ctx, ch3, ch4)); - - actors.push_back(actor::pull_from_channel(ctx, ch4, output_chunks)); - - run_actor_network(std::move(actors)); - } - - auto comm = GlobalEnvironment->comm_; - std::unique_ptr expected_table; - if (comm->nranks() == 1) { // full_input table is expected - expected_table = std::make_unique(std::move(full_input_table)); - } else { // full_input table is replicated on all ranks - // local partitions - auto [table, offsets] = - cudf::hash_partition(full_input_table.view(), {1}, num_partitions, hash_function, seed); - - auto local_pids = - shuffler::Shuffler::local_partitions(comm, num_partitions, shuffler::Shuffler::round_robin); - - // every partition is replicated on all ranks - std::vector expected_tables; - for (auto pid : local_pids) { - auto t_view = cudf::slice(table->view(), {offsets[pid], offsets[pid + 1]}).at(0); - // this will be replicated on all ranks - for (rapidsmpf::Rank rank = 0; rank < comm->nranks(); ++rank) { - expected_tables.push_back(t_view); - } - } - expected_table = cudf::concatenate(expected_tables); - } + void SetUp() override { BaseStreamingFixture::SetUpWithThreads(GetParam()); } - // Concat all output chunks to a single table. - std::vector output_chunks_as_views; - for (auto& chunk : output_chunks) { - output_chunks_as_views.push_back(chunk.get().table_view()); - } - auto result_table = cudf::concatenate(output_chunks_as_views); - - CUDF_TEST_EXPECT_TABLES_EQUIVALENT(sort_table(result_table->view()), - sort_table(expected_table->view())); - } + void TearDown() override { BaseStreamingFixture::TearDown(); } }; INSTANTIATE_TEST_SUITE_P(StreamingShuffler, @@ -157,68 +54,78 @@ INSTANTIATE_TEST_SUITE_P(StreamingShuffler, TEST_P(StreamingShuffler, basic_shuffler) { - EXPECT_NO_FATAL_FAILURE(run_test([&](auto ch_in, auto ch_out) -> Actor { - return actor::shuffler(ctx, GlobalEnvironment->comm_, ch_in, ch_out, op_id, num_partitions); - })); -} + // Create the full input table and slice it into chunks. + cudf::table full_input_table = random_table_with_index(seed, num_rows, 0, 10); + std::vector input_chunks; + for (unsigned int i = 0; i < num_chunks; ++i) { + input_chunks.emplace_back( + to_message(i, + std::make_unique( + std::make_unique( + cudf::slice(full_input_table, + {static_cast(i * chunk_size), + static_cast((i + 1) * chunk_size)}, + stream) + .at(0), + stream, + ctx->br()->device_mr()), + stream))); + } -class ShufflerAsyncTest - : public BaseStreamingShuffle, - public ::testing::WithParamInterface> { - protected: - std::size_t n_inserts; - std::uint32_t n_partitions; + // Create and run the streaming pipeline. + std::vector output_chunks; + { + std::vector actors; + auto ch1 = ctx->create_channel(); + actors.push_back(actor::push_to_channel(ctx, ch1, std::move(input_chunks))); - static constexpr OpID op_id = 0; - static constexpr std::size_t n_elements = 100; + auto ch2 = ctx->create_channel(); + actors.push_back( + cs_actor::partition_and_pack(ctx, ch1, ch2, {1}, num_partitions, hash_function, seed)); - void SetUp() override - { - std::tie(n_inserts, n_partitions) = GetParam(); + auto ch3 = ctx->create_channel(); + actors.emplace_back( + actor::shuffler(ctx, GlobalEnvironment->comm_, ch2, ch3, op_id, num_partitions)); - BaseStreamingShuffle::SetUpWithThreads(4); - } + auto ch4 = ctx->create_channel(); + actors.push_back(cs_actor::unpack_and_concat(ctx, ch3, ch4)); - void TearDown() override { BaseStreamingShuffle::TearDown(); } -}; + actors.push_back(actor::pull_from_channel(ctx, ch4, output_chunks)); -INSTANTIATE_TEST_SUITE_P(StreamingShuffler, - ShufflerAsyncTest, - ::testing::Combine(::testing::Values(1, 10), // number of inserts - ::testing::Values(1, 10, 100) // number of partitions - ), - [](const testing::TestParamInfo& info) { - return "ninserts_" + std::to_string(std::get<0>(info.param)) + - "_nparts_" + std::to_string(std::get<1>(info.param)); - }); + run_actor_network(std::move(actors)); + } -TEST_P(ShufflerAsyncTest, insert_wait_extract) -{ - auto comm = GlobalEnvironment->comm_; - auto shuffler = std::make_unique(ctx, comm, op_id, n_partitions); - - for (std::size_t i = 0; i < n_inserts; ++i) { - std::unordered_map data; - data.reserve(n_partitions); - for (shuffler::PartID pid = 0; pid < n_partitions; ++pid) { - data.emplace(pid, generate_packed_data(n_elements, 0, stream, *br)); + auto comm = GlobalEnvironment->comm_; + std::unique_ptr expected_table; + if (comm->nranks() == 1) { // full_input table is expected + expected_table = std::make_unique(std::move(full_input_table)); + } else { // full_input table is replicated on all ranks + // local partitions + auto [table, offsets] = + cudf::hash_partition(full_input_table.view(), {1}, num_partitions, hash_function, seed); + + auto local_pids = + shuffler::Shuffler::local_partitions(comm, num_partitions, shuffler::Shuffler::round_robin); + + // every partition is replicated on all ranks + std::vector expected_tables; + for (auto pid : local_pids) { + auto t_view = cudf::slice(table->view(), {offsets[pid], offsets[pid + 1]}).at(0); + // this will be replicated on all ranks + for (rapidsmpf::Rank rank = 0; rank < comm->nranks(); ++rank) { + expected_tables.push_back(t_view); + } } - shuffler->insert(std::move(data)); + expected_table = cudf::concatenate(expected_tables); } - coro::sync_wait(shuffler->insert_finished()); - - auto local_pids = - shuffler::Shuffler::local_partitions(comm, n_partitions, &shuffler::Shuffler::round_robin); - - std::vector finished_pids; - std::size_t n_chunks_received = 0; - for (auto pid : local_pids) { - auto chunks = shuffler->extract(pid); - n_chunks_received += chunks.size(); - finished_pids.push_back(pid); + // Concat all output chunks to a single table. + std::vector output_chunks_as_views; + for (auto& chunk : output_chunks) { + output_chunks_as_views.push_back(chunk.get().table_view()); } + auto result_table = cudf::concatenate(output_chunks_as_views); - EXPECT_EQ(n_inserts * local_pids.size() * comm->nranks(), n_chunks_received); - EXPECT_EQ(local_pids, finished_pids); + CUDF_TEST_EXPECT_TABLES_EQUIVALENT(sort_table(result_table->view()), + sort_table(expected_table->view())); } diff --git a/cpp/libcudf_streaming/tests/test_shuffler.cpp b/cpp/libcudf_streaming/tests/test_shuffler.cpp index d74c734f8800..9dbbf1d256cc 100644 --- a/cpp/libcudf_streaming/tests/test_shuffler.cpp +++ b/cpp/libcudf_streaming/tests/test_shuffler.cpp @@ -7,8 +7,6 @@ #include "utils.hpp" #include -#include -#include #include #include @@ -18,7 +16,6 @@ #include #include #include -#include #include #include @@ -32,78 +29,6 @@ extern Environment* GlobalEnvironment; using namespace cudf_streaming::integrations; -TEST(ReceivedChunks, spill_skips_control_messages) -{ - auto mr = cudf::get_current_device_resource_ref(); - auto br = rapidsmpf::BufferResource::create(mr); - - rapidsmpf::shuffler::detail::ReceivedChunks received; - - // Control messages have no data buffer (data_ == nullptr); spill must skip them - // rather than calling data_memory_type(), which throws if data_ is null. - received.insert(rapidsmpf::shuffler::detail::Chunk::from_finished_partition( - /*chunk_id=*/0, /*part_id=*/0, /*expected_num_chunks=*/1)); - - EXPECT_EQ(received.spill(br.get(), /*amount=*/1024), 0UL); -} - -TEST(ReceivedChunks, spill_respects_amount) -{ - auto mr = cudf::get_current_device_resource_ref(); - auto br = rapidsmpf::BufferResource::create(mr); - auto stream = cudf::get_default_stream(); - - rapidsmpf::shuffler::detail::ReceivedChunks received; - constexpr std::size_t chunk_size = 100; - - for (rapidsmpf::shuffler::PartID pid = 0; pid < 2; ++pid) { - auto metadata = std::make_unique>(std::size_t{1}, std::uint8_t{0}); - auto res = br->reserve_or_fail(chunk_size, rapidsmpf::MemoryType::DEVICE); - auto data = br->make_buffer(chunk_size, stream, res); - received.insert(rapidsmpf::shuffler::detail::Chunk::from_packed_data( - 0, pid, rapidsmpf::PackedData{std::move(metadata), std::move(data)})); - } - - // Two partitions, one 100-byte chunk each. spill() must stop after the first - // partition satisfies the request; the outer loop must not continue into partition 1. - EXPECT_EQ(received.spill(br.get(), chunk_size), chunk_size); -} - -TEST(MetadataMessage, round_trip) -{ - auto stream = cudf::get_default_stream(); - auto mr = cudf::get_current_device_resource_ref(); - auto br = rapidsmpf::BufferResource::create(mr); - - auto metadata = iota_vector(100); - - auto expect = rapidsmpf::shuffler::detail::Chunk::from_packed_data( - 1, // chunk_id - 2, // part_id - rapidsmpf::PackedData{ - std::make_unique>(metadata), // non-empty metadata - br->move(std::make_unique(), stream) // empty data - }); - - // Extract the metadata from then chunk. - auto msg = expect.serialize(); - EXPECT_FALSE(expect.is_metadata_buffer_set()); - - // Create a new chunk by deserializing the message. - auto result = rapidsmpf::shuffler::detail::Chunk::deserialize(*msg, br.get()); - - EXPECT_TRUE(expect.data_size() == 0 || result.is_data_buffer_set()); - // They should be identical. - EXPECT_EQ(expect.part_id(), result.part_id()); - EXPECT_EQ(expect.chunk_id(), result.chunk_id()); - EXPECT_EQ(expect.expected_num_chunks(), result.expected_num_chunks()); - EXPECT_EQ(expect.data_size(), result.data_size()); - EXPECT_EQ(expect.metadata_size(), result.metadata_size()); - - // The metadata should be identical to the original. - EXPECT_EQ(metadata, *result.release_metadata_buffer()); -} - using MemoryLimitsMap = std::unordered_map; // Help function to get the `memory_limits` argument for a `BufferResource` @@ -123,16 +48,9 @@ MemoryLimitsMap get_memory_limits_map(rapidsmpf::MemoryType priorities) return ret; } -/// @tparam InsertFn: lambda that inserts the packed chunks into the shuffler. -/// Signature: void(std::vector&& packed_chunks) -/// @tparam InsertFinishedFn: lambda that inserts the finished flag into the shuffler. -/// Signature: void() -template void test_shuffler(std::shared_ptr const& comm, rapidsmpf::shuffler::Shuffler& shuffler, rapidsmpf::shuffler::PartID total_num_partitions, - InsertFn&& insert_fn, - InsertFinishedFn&& insert_finished_fn, std::size_t total_num_rows, std::int64_t seed, cudf::hash_id hash_fn, @@ -185,12 +103,12 @@ void test_shuffler(std::shared_ptr const& comm, br, rapidsmpf::AllowOverbooking::YES); // Add the chunks to the shuffle - insert_fn(std::move(packed_chunks)); + shuffler.insert(std::move(packed_chunks)); } row_offset += partiton_size; } // Tell the shuffler that we have no more input partitions. - insert_finished_fn(); + shuffler.insert_finished(); EXPECT_NO_THROW(shuffler.wait(wait_timeout)); for (auto finished_partition : shuffler.local_partitions()) { @@ -259,17 +177,14 @@ INSTANTIATE_TEST_SUITE_P( TEST_P(MemoryLimits_NumPartition, round_trip) { - EXPECT_NO_FATAL_FAILURE(test_shuffler( - GlobalEnvironment->comm_, - *shuffler, - total_num_partitions, - [&](auto&& packed_chunks) { shuffler->insert(std::move(packed_chunks)); }, - [&]() { shuffler->insert_finished(); }, - total_num_rows, - seed, - hash_fn, - stream, - br.get())); + EXPECT_NO_FATAL_FAILURE(test_shuffler(GlobalEnvironment->comm_, + *shuffler, + total_num_partitions, + total_num_rows, + seed, + hash_fn, + stream, + br.get())); } // Test that the same communicator can be used concurrently by multiple shufflers in @@ -288,50 +203,6 @@ class ConcurrentShuffleTest : public cudf::test::BaseFixtureWithParam - void RunTest(int t_id, InsertFn&& insert_fn, InsertFinishedFn&& insert_finished_fn) - { - rapidsmpf::shuffler::Shuffler shuffler(GlobalEnvironment->comm_, - t_id, // op_id, use t_id as a proxy - total_num_partitions, - br.get()); - - EXPECT_NO_FATAL_FAILURE(test_shuffler( - GlobalEnvironment->comm_, - shuffler, - total_num_partitions, - [&](auto&& packed_chunks) { insert_fn(shuffler, std::move(packed_chunks)); }, - [&]() { insert_finished_fn(shuffler); }, - 100'000, // total_num_rows - t_id, // seed - cudf::hash_id::HASH_MURMUR3, - stream, - br.get())); - } - - template - void RunTestTemplate(InsertFn insert_fn, InsertFinishedFn insert_finished_fn) - { - std::vector> futures; - futures.reserve(static_cast(num_shufflers)); - - for (int t_id = 0; t_id < num_shufflers; t_id++) { - // pass a copy of the insert_fn and insert_finished_fn to each thread - futures.push_back( - std::async(std::launch::async, - [this, t_id, insert_fn1 = insert_fn, insert_finished_fn1 = insert_finished_fn] { - ASSERT_NO_FATAL_FAILURE( - this->RunTest(t_id, std::move(insert_fn1), std::move(insert_finished_fn1))); - })); - } - - for (auto& f : futures) { - ASSERT_NO_THROW(f.wait()); - } - } - int num_shufflers; rapidsmpf::shuffler::PartID total_num_partitions; @@ -341,9 +212,29 @@ class ConcurrentShuffleTest : public cudf::test::BaseFixtureWithParam> futures; + futures.reserve(static_cast(num_shufflers)); + + for (int t_id = 0; t_id < num_shufflers; t_id++) { + futures.push_back(std::async(std::launch::async, [this, t_id] { + rapidsmpf::shuffler::Shuffler shuffler(GlobalEnvironment->comm_, + t_id, // op_id, use t_id as a proxy + total_num_partitions, + br.get()); + EXPECT_NO_FATAL_FAILURE(test_shuffler(GlobalEnvironment->comm_, + shuffler, + total_num_partitions, + 100'000, // total_num_rows + t_id, // seed + cudf::hash_id::HASH_MURMUR3, + stream, + br.get())); + })); + } + + for (auto& f : futures) { + ASSERT_NO_THROW(f.get()); + } } // test different `num_shufflers` and `total_num_partitions`. @@ -455,193 +346,6 @@ TEST(Shuffler, SpillOnInsertAndExtraction) shuffler.insert_finished(); } -TEST(FinishCounterTests, zero_local_partitions_immediately_finished) -{ - rapidsmpf::shuffler::detail::FinishCounter finish_counter( - /*nranks=*/2, /*n_local_partitions=*/0); - - EXPECT_TRUE(finish_counter.all_finished()); -} - -TEST(FinishCounterTests, nonzero_local_partitions_finishes_after_all_chunks) -{ - rapidsmpf::shuffler::detail::FinishCounter finish_counter( - /*nranks=*/1, /*n_local_partitions=*/2); - - EXPECT_FALSE(finish_counter.all_finished()); - - // One rank sends 3 chunks total. - finish_counter.move_goalpost(0, 3); - finish_counter.add_finished_chunk(); - finish_counter.add_finished_chunk(); - EXPECT_FALSE(finish_counter.all_finished()); - - finish_counter.add_finished_chunk(); - EXPECT_TRUE(finish_counter.all_finished()); -} - -TEST(FinishCounterTests, multi_rank_completion) -{ - auto comm = GlobalEnvironment->comm_; - - if (comm->rank() != 0) { GTEST_SKIP() << "Test only runs on rank 0"; } - - // Use nranks partitions so each rank owns exactly 1 partition (round robin). - auto out_nparts = rapidsmpf::safe_cast(comm->nranks()); - - auto local_partitions = rapidsmpf::shuffler::Shuffler::local_partitions( - comm, out_nparts, &rapidsmpf::shuffler::Shuffler::round_robin); - ASSERT_EQ(local_partitions.size(), 1); - - rapidsmpf::shuffler::detail::FinishCounter finish_counter(comm->nranks(), - local_partitions.size()); - - // Not finished yet. - EXPECT_FALSE(finish_counter.all_finished()); - - // For nranks ranks, each rank sends 1 data chunk + 1 control, so - // move_goalpost(rank, 2) per rank. - for (rapidsmpf::Rank r = 0; r < comm->nranks(); r++) { - finish_counter.move_goalpost(r, 2); - } - - // Add finished chunks: 1 data chunk per rank + 1 control per rank = 2 * nranks - for (rapidsmpf::Rank r = 0; r < comm->nranks(); r++) { - finish_counter.add_finished_chunk(); // data chunk - finish_counter.add_finished_chunk(); // control chunk - } - - EXPECT_TRUE(finish_counter.all_finished()); -} - -class FinishCounterMultithreadingTest - : public ::testing::TestWithParam> { - protected: - rapidsmpf::Rank const nranks{1}; // simulate a single rank - - std::unique_ptr finish_counter; - rapidsmpf::shuffler::PartID npartitions; - std::uint32_t nthreads; - - void SetUp() override - { - std::tie(npartitions, nthreads) = GetParam(); - - finish_counter = - std::make_unique(nranks, npartitions); - } - - void produce_data() - { - // Simulate nranks=1: one rank reports chunk count = npartitions + 1 - // (one data chunk per partition + 1 control message) - finish_counter->move_goalpost(rapidsmpf::Rank{0}, npartitions + 1); - for (rapidsmpf::shuffler::PartID i = 0; i <= npartitions; i++) { - finish_counter->add_finished_chunk(); - } - } -}; - -// Parametrize on number of partitions and number of consumer threads -INSTANTIATE_TEST_SUITE_P(FinishCounterMultithreadingTestP, - FinishCounterMultithreadingTest, - testing::Combine(testing::Values(1, 2, 100, 101), - testing::Values(1, 2, 3)), - [](const auto& info) { - return "npartitions_" + std::to_string(std::get<0>(info.param)) + - "__nthreads_" + std::to_string(std::get<1>(info.param)); - }); - -TEST_P(FinishCounterMultithreadingTest, concurrent_all_finished_check) -{ - produce_data(); - - std::atomic n_checks{0}; - std::vector> futures; - for (std::uint32_t tid = 0; tid < nthreads; tid++) { - futures.emplace_back(std::async(std::launch::async, [&, tid] { - for (std::uint32_t i = tid; i < npartitions; i += nthreads) { - EXPECT_TRUE(finish_counter->all_finished()); - n_checks.fetch_add(1, std::memory_order_relaxed); - } - })); - } - - EXPECT_NO_THROW(std::ranges::for_each(futures, [](auto& f) { f.get(); })); - - EXPECT_EQ(npartitions, n_checks); - EXPECT_TRUE(finish_counter->all_finished()); -} - -class ContiguousPartitionAssignmentTest - : public ::testing::TestWithParam { - protected: - void SetUp() override - { - comm = GlobalEnvironment->comm_; - nranks = comm->nranks(); - rank = comm->rank(); - total_num_partitions = GetParam(); - } - - std::shared_ptr comm; - rapidsmpf::Rank nranks; - rapidsmpf::Rank rank; - rapidsmpf::shuffler::PartID total_num_partitions; -}; - -INSTANTIATE_TEST_SUITE_P(PartitionAssignment, - ContiguousPartitionAssignmentTest, - testing::Values(1, 2, 3, 5, 7, 10, 16, 100), - [](const testing::TestParamInfo& info) { - return "nparts_" + std::to_string(info.param); - }); - -TEST_P(ContiguousPartitionAssignmentTest, contiguous) -{ - std::vector> rank_partitions(nranks); - for (rapidsmpf::shuffler::PartID pid = 0; pid < total_num_partitions; ++pid) { - auto owner = rapidsmpf::shuffler::Shuffler::contiguous(comm, pid, total_num_partitions); - EXPECT_GE(owner, 0); - EXPECT_LT(owner, nranks); - rank_partitions[owner].push_back(pid); - } - - // Each rank's partitions must be contiguous. - for (rapidsmpf::Rank r = 0; r < nranks; ++r) { - auto const& pids = rank_partitions[r]; - for (std::size_t i = 1; i < pids.size(); ++i) { - EXPECT_EQ(pids[i], pids[i - 1] + 1); - } - } - - // Concatenating all rank partitions should cover [0, total_num_partitions). - std::vector all_pids; - for (auto const& pids : rank_partitions) { - all_pids.insert(all_pids.end(), pids.begin(), pids.end()); - } - EXPECT_EQ(all_pids, iota_vector(total_num_partitions)); -} - -TEST(Shuffler, ShutdownWhilePaused) -{ - auto progress_thread = GlobalEnvironment->comm_->progress_thread(); - auto mr = cudf::get_current_device_resource_ref(); - - auto br = rapidsmpf::BufferResource::create(mr); - - auto shuffler = rapidsmpf::shuffler::Shuffler(GlobalEnvironment->comm_, 0, 1, br.get()); - - progress_thread->pause(); - EXPECT_FALSE(progress_thread->is_running()); - shuffler.insert_finished(); - // Progress thread must be running before shuffle shutdown, otherwise we have some - // orphan messages in the shuffle that are never sent/received. - progress_thread->resume(); - EXPECT_TRUE(progress_thread->is_running()); - EXPECT_NO_THROW(shuffler.shutdown()); -} - // check cudf pack conditions for empty table TEST(EmptyPartitions, cudf_pack) { @@ -656,127 +360,6 @@ TEST(EmptyPartitions, cudf_pack) EXPECT_EQ(0, packed.gpu_data->size()); } -class ExtractEmptyPartitionsTest : public cudf::test::BaseFixture { - public: - static constexpr rapidsmpf::shuffler::PartID nparts = 10; - static constexpr auto wait_timeout = std::chrono::seconds(30); - - void SetUp() override - { - stream = cudf::get_default_stream(); - br = rapidsmpf::BufferResource::create(mr()); - - shuffler = std::make_unique( - GlobalEnvironment->comm_, 0, nparts, br.get()); - } - - void TearDown() override { shuffler.reset(); } - - void insert_chunks( - std::unordered_map&& chunks) - { - if (!chunks.empty()) { shuffler->insert(std::move(chunks)); } - shuffler->insert_finished(); - } - - void verify_extracted_chunks(auto expected_empty_fn) - { - EXPECT_NO_THROW(shuffler->wait(wait_timeout)); - for (auto pid : shuffler->local_partitions()) { - SCOPED_TRACE("pid: " + std::to_string(pid)); - std::vector chunks; - EXPECT_NO_THROW({ chunks = shuffler->extract(pid); }); - - if (expected_empty_fn(pid)) { - EXPECT_TRUE(chunks.empty()); - } else { - EXPECT_EQ(GlobalEnvironment->comm_->nranks(), chunks.size()); - } - } - } - - auto empty_packed_data() - { - return rapidsmpf::PackedData{std::make_unique>(), - br->move(std::make_unique(), stream)}; - } - - auto non_empty_packed_data() - { - return rapidsmpf::PackedData{ - std::make_unique>(10), - br->move(std::make_unique(10, stream), stream)}; - } - - rmm::cuda_stream_view stream; - std::shared_ptr br; - std::unique_ptr shuffler; -}; - -TEST_F(ExtractEmptyPartitionsTest, NoInsertions) -{ - insert_chunks({}); - EXPECT_NO_FATAL_FAILURE(verify_extracted_chunks([](auto) { return true; })); -} - -TEST_F(ExtractEmptyPartitionsTest, AllEmptyInsertions) -{ - std::unordered_map chunks; - for (rapidsmpf::shuffler::PartID pid = 0; pid < nparts; ++pid) { - chunks.emplace(pid, empty_packed_data()); - } - - insert_chunks(std::move(chunks)); - EXPECT_NO_FATAL_FAILURE(verify_extracted_chunks([](auto) { return true; })); -} - -TEST_F(ExtractEmptyPartitionsTest, SomeEmptyInsertions) -{ - std::unordered_map chunks; - for (rapidsmpf::shuffler::PartID pid = 0; pid < nparts; ++pid) { - if (pid % 3 == 0) { chunks.emplace(pid, empty_packed_data()); } - } - - insert_chunks(std::move(chunks)); - EXPECT_NO_FATAL_FAILURE(verify_extracted_chunks([](auto) { return true; })); -} - -TEST_F(ExtractEmptyPartitionsTest, SomeEmptyAndNonEmptyInsertions) -{ - std::unordered_map chunks; - for (rapidsmpf::shuffler::PartID pid = 0; pid < nparts; ++pid) { - if (pid % 3 == 0) { - chunks.emplace(pid, empty_packed_data()); - } else { - chunks.emplace(pid, non_empty_packed_data()); - } - } - - insert_chunks(std::move(chunks)); - EXPECT_NO_FATAL_FAILURE(verify_extracted_chunks([](auto pid) { return pid % 3 == 0; })); -} - -TEST(ShufflerTest, multiple_shutdowns) -{ - auto& comm = GlobalEnvironment->comm_; - auto br = rapidsmpf::BufferResource::create(cudf::get_current_device_resource_ref()); - auto shuffler = - std::make_unique(comm, 0, comm->nranks(), br.get()); - - shuffler->insert_finished(); - EXPECT_NO_THROW(shuffler->wait(std::chrono::seconds(30))); - for (auto pid : shuffler->local_partitions()) { - std::ignore = shuffler->extract(pid); - } - - constexpr int n_threads = 10; - std::vector> futures; - for (int i = 0; i < n_threads; ++i) { - futures.emplace_back(std::async(std::launch::async, [&] { shuffler->shutdown(); })); - } - std::ranges::for_each(futures, [](auto& future) { future.get(); }); -} - // Test that multiple threads can call wait() concurrently. TEST(Shuffler, concurrent_wait) { diff --git a/cpp/libcudf_streaming/tests/test_shuffler_many_streams.cpp b/cpp/libcudf_streaming/tests/test_shuffler_many_streams.cpp deleted file mode 100644 index ffd8ecdd6b2e..000000000000 --- a/cpp/libcudf_streaming/tests/test_shuffler_many_streams.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/** - * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights - * reserved. SPDX-License-Identifier: Apache-2.0 - */ - -#include "environment.hpp" -#include "utils.hpp" - -#include - -#include - -#include - -#include -#include -#include -#include -#include -#include - -#include - -extern Environment* GlobalEnvironment; - -namespace { - -/** - * @brief Generate a random CUDA stream priority. - * - * @param random_generator A random number generator used to produce the priority. - * @return A valid CUDA stream priority in the device range. - */ -int gen_stream_priority(std::mt19937& random_generator) -{ - int least_priority = 0; // numerically larger (often 0) => lower priority - int greatest_priority = 0; // numerically smaller (often negative) => higher priority - RAPIDSMPF_CUDA_TRY(cudaDeviceGetStreamPriorityRange(&least_priority, &greatest_priority)); - int const num_priorities = least_priority - greatest_priority + 1; - std::uniform_int_distribution dist(0, num_priorities - 1); - return greatest_priority + dist(random_generator); -} - -// To expose unexpected deadlocks, we use a 30s timeout. In a normal run, the -// shuffle shouldn't get near 30s. -constexpr auto wait_timeout = std::chrono::seconds{30}; - -} // namespace - -TEST(ShufflerManyStreams, Test) -{ - std::mt19937 random_generator{42}; - constexpr std::size_t chunksize = 1 << 20; - constexpr int num_partitions = 100; - auto br = rapidsmpf::BufferResource::create(cudf::get_current_device_resource_ref()); - - // Create a CUDA stream for each partition. - // To stress-test stream handling, assign random priorities so streams are more - // likely to execute in mixed order. - std::array partition_streams{}; - for (rapidsmpf::shuffler::PartID pid = 0; pid < num_partitions; ++pid) { - RAPIDSMPF_CUDA_TRY(cudaStreamCreateWithPriority( - &partition_streams[pid], cudaStreamNonBlocking, gen_stream_priority(random_generator))); - } - - rapidsmpf::shuffler::Shuffler shuffler(GlobalEnvironment->comm_, 0, num_partitions, br.get()); - - std::unordered_map partitions; - for (rapidsmpf::shuffler::PartID pid = 0; pid < num_partitions; ++pid) { - partitions.insert({pid, generate_packed_data(chunksize, pid, partition_streams[pid], *br)}); - } - - shuffler.insert(std::move(partitions)); - shuffler.insert_finished(); - - EXPECT_NO_THROW(shuffler.wait(wait_timeout)); - for (auto const pid : shuffler.local_partitions()) { - auto partition_chunks = shuffler.extract(pid); - for (auto& chunk : partition_chunks) { - auto const stream = chunk.data->stream(); - EXPECT_NO_FATAL_FAILURE(validate_packed_data(std::move(chunk), chunksize, pid, stream, *br)); - } - } - - for (auto& stream : partition_streams) { - RAPIDSMPF_CUDA_TRY(cudaStreamDestroy(stream)); - } -}