Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod find;
mod is_sorted;
mod max;
mod min;
mod minmax;
mod position;

pub use all_equal::AllEqualSimd;
Expand All @@ -22,4 +23,5 @@ pub use find::FindSimd;
pub use is_sorted::IsSortedSimd;
pub use max::MaxSimd;
pub use min::MinSimd;
pub use minmax::MinMaxSimd;
pub use position::PositionSimd;
154 changes: 154 additions & 0 deletions src/minmax.rs
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,
Copy link

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 in MaxSimd and MinSimd).

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, ...) and impl_minmax_floats!(f32, f64) would allow for bringing in the appropriate trait for simd::num::SimdFloat in the latter impl while keeping the simplicity of the current implementation for integers.

Copy link
Owner Author

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!

Copy link

@okaneco okaneco Jul 19, 2024

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.

Personally I'm not a huge rust macro fan but will see!

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.)

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>();
}
}