|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include <rapidsmpf/allgather/allgather.hpp> |
| 12 | +#include <rapidsmpf/buffer/packed_data.hpp> |
| 13 | +#include <rapidsmpf/communicator/communicator.hpp> |
| 14 | +#include <rapidsmpf/streaming/chunks/packed_data.hpp> |
| 15 | +#include <rapidsmpf/streaming/core/channel.hpp> |
| 16 | +#include <rapidsmpf/streaming/core/context.hpp> |
| 17 | + |
| 18 | +#include <coro/event.hpp> |
| 19 | +#include <coro/task.hpp> |
| 20 | + |
| 21 | +namespace rapidsmpf::streaming { |
| 22 | + |
| 23 | +/** |
| 24 | + * @brief Asynchronous (coroutine) interface to `allgather::AllGather`. |
| 25 | + * |
| 26 | + * Once the AllGather is created, many tasks may insert data into it. If multiple tasks |
| 27 | + * insert data, the user is responsible for arranging that `insert_finished` is only |
| 28 | + * called after all `insert`ions have completed. A single consumer task should extract |
| 29 | + * data. |
| 30 | + */ |
| 31 | +class AllGather { |
| 32 | + public: |
| 33 | + /// @copydoc allgather::AllGather::Ordered |
| 34 | + using Ordered = rapidsmpf::allgather::AllGather::Ordered; |
| 35 | + /** |
| 36 | + * @brief Construct an asynchronous allgather. |
| 37 | + * |
| 38 | + * @param ctx Streaming context |
| 39 | + * @param op_id Unique identifier for the allgather. |
| 40 | + */ |
| 41 | + AllGather(std::shared_ptr<Context> ctx, OpID op_id); |
| 42 | + |
| 43 | + AllGather(AllGather const&) = delete; |
| 44 | + AllGather& operator=(AllGather const&) = delete; |
| 45 | + AllGather(AllGather&&) = delete; |
| 46 | + AllGather& operator=(AllGather&&) = delete; |
| 47 | + |
| 48 | + ~AllGather(); |
| 49 | + |
| 50 | + /** |
| 51 | + * @brief Gets the streaming context associated with this AllGather object. |
| 52 | + * |
| 53 | + * @return Shared pointer to context. |
| 54 | + */ |
| 55 | + [[nodiscard]] std::shared_ptr<Context> ctx() const noexcept; |
| 56 | + |
| 57 | + /** |
| 58 | + * @brief Insert a chunk into the allgather. |
| 59 | + * |
| 60 | + * @param chunk The chunk to insert holding data and a sequence number. |
| 61 | + */ |
| 62 | + void insert(PackedDataChunk&& chunk); |
| 63 | + |
| 64 | + /// @copydoc rapidsmpf::allgather::AllGather::insert_finished() |
| 65 | + void insert_finished(); |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Extract all gathered data. |
| 69 | + * |
| 70 | + * @param ordered If the extracted data should be ordered. If ordered, return data |
| 71 | + * will be ordered first by rank and then by sequence number of the inserted chunks on |
| 72 | + * that rank. |
| 73 | + * |
| 74 | + * @return Coroutine that completes when all data is available for extraction and |
| 75 | + * returns the data. |
| 76 | + */ |
| 77 | + coro::task<std::vector<PackedDataChunk>> extract_all(Ordered ordered = Ordered::YES); |
| 78 | + |
| 79 | + private: |
| 80 | + coro::event |
| 81 | + event_{}; ///< Event tracking whether all data has arrived and can be extracted. |
| 82 | + std::shared_ptr<Context> ctx_; ///< Streaming context. |
| 83 | + allgather::AllGather gatherer_; ///< Underlying collective allgather. |
| 84 | +}; |
| 85 | + |
| 86 | +namespace node { |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Create an allgather node for a single allgather operation. |
| 90 | + * |
| 91 | + * This is a streaming version of `rapidsmpf::allgather::AllGather` that operates on |
| 92 | + * packed data received through `Channel`s. |
| 93 | + * |
| 94 | + * @param ctx The streaming context to use. |
| 95 | + * @param ch_in Input channel providing `PackedDataChunk`s to be gathered. |
| 96 | + * @param ch_out Output channel where the gathered `PackedDataChunk`s are sent. |
| 97 | + * @param op_id Unique identifier for the operation. |
| 98 | + * @param ordered If the extracted data should be sent to the output channel with sequence |
| 99 | + * numbers corresponding to the global total order of input chunks. If yes, then the |
| 100 | + * sequence numbers of the extracted data will be ordered first by rank and then by input |
| 101 | + * sequence number. If no, the sequence number of the extracted chunks will have no |
| 102 | + * relation to any input sequence order. |
| 103 | + * |
| 104 | + * @return A streaming node that completes when the allgather is finished and the output |
| 105 | + * channel is drained. |
| 106 | + */ |
| 107 | +Node allgather( |
| 108 | + std::shared_ptr<Context> ctx, |
| 109 | + std::shared_ptr<Channel> ch_in, |
| 110 | + std::shared_ptr<Channel> ch_out, |
| 111 | + OpID op_id, |
| 112 | + AllGather::Ordered ordered = AllGather::Ordered::YES |
| 113 | +); |
| 114 | +} // namespace node |
| 115 | +} // namespace rapidsmpf::streaming |
0 commit comments