-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathlib.rs
148 lines (141 loc) · 3.97 KB
/
lib.rs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Parallelization features for ndarray.
//!
//! The array views and references to owned arrays all implement
//! `NdarrayIntoParallelIterator` (*); the default parallel iterators (each element
//! by reference or mutable reference) have no ordering guarantee in their
//! parallel implementations.
//!
//! `.axis_iter()` and `.axis_iter_mut()` also have parallel counterparts,
//! and their parallel iterators are indexed (and thus ordered) and exact length.
//!
//! `Zip` also implements `NdarrayIntoParallelIterator`, and there is an
//! extension trait so that it can use a method `.par_apply` directly.
//!
//! (*) This regime of a custom trait instead of rayon’s own is since we
//! use this intermediate ndarray-parallel crate.
//!
//! # Examples
//!
//!
//! ## Arrays and array views
//!
//! Compute the exponential of each element in an array, parallelized.
//!
//! ```
//! extern crate ndarray;
//! extern crate ndarray_parallel;
//!
//! use ndarray::Array2;
//! use ndarray_parallel::prelude::*;
//!
//! fn main() {
//! let mut a = Array2::<f64>::zeros((128, 128));
//!
//! // Parallel versions of regular array methods (ParMap trait)
//! a.par_map_inplace(|x| *x = x.exp());
//! a.par_mapv_inplace(f64::exp);
//!
//! // You can also use the parallel iterator directly
//! a.par_iter_mut().for_each(|x| *x = x.exp());
//! }
//! ```
//!
//! ## Axis iterators
//!
//! Use the parallel `.axis_iter()` to compute the sum of each row.
//!
//! ```
//! extern crate ndarray;
//! extern crate ndarray_parallel;
//!
//! use ndarray::Array;
//! use ndarray::Axis;
//! use ndarray_parallel::prelude::*;
//!
//! fn main() {
//! let a = Array::linspace(0., 63., 64).into_shape((4, 16)).unwrap();
//! let mut sums = Vec::new();
//! a.axis_iter(Axis(0))
//! .into_par_iter()
//! .map(|row| row.sum())
//! .collect_into_vec(&mut sums);
//!
//! assert_eq!(sums, [120., 376., 632., 888.]);
//! }
//! ```
//!
//! ## Axis chunks iterators
//!
//! Use the parallel `.axis_chunks_iter()` to process your data in chunks.
//!
//! ```
//! extern crate ndarray;
//!
//! use ndarray::Array;
//! use ndarray::Axis;
//! use ndarray_parallel::prelude::*;
//!
//! fn main() {
//! let a = Array::linspace(0., 63., 64).into_shape((4, 16)).unwrap();
//! let mut shapes = Vec::new();
//! a.axis_chunks_iter(Axis(0), 3)
//! .into_par_iter()
//! .map(|chunk| chunk.shape().to_owned())
//! .collect_into_vec(&mut shapes);
//!
//! assert_eq!(shapes, [vec![3, 16], vec![1, 16]]);
//! }
//! ```
//!
//! ## Zip
//!
//! Use zip for lock step function application across several arrays
//!
//! ```
//! extern crate ndarray;
//! extern crate ndarray_parallel;
//!
//! use ndarray::Array3;
//! use ndarray::Zip;
//! use ndarray_parallel::prelude::*;
//!
//! type Array3f64 = Array3<f64>;
//!
//! fn main() {
//! const N: usize = 128;
//! let a = Array3f64::from_elem((N, N, N), 1.);
//! let b = Array3f64::from_elem(a.dim(), 2.);
//! let mut c = Array3f64::zeros(a.dim());
//!
//! Zip::from(&mut c)
//! .and(&a)
//! .and(&b)
//! .par_apply(|c, &a, &b| {
//! *c += a - b;
//! });
//! }
//! ```
#![doc(html_root_url = "http://docs.rs/ndarray-parallel/0.9/")]
pub extern crate ndarray;
pub extern crate rayon;
/// Into- traits for creating parallelized iterators and `par_azip` macro.
pub mod prelude {
pub use par_azip;
// happy and insane; ignorance is bluss
pub use NdarrayIntoParallelIterator;
pub use NdarrayIntoParallelRefIterator;
pub use NdarrayIntoParallelRefMutIterator;
#[doc(no_inline)]
pub use rayon::prelude::{IndexedParallelIterator, ParallelIterator};
pub use ext_traits::ParMap;
pub use ext_traits::{ParApply1, ParApply2, ParApply3, ParApply4, ParApply5, ParApply6};
}
pub use into_traits::{
NdarrayIntoParallelIterator, NdarrayIntoParallelRefIterator, NdarrayIntoParallelRefMutIterator,
};
pub use par::Parallel;
mod ext_traits;
mod into_impls;
mod into_traits;
mod par;
mod zipmacro;