cano 0.8.0

High-performance orchestration engine for building resilient, self-healing systems in Rust. Uses Finite State Machines (FSM) for strict, type-safe transitions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! # SIMD Matrix Processing Pipeline Example
//!
//! This example demonstrates a sophisticated data processing pipeline using the `wide` crate
//! for SIMD (Single Instruction, Multiple Data) acceleration in a Cano workflow.
//!
//! ## Pipeline Architecture
//!
//! The pipeline consists of four stages, each implementing different SIMD-accelerated operations:
//!
//! 1. **Data Generation** (`MatrixGenerator`): Creates random 64x64 matrices filled with float32 values
//! 2. **Matrix Multiplication** (`SimdMatrixMultiplier`): Performs matrix multiplication using SIMD f32x8 vectors
//! 3. **Matrix Transformation** (`SimdMatrixTransformer`): Applies scaling and element-wise addition using SIMD
//! 4. **Statistical Analysis** (`SimdStatisticsCalculator`): Computes sum, mean, and variance using SIMD operations
//!
//! ## SIMD Optimizations Demonstrated
//!
//! - **f32x8 Vector Operations**: Processes 8 float32 values simultaneously
//! - **Matrix Multiplication**: Optimized inner loops using SIMD for better cache efficiency
//! - **Element-wise Operations**: Vectorized addition, scaling, and statistical calculations
//! - **Memory Access Patterns**: Aligned memory access for optimal SIMD performance
//!
//! ## Key Features
//!
//! - Uses Cano's three-phase node execution (prep, exec, post)
//! - Inter-node communication through shared MemoryStore
//! - State-driven workflow orchestration with typed transitions
//! - Performance monitoring with execution timing
//! - Graceful handling of partial SIMD vectors (when data size isn't divisible by 8)
//!
//! ## Performance Benefits
//!
//! The SIMD operations provide significant performance improvements over scalar operations:
//! - Matrix multiplication: ~8x theoretical speedup for float32 operations
//! - Element-wise operations: Nearly linear scaling with vector width
//! - Statistical calculations: Reduced loop iterations and improved cache utilization
//!
//! ## Usage
//!
//! ```bash
//! cargo run --example workflow_simd_matrix_pipeline
//! ```
//!
//! The example generates 20 matrices of size 64x64, processes them through the pipeline,
//! and reports timing information for each stage.

use cano::prelude::*;
use std::time::Instant;
use wide::f32x8;

/// Matrix structure optimized for SIMD operations
#[derive(Debug, Clone)]
struct SimdMatrix {
    data: Vec<f32>,
    rows: usize,
    cols: usize,
}

impl SimdMatrix {
    fn new(rows: usize, cols: usize) -> Self {
        Self {
            data: vec![0.0; rows * cols],
            rows,
            cols,
        }
    }

    fn from_data(data: Vec<f32>, rows: usize, cols: usize) -> Self {
        assert_eq!(data.len(), rows * cols);
        Self { data, rows, cols }
    }

    fn get(&self, row: usize, col: usize) -> f32 {
        self.data[row * self.cols + col]
    }

    fn set(&mut self, row: usize, col: usize, value: f32) {
        self.data[row * self.cols + col] = value;
    }

    /// Standard scalar matrix multiplication for comparison
    fn multiply_scalar(&self, other: &SimdMatrix) -> SimdMatrix {
        assert_eq!(self.cols, other.rows);

        let mut result = SimdMatrix::new(self.rows, other.cols);

        for i in 0..self.rows {
            for j in 0..other.cols {
                let mut sum = 0.0;
                for k in 0..self.cols {
                    sum += self.get(i, k) * other.get(k, j);
                }
                result.set(i, j, sum);
            }
        }

        result
    }

    /// SIMD-accelerated matrix multiplication
    fn multiply_simd(&self, other: &SimdMatrix) -> SimdMatrix {
        assert_eq!(self.cols, other.rows);

        let mut result = SimdMatrix::new(self.rows, other.cols);

        // Process 8 elements at a time using SIMD
        for i in 0..self.rows {
            for j in (0..other.cols).step_by(8) {
                let mut sum = f32x8::ZERO;

                for k in 0..self.cols {
                    let a_val = f32x8::splat(self.get(i, k));

                    // Load 8 consecutive elements from matrix B
                    let remaining = (other.cols - j).min(8);
                    let mut b_vals = [0.0f32; 8];
                    for (l, item) in b_vals.iter_mut().enumerate().take(remaining) {
                        if j + l < other.cols {
                            *item = other.get(k, j + l);
                        }
                    }
                    let b_vec = f32x8::from(b_vals);

                    sum += a_val * b_vec;
                }

                // Store the results back
                let sum_array: [f32; 8] = sum.into();
                for (l, &value) in sum_array.iter().enumerate() {
                    if j + l < other.cols {
                        result.set(i, j + l, value);
                    }
                }
            }
        }

        result
    }

    /// SIMD-accelerated element-wise operations
    fn add_simd(&self, other: &SimdMatrix) -> SimdMatrix {
        assert_eq!(self.rows, other.rows);
        assert_eq!(self.cols, other.cols);

        let mut result = SimdMatrix::new(self.rows, self.cols);

        // Process 8 elements at a time
        for i in (0..self.data.len()).step_by(8) {
            let remaining = (self.data.len() - i).min(8);
            let mut a_vals = [0.0f32; 8];
            let mut b_vals = [0.0f32; 8];

            a_vals[..remaining].copy_from_slice(&self.data[i..(remaining + i)]);
            b_vals[..remaining].copy_from_slice(&other.data[i..(remaining + i)]);

            let a_vec = f32x8::from(a_vals);
            let b_vec = f32x8::from(b_vals);
            let sum_vec = a_vec + b_vec;
            let sum_array: [f32; 8] = sum_vec.into();

            result.data[i..(remaining + i)].copy_from_slice(&sum_array[..remaining]);
        }

        result
    }

    /// SIMD-accelerated scalar multiplication
    fn scale_simd(&self, scalar: f32) -> SimdMatrix {
        let mut result = SimdMatrix::new(self.rows, self.cols);
        let scalar_vec = f32x8::splat(scalar);

        for i in (0..self.data.len()).step_by(8) {
            let remaining = (self.data.len() - i).min(8);
            let mut vals = [0.0f32; 8];

            vals[..remaining].copy_from_slice(&self.data[i..(remaining + i)]);

            let vec = f32x8::from(vals);
            let scaled = vec * scalar_vec;
            let scaled_array: [f32; 8] = scaled.into();

            result.data[i..(remaining + i)].copy_from_slice(&scaled_array[..remaining]);
        }

        result
    }
}

/// Pipeline states for the SIMD matrix processing workflow
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum PipelineState {
    Generate,
    Multiply,
    Transform,
    Statistics,
    Complete,
    Error,
}

/// Data generation node - creates random matrices
#[derive(Clone)]
struct MatrixGenerator {
    size: usize,
    count: usize,
}

impl MatrixGenerator {
    fn new(size: usize, count: usize) -> Self {
        Self { size, count }
    }
}

#[async_trait::async_trait]
impl Node<PipelineState> for MatrixGenerator {
    type PrepResult = ();
    type ExecResult = Vec<SimdMatrix>;

    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    async fn prep(&self, _store: &MemoryStore) -> Result<Self::PrepResult, CanoError> {
        println!(
            "🔢 Preparing to generate {} {}x{} matrices...",
            self.count, self.size, self.size
        );
        Ok(())
    }

    async fn exec(&self, _prep_res: Self::PrepResult) -> Self::ExecResult {
        println!("🔢 Generating matrices...");

        let mut matrices = Vec::new();

        for i in 0..self.count {
            let mut data = Vec::with_capacity(self.size * self.size);

            // Generate random matrix data
            for _ in 0..(self.size * self.size) {
                data.push(rand::random::<f32>() * 10.0);
            }

            let matrix = SimdMatrix::from_data(data, self.size, self.size);
            matrices.push(matrix);

            if i % 10 == 0 {
                println!("  Generated matrix {}/{}", i + 1, self.count);
            }
        }

        matrices
    }

    async fn post(
        &self,
        store: &MemoryStore,
        exec_res: Self::ExecResult,
    ) -> Result<PipelineState, CanoError> {
        store.put("matrices", exec_res)?;
        println!("✅ Matrix generation complete!");
        Ok(PipelineState::Multiply)
    }
}

/// Matrix multiplication node using SIMD
#[derive(Clone)]
struct SimdMatrixMultiplier;

#[async_trait::async_trait]
impl Node<PipelineState> for SimdMatrixMultiplier {
    type PrepResult = Vec<SimdMatrix>;
    type ExecResult = Vec<SimdMatrix>;

    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    async fn prep(&self, store: &MemoryStore) -> Result<Self::PrepResult, CanoError> {
        println!("🧮 Loading matrices for SIMD multiplication...");
        let matrices: Vec<SimdMatrix> = store.get("matrices")?;
        Ok(matrices)
    }

    async fn exec(&self, prep_res: Self::PrepResult) -> Self::ExecResult {
        println!("🧮 Performing SIMD matrix multiplications...");
        let start = Instant::now();

        let mut results = Vec::new();

        // Multiply consecutive pairs of matrices
        for chunk in prep_res.chunks(2) {
            if chunk.len() == 2 {
                let result = chunk[0].multiply_simd(&chunk[1]);
                results.push(result);
            }
        }

        let duration = start.elapsed();
        println!(
            "✅ Matrix multiplications complete in {:?} (SIMD accelerated)",
            duration
        );
        println!("  Processed {} matrix pairs", results.len());

        results
    }

    async fn post(
        &self,
        store: &MemoryStore,
        exec_res: Self::ExecResult,
    ) -> Result<PipelineState, CanoError> {
        store.put("multiplied_matrices", exec_res)?;
        Ok(PipelineState::Transform)
    }
}

/// Matrix transformation node - applies scaling and addition using SIMD
#[derive(Clone)]
struct SimdMatrixTransformer {
    scale_factor: f32,
}

impl SimdMatrixTransformer {
    fn new(scale_factor: f32) -> Self {
        Self { scale_factor }
    }
}

#[async_trait::async_trait]
impl Node<PipelineState> for SimdMatrixTransformer {
    type PrepResult = Vec<SimdMatrix>;
    type ExecResult = Vec<SimdMatrix>;

    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    async fn prep(&self, store: &MemoryStore) -> Result<Self::PrepResult, CanoError> {
        println!("🔄 Loading matrices for SIMD transformations...");
        let matrices: Vec<SimdMatrix> = store.get("multiplied_matrices")?;
        Ok(matrices)
    }

    async fn exec(&self, prep_res: Self::PrepResult) -> Self::ExecResult {
        println!(
            "🔄 Applying SIMD transformations (scale factor: {})...",
            self.scale_factor
        );
        let start = Instant::now();

        let mut results: Vec<SimdMatrix> = Vec::new();

        for (i, matrix) in prep_res.iter().enumerate() {
            // Scale the matrix using SIMD
            let scaled = matrix.scale_simd(self.scale_factor);

            // If we have multiple matrices, add them together using SIMD
            if i > 0 && !results.is_empty() {
                let last_idx = results.len() - 1;
                let combined = results[last_idx].add_simd(&scaled);
                results[last_idx] = combined;
            } else {
                results.push(scaled);
            }
        }

        let duration = start.elapsed();
        println!(
            "✅ Matrix transformations complete in {:?} (SIMD accelerated)",
            duration
        );
        println!("  Processed {} matrices", prep_res.len());

        results
    }

    async fn post(
        &self,
        store: &MemoryStore,
        exec_res: Self::ExecResult,
    ) -> Result<PipelineState, CanoError> {
        store.put("transformed_matrices", exec_res)?;
        Ok(PipelineState::Statistics)
    }
}

/// Statistical analysis node using SIMD for vector operations
#[derive(Clone)]
struct SimdStatisticsCalculator;

#[async_trait::async_trait]
impl Node<PipelineState> for SimdStatisticsCalculator {
    type PrepResult = Vec<SimdMatrix>;
    type ExecResult = Vec<(f32, f32, f32)>; // (sum, mean, variance)

    fn config(&self) -> TaskConfig {
        TaskConfig::minimal()
    }

    async fn prep(&self, store: &MemoryStore) -> Result<Self::PrepResult, CanoError> {
        println!("📊 Loading matrices for statistics calculation...");
        let matrices: Vec<SimdMatrix> = store.get("transformed_matrices")?;
        Ok(matrices)
    }

    async fn exec(&self, prep_res: Self::PrepResult) -> Self::ExecResult {
        println!("📊 Calculating matrix statistics using SIMD...");
        let start = Instant::now();

        let mut results = Vec::new();

        for matrix in prep_res {
            // Calculate sum using SIMD
            let mut sum = f32x8::ZERO;
            let data_len = matrix.data.len();

            for i in (0..data_len).step_by(8) {
                let remaining = (data_len - i).min(8);
                let mut vals = [0.0f32; 8];

                vals[..remaining].copy_from_slice(&matrix.data[i..(remaining + i)]);

                let vec = f32x8::from(vals);
                sum += vec;
            }

            // Sum all elements in the SIMD vector
            let sum_array: [f32; 8] = sum.into();
            let total_sum = sum_array.iter().sum::<f32>();
            let mean = total_sum / data_len as f32;

            // Calculate variance using SIMD
            let mean_vec = f32x8::splat(mean);
            let mut variance_sum = f32x8::ZERO;

            for i in (0..data_len).step_by(8) {
                let remaining = (data_len - i).min(8);
                let mut vals = [0.0f32; 8];

                vals[..remaining].copy_from_slice(&matrix.data[i..(remaining + i)]);

                let vec = f32x8::from(vals);
                let diff = vec - mean_vec;
                variance_sum += diff * diff;
            }

            let variance_array: [f32; 8] = variance_sum.into();
            let total_variance = variance_array.iter().sum::<f32>() / data_len as f32;

            results.push((total_sum, mean, total_variance));
        }

        let duration = start.elapsed();
        println!(
            "✅ Statistics calculation complete in {:?} (SIMD accelerated)",
            duration
        );

        results
    }

    async fn post(
        &self,
        store: &MemoryStore,
        exec_res: Self::ExecResult,
    ) -> Result<PipelineState, CanoError> {
        store.put("statistics", exec_res.clone())?;

        for (i, (sum, mean, variance)) in exec_res.iter().enumerate() {
            println!(
                "  Matrix {}: sum={:.2}, mean={:.2}, variance={:.2}",
                i + 1,
                sum,
                mean,
                variance
            );
        }

        Ok(PipelineState::Complete)
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🚀 Starting SIMD Matrix Processing Pipeline");
    println!("This example demonstrates SIMD-accelerated mathematical operations");
    println!("using the 'wide' crate in a Cano workflow pipeline.\n");

    // Quick SIMD vs Scalar performance comparison
    println!("🏁 Quick Performance Comparison (SIMD vs Scalar):");
    let test_matrix_a = {
        let mut data = Vec::with_capacity(16 * 16);
        for _ in 0..(16 * 16) {
            data.push(rand::random::<f32>() * 10.0);
        }
        SimdMatrix::from_data(data, 16, 16)
    };
    let test_matrix_b = {
        let mut data = Vec::with_capacity(16 * 16);
        for _ in 0..(16 * 16) {
            data.push(rand::random::<f32>() * 10.0);
        }
        SimdMatrix::from_data(data, 16, 16)
    };

    // Scalar multiplication timing
    let scalar_start = Instant::now();
    let _scalar_result = test_matrix_a.multiply_scalar(&test_matrix_b);
    let scalar_duration = scalar_start.elapsed();

    // SIMD multiplication timing
    let simd_start = Instant::now();
    let _simd_result = test_matrix_a.multiply_simd(&test_matrix_b);
    let simd_duration = simd_start.elapsed();

    println!("  Scalar 16x16 matrix multiplication: {scalar_duration:?}");
    println!("  SIMD 16x16 matrix multiplication:   {simd_duration:?}");
    if scalar_duration > simd_duration {
        let speedup = scalar_duration.as_nanos() as f64 / simd_duration.as_nanos() as f64;
        println!("  🚀 SIMD speedup: {speedup:.2}x faster!\n");
    } else {
        println!("  📊 Results may vary based on CPU architecture\n");
    }

    let start_time = Instant::now();

    let store = MemoryStore::default();

    // Create the workflow with SIMD-accelerated nodes using new API
    let workflow = Workflow::new(store.clone())
        .register(PipelineState::Generate, MatrixGenerator::new(64, 20)) // 64x64 matrices, 20 of them
        .register(PipelineState::Multiply, SimdMatrixMultiplier)
        .register(PipelineState::Transform, SimdMatrixTransformer::new(1.5))
        .register(PipelineState::Statistics, SimdStatisticsCalculator)
        .add_exit_states(vec![PipelineState::Complete, PipelineState::Error]);

    println!("🔧 Pipeline configured with nodes");
    println!("Pipeline: Generate → Multiply → Transform → Statistics → Complete\n");

    // Execute the workflow
    let _final_state = workflow.orchestrate(PipelineState::Generate).await?;

    let total_duration = start_time.elapsed();
    println!("\n🎉 SIMD Matrix Processing Pipeline completed!");
    println!("Total execution time: {total_duration:?}");

    if let Ok(stats) = store.get::<Vec<(f32, f32, f32)>>("statistics") {
        println!(
            "📈 Final results: {} statistical summaries generated",
            stats.len()
        );
    }

    println!("\n💡 This example showcased:");
    println!("  • Data generation in the first pipeline stage");
    println!("  • SIMD-accelerated matrix multiplication");
    println!("  • SIMD-accelerated element-wise operations");
    println!("  • SIMD-accelerated statistical calculations");
    println!("  • Processing 8 float32 values simultaneously using f32x8");
    println!("  • Three-phase node execution (prep, exec, post)");
    println!("  • Inter-node communication through shared store");

    Ok(())
}