context.hpp
1 
6 #pragma once
7 
8 #include <memory>
9 #include <thread>
10 
11 #include <coro/coro.hpp>
12 
13 #include <rapidsmpf/communicator/logger.hpp>
14 #include <rapidsmpf/config.hpp>
15 #include <rapidsmpf/error.hpp>
16 #include <rapidsmpf/memory/resource_types.hpp>
17 #include <rapidsmpf/statistics.hpp>
18 #include <rapidsmpf/streaming/core/channel.hpp>
19 #include <rapidsmpf/streaming/core/coro_executor.hpp>
20 #include <rapidsmpf/streaming/core/memory_reserve_or_wait.hpp>
21 #include <rapidsmpf/streaming/core/queue.hpp>
22 
23 namespace rapidsmpf::streaming {
24 
42 class Context {
43  public:
56  std::shared_ptr<Logger> logger,
57  std::shared_ptr<CoroThreadPoolExecutor> executor,
58  std::shared_ptr<BufferResource> br
59  );
60 
70  std::shared_ptr<Logger> logger,
71  std::shared_ptr<BufferResource> br
72  );
73 
106  static std::shared_ptr<Context> from_options(
108  std::shared_ptr<Logger> logger,
110  std::shared_ptr<Statistics> statistics = Statistics::disabled()
111  );
112 
113  // No copy constructor and assignment operator.
114  Context(Context const&) = delete;
115  Context& operator=(Context const&) = delete;
116 
117  // No move constructor and assignment operator.
118  Context(Context&&) = delete;
119  Context& operator=(Context&&) = delete;
120 
121  ~Context() noexcept;
122 
133  void shutdown() noexcept;
134 
140  [[nodiscard]] config::Options options() const noexcept;
141 
145  [[nodiscard]] std::shared_ptr<Logger> const& logger() const noexcept;
146 
152  [[nodiscard]] std::shared_ptr<CoroThreadPoolExecutor> const&
153  executor() const noexcept;
154 
160  [[nodiscard]] std::shared_ptr<BufferResource> const& br() const noexcept;
161 
178  [[nodiscard]] std::shared_ptr<MemoryReserveOrWait> const& memory(
179  MemoryType mem_type
180  ) const noexcept;
181 
187  [[nodiscard]] std::shared_ptr<Statistics> statistics() const noexcept;
188 
194  [[nodiscard]] std::shared_ptr<Channel> create_channel() const noexcept;
195 
201  [[nodiscard]] std::shared_ptr<SpillableMessages> const&
202  spillable_messages() const noexcept;
203 
211  [[nodiscard]] std::shared_ptr<BoundedQueue> create_bounded_queue(
212  std::size_t buffer_size
213  ) const noexcept;
214 
224  [[nodiscard]] std::size_t uid() const noexcept;
225 
226  private:
227  std::size_t const uid_;
228  std::atomic<bool> is_shutdown_{false};
229  std::thread::id creator_thread_id_;
230  config::Options options_;
231  std::shared_ptr<Logger> logger_;
232  std::shared_ptr<CoroThreadPoolExecutor> executor_;
233  std::shared_ptr<BufferResource> br_;
234  std::array<std::shared_ptr<MemoryReserveOrWait>, MEMORY_TYPES.size()> memory_ = {};
235  std::shared_ptr<SpillableMessages> spillable_messages_;
236  SpillManager::SpillFunctionID spill_function_id_{};
237 };
238 
239 static_assert(StatisticsProvider<Context>);
240 
241 } // namespace rapidsmpf::streaming
Class managing buffer resources.
A logger base class for handling different levels of log messages.
Definition: logger.hpp:37
std::size_t SpillFunctionID
Represents a unique identifier for a registered spill function.
Tracks statistics across rapidsmpf operations.
Definition: statistics.hpp:74
static std::shared_ptr< Statistics > disabled()
Returns a disabled Statistics instance which can be enabled later.
Definition: statistics.hpp:134
Manages configuration options for RapidsMPF operations.
Definition: config.hpp:144
A bounded queue for type-erased Messages.
Definition: queue.hpp:31
A coroutine-based channel for sending and receiving messages asynchronously.
Definition: channel.hpp:52
Context for actors (coroutines) in rapidsmpf.
Definition: context.hpp:42
std::shared_ptr< SpillableMessages > const & spillable_messages() const noexcept
Returns the spillable messages collection.
std::size_t uid() const noexcept
Return a unique identifier for this context.
std::shared_ptr< Channel > create_channel() const noexcept
Create a new channel associated with this context.
Context(config::Options options, std::shared_ptr< Logger > logger, std::shared_ptr< BufferResource > br)
Convenience constructor using the provided configuration options.
std::shared_ptr< BoundedQueue > create_bounded_queue(std::size_t buffer_size) const noexcept
Create a new bounded queue associated with this context.
std::shared_ptr< CoroThreadPoolExecutor > const & executor() const noexcept
Returns the coroutine executor.
config::Options options() const noexcept
Returns the configuration options.
std::shared_ptr< Logger > const & logger() const noexcept
std::shared_ptr< MemoryReserveOrWait > const & memory(MemoryType mem_type) const noexcept
Get the handle for memory reservations for a given memory type.
static std::shared_ptr< Context > from_options(any_device_resource mr, std::shared_ptr< Logger > logger, config::Options options, std::shared_ptr< Statistics > statistics=Statistics::disabled())
Create a Context based on configuration options.
void shutdown() noexcept
Shut down the context.
std::shared_ptr< BufferResource > const & br() const noexcept
Returns the buffer resource.
std::shared_ptr< Statistics > statistics() const noexcept
Returns the statistics collector.
Context(config::Options options, std::shared_ptr< Logger > logger, std::shared_ptr< CoroThreadPoolExecutor > executor, std::shared_ptr< BufferResource > br)
Full constructor for the Context.
Executor wrapper around a coro::thread_pool used for coroutine execution.
Asynchronous coordinator for memory reservation requests.
Container for individually spillable messages.
constexpr std::array< MemoryType, 3 > MEMORY_TYPES
All memory types sorted in decreasing order of preference.
Definition: memory_type.hpp:23
MemoryType
Enum representing the type of memory sorted in decreasing order of preference.
Definition: memory_type.hpp:16
cuda::mr::any_resource< cuda::mr::device_accessible > any_device_resource
Owning type-erased device memory resource.