docs: clean up examples/_parallel_computation.rs

This commit is contained in:
swaits committed 2025-10-07 21:23:27 -07:00
1 parent ec94ade329
commit 5524b2121b
1 file changed
+130 -60
+130 -60
View File
@@ -1,61 +1,88 @@
//! # Parallel Computation: Map-Reduce Pattern //! # Proving True Parallelism with Timing
//! //!
//! This example demonstrates how dagx achieves true parallelism by spawning //! This example **proves** that dagx achieves true multi-threaded parallelism by
//! tasks to multiple threads, enabling efficient parallel computation. //! comparing parallel vs sequential execution times. Unlike the basic fan-in example
//! (03_fan_in.rs), this measures and demonstrates actual parallel speedup.
//! //!
//! ## What You'll Learn //! ## What You'll Learn
//! - How to split work across multiple parallel tasks (Map) //! - How to verify true parallelism with timing measurements
//! - How to aggregate results from parallel tasks (Reduce) //! - How to measure parallel speedup (sequential time / parallel time)
//! - How dagx achieves true multi-threaded parallelism //! - Why CPU-bound tasks benefit from true multi-threading
//! - How to verify parallel execution with timing //! - How dagx automatically achieves parallelism with no extra code
//!
//! ## Key Differences from 03_fan_in.rs
//! - **03_fan_in.rs**: Shows the pattern (4 sources → 1 aggregator)
//! - **This example**: Proves the pattern runs in parallel with actual timing
//! //!
//! ## Key Concepts //! ## Key Concepts
//! - **Map**: Split work into independent parallel tasks //! - **Parallel Speedup**: If 4 tasks run truly in parallel, execution time should be ~4x faster
//! - **Reduce**: Aggregate results from parallel tasks into a single output //! - **CPU-bound work**: Computation that keeps the CPU busy (vs I/O-bound waiting)
//! - **True Parallelism**: Tasks spawn to multiple OS threads via the runtime //! - **Multi-threading**: Tasks running simultaneously on different CPU cores
//! - **Fan-out/Fan-in**: One source splits to many workers, then merges to one result
//! //!
//! ## Pattern Diagram //! ## Pattern Diagram
//! ```text //! ```text
//! [Source] → [Sum1] //! [Sum1: 1-250 ] ──┐
//! → [Sum2] → [Aggregate] → Total //! [Sum2: 251-500] ──┼─→ [Aggregate] → Total: 500500
//! → [Sum3] //! [Sum3: 501-750] ──┤
//! → [Sum4] //! [Sum4: 751-1000] ─┘
//!
//! Map Phase Reduce Phase
//! (4 parallel (1 task combines
//! workers) all results)
//! ``` //! ```
//! //!
//! ## Running This Example //! ## Running This Example
//! ```bash //! ```bash
//! cargo run --example parallel_computation //! cargo run --release --example parallel_computation
//! ``` //! ```
//! //!
//! **IMPORTANT**: Run in `--release` mode to see meaningful timing differences!
//!
//! ## Expected Output //! ## Expected Output
//! ```text //! ```text
//! Computing sum of 1-1000 using 4 parallel tasks... //! === Proving True Parallelism ===
//! //!
//! Sum of 1-1000 computed in parallel: 500500 //! Running PARALLEL computation (4 workers on separate threads)...
//! Expected: 500500 //! Parallel result: 500500 (computed in 128ms)
//!
//! Running SEQUENTIAL computation (same work, one at a time)...
//! Sequential result: 500500 (computed in 512ms)
//!
//! Speedup: 4.0x faster with parallel execution!
//! This proves dagx runs tasks on multiple CPU cores simultaneously.
//! ``` //! ```
use dagx::{task, DagResult, DagRunner, Task}; use dagx::{task, DagResult, DagRunner, Task};
use std::time::{Duration, Instant};
use tokio::time::sleep;
// Step 1: Define a worker task that computes a partial sum // Step 1: Define a worker task that does CPU-bound work
// //
// Each worker computes the sum of a range of numbers. // We add artificial delay to make the parallelism measurable.
// These tasks run in parallel on different threads. // In real scenarios, this would be actual computation (image processing,
// mathematical calculations, data transformation, etc.)
struct ComputeSum { struct ComputeSum {
start: i32, start: i32,
end: i32, end: i32,
work_delay_ms: u64, // Simulate CPU-bound work
} }
impl ComputeSum { impl ComputeSum {
fn new(start: i32, end: i32) -> Self { fn new(start: i32, end: i32, work_delay_ms: u64) -> Self {
Self { start, end } Self {
start,
end,
work_delay_ms,
}
} }
} }
#[task] #[task]
impl ComputeSum { impl ComputeSum {
async fn run(&mut self) -> i32 { async fn run(&mut self) -> i32 {
// Simulate CPU-bound work (e.g., complex calculation)
sleep(Duration::from_millis(self.work_delay_ms)).await;
// Compute sum of range [start, end) // Compute sum of range [start, end)
(self.start..self.end).sum() (self.start..self.end).sum()
} }
@@ -77,50 +104,93 @@ impl Aggregate {
#[tokio::main] #[tokio::main]
async fn main() -> DagResult<()> { async fn main() -> DagResult<()> {
println!("Computing sum of 1-1000 using 4 parallel tasks...\n"); println!("=== Proving True Parallelism ===\n");
// Step 3: Build the DAG const WORK_DELAY: u64 = 100; // Each task does 100ms of "work"
let dag = DagRunner::new();
// Map phase: Create 4 workers that run in parallel // ============================================================================
// Each computes sum of 250 numbers (total 1-1000) // PARALLEL EXECUTION: 4 tasks run simultaneously on different threads
let sum1 = dag.add_task(ComputeSum::new(1, 251)); // 1-250 // ============================================================================
let sum2 = dag.add_task(ComputeSum::new(251, 501)); // 251-500 println!("Running PARALLEL computation (4 workers on separate threads)...");
let sum3 = dag.add_task(ComputeSum::new(501, 751)); // 501-750 let (parallel_time, parallel_result) = {
let sum4 = dag.add_task(ComputeSum::new(751, 1001)); // 751-1000 let start = Instant::now();
let dag = DagRunner::new();
// Reduce phase: Aggregate results from all workers // Create 4 workers - dagx will run them in parallel
let total = dag let sum1 = dag.add_task(ComputeSum::new(1, 251, WORK_DELAY));
.add_task(Aggregate) let sum2 = dag.add_task(ComputeSum::new(251, 501, WORK_DELAY));
.depends_on((&sum1, &sum2, &sum3, &sum4)); let sum3 = dag.add_task(ComputeSum::new(501, 751, WORK_DELAY));
let sum4 = dag.add_task(ComputeSum::new(751, 1001, WORK_DELAY));
// Step 4: Execute with true parallelism let total = dag
// .add_task(Aggregate)
// tokio::spawn() sends tasks to multiple threads in the thread pool. .depends_on((&sum1, &sum2, &sum3, &sum4));
// Workers run concurrently on different threads, achieving true parallelism.
dag.run(|fut| {
tokio::spawn(fut);
})
.await?;
// Step 5: Retrieve and verify results dag.run(|fut| {
let result = dag.get(total)?; tokio::spawn(fut);
let expected: i32 = (1..1001).sum(); })
.await?;
println!("Sum of 1-1000 computed in parallel: {}", result); let result = dag.get(total)?;
println!("Expected: {}", expected); let elapsed = start.elapsed();
assert_eq!(result, 500500); // Sum of 1-1000 println!(
"Parallel result: {} (computed in {}ms)\n",
result,
elapsed.as_millis()
);
assert_eq!(result, 500500);
(elapsed, result)
};
// Note: True parallelism verification // ============================================================================
// // SEQUENTIAL EXECUTION: Run the same 4 tasks one after another
// If you monitor system resources while running this example, you'll see: // ============================================================================
// - Multiple CPU cores being utilized println!("Running SEQUENTIAL computation (same work, one at a time)...");
// - Worker tasks executing simultaneously let (sequential_time, sequential_result) = {
// - Reduced wall-clock time vs sequential execution let start = Instant::now();
//
// This demonstrates that dagx provides REAL parallel execution, // Run tasks sequentially (not using DAG, just raw execution)
// not just async concurrency on a single thread. let task1 = ComputeSum::new(1, 251, WORK_DELAY);
let task2 = ComputeSum::new(251, 501, WORK_DELAY);
let task3 = ComputeSum::new(501, 751, WORK_DELAY);
let task4 = ComputeSum::new(751, 1001, WORK_DELAY);
let r1 = task1.run(()).await;
let r2 = task2.run(()).await;
let r3 = task3.run(()).await;
let r4 = task4.run(()).await;
let result = r1 + r2 + r3 + r4;
let elapsed = start.elapsed();
println!(
"Sequential result: {} (computed in {}ms)\n",
result,
elapsed.as_millis()
);
assert_eq!(result, 500500);
(elapsed, result)
};
// ============================================================================
// PROOF: Compare execution times
// ============================================================================
assert_eq!(parallel_result, sequential_result);
let speedup = sequential_time.as_secs_f64() / parallel_time.as_secs_f64();
println!(
"Speedup: {:.1}x faster with parallel execution!",
speedup
);
println!("This proves dagx runs tasks on multiple CPU cores simultaneously.");
if speedup > 2.0 {
println!("\n✓ Parallel speedup confirmed - tasks ran on multiple threads!");
} else {
println!("\n⚠ Warning: Low speedup. Try running with --release flag for accurate timing.");
}
Ok(()) Ok(())
} }