1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

/// Returns either a regular or a parallel iterator depending on whether `concurrent` feature
/// is enabled.
///
/// When `concurrent` feature is enabled, creates a parallel iterator; otherwise, creates a
/// regular iterator. Optionally, `min_length` can be used to specify the minimum length of
/// iterator to be processed in each thread.
///
/// Adapted from: <https://github.com/arkworks-rs/utils/blob/master/src/lib.rs>
#[macro_export]
macro_rules! iter {
    ($e: expr) => {{
        #[cfg(feature = "concurrent")]
        let result = $e.par_iter();

        #[cfg(not(feature = "concurrent"))]
        let result = $e.iter();

        result
    }};
    ($e: expr, $min_len: expr) => {{
        #[cfg(feature = "concurrent")]
        let result = $e.par_iter().with_min_len($min_len);

        #[cfg(not(feature = "concurrent"))]
        let result = $e.iter();

        result
    }};
}

/// Returns either a regular or a parallel mutable iterator depending on whether `concurrent`
/// feature is enabled.
///
/// When `concurrent` feature is enabled, creates a mutable parallel iterator; otherwise,
/// creates a regular mutable iterator. Optionally, `min_length` can be used to specify the
/// minimum length of iterator to be processed in each thread.
///
/// Adapted from: <https://github.com/arkworks-rs/utils/blob/master/src/lib.rs>
#[macro_export]
macro_rules! iter_mut {
    ($e: expr) => {{
        #[cfg(feature = "concurrent")]
        let result = $e.par_iter_mut();

        #[cfg(not(feature = "concurrent"))]
        let result = $e.iter_mut();

        result
    }};
    ($e: expr, $min_len: expr) => {{
        #[cfg(feature = "concurrent")]
        let result = $e.par_iter_mut().with_min_len($min_len);

        #[cfg(not(feature = "concurrent"))]
        let result = $e.iter_mut();

        result
    }};
}

/// Applies a procedure to the provided slice either in a single thread or multiple threads
/// based on whether `concurrent` feature is enabled.
///
/// When `concurrent` feature is enabled, breaks the slice into batches and processes each
/// batch in a separate thread; otherwise, the entire slice is processed as a single batch
/// in one thread. Optionally, `min_batch_size` can be used to specify the minimum size of
/// the resulting batches.
#[macro_export]
macro_rules! batch_iter_mut {
    ($e: expr, $c: expr) => {
        #[cfg(feature = "concurrent")]
        {
            let batch_size = $e.len() / rayon::current_num_threads().next_power_of_two();
            if batch_size < 1 {
                $c($e, 0);
            }
            else {
                $e.par_chunks_mut(batch_size).enumerate().for_each(|(i, batch)| {
                    $c(batch, i * batch_size);
                });
            }
        }

        #[cfg(not(feature = "concurrent"))]
        $c($e, 0);
    };
    ($e: expr, $min_batch_size: expr, $c: expr) => {
        #[cfg(feature = "concurrent")]
        {
            let batch_size = $e.len() / rayon::current_num_threads().next_power_of_two();
            if batch_size < $min_batch_size {
                $c($e, 0);
            }
            else {
                $e.par_chunks_mut(batch_size).enumerate().for_each(|(i, batch)| {
                    $c(batch, i * batch_size);
                });
            }
        }

        #[cfg(not(feature = "concurrent"))]
        $c($e, 0);
    };
}