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

use ndarray::{
    Dimension,
    NdProducer,
    Zip,
    ArrayBase,
    DataMut,
};

use prelude::*;

// Arrays

/// Parallel versions of `map_inplace` and `mapv_inplace`.
pub trait ParMap {
    type Item;
    fn par_map_inplace<F>(&mut self, f: F)
        where F: Fn(&mut Self::Item) + Sync + Send;
    fn par_mapv_inplace<F>(&mut self, f: F)
        where F: Fn(Self::Item) -> Self::Item + Sync + Send,
              Self::Item: Clone;
}

impl<A, S, D> ParMap for ArrayBase<S, D>
    where S: DataMut<Elem=A>,
          D: Dimension,
          A: Send + Sync,
{
    type Item = A;
    fn par_map_inplace<F>(&mut self, f: F)
        where F: Fn(&mut Self::Item) + Sync + Send
    {
        self.view_mut().into_par_iter().for_each(f)
    }
    fn par_mapv_inplace<F>(&mut self, f: F)
        where F: Fn(Self::Item) -> Self::Item + Sync + Send,
              Self::Item: Clone
    {
        self.view_mut().into_par_iter()
            .for_each(move |x| *x = f(x.clone()))
    }
}




// Zip

macro_rules! zip_impl {
    ($([$name:ident $($p:ident)*],)+) => {
        $(
        /// The `par_apply` method for `Zip`.
        ///
        /// This is a shorthand for using `.into_par_iter().for_each()` on
        /// `Zip`.
        pub trait $name<$($p),*> {
            fn par_apply<F>(self, function: F)
                where F: Fn($($p),*) + Sync + Send;
        }

        #[allow(non_snake_case)]
        impl<Dim: Dimension, $($p: NdProducer<Dim=Dim>),*> $name<$($p::Item),*> for Zip<($($p,)*), Dim>
            where $($p::Item : Send , )*
                  $($p : Send , )*
        {
            fn par_apply<F>(self, function: F)
                where F: Fn($($p::Item),*) + Sync + Send
            {
                self.into_par_iter().for_each(move |($($p,)*)| function($($p),*))
            }
        }
        )+
    }
}

zip_impl!{
    [ParApply1 P1],
    [ParApply2 P1 P2],
    [ParApply3 P1 P2 P3],
    [ParApply4 P1 P2 P3 P4],
    [ParApply5 P1 P2 P3 P4 P5],
    [ParApply6 P1 P2 P3 P4 P5 P6],
}