Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Improved performance of sum aggregation via aligned loads (-10%) #445

Merged
merged 5 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 13 additions & 12 deletions src/compute/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ pub trait Sum<T> {
#[clone(target = "x86_64+avx")]
fn nonnull_sum<T>(values: &[T]) -> T
where
T: NativeType + Simd,
T::Simd: Add<Output = T::Simd> + Sum<T>,
T: NativeType + Simd + Add<Output = T> + std::iter::Sum<T>,
T::Simd: Sum<T> + Add<Output = T::Simd>,
{
let mut chunks = values.chunks_exact(T::Simd::LANES);

let sum = chunks.by_ref().fold(T::Simd::default(), |acc, chunk| {
acc + T::Simd::from_chunk(chunk)
});

let remainder = T::Simd::from_incomplete_chunk(chunks.remainder(), T::default());
let reduced = sum + remainder;
// Safety:
// T::Simd is the vector type T and the alignment is similar to aligning to [T; alignment]
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
// the alignment of T::Simd ensures that it fits T.
let (head, simd_vals, tail) = unsafe { values.align_to::<T::Simd>() };

let mut reduced = T::Simd::from_incomplete_chunk(&[], T::default());
for chunk in simd_vals {
reduced = reduced + chunk.clone()
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
}

reduced.simd_sum()
reduced.simd_sum() + head.iter().copied().sum() + tail.iter().copied().sum()
}

/// # Panics
Expand Down Expand Up @@ -90,7 +91,7 @@ where
/// Returns `None` if the array is empty or only contains null values.
pub fn sum_primitive<T>(array: &PrimitiveArray<T>) -> Option<T>
where
T: NativeType + Simd,
T: NativeType + Simd + Add<Output = T> + std::iter::Sum<T>,
T::Simd: Add<Output = T::Simd> + Sum<T>,
{
let null_count = array.null_count();
Expand Down
2 changes: 2 additions & 0 deletions src/types/simd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub trait NativeSimd: Default {
/// Items from `v` at positions larger than the number of lanes are ignored;
/// remaining items are populated with `remaining`.
fn from_incomplete_chunk(v: &[Self::Native], remaining: Self::Native) -> Self;

fn clone(&self) -> Self;
}

/// Trait implemented by some [`NativeType`] that have a SIMD representation.
Expand Down
4 changes: 4 additions & 0 deletions src/types/simd/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ macro_rules! simd {
a.iter_mut().zip(v.iter()).for_each(|(a, b)| *a = *b);
Self(a)
}
#[inline]
fn clone(&self) -> Self {
($name)(self.0).into()
}
}

impl std::ops::Index<usize> for $name {
Expand Down
5 changes: 5 additions & 0 deletions src/types/simd/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ macro_rules! simd {
a.iter_mut().zip(v.iter()).for_each(|(a, b)| *a = *b);
<$name>::from_chunk(a.as_ref())
}

#[inline]
fn clone(&self) -> Self {
*self
}
}
};
}
Expand Down