Skip to content

Commit

Permalink
Updates to benchmarks without planners
Browse files Browse the repository at this point in the history
  • Loading branch information
smu160 committed Jul 2, 2024
1 parent b882363 commit 8c24e08
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
10 changes: 7 additions & 3 deletions examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ use std::str::FromStr;

use utilities::gen_random_signal;

use phastft::fft_64;
use phastft::planner::Direction;
use phastft::fft_64_with_opts_and_plan;
use phastft::options::Options;
use phastft::planner::{Direction, Planner64};

fn benchmark_fft_64(n: usize) {
let big_n = 1 << n;
let mut reals = vec![0.0; big_n];
let mut imags = vec![0.0; big_n];
gen_random_signal(&mut reals, &mut imags);

let planner = Planner64::new(reals.len(), Direction::Forward);
let opts = Options::guess_options(reals.len());

let now = std::time::Instant::now();
fft_64(&mut reals, &mut imags, Direction::Forward);
fft_64_with_opts_and_plan(&mut reals, &mut imags, &opts, &planner);
let elapsed = now.elapsed().as_micros();
println!("{elapsed}");
}
Expand Down
27 changes: 18 additions & 9 deletions examples/profile.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use std::env;
use std::str::FromStr;

use phastft::fft_64;
use phastft::planner::Direction;

fn benchmark_fft(num_qubits: usize) {
let n = 1 << num_qubits;
let mut reals: Vec<f64> = (1..=n).map(|i| i as f64).collect();
let mut imags: Vec<f64> = (1..=n).map(|i| i as f64).collect();
fft_64(&mut reals, &mut imags, Direction::Forward);
use utilities::gen_random_signal;

use phastft::fft_64_with_opts_and_plan;
use phastft::options::Options;
use phastft::planner::{Direction, Planner64};

fn benchmark_fft_64(n: usize) {
let big_n = 1 << n;
let mut reals = vec![0.0; big_n];
let mut imags = vec![0.0; big_n];
gen_random_signal(&mut reals, &mut imags);

let planner = Planner64::new(reals.len(), Direction::Forward);
let opts = Options::guess_options(reals.len());

fft_64_with_opts_and_plan(&mut reals, &mut imags, &opts, &planner);
}

fn main() {
let args: Vec<String> = env::args().collect();
assert_eq!(args.len(), 2, "Usage {} <n>", args[0]);

let n = usize::from_str(&args[1]).unwrap();
benchmark_fft(n);

benchmark_fft_64(n);
}
7 changes: 4 additions & 3 deletions examples/rustfft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use utilities::{
fn benchmark_rustfft(n: usize) {
let big_n = 1 << n;

let mut reals = vec![0.0; big_n];
let mut imags = vec![0.0; big_n];
let mut reals = vec![0.0f64; big_n];
let mut imags = vec![0.0f64; big_n];

gen_random_signal(&mut reals, &mut imags);
let mut signal = vec![Complex64::default(); big_n];
Expand All @@ -23,9 +23,10 @@ fn benchmark_rustfft(n: usize) {
z.im = im;
});

let now = std::time::Instant::now();
let mut planner = FftPlanner::new();
let fft = planner.plan_fft_forward(signal.len());

let now = std::time::Instant::now();
fft.process(&mut signal);
let elapsed = now.elapsed().as_micros();
println!("{elapsed}");
Expand Down
32 changes: 32 additions & 0 deletions src/kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ use num_traits::Float;

macro_rules! fft_butterfly_n_simd {
($func_name:ident, $precision:ty, $lanes:literal, $simd_vector:ty) => {
#[multiversion::multiversion(targets("x86_64+avx512f+avx512bw+avx512cd+avx512dq+avx512vl", // x86_64-v4
"x86_64+avx2+fma", // x86_64-v3
"x86_64+sse4.2", // x86_64-v2
"x86+avx512f+avx512bw+avx512cd+avx512dq+avx512vl",
"x86+avx2+fma",
"x86+sse4.2",
"x86+sse2",
))]
#[inline]
pub fn $func_name(
reals: &mut [$precision],
Expand Down Expand Up @@ -52,6 +60,14 @@ macro_rules! fft_butterfly_n_simd {
fft_butterfly_n_simd!(fft_64_chunk_n_simd, f64, 8, f64x8);
fft_butterfly_n_simd!(fft_32_chunk_n_simd, f32, 16, f32x16);

#[multiversion::multiversion(targets("x86_64+avx512f+avx512bw+avx512cd+avx512dq+avx512vl", // x86_64-v4
"x86_64+avx2+fma", // x86_64-v3
"x86_64+sse4.2", // x86_64-v2
"x86+avx512f+avx512bw+avx512cd+avx512dq+avx512vl",
"x86+avx2+fma",
"x86+sse4.2",
"x86+sse2",
))]
#[inline]
pub(crate) fn fft_chunk_n<T: Float>(
reals: &mut [T],
Expand Down Expand Up @@ -93,6 +109,14 @@ pub(crate) fn fft_chunk_n<T: Float>(
}

/// `chunk_size == 4`, so hard code twiddle factors
#[multiversion::multiversion(targets("x86_64+avx512f+avx512bw+avx512cd+avx512dq+avx512vl", // x86_64-v4
"x86_64+avx2+fma", // x86_64-v3
"x86_64+sse4.2", // x86_64-v2
"x86+avx512f+avx512bw+avx512cd+avx512dq+avx512vl",
"x86+avx2+fma",
"x86+sse4.2",
"x86+sse2",
))]
#[inline]
pub(crate) fn fft_chunk_4<T: Float>(reals: &mut [T], imags: &mut [T]) {
let dist = 2;
Expand Down Expand Up @@ -128,6 +152,14 @@ pub(crate) fn fft_chunk_4<T: Float>(reals: &mut [T], imags: &mut [T]) {
}

/// `chunk_size == 2`, so skip phase
#[multiversion::multiversion(targets("x86_64+avx512f+avx512bw+avx512cd+avx512dq+avx512vl", // x86_64-v4
"x86_64+avx2+fma", // x86_64-v3
"x86_64+sse4.2", // x86_64-v2
"x86+avx512f+avx512bw+avx512cd+avx512dq+avx512vl",
"x86+avx2+fma",
"x86+sse4.2",
"x86+sse2",
))]
#[inline]
pub(crate) fn fft_chunk_2<T: Float>(reals: &mut [T], imags: &mut [T]) {
reals
Expand Down
9 changes: 9 additions & 0 deletions src/twiddles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ macro_rules! generate_twiddles_simd {
generate_twiddles_simd!(generate_twiddles_simd_64, f64, 8, f64x8);
generate_twiddles_simd!(generate_twiddles_simd_32, f32, 8, f32x8);

#[multiversion::multiversion(
targets("x86_64+avx512f+avx512bw+avx512cd+avx512dq+avx512vl", // x86_64-v4
"x86_64+avx2+fma", // x86_64-v3
"x86_64+sse4.2", // x86_64-v2
"x86+avx512f+avx512bw+avx512cd+avx512dq+avx512vl",
"x86+avx2+fma",
"x86+sse4.2",
"x86+sse2",
))]
#[inline]
pub(crate) fn filter_twiddles<T: Float>(twiddles_re: &[T], twiddles_im: &[T]) -> (Vec<T>, Vec<T>) {
assert_eq!(twiddles_re.len(), twiddles_im.len());
Expand Down

0 comments on commit 8c24e08

Please sign in to comment.