dummy_project/dummy_cluster.rs
1/// ==============================================================================
2/// examples/dummy_project/dummy_cluster.rs
3/// Minimal dummy cluster for running simulators in parallel.
4/// ==============================================================================
5
6use std::thread;
7
8use logger_bro::prelude::client::*;
9
10use super::dummy_simulator::DummySimulator;
11
12/// A cluster that runs multiple dummy simulators in parallel threads.
13#[derive(Debug)]
14pub struct DummyCluster {
15 /// Number of simulators to spawn.
16 pub count: usize,
17}
18
19impl DummyCluster {
20 /// Create a cluster with `count` simulators.
21 pub fn new(count: usize) -> Self {
22 Self { count }
23 }
24
25 /// Run all simulators in parallel, each for `steps` ticks.
26 ///
27 /// Each simulator gets its own client handle and reports progress.
28 pub fn run(&self, steps: usize, reporter: ClientReporter) {
29 let mut handles = Vec::with_capacity(self.count);
30 for i in 0..self.count {
31 let reporter = reporter.clone();
32 let label = format!("sim-{i}");
33 handles.push(thread::spawn(move || {
34 let mut sim = DummySimulator::new();
35 let client = reporter.start(label, Some(steps as u64));
36 if let Ok(client) = client {
37 for _ in 0..steps {
38 sim.tick();
39 let _ = client.set_current(sim.step as u64);
40 }
41 let _ = client.complete();
42 }
43 sim
44 }));
45 }
46
47 for handle in handles {
48 let _ = handle.join();
49 }
50 }
51}