-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[WIP] [Rust] Add explicit SIMD vectorization for arithmetic ops in "array_ops" #3451
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
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
5be0f45
Working with packed-simd
paddyhoran ddd0415
Simd moved
paddyhoran 0cb88ea
sse and avx2 with benchmark
paddyhoran 27b1c0d
Added conditional compilation and renamed
paddyhoran 6a26c53
Fixed lints
paddyhoran e5b8780
Cleaned up implementation
paddyhoran a07d99a
Fixed lints
paddyhoran b32df1a
Generics working
paddyhoran dd71421
Simd operations added to `ArrowNumericType`
paddyhoran 108238f
Cleaned up testing.
paddyhoran 3ee5a29
Added documentation
paddyhoran 6cf1fd1
Removed add tests from array_ops.
paddyhoran 236608b
Moved test with nulls.
paddyhoran 2f8dd56
Fixed benchmarks.
paddyhoran f307436
Restored test with nulls.
paddyhoran 24fe8bb
Fixed value slice.
paddyhoran 627c5ae
Skipping test for now.
paddyhoran 6c47424
Removed temp example
paddyhoran 9925e6a
Fixed lints
paddyhoran 7f115e8
Split SIMD into separate trait.
paddyhoran bc960c5
Merge remote-tracking branch 'origin/master' into simd-merge
paddyhoran e5fad8a
Updated to be generic over binary ops
paddyhoran 36608f8
Implemented `Bitor` trait for `Buffer`
paddyhoran cb00309
Fixed linting
paddyhoran f866d57
Changed `BitOr` to `BitAnd`
paddyhoran 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 hidden or 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 hidden or 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,63 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#[macro_use] | ||
extern crate criterion; | ||
use criterion::Criterion; | ||
|
||
extern crate arrow; | ||
|
||
use arrow::array::*; | ||
use arrow::builder::*; | ||
use arrow::compute::arithmetic_kernels::*; | ||
use arrow::compute::array_ops::*; | ||
|
||
fn create_array(size: usize) -> Float32Array { | ||
let mut builder = Float32Builder::new(size); | ||
for _i in 0..size { | ||
builder.append_value(1.0).unwrap(); | ||
} | ||
builder.finish() | ||
} | ||
|
||
fn primitive_array_add(size: usize) { | ||
let arr_a = create_array(size); | ||
let arr_b = create_array(size); | ||
criterion::black_box(math_op(&arr_a, &arr_b, |a, b| Ok(a + b)).unwrap()); | ||
} | ||
|
||
fn primitive_array_add_simd(size: usize) { | ||
let arr_a = create_array(size); | ||
let arr_b = create_array(size); | ||
criterion::black_box(add(&arr_a, &arr_b).unwrap()); | ||
} | ||
|
||
fn add_benchmark(c: &mut Criterion) { | ||
c.bench_function("add 128", |b| b.iter(|| primitive_array_add(128))); | ||
c.bench_function("add 128 simd", |b| b.iter(|| primitive_array_add_simd(128))); | ||
c.bench_function("add 256", |b| b.iter(|| primitive_array_add(256))); | ||
c.bench_function("add 256 simd", |b| b.iter(|| primitive_array_add_simd(256))); | ||
c.bench_function("add 512", |b| b.iter(|| primitive_array_add(512))); | ||
c.bench_function("add 512 simd", |b| b.iter(|| primitive_array_add_simd(512))); | ||
c.bench_function("add 1024", |b| b.iter(|| primitive_array_add(1024))); | ||
c.bench_function("add 1024 simd", |b| { | ||
b.iter(|| primitive_array_add_simd(1024)) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, add_benchmark); | ||
criterion_main!(benches); |
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//! Defines basic arithmetic kernels for `PrimitiveArrays`. | ||
//! | ||
//! These kernels can leverage SIMD if available on your system. Currently no runtime | ||
//! detection is provided, you should enable the specific SIMD intrinsics using | ||
//! `RUSTFLAGS="-C target-feature=+avx2"` for example. See the | ||
//! [here] (https://doc.rust-lang.org/stable/std/arch/) for more information. | ||
|
||
use std::mem; | ||
use std::ops::{Add, Div, Mul, Sub}; | ||
use std::slice::from_raw_parts_mut; | ||
|
||
use num::Zero; | ||
|
||
use crate::array::*; | ||
use crate::buffer::MutableBuffer; | ||
use crate::compute::array_ops::math_op; | ||
use crate::datatypes; | ||
use crate::error::{ArrowError, Result}; | ||
|
||
/// Vectorized version of add operation | ||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
fn simd_bin_op<T, F>( | ||
left: &PrimitiveArray<T>, | ||
right: &PrimitiveArray<T>, | ||
op: F, | ||
) -> Result<PrimitiveArray<T>> | ||
where | ||
T: datatypes::ArrowNumericType + datatypes::ArrowSIMDType, | ||
T::Simd: Add<Output = T::Simd> | ||
+ Sub<Output = T::Simd> | ||
+ Mul<Output = T::Simd> | ||
+ Div<Output = T::Simd>, | ||
F: Fn(T::Simd, T::Simd) -> T::Simd, | ||
{ | ||
if left.len() != right.len() { | ||
return Err(ArrowError::ComputeError( | ||
"Cannot perform math operation on arrays of different length".to_string(), | ||
)); | ||
} | ||
|
||
let lanes = T::lanes(); | ||
let buffer_size = left.len() * mem::size_of::<T::Native>(); | ||
let mut result = MutableBuffer::new(buffer_size).with_bitset(buffer_size, false); | ||
|
||
for i in (0..left.len()).step_by(lanes) { | ||
let simd_left = T::load(left.value_slice(i, lanes)); | ||
let simd_right = T::load(right.value_slice(i, lanes)); | ||
let simd_result = T::bin_op(simd_left, simd_right, &op); | ||
|
||
let result_slice: &mut [T::Native] = unsafe { | ||
from_raw_parts_mut( | ||
(result.data_mut().as_mut_ptr() as *mut T::Native).offset(i as isize), | ||
lanes, | ||
) | ||
}; | ||
T::write(simd_result, result_slice); | ||
} | ||
|
||
Ok(PrimitiveArray::<T>::new(left.len(), result.freeze(), 0, 0)) | ||
} | ||
|
||
/// Perform `left + right` operation on two arrays. If either left or right value is null | ||
/// then the result is also null. | ||
pub fn add<T>( | ||
left: &PrimitiveArray<T>, | ||
right: &PrimitiveArray<T>, | ||
) -> Result<PrimitiveArray<T>> | ||
where | ||
T: datatypes::ArrowNumericType + datatypes::ArrowSIMDType, | ||
T::Native: Add<Output = T::Native> | ||
+ Sub<Output = T::Native> | ||
+ Mul<Output = T::Native> | ||
+ Div<Output = T::Native> | ||
+ Zero, | ||
{ | ||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
return simd_bin_op(&left, &right, |a, b| a + b); | ||
|
||
#[allow(unreachable_code)] | ||
math_op(left, right, |a, b| Ok(a + b)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::array::Int32Array; | ||
|
||
#[test] | ||
fn test_primitive_array_add() { | ||
let a = Int32Array::from(vec![5, 6, 7, 8, 9]); | ||
let b = Int32Array::from(vec![6, 7, 8, 9, 8]); | ||
|
||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
{ | ||
let c = simd_bin_op(&a, &b, |x, y| x + y).unwrap(); | ||
|
||
assert_eq!(11, c.value(0)); | ||
assert_eq!(13, c.value(1)); | ||
assert_eq!(15, c.value(2)); | ||
assert_eq!(17, c.value(3)); | ||
assert_eq!(17, c.value(4)); | ||
} | ||
|
||
let d = add(&a, &b).unwrap(); | ||
assert_eq!(11, d.value(0)); | ||
assert_eq!(13, d.value(1)); | ||
assert_eq!(15, d.value(2)); | ||
assert_eq!(17, d.value(3)); | ||
assert_eq!(17, d.value(4)); | ||
} | ||
|
||
#[test] | ||
fn test_primitive_array_add_mismatched_length() { | ||
let a = Int32Array::from(vec![5, 6, 7, 8, 9]); | ||
let b = Int32Array::from(vec![6, 7, 8]); | ||
let e = add(&a, &b) | ||
.err() | ||
.expect("should have failed due to different lengths"); | ||
assert_eq!( | ||
"ComputeError(\"Cannot perform math operation on arrays of different length\")", | ||
format!("{:?}", e) | ||
); | ||
} | ||
|
||
#[ignore] | ||
#[test] | ||
fn test_primitive_array_add_with_nulls() { | ||
let a = Int32Array::from(vec![Some(5), None, Some(7), None]); | ||
let b = Int32Array::from(vec![None, None, Some(6), Some(7)]); | ||
let c = add(&a, &b).unwrap(); | ||
assert_eq!(true, c.is_null(0)); | ||
assert_eq!(true, c.is_null(1)); | ||
assert_eq!(false, c.is_null(2)); | ||
assert_eq!(true, c.is_null(3)); | ||
assert_eq!(13, c.value(2)); | ||
} | ||
} |
Oops, something went wrong.
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.
Can we make this runtime detection? e.g.:
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.
See below.