ndarray/parallel/zipmacro.rs
1// Copyright 2017 bluss and ndarray developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#[macro_export]
10/// Parallelized array zip macro: lock step function application across several
11/// arrays and producers.
12///
13/// This is a version of the [`azip`] macro that requires the crate feature
14/// `rayon` to be enabled.
15///
16/// See the [`azip`] macro for more details about the macro syntax!
17///
18/// This example:
19///
20/// ```rust,ignore
21/// par_azip!((a in &mut a, &b in &b, &c in &c) { *a = b + c })
22/// ```
23///
24/// Is equivalent to:
25///
26/// ```rust,ignore
27/// Zip::from(&mut a).and(&b).and(&c).par_for_each(|a, &b, &c| {
28/// *a = b + c;
29/// });
30/// ```
31///
32/// **Panics** if any of the arrays are not of the same shape.
33///
34/// ## Examples
35///
36/// ```rust
37/// use ndarray::Array2;
38/// use ndarray::parallel::par_azip;
39///
40/// type M = Array2<f32>;
41///
42/// let mut a = M::zeros((16, 16));
43/// let b = M::from_elem(a.dim(), 1.);
44/// let c = M::from_elem(a.dim(), 2.);
45///
46/// // Compute a simple ternary operation:
47/// // elementwise addition of b and c, stored in a
48///
49/// par_azip!((a in &mut a, &b in &b, &c in &c) *a = b + c);
50///
51/// assert_eq!(a, &b + &c);
52/// ```
53macro_rules! par_azip {
54 ($($t:tt)*) => {
55 $crate::azip!(@build par_for_each $($t)*)
56 };
57}