chunked_parallel_compute

Function chunked_parallel_compute 

Source
pub fn chunked_parallel_compute<T, R>(
    data: &[T],
    chunk_size: usize,
    chunk_op: impl Fn(&[T]) -> Result<R> + Send + Sync,
    reducer: impl Fn(Vec<R>) -> Result<R>,
) -> Result<R>
where T: Clone + Send + Sync, R: Send + Sync,
Expand description

Process a large array in chunks with parallel execution

This function splits a large array into chunks and processes each chunk in parallel.

§Arguments

  • data - Input data
  • chunk_size - Size of each chunk
  • chunk_op - Operation to perform on each chunk
  • reducer - Function to combine results from all chunks

§Returns

  • Combined result

§Examples

use scirs2_metrics::optimization::parallel::{chunked_parallel_compute, ParallelConfig};
use scirs2_metrics::error::Result;

// Create sample data
let data: Vec<f64> = (0..1000).map(|x| x as f64).collect();

// Define chunk operation (sum of squares)
let chunk_op = |chunk: &[f64]| -> Result<f64> {
    Ok(chunk.iter().map(|x| x * x).sum())
};

// Define reducer (sum of partial results)
let reducer = |results: Vec<f64>| -> Result<f64> {
    Ok(results.iter().sum())
};

// Process data in chunks
let result = chunked_parallel_compute(&data, 100, chunk_op, reducer).unwrap();

// Verify result
let expected: f64 = (0..1000).map(|x| (x * x) as f64).sum();
assert!((result - expected).abs() < 1e-10);