Skip to content

Commit

Permalink
Update pre-commit hook to run clippy everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
smu160 committed Jul 17, 2024
1 parent e94f2fc commit 713b3a6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 70 deletions.
26 changes: 5 additions & 21 deletions benches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,26 @@

### Setup Environment

1. Install [FFTW3](http://www.fftw.org/download.html)[^1]
1. Clone the `PhastFT` git repository [^2].

It may be possible to install `fftw3` using a package manager.

##### debian
```bash
sudo apt install libfftw3-dev
```

2. Clone the `PhastFT` git repository [^2].

3. Create virtual env
2. Create virtual env

```bash
cd ~/PhastFT/benches && python3 -m venv .env && source .env/bin/activate
```

4. Install python dependencies[^1]
3. Install python dependencies[^1]

```bash
pip install -r requirements.txt
cd ~/PhastFT/pyphastft
pip install .
```

5. Run the `FFTW3` vs. `RustFFT` vs. `PhastFT` benchmark for all inputs of size `2^n`, where `n \in [4, 30].`
5. Run the `FFTW3-RB` vs. `RustFFT` vs. `PhastFT` benchmarks`

```bash
./benchmark.sh 4 29
python run_benches.py
```

6. Plot the results
Expand Down Expand Up @@ -101,13 +92,6 @@ On linux, open access to performance monitoring, and observability operations fo
echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid
```

Add debug to `Cargo.toml` under `profile.release`:

```bash
[profile.release]
debug = true
```

Finally, run:

```bash
Expand Down
32 changes: 19 additions & 13 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn benchmark_forward_f32(c: &mut Criterion) {
let planner = Planner32::new(len, Direction::Forward);
let (mut reals, mut imags) = generate_numbers(len);

group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &len| {
group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &_len| {
b.iter(|| {
fft_32_with_opts_and_plan(
black_box(&mut reals),
Expand All @@ -85,7 +85,7 @@ fn benchmark_forward_f32(c: &mut Criterion) {
let fft = planner.plan_fft_forward(len);
let mut signal = generate_complex_numbers(len);

group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &len| {
group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &_len| {
b.iter(|| fft.process(black_box(&mut signal)));
});
}
Expand All @@ -103,9 +103,12 @@ fn benchmark_inverse_f32(c: &mut Criterion) {
c.bench_function(&id, |b| {
let (mut reals, mut imags) = generate_numbers(len);
b.iter(|| {
black_box(fft_32_with_opts_and_plan(
&mut reals, &mut imags, &options, &planner,
));
fft_32_with_opts_and_plan(
black_box(&mut reals),
black_box(&mut imags),
black_box(&options),
black_box(&planner),
);
});
});
}
Expand All @@ -122,7 +125,7 @@ fn benchmark_forward_f64(c: &mut Criterion) {
let (mut reals, mut imags) = generate_numbers(len);
group.throughput(Throughput::Elements(len as u64));

group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &len| {
group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &_len| {
b.iter(|| {
fft_64_with_opts_and_plan(
black_box(&mut reals),
Expand All @@ -138,7 +141,7 @@ fn benchmark_forward_f64(c: &mut Criterion) {
let fft = planner.plan_fft_forward(len);
let mut signal = generate_complex_numbers(len);

group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &len| {
group.bench_with_input(BenchmarkId::new(id, len), &len, |b, &_len| {
b.iter(|| fft.process(black_box(&mut signal)));
});
}
Expand All @@ -156,19 +159,22 @@ fn benchmark_inverse_f64(c: &mut Criterion) {
c.bench_function(&id, |b| {
let (mut reals, mut imags) = generate_numbers(len);
b.iter(|| {
black_box(fft_64_with_opts_and_plan(
&mut reals, &mut imags, &options, &planner,
));
fft_64_with_opts_and_plan(
black_box(&mut reals),
black_box(&mut imags),
black_box(&options),
black_box(&planner),
);
});
});
}
}

criterion_group!(
benches,
// benchmark_forward_f32,
// benchmark_inverse_f32,
benchmark_forward_f32,
benchmark_inverse_f32,
benchmark_forward_f64,
// benchmark_inverse_f64
benchmark_inverse_f64
);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ then
exit 1
fi

if ! cargo clippy -- -D warnings
if ! cargo clippy --all-targets --all-features --tests -- -D warnings
then
echo "There are some clippy issues."
exit 1
fi

if ! cargo test
if ! cargo test --all-features
then
echo "There are some test issues."
exit 1
Expand Down
38 changes: 7 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
#![feature(portable_simd, avx512_target_feature)]

#[cfg(feature = "complex-nums")]
use std::simd::{f32x16, f64x8};

#[cfg(feature = "complex-nums")]
use bytemuck::cast_slice;
use crate::utils::{combine_re_im, deinterleave_complex32, deinterleave_complex64};
#[cfg(feature = "complex-nums")]
use num_complex::Complex;
#[cfg(feature = "complex-nums")]
use num_traits::Float;

use crate::cobra::cobra_apply;
use crate::kernels::{
Expand Down Expand Up @@ -96,9 +91,9 @@ macro_rules! impl_fft_interleaved_for {
}

#[cfg(feature = "complex-nums")]
impl_fft_interleaved_for!(fft_32_interleaved, f32, fft_32, separate_re_im_f32);
impl_fft_interleaved_for!(fft_32_interleaved, f32, fft_32, deinterleave_complex32);
#[cfg(feature = "complex-nums")]
impl_fft_interleaved_for!(fft_64_interleaved, f64, fft_64, separate_re_im_f64);
impl_fft_interleaved_for!(fft_64_interleaved, f64, fft_64, deinterleave_complex64);

macro_rules! impl_fft_with_opts_and_plan_for {
($func_name:ident, $precision:ty, $planner:ty, $simd_butterfly_kernel:ident, $lanes:literal) => {
Expand Down Expand Up @@ -240,25 +235,6 @@ mod tests {

use super::*;

#[cfg(feature = "complex-nums")]
#[test]
fn test_separate_and_combine_re_im() {
use utils::deinterleave;

let complex_vec: Vec<_> = vec![
Complex::new(1.0, 2.0),
Complex::new(3.0, 4.0),
Complex::new(5.0, 6.0),
Complex::new(7.0, 8.0),
];

let (reals, imags) = deinterleave(&complex_vec);

let recombined_vec = combine_re_im(&reals, &imags);

assert_eq!(complex_vec, recombined_vec);
}

macro_rules! non_power_of_2_planner {
($test_name:ident, $planner:ty) => {
#[should_panic]
Expand Down Expand Up @@ -436,9 +412,9 @@ mod tests {
let mut re = reals.clone();
let mut im = imags.clone();

let mut planner = Planner64::new(num_points, Direction::Forward);
let planner = Planner64::new(num_points, Direction::Forward);
let opts = Options::guess_options(reals.len());
fft_64_with_opts_and_plan(&mut reals, &mut imags, &opts, &mut planner);
fft_64_with_opts_and_plan(&mut reals, &mut imags, &opts, &planner);

fft_64(&mut re, &mut im, Direction::Forward);

Expand Down Expand Up @@ -467,9 +443,9 @@ mod tests {
let mut re = reals.clone();
let mut im = imags.clone();

let mut planner = Planner32::new(num_points, direction);
let planner = Planner32::new(num_points, direction);
let opts = Options::guess_options(reals.len());
fft_32_with_opts_and_plan(&mut reals, &mut imags, &opts, &mut planner);
fft_32_with_opts_and_plan(&mut reals, &mut imags, &opts, &planner);

fft_32(&mut re, &mut im, direction);

Expand Down
2 changes: 1 addition & 1 deletion src/twiddles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ mod tests {

let actual_w_im = -fwd_twiddles_re[(i + dist / 2) % dist];
//assert_float_closeness(actual_w_im, expected_w_im, 1e-6);
print!("actual: {actual_w_im} expected: {expected_w_im}\n");
println!("actual: {actual_w_im} expected: {expected_w_im}");
}
println!("{:?}", fwd_twiddles_re);
println!("{:?}", fwd_twiddles_im);
Expand Down
54 changes: 52 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
//! Utility functions such as interleave/deinterleave
#[cfg(feature = "complex-nums")]
use num_complex::Complex;

#[cfg(feature = "complex-nums")]
use num_traits::Float;

#[cfg(feature = "complex-nums")]
use bytemuck::cast_slice;

use std::simd::{prelude::Simd, simd_swizzle, SimdElement};

// We don't multiversion for AVX-512 here and keep the chunk size below AVX-512
Expand Down Expand Up @@ -59,6 +68,30 @@ pub(crate) fn deinterleave<T: Copy + Default + SimdElement>(input: &[T]) -> (Vec
(out_odd, out_even)
}

/// Utility function to separate a slice of [`Complex64``]
/// into a single vector of Complex Number Structs.
///
/// # Panics
///
/// Panics if `reals.len() != imags.len()`.
#[cfg(feature = "complex-nums")]
pub(crate) fn deinterleave_complex64(signal: &[Complex<f64>]) -> (Vec<f64>, Vec<f64>) {
let complex_t: &[f64] = cast_slice(signal);
deinterleave(complex_t)
}

/// Utility function to separate a slice of [`Complex32``]
/// into a single vector of Complex Number Structs.
///
/// # Panics
///
/// Panics if `reals.len() != imags.len()`.
#[cfg(feature = "complex-nums")]
pub(crate) fn deinterleave_complex32(signal: &[Complex<f32>]) -> (Vec<f32>, Vec<f32>) {
let complex_t: &[f32] = cast_slice(signal);
deinterleave(complex_t)
}

/// Utility function to combine separate vectors of real and imaginary components
/// into a single vector of Complex Number Structs.
///
Expand All @@ -78,10 +111,10 @@ pub(crate) fn combine_re_im<T: Float>(reals: &[T], imags: &[T]) -> Vec<Complex<T

#[cfg(test)]
mod tests {
use super::deinterleave;
use super::*;

fn gen_test_vec(len: usize) -> Vec<usize> {
(0..len).into_iter().collect()
(0..len).collect()
}

/// Slow but obviously correct implementation of deinterleaving,
Expand All @@ -100,4 +133,21 @@ mod tests {
assert_eq!(naive_b, opt_b);
}
}

#[cfg(feature = "complex-nums")]
#[test]
fn test_separate_and_combine_re_im() {
let complex_vec: Vec<_> = vec![
Complex::new(1.0, 2.0),
Complex::new(3.0, 4.0),
Complex::new(5.0, 6.0),
Complex::new(7.0, 8.0),
];

let (reals, imags) = deinterleave_complex64(&complex_vec);

let recombined_vec = combine_re_im(&reals, &imags);

assert_eq!(complex_vec, recombined_vec);
}
}

0 comments on commit 713b3a6

Please sign in to comment.