-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add minmax and minmax_position #9
Open
LaihoE
wants to merge
1
commit into
master
Choose a base branch
from
minmax
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use crate::position::PositionSimd; | ||
use crate::SIMD_LEN; | ||
use std::simd::prelude::SimdPartialEq; | ||
use std::simd::Mask; | ||
use std::simd::Simd; | ||
use std::simd::SimdElement; | ||
use std::slice; | ||
|
||
pub trait MinMaxSimd<'a, T> | ||
where | ||
T: PartialOrd + Ord + Copy, | ||
T: SimdElement + std::cmp::PartialEq, | ||
Simd<T, SIMD_LEN>: SimdPartialEq<Mask = Mask<T::Mask, SIMD_LEN>>, | ||
{ | ||
/// Returns Option<min_element, max_element> | ||
fn minmax_simd(&self) -> Option<(T, T)>; | ||
/// Returns Option<position_min, position_max> | ||
fn minmax_position_simd(&self) -> Option<(usize, usize)>; | ||
} | ||
|
||
impl<'a, T> MinMaxSimd<'a, T> for slice::Iter<'a, T> | ||
where | ||
T: SimdElement + std::cmp::PartialEq + std::cmp::Ord, | ||
Simd<T, SIMD_LEN>: SimdPartialEq<Mask = Mask<T::Mask, SIMD_LEN>>, | ||
{ | ||
fn minmax_simd(&self) -> Option<(T, T)> | ||
where | ||
T: PartialOrd + Ord + Copy, | ||
{ | ||
// This seems to robustly produce great assembly. | ||
let arr = self.as_slice(); | ||
if arr.is_empty() { | ||
return None; | ||
} | ||
let first_element = *arr.first().unwrap(); | ||
let mut smallest = first_element; | ||
let mut largest = first_element; | ||
for val in arr { | ||
smallest = std::cmp::min(*val, smallest); | ||
largest = std::cmp::max(*val, largest); | ||
} | ||
Some((smallest, largest)) | ||
} | ||
fn minmax_position_simd(&self) -> Option<(usize, usize)> | ||
where | ||
T: PartialOrd + Ord + Copy, | ||
T: SimdElement + std::cmp::PartialEq, | ||
Simd<T, SIMD_LEN>: SimdPartialEq<Mask = Mask<T::Mask, SIMD_LEN>>, | ||
{ | ||
let arr = self.as_slice(); | ||
if let Some((min, max)) = arr.iter().minmax_simd() { | ||
// This seems to do oddly well even with big N, maybe some sort of ilp? | ||
let pos_min = arr.iter().position_simd(min)?; | ||
let pos_max = arr.iter().position_simd(max)?; | ||
return Some((pos_min, pos_max)); | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::SIMD_LEN; | ||
use itertools::Itertools; | ||
use rand::distributions::Standard; | ||
use rand::prelude::Distribution; | ||
use rand::Rng; | ||
use std::fmt::Debug; | ||
use std::simd::prelude::SimdPartialEq; | ||
use std::simd::Mask; | ||
use std::simd::Simd; | ||
use std::simd::SimdElement; | ||
|
||
fn test_simd_for_type<T>() | ||
where | ||
T: rand::distributions::uniform::SampleUniform | ||
+ PartialEq | ||
+ Debug | ||
+ Copy | ||
+ Default | ||
+ SimdElement | ||
+ std::cmp::PartialEq | ||
+ Ord, | ||
Simd<T, SIMD_LEN>: SimdPartialEq<Mask = Mask<T::Mask, SIMD_LEN>>, | ||
Standard: Distribution<T>, | ||
{ | ||
for len in 0..1000 { | ||
for _ in 0..5 { | ||
let mut v: Vec<T> = vec![T::default(); len]; | ||
let mut rng = rand::thread_rng(); | ||
for x in v.iter_mut() { | ||
*x = rng.gen() | ||
} | ||
let ans = v.iter().minmax_simd(); | ||
|
||
// Test against itertools | ||
let correct = v.iter().minmax(); | ||
if let Some((min, max)) = ans { | ||
match correct { | ||
itertools::MinMaxResult::MinMax(x, b) => { | ||
assert_eq!((min, max), (*x, *b)); | ||
} | ||
itertools::MinMaxResult::OneElement(x) => { | ||
assert_eq!(*x, min); | ||
assert_eq!(*x, max); | ||
} | ||
itertools::MinMaxResult::NoElements => { | ||
assert_eq!(None, ans); | ||
} | ||
} | ||
} | ||
// Test against manual stdlib | ||
let min = v.iter().min(); | ||
let max = v.iter().max(); | ||
match (min, max) { | ||
(Some(min), Some(max)) => { | ||
let pos_min = v.iter().position(|x| x == min); | ||
let pos_max = v.iter().position(|x| x == max); | ||
match (pos_min, pos_max) { | ||
(Some(p_min), Some(p_max)) => { | ||
assert_eq!(Some((p_min, p_max)), v.iter().minmax_position_simd()); | ||
} | ||
_ => panic!("Min and max some but could not be found in arr?"), | ||
} | ||
} | ||
(Some(_min), None) => { | ||
panic!("min some but max none?"); | ||
} | ||
(None, Some(_max)) => { | ||
panic!("min some but max none?"); | ||
} | ||
(None, None) => { | ||
assert_eq!(None, ans); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_simd_min() { | ||
test_simd_for_type::<i8>(); | ||
test_simd_for_type::<i16>(); | ||
test_simd_for_type::<i32>(); | ||
test_simd_for_type::<i64>(); | ||
test_simd_for_type::<u8>(); | ||
test_simd_for_type::<u16>(); | ||
test_simd_for_type::<u32>(); | ||
test_simd_for_type::<u64>(); | ||
test_simd_for_type::<usize>(); | ||
test_simd_for_type::<isize>(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Ord
bound here prevents floats from being used (also inMaxSimd
andMinSimd
).I'm not sure if it makes sense to mirror the traits with
Float
variants (for example,MinMaxFloatSimd
), use declarative macros to implement functions, or something else.Using declarative macros for the separate implementations like
impl_minmax_integers!(u8, i8, ...)
andimpl_minmax_floats!(f32, f64)
would allow for bringing in the appropriate trait forsimd::num::SimdFloat
in the latter impl while keeping the simplicity of the current implementation for integers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry floats are coming! I just started with the non floats. Not sure how I'll go about it quite yet. Personally I'm not a huge rust macro fan but will see!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, and take your time.
I don't blame you, I'm not either 🙂
My own experience with
portable_simd
has been more concrete uses and not generic interfaces. This ended up being a more delicate design situation than I gave any thought to initially.(Feel free to mark this as resolved, since I don't seem to be able to.)