jf_utils/
par_utils.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the Jellyfish library.
3// You should have received a copy of the MIT License
4// along with the Jellyfish library. If not, see <https://mit-license.org/>.
5
6//! Utilities for parallel code.
7
8/// this function helps with slice iterator creation that optionally use
9/// `par_iter()` when feature flag `parallel` is on.
10///
11/// # Usage
12/// let v = [1, 2, 3, 4, 5];
13/// let sum = parallelizable_slice_iter(&v).sum();
14///
15/// // the above code is a shorthand for (thus equivalent to)
16/// #[cfg(feature = "parallel")]
17/// let sum = v.par_iter().sum();
18/// #[cfg(not(feature = "parallel"))]
19/// let sum = v.iter().sum();
20#[cfg(feature = "parallel")]
21pub fn parallelizable_slice_iter<T: Sync>(data: &[T]) -> rayon::slice::Iter<T> {
22    use rayon::iter::IntoParallelIterator;
23    data.into_par_iter()
24}
25
26#[cfg(not(feature = "parallel"))]
27pub fn parallelizable_slice_iter<T>(data: &[T]) -> ark_std::slice::Iter<T> {
28    data.iter()
29}
30
31#[cfg(feature = "parallel")]
32pub fn parallelizable_chunks<T: Sync>(data: &[T], chunk_size: usize) -> rayon::slice::Chunks<T> {
33    use rayon::slice::ParallelSlice;
34    data.par_chunks(chunk_size)
35}
36
37#[cfg(not(feature = "parallel"))]
38pub fn parallelizable_chunks<T>(data: &[T], chunk_size: usize) -> ark_std::slice::Chunks<T> {
39    data.chunks(chunk_size)
40}