Skip to main content

ruda_runtime/runtime/stream/
scheduler.rs

1use crate::runtime::{
2    config::streaming::StreamingLogLevel,
3    logging::ServerLogger,
4    stream::{StreamFactory, StreamPool},
5};
6use alloc::{format, sync::Arc, vec, vec::Vec};
7use ruda_core::stream_id::StreamId;
8
9/// Defines a trait for a scheduler stream backend, specifying the types and behavior for task scheduling.
10pub trait SchedulerStreamBackend {
11    /// Type representing a task.
12    type Task: core::fmt::Debug;
13    /// Type representing a stream.
14    type Stream: core::fmt::Debug;
15    /// Type for the stream factory, which creates streams of type `Self::Stream`.
16    type Factory: StreamFactory<Stream = Self::Stream>;
17
18    /// Enqueues a task onto a given stream for execution.
19    fn enqueue(task: Self::Task, stream: &mut Self::Stream);
20    /// Flush the inner stream queue to ensure ordering between different streams.
21    fn flush(stream: &mut Self::Stream);
22    /// Returns a mutable reference to the stream factory.
23    fn factory(&mut self) -> &mut Self::Factory;
24}
25
26/// Represents a multi-stream scheduler that manages task execution across multiple streams.
27#[derive(Debug)]
28pub struct SchedulerMultiStream<B: SchedulerStreamBackend> {
29    /// Pool of streams managed by the scheduler.
30    pool: StreamPool<SchedulerPoolMarker<B>>,
31    /// Strategy for scheduling tasks (e.g., Interleave or Sequential).
32    strategy: SchedulerStrategy,
33    /// Maximum number of tasks allowed per stream before execution is triggered.
34    max_tasks: usize,
35    /// Server logger.
36    pub logger: Arc<ServerLogger>,
37}
38
39/// Defines the scheduling strategy for task execution.
40#[derive(Debug)]
41pub enum SchedulerStrategy {
42    /// Tasks from different streams are interleaved during execution.
43    Interleave,
44    /// Tasks from each stream are executed sequentially.
45    Sequential,
46}
47
48/// Represents a single stream that holds tasks and a backend stream.
49#[derive(Debug)]
50pub struct Stream<B: SchedulerStreamBackend> {
51    /// List of tasks queued for execution in this stream.
52    tasks: Vec<B::Task>,
53    /// The backend stream used for task execution.
54    stream: B::Stream,
55}
56
57impl<B: SchedulerStreamBackend> Stream<B> {
58    /// Flushes all tasks from the stream, returning them and clearing the internal task list.
59    fn flush(&mut self) -> Vec<B::Task> {
60        if self.tasks.is_empty() {
61            return Vec::new();
62        }
63        let mut returned = Vec::with_capacity(self.tasks.capacity());
64        core::mem::swap(&mut returned, &mut self.tasks);
65        returned
66    }
67}
68
69#[derive(Debug)]
70struct SchedulerPoolMarker<B: SchedulerStreamBackend> {
71    backend: B,
72}
73
74impl<B: SchedulerStreamBackend> StreamFactory for SchedulerPoolMarker<B> {
75    // The type of stream produced by this factory.
76    type Stream = Stream<B>;
77
78    // Creates a new stream with an empty task list and a backend stream.
79    fn create(&mut self) -> Self::Stream {
80        Stream {
81            tasks: Vec::new(),
82            // Uses the backend's factory to create a new stream.
83            stream: self.backend.factory().create(),
84        }
85    }
86}
87
88/// Options for configuring a `SchedulerMultiStream`.
89#[derive(Debug)]
90pub struct SchedulerMultiStreamOptions {
91    /// Maximum number of streams allowed in the pool.
92    pub max_streams: u8,
93    /// Maximum number of tasks per stream before execution is triggered.
94    pub max_tasks: usize,
95    /// The scheduling strategy to use.
96    pub strategy: SchedulerStrategy,
97}
98
99impl<B: SchedulerStreamBackend> SchedulerMultiStream<B> {
100    /// Creates a new `SchedulerMultiStream` with the given backend and options.
101    pub fn new(
102        logger: Arc<ServerLogger>,
103        backend: B,
104        options: SchedulerMultiStreamOptions,
105    ) -> Self {
106        Self {
107            pool: StreamPool::new(SchedulerPoolMarker { backend }, options.max_streams, 0),
108            max_tasks: options.max_tasks,
109            strategy: options.strategy,
110            logger,
111        }
112    }
113
114    /// Returns a mutable reference to the backend stream for a given stream ID.
115    pub fn stream(&mut self, stream_id: &StreamId) -> &mut B::Stream {
116        let stream = self.pool.get_mut(stream_id);
117        &mut stream.stream
118    }
119
120    /// Registers a task for execution on a specific stream, ensuring stream alignment.
121    pub fn register(&mut self, stream_id: StreamId, task: B::Task, args_streams: &[StreamId]) {
122        // Align streams to ensure dependencies are handled correctly.
123        self.align_streams(stream_id, args_streams);
124
125        // Get the stream for the given stream ID and add the task to its queue.
126        let current = self.pool.get_mut(&stream_id);
127        current.tasks.push(task);
128
129        // If the task queue exceeds the maximum, execute the stream.
130        if current.tasks.len() >= self.max_tasks {
131            self.execute_streams(vec![stream_id]);
132        }
133    }
134
135    /// Aligns streams by flushing tasks from streams that conflict with the given bindings.
136    pub(crate) fn align_streams(&mut self, stream_id: StreamId, args_streams: &[StreamId]) {
137        let mut to_flush = Vec::new();
138        // Get the index of the target stream.
139        let index = self.pool.stream_index(&stream_id);
140
141        // Identify streams that need to be flushed due to conflicting bindings.
142        for arg_stream in args_streams {
143            let index_stream = self.pool.stream_index(arg_stream);
144            if index != index_stream {
145                to_flush.push(*arg_stream);
146
147                self.logger.log_streaming(
148                    |level| matches!(level, StreamingLogLevel::Full),
149                    || format!("Binding on {} is shared on {}", arg_stream, stream_id),
150                );
151            }
152        }
153
154        // If no streams need flushing, return early.
155        if to_flush.is_empty() {
156            return;
157        }
158
159        self.logger.log_streaming(
160            |level| !matches!(level, StreamingLogLevel::Disabled),
161            || {
162                format!(
163                    "Flushing streams {to_flush:?} before registering more tasks on {stream_id}"
164                )
165            },
166        );
167        // Execute the streams that need to be flushed.
168        self.execute_streams(to_flush);
169    }
170
171    /// Executes tasks from the specified streams based on the scheduling strategy.
172    pub fn execute_streams(&mut self, stream_ids: Vec<StreamId>) {
173        let mut indices = Vec::with_capacity(stream_ids.len());
174
175        // Collect unique stream indices to avoid redundant processing.
176        for id in stream_ids {
177            let index = self.pool.stream_index(&id);
178            if !indices.contains(&index) {
179                indices.push(index);
180            }
181        }
182
183        // Create schedules for each stream to be executed.
184        let mut schedules = Vec::new();
185        for index in indices {
186            let stream = unsafe { self.pool.get_mut_index(index) }; // Note: `unsafe` usage assumes valid index.
187            let tasks = stream.flush();
188            let num_tasks = tasks.len();
189
190            schedules.push(Schedule {
191                tasks: tasks.into_iter(),
192                num_tasks,
193                stream_index: index,
194            });
195        }
196
197        // If no schedules were created, return early.
198        if schedules.is_empty() {
199            return;
200        }
201
202        // Execute schedules based on the configured strategy.
203        match self.strategy {
204            SchedulerStrategy::Interleave => self.execute_schedules_interleave(schedules),
205            SchedulerStrategy::Sequential => self.execute_schedules_sequence(schedules),
206        }
207    }
208
209    /// Executes schedules sequentially, processing each stream's tasks in order.
210    fn execute_schedules_sequence(&mut self, schedules: Vec<Schedule<B>>) {
211        for schedule in schedules {
212            let stream = unsafe { self.pool.get_mut_index(schedule.stream_index) }; // Note: `unsafe` usage assumes valid index.
213            for task in schedule.tasks {
214                // Enqueue each task on the stream.
215                B::enqueue(task, &mut stream.stream);
216            }
217
218            // Makes sure the tasks are ordered on the compute queue.
219            B::flush(&mut stream.stream);
220        }
221    }
222
223    //// Executes schedules in an interleaved manner, alternating tasks from different streams.
224    ///
225    /// We chose the first stream as the one executing the tasks, ensuring proper ordering by
226    /// flushing all other streams first and flushing the execution stream at the end.
227    /// This way, we ensure that most tasks are actually interleaved on the real compute queue
228    /// shared across all streams.
229    fn execute_schedules_interleave(&mut self, mut schedules: Vec<Schedule<B>>) {
230        // Makes sure the tasks are ordered on the compute queue.
231        for schedule in schedules.iter_mut().skip(1) {
232            let stream = unsafe { self.pool.get_mut_index(schedule.stream_index) };
233            B::flush(&mut stream.stream);
234        }
235
236        let execution_index = schedules.first().expect("At least one stream").stream_index;
237        let stream = unsafe { self.pool.get_mut_index(execution_index) };
238
239        // Find the maximum number of tasks across all schedules.
240        let num_tasks_max = schedules
241            .iter()
242            .map(|s| s.num_tasks)
243            .max()
244            .expect("At least one schedule");
245
246        // Iterate through tasks, interleaving them across streams.
247        for _ in 0..num_tasks_max {
248            for schedule in schedules.iter_mut() {
249                // If there are tasks remaining in the schedule, enqueue the next one.
250                if let Some(task) = schedule.tasks.next() {
251                    B::enqueue(task, &mut stream.stream);
252                }
253            }
254        }
255
256        // Making sure all tasks are registered to the queue.
257        B::flush(&mut stream.stream);
258    }
259}
260
261// Represents a schedule for executing tasks on a specific stream.
262struct Schedule<B: SchedulerStreamBackend> {
263    // Iterator over the tasks to be executed.
264    tasks: alloc::vec::IntoIter<B::Task>,
265    // Number of tasks in the schedule.
266    num_tasks: usize,
267    // Index of the stream in the pool.
268    stream_index: usize,
269}