-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathminmax.rs
More file actions
433 lines (360 loc) · 15.1 KB
/
minmax.rs
File metadata and controls
433 lines (360 loc) · 15.1 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use rayon::prelude::*;
use argminmax::{ArgMinMax, NaNArgMinMax};
use num_traits::{AsPrimitive, FromPrimitive};
use super::searchsorted::{
get_equidistant_bin_idx_iterator, get_equidistant_bin_idx_iterator_parallel,
};
use super::types::Num;
use super::POOL;
// ----------------------------------- NON-PARALLEL ------------------------------------
// ----------- WITH X
macro_rules! min_max_with_x {
($func_name:ident, $trait:path, $f_argminmax:expr) => {
pub fn $func_name<Tx, Ty>(x: &[Tx], arr: &[Ty], n_out: usize) -> Vec<usize>
where
for<'a> &'a [Ty]: $trait,
Tx: Num + FromPrimitive + AsPrimitive<f64>,
Ty: Copy + PartialOrd,
{
assert_eq!(n_out % 2, 0);
let bin_idx_iterator = get_equidistant_bin_idx_iterator(x, n_out / 2);
min_max_generic_with_x(arr, bin_idx_iterator, n_out, $f_argminmax)
}
};
}
min_max_with_x!(min_max_with_x, ArgMinMax, |arr| arr.argminmax());
min_max_with_x!(min_max_with_x_nan, NaNArgMinMax, |arr| arr.nanargminmax());
// ----------- WITHOUT X
macro_rules! min_max_without_x {
($func_name:ident, $trait:path, $f_argminmax:expr) => {
pub fn $func_name<T: Copy + PartialOrd>(arr: &[T], n_out: usize) -> Vec<usize>
where
for<'a> &'a [T]: $trait,
{
assert_eq!(n_out % 2, 0);
min_max_generic(arr, n_out, $f_argminmax)
}
};
}
min_max_without_x!(min_max_without_x, ArgMinMax, |arr| arr.argminmax());
min_max_without_x!(min_max_without_x_nan, NaNArgMinMax, |arr| arr
.nanargminmax());
// ------------------------------------- PARALLEL --------------------------------------
// ----------- WITH X
macro_rules! min_max_with_x_parallel {
($func_name:ident, $trait:path, $f_argminmax:expr) => {
pub fn $func_name<Tx, Ty>(x: &[Tx], arr: &[Ty], n_out: usize) -> Vec<usize>
where
for<'a> &'a [Ty]: $trait,
Tx: Num + FromPrimitive + AsPrimitive<f64> + Send + Sync,
Ty: Copy + PartialOrd + Send + Sync,
{
assert_eq!(n_out % 2, 0);
let bin_idx_iterator = get_equidistant_bin_idx_iterator_parallel(x, n_out / 2);
min_max_generic_with_x_parallel(arr, bin_idx_iterator, n_out, $f_argminmax)
}
};
}
min_max_with_x_parallel!(min_max_with_x_parallel, ArgMinMax, |arr| arr.argminmax());
min_max_with_x_parallel!(min_max_with_x_parallel_nan, NaNArgMinMax, |arr| arr
.nanargminmax());
// ----------- WITHOUT X
macro_rules! min_max_without_x_parallel {
($func_name:ident, $trait:path, $f_argminmax:expr) => {
pub fn $func_name<T: Copy + PartialOrd + Send + Sync>(arr: &[T], n_out: usize) -> Vec<usize>
where
for<'a> &'a [T]: $trait,
{
assert_eq!(n_out % 2, 0);
min_max_generic_parallel(arr, n_out, $f_argminmax)
}
};
}
min_max_without_x_parallel!(min_max_without_x_parallel, ArgMinMax, |arr| arr.argminmax());
min_max_without_x_parallel!(min_max_without_x_parallel_nan, NaNArgMinMax, |arr| arr
.nanargminmax());
// ----------------------------------- GENERICS ------------------------------------
// --------------------- WITHOUT X
#[inline(always)]
pub(crate) fn min_max_generic<T: Copy>(
arr: &[T],
n_out: usize,
f_argminmax: fn(&[T]) -> (usize, usize),
) -> Vec<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return (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 mut sampled_indices = vec![usize::default(); n_out];
let mut start_idx: usize = 0;
for i in 0..n_out / 2 {
// Decided to use multiplication instead of adding to the accumulator (end)
// as multiplication seems to be less prone to rounding errors.
let end: f64 = block_size * (i + 1) as f64;
let end_idx: usize = end as usize + 1;
let (min_index, max_index) = f_argminmax(&arr[start_idx..end_idx]);
// Add the indexes in sorted order
if min_index < max_index {
sampled_indices[2 * i] = min_index + start_idx;
sampled_indices[2 * i + 1] = max_index + start_idx;
} else {
sampled_indices[2 * i] = max_index + start_idx;
sampled_indices[2 * i + 1] = min_index + start_idx;
}
start_idx = end_idx;
}
sampled_indices
}
#[inline(always)]
pub(crate) fn min_max_generic_parallel<T: Copy + PartialOrd + Send + Sync>(
arr: &[T],
n_out: usize,
f_argminmax: fn(&[T]) -> (usize, usize),
) -> Vec<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return (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
// These indexes are used to calculate the start and end indexes of each bin in
// the multi-threaded execution
let mut sampled_indices: Vec<usize> = (0..n_out).collect::<Vec<usize>>();
POOL.install(|| {
sampled_indices
.par_chunks_exact_mut(2)
.for_each(|sampled_index_chunk| {
let i: f64 = unsafe { *sampled_index_chunk.get_unchecked(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(&arr[start_idx..end_idx]);
// Add the indexes in sorted order
if min_index < max_index {
sampled_index_chunk[0] = min_index + start_idx;
sampled_index_chunk[1] = max_index + start_idx;
} else {
sampled_index_chunk[0] = max_index + start_idx;
sampled_index_chunk[1] = min_index + start_idx;
}
})
});
sampled_indices
}
// --------------------- WITH X
#[inline(always)]
pub(crate) fn min_max_generic_with_x<T: Copy>(
arr: &[T],
bin_idx_iterator: impl Iterator<Item = Option<(usize, usize)>>,
n_out: usize,
f_argminmax: fn(&[T]) -> (usize, usize),
) -> Vec<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return (0..arr.len()).collect::<Vec<usize>>();
}
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 = &arr[start..end];
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);
}
}
}
});
sampled_indices
}
#[inline(always)]
pub(crate) fn min_max_generic_with_x_parallel<T: Copy + Send + Sync>(
arr: &[T],
bin_idx_iterator: impl IndexedParallelIterator<Item = impl Iterator<Item = Option<(usize, usize)>>>,
n_out: usize,
f_argminmax: fn(&[T]) -> (usize, usize),
) -> Vec<usize> {
// Assumes n_out is a multiple of 2
if n_out >= arr.len() {
return (0..arr.len()).collect::<Vec<usize>>();
}
POOL.install(|| {
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 = &arr[start..end];
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>>()
})
}
#[cfg(test)]
mod tests {
use num_traits::AsPrimitive;
use rstest::rstest;
use rstest_reuse::{self, *};
use super::{min_max_with_x, min_max_without_x};
use super::{min_max_with_x_parallel, min_max_without_x_parallel};
use dev_utils::utils;
fn get_array_f32(n: usize) -> Vec<f32> {
utils::get_random_array(n, f32::MIN, f32::MAX)
}
// Template for n_out
#[template]
#[rstest]
#[case(198)]
#[case(200)]
#[case(202)]
fn n_outs(#[case] n_out: usize) {}
#[test]
fn test_min_max_scalar_without_x_correct() {
let arr: [f32; 100] = core::array::from_fn(|i| i.as_());
let sampled_indices = min_max_without_x(&arr, 10);
let sampled_values = sampled_indices
.iter()
.map(|x| arr[*x])
.collect::<Vec<f32>>();
let expected_indices = vec![0, 19, 20, 39, 40, 59, 60, 79, 80, 99];
let expected_values = expected_indices
.iter()
.map(|x| *x as f32)
.collect::<Vec<f32>>();
assert_eq!(sampled_indices, expected_indices);
assert_eq!(sampled_values, expected_values);
}
#[test]
fn test_min_max_scalar_without_x_parallel_correct() {
let arr: [f32; 100] = core::array::from_fn(|i| i.as_());
let sampled_indices = min_max_without_x_parallel(&arr, 10);
let sampled_values = sampled_indices
.iter()
.map(|x| arr[*x])
.collect::<Vec<f32>>();
let expected_indices = vec![0, 19, 20, 39, 40, 59, 60, 79, 80, 99];
let expected_values = expected_indices
.iter()
.map(|x| *x as f32)
.collect::<Vec<f32>>();
assert_eq!(sampled_indices, expected_indices);
assert_eq!(sampled_values, expected_values);
}
#[test]
fn test_min_max_scalar_with_x_correct() {
let x: [i32; 100] = core::array::from_fn(|i| i.as_());
let arr: [f32; 100] = core::array::from_fn(|i| i.as_());
let sampled_indices = min_max_with_x(&x, &arr, 10);
let sampled_values = sampled_indices
.iter()
.map(|x| arr[*x])
.collect::<Vec<f32>>();
let expected_indices = vec![0, 19, 20, 39, 40, 59, 60, 79, 80, 99];
let expected_values = expected_indices
.iter()
.map(|x| *x as f32)
.collect::<Vec<f32>>();
assert_eq!(sampled_indices, expected_indices);
assert_eq!(sampled_values, expected_values);
}
#[test]
fn test_min_max_scalar_with_x_parallel_correct() {
let x: [i32; 100] = core::array::from_fn(|i| i.as_());
let arr: [f32; 100] = core::array::from_fn(|i| i.as_());
let sampled_indices = min_max_with_x_parallel(&x, &arr, 10);
let sampled_values = sampled_indices
.iter()
.map(|x| arr[*x])
.collect::<Vec<f32>>();
let expected_indices = vec![0, 19, 20, 39, 40, 59, 60, 79, 80, 99];
let expected_values = expected_indices
.iter()
.map(|x| *x as f32)
.collect::<Vec<f32>>();
assert_eq!(sampled_indices, expected_indices);
assert_eq!(sampled_values, expected_values);
}
#[test]
fn test_min_max_scalar_with_x_gap() {
// We will create a gap in the middle of the array
// Increment the second half of the array by 50
let x: [i32; 100] = core::array::from_fn(|i| if i > 50 { (i + 50).as_() } else { i.as_() });
let arr: [f32; 100] = core::array::from_fn(|i| i.as_());
let sampled_indices = min_max_with_x(&x, &arr, 10);
assert_eq!(sampled_indices.len(), 8); // One full gap
let expected_indices = vec![0, 29, 30, 50, 51, 69, 70, 99];
assert_eq!(sampled_indices, expected_indices);
// Increment the second half of the array by 50 again
let x = x.map(|i| if i > 101 { i + 50 } else { i });
let sampled_indices = min_max_with_x(&x, &arr, 10);
assert_eq!(sampled_indices.len(), 9); // Gap with 1 value
let expected_indices = vec![0, 39, 40, 50, 51, 52, 59, 60, 99];
assert_eq!(sampled_indices, expected_indices);
}
#[test]
fn test_min_max_scalar_with_x_parallel_gap() {
// Create a gap in the middle of the array
// Increment the second half of the array by 50
let x: [i32; 100] = core::array::from_fn(|i| if i > 50 { (i + 50).as_() } else { i.as_() });
let arr: [f32; 100] = core::array::from_fn(|i| i.as_());
let sampled_indices = min_max_with_x_parallel(&x, &arr, 10);
assert_eq!(sampled_indices.len(), 8); // One full gap
let expected_indices = vec![0, 29, 30, 50, 51, 69, 70, 99];
assert_eq!(sampled_indices, expected_indices);
// Increment the second half of the array by 50 again
let x = x.map(|i| if i > 101 { i + 50 } else { i });
let sampled_indices = min_max_with_x_parallel(&x, &arr, 10);
assert_eq!(sampled_indices.len(), 9); // Gap with 1 value
let expected_indices = vec![0, 39, 40, 50, 51, 52, 59, 60, 99];
assert_eq!(sampled_indices, expected_indices);
}
#[apply(n_outs)]
fn test_many_random_runs_same_output(n_out: usize) {
const N: usize = 20_003;
let x: [i32; N] = core::array::from_fn(|i| i.as_());
for _ in 0..100 {
let mut arr = get_array_f32(N);
arr[N - 1] = f32::INFINITY; // Make sure the last value is always the max
let idxs1 = min_max_without_x(arr.as_slice(), n_out);
let idxs2 = min_max_without_x_parallel(arr.as_slice(), n_out);
let idxs3 = min_max_with_x(&x, arr.as_slice(), n_out);
let idxs4 = min_max_with_x_parallel(&x, arr.as_slice(), n_out);
assert_eq!(idxs1, idxs2);
assert_eq!(idxs1, idxs3);
assert_eq!(idxs1, idxs4);
}
}
}