-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgeneric.rs
More file actions
179 lines (153 loc) · 6.54 KB
/
Copy pathgeneric.rs
File metadata and controls
179 lines (153 loc) · 6.54 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use ndarray::Zip;
use ndarray::{Array1, ArrayView1};
use rayon::iter::IndexedParallelIterator;
use rayon::prelude::*;
// --------------------- WITHOUT X
#[inline(always)]
pub(crate) fn min_max_generic<T: Copy>(
arr: ArrayView1<T>,
n_out: usize,
f_argminmax: fn(ArrayView1<T>) -> (usize, usize),
) -> Array1<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return Array1::from((0..arr.len()).collect::<Vec<usize>>());
}
// arr.len() - 1 is used to match the delta of a range-index (0..arr.len()-1)
let block_size: f64 = (arr.len() - 1) as f64 / (n_out / 2) as f64;
let arr_ptr = arr.as_ptr();
// Store the enumerated indexes in the output array
let mut sampled_indices: Array1<usize> = Array1::from_vec((0..n_out).collect::<Vec<usize>>());
Zip::from(sampled_indices.exact_chunks_mut(2)).for_each(|mut sampled_index| {
let i: f64 = unsafe { *sampled_index.uget(0) >> 1 } as f64;
let start_idx: usize = (block_size * i) as usize + (i != 0.0) as usize;
let end_idx: usize = (block_size * (i + 1.0)) as usize + 1;
let (min_index, max_index) = f_argminmax(unsafe {
ArrayView1::from_shape_ptr((end_idx - start_idx,), arr.as_ptr().add(start_idx))
});
// Add the indexes in sorted order
if min_index < max_index {
sampled_index[0] = min_index + start_idx;
sampled_index[1] = max_index + start_idx;
} else {
sampled_index[0] = max_index + start_idx;
sampled_index[1] = min_index + start_idx;
}
});
sampled_indices
}
#[inline(always)]
pub(crate) fn min_max_generic_parallel<T: Copy + PartialOrd + Send + Sync>(
arr: ArrayView1<T>,
n_out: usize,
f_argminmax: fn(ArrayView1<T>) -> (usize, usize),
) -> Array1<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return Array1::from((0..arr.len()).collect::<Vec<usize>>());
}
// arr.len() - 1 is used to match the delta of a range-index (0..arr.len()-1)
let block_size: f64 = (arr.len() - 1) as f64 / (n_out / 2) as f64;
// Store the enumerated indexes in the output array
let mut sampled_indices: Array1<usize> = Array1::from_vec((0..n_out).collect::<Vec<usize>>());
Zip::from(sampled_indices.exact_chunks_mut(2)).par_for_each(|mut sampled_index| {
let i: f64 = unsafe { *sampled_index.uget(0) >> 1 } as f64;
let start_idx: usize = (block_size * i) as usize + (i != 0.0) as usize;
let end_idx: usize = (block_size * (i + 1.0)) as usize + 1;
let (min_index, max_index) = f_argminmax(unsafe {
ArrayView1::from_shape_ptr((end_idx - start_idx,), arr.as_ptr().add(start_idx))
});
// Add the indexes in sorted order
if min_index < max_index {
sampled_index[0] = min_index + start_idx;
sampled_index[1] = max_index + start_idx;
} else {
sampled_index[0] = max_index + start_idx;
sampled_index[1] = min_index + start_idx;
}
});
sampled_indices
}
// --------------------- WITH X
#[inline(always)]
pub(crate) fn min_max_generic_with_x<T: Copy>(
arr: ArrayView1<T>,
bin_idx_iterator: impl Iterator<Item = Option<(usize, usize)>>,
n_out: usize,
f_argminmax: fn(ArrayView1<T>) -> (usize, usize),
) -> Array1<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return Array1::from((0..arr.len()).collect::<Vec<usize>>());
}
let ptr = arr.as_ptr();
let mut sampled_indices: Vec<usize> = Vec::with_capacity(n_out);
bin_idx_iterator.for_each(|bin| {
if let Some((start, end)) = bin {
if end <= start + 2 {
// If the bin has <= 2 elements, just add them all
for i in start..end {
sampled_indices.push(i);
}
} else {
// If the bin has at least two elements, add the argmin and argmax
let step = unsafe { ArrayView1::from_shape_ptr(end - start, ptr.add(start)) };
let (min_index, max_index) = f_argminmax(step);
// Add the indexes in sorted order
if min_index < max_index {
sampled_indices.push(min_index + start);
sampled_indices.push(max_index + start);
} else {
sampled_indices.push(max_index + start);
sampled_indices.push(min_index + start);
}
}
}
});
Array1::from_vec(sampled_indices)
}
#[inline(always)]
pub(crate) fn min_max_generic_with_x_parallel<T: Copy + Send + Sync>(
arr: ArrayView1<T>,
bin_idx_iterator: impl IndexedParallelIterator<Item = impl Iterator<Item = Option<(usize, usize)>>>,
n_out: usize,
f_argminmax: fn(ArrayView1<T>) -> (usize, usize),
) -> Array1<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return Array1::from((0..arr.len()).collect::<Vec<usize>>());
}
Array1::from_vec(
bin_idx_iterator
.flat_map(|bin_idx_iterator| {
bin_idx_iterator
.map(|bin| {
match bin {
Some((start, end)) => {
if end <= start + 2 {
// If the bin has <= 2 elements, just return them all
return (start..end).collect::<Vec<usize>>();
}
// If the bin has at least two elements, return the argmin and argmax
let step = unsafe {
ArrayView1::from_shape_ptr(end - start, arr.as_ptr().add(start))
};
let (min_index, max_index) = f_argminmax(step);
// Return the indexes in sorted order
if min_index < max_index {
vec![min_index + start, max_index + start]
} else {
vec![max_index + start, min_index + start]
}
} // If the bin is empty, return empty Vec
None => {
vec![]
}
}
})
.collect::<Vec<Vec<usize>>>()
})
.flatten()
.collect::<Vec<usize>>(),
)
}