Skip to content

Commit

Permalink
Ajust documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gendx committed Sep 20, 2024
1 parent 9a76f24 commit 84d4fe3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ let pool_builder = ThreadPoolBuilder {
range_strategy: RangeStrategy::WorkStealing,
};

// Create a scoped thread pool attached to the given input and accumulator (see below).
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Create a scoped thread pool.
let sum = pool_builder.scope(
|thread_pool| {
// Compute the sum of the inputs.
// Compute the sum of a slice.
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
thread_pool
.process_inputs(&input, || 0u64, |acc, _, x| *acc += *x, |acc| acc)
.process_inputs(
&input,
/* init */ || 0u64,
/* process_item */ |acc, _, x| *acc += *x,
/* finalize */ |acc| acc,
)
.reduce(|a, b| a + b)
.unwrap()
},
Expand Down
35 changes: 30 additions & 5 deletions src/thread_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct ThreadPoolBuilder {
}

impl ThreadPoolBuilder {
/// Spawn a scoped thread pool using the given input and accumulator.
/// Spawn a scoped thread pool.
///
/// ```rust
/// # use paralight::{RangeStrategy, ThreadPoolBuilder};
Expand All @@ -52,8 +52,8 @@ impl ThreadPoolBuilder {
/// range_strategy: RangeStrategy::WorkStealing,
/// };
///
/// let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let sum = pool_builder.scope(|thread_pool| {
/// let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// thread_pool
/// .process_inputs(&input, || 0u64, |acc, _, x| *acc += *x, |acc| acc)
/// .reduce(|a, b| a + b)
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<'scope, Input: Sync + 'scope, Output: Send + 'scope, Accum: 'scope>
ThreadPool<'scope, Input, Output, Accum>
{
/// Creates a new pool tied to the given scope, spawning the given number of
/// threads and using the given input slice.
/// worker threads.
fn new<'env>(
thread_scope: &'scope Scope<'scope, 'env>,
num_threads: NonZeroUsize,
Expand Down Expand Up @@ -257,8 +257,33 @@ impl<'scope, Input: Sync + 'scope, Output: Send + 'scope, Accum: 'scope>
}
}

/// Performs a computation round, processing the input slice in parallel and
/// returning an iterator over the threads' outputs.
/// Processes an input slice in parallel and returns an iterator over the
/// threads' outputs.
///
/// # Parameters
///
/// - `input` slice to process in parallel,
/// - `init` function to create a new (per-thread) accumulator,
/// - `process_item` function to accumulate an item from the slice into the
/// accumulator,
/// - `finalize` function to transform an accumulator into an output.
///
/// ```rust
/// # use paralight::{RangeStrategy, ThreadPoolBuilder};
/// # use std::num::NonZeroUsize;
/// # let pool_builder = ThreadPoolBuilder {
/// # num_threads: NonZeroUsize::try_from(4).unwrap(),
/// # range_strategy: RangeStrategy::WorkStealing,
/// # };
/// # pool_builder.scope(|thread_pool| {
/// let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/// let sum = thread_pool
/// .process_inputs(&input, || 0u64, |acc, _, x| *acc += *x, |acc| acc)
/// .reduce(|a, b| a + b)
/// .unwrap();
/// assert_eq!(sum, 5 * 11);
/// # });
/// ```
pub fn process_inputs(
&self,
input: &[Input],
Expand Down

0 comments on commit 84d4fe3

Please sign in to comment.