Skip to main content

chunked_parallel_process

Function chunked_parallel_process 

Source
pub fn chunked_parallel_process<T, R, F>(
    data: &[T],
    process_fn: F,
    chunk_size: usize,
    n_workers: usize,
) -> Vec<R>
where T: Send + Sync + Clone + 'static, R: Send + 'static, F: Fn(&[T]) -> Vec<R> + Send + Clone + 'static,
Expand description

Parallel processing of data divided into chunk_size slices.

Each chunk is processed by process_fn on one of n_workers threads. The flat results from all chunks are concatenated in input order.

§Type constraints

T must implement Clone because each chunk is cloned into an owned Arc<Vec<T>> before being sent to a worker thread.

§Argument clamping

  • chunk_size == 0 is clamped to 1.
  • n_workers == 0 is clamped to 1.

§Example

use scirs2_core::distributed::primitives::chunked_parallel_process;

let data: Vec<i32> = (1..=12).collect();
let doubled = chunked_parallel_process(
    &data,
    |chunk| chunk.iter().map(|&x| x * 2).collect(),
    4,
    3,
);
assert_eq!(doubled, vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]);