Paralight: a lightweight parallelism library for indexed structures
This library allows you to distribute computation over slices among multiple threads. Each thread processes a subset of the items, and a final step reduces the outputs from all threads into a single result.
use ;
use NonZeroUsize;
// Define thread pool parameters.
let pool_builder = ThreadPoolBuilder ;
// Create a scoped thread pool.
let sum = pool_builder.scope;
assert_eq!;
Note: In principle, Paralight could be extended to support other inputs than slices as long as they are indexed, but for now only slices are supported. Come back to check when future versions are published!
Work-stealing strategy
Paralight offers two strategies to distribute computation among threads:
- [
RangeStrategy::Fixed] splits the input evenly and hands out a fixed sequential range of items to each thread, - [
RangeStrategy::WorkStealing] starts with the fixed distribution, but lets each thread steal items from others once it is done computing its items.
Note: In work-stealing mode, each thread processes an arbitrary subset of items
in arbitrary order, meaning that the reduction operation must be both
commutative and associative to yield a deterministic result (in contrast with
the standard library's Iterator trait that processes items in order).
Fortunately, a lot of common operations are commutative and associative, but be
mindful of this.
Debugging
Two optional features are available if you want to debug performance.
log, based on thelogcrate prints basic information about inter-thread synchronization: thread creation/shutdown, when each thread starts/finishes a computation, etc.log_parallelismprints detailed traces about which items are processed by which thread, and work-stealing statistics (e.g. how many times work was stolen among threads).
Note that in any case neither the input items nor the resulting computation are
logged. Only the indices of the items in the input may be present in the logs.
If you're concerned that these indices leak too much information about your
data, you need to make sure that you depend on Paralight with the log and
log_parallelism features disabled.
Disclaimer
This is not an officially supported Google product.
Contributing
See CONTRIBUTING.md for details.
License
This software is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE for details.