yum/vfft
A (very) fast fourier transform.
git clone https://git.yummers.dev/yum/vfft
f99f221
master
1use criterion :: { BatchSize , Criterion , black_box , criterion_group , criterion_main } ; 2use num :: complex :: Complex ; 3use num :: traits :: Float ; 4use vfft :: cpu :: { ExecutionPlan , fft } ; 5 6// Cast one float type to another, truncating if needed. 7fn t0_to_t1 < T0 : Float , T1 : Float > ( val : T0 ) -> T1 { 8return num :: cast ( val ) . unwrap () ; 9} 10 11// Casts one Complex<T> to another. 12fn complex_to_t < T0 : Float , T1 : Float > ( data : & [ Complex < T0 > ]) -> Vec < Complex < T1 >> { 13data . iter () 14. map ( | c | Complex :: new ( t0_to_t1 ( c . re ) , t0_to_t1 ( c . im ))) 15. collect () 16} 17 18fn get_data ( n : usize ) -> Vec < Complex < f32 >> { 19let mut data : Vec < Complex < f32 >> = Vec :: new () ; 20data . resize ( n , Complex :: ZERO ) ; 21// Randomize data. 22for i in 0 .. data . len () { 23data [ i ] = Complex :: new ( rand :: random () , rand :: random ()) ; 24} 25data 26} 27 28fn do_fft_v8_benchmark_for_n ( c : & mut Criterion , n : usize ) { 29let data = get_data ( n ) ; 30let mut plan = ExecutionPlan :: new ( & data ) . unwrap () ; 31let result_ref : Vec < rustfft :: num_complex :: Complex < f32 >> = complex_to_t ( & data ) ; 32 33c . bench_function ( format ! ( "size {}, fft v8" , n ) . as_str () , | b | { 34b . iter_batched_ref ( 35|| result_ref . clone () , 36| result_ref | { 37fft :: < f32 > ( result_ref , & mut plan ) ; 38black_box ( result_ref ) ; 39} , 40BatchSize :: PerIteration , 41) ; 42}) ; 43} 44 45fn do_ref_benchmark_for_n ( c : & mut Criterion , n : usize ) { 46let data = get_data ( n ) ; 47 48let result_ref : Vec < rustfft :: num_complex :: Complex < f32 >> = complex_to_t ( & data ) ; 49let mut ref_planner = rustfft :: FftPlanner :: < f32 > :: new () ; 50let mut scratch : Vec < rustfft :: num_complex :: Complex < f32 >> = 51vec ! [ Complex :: < f32 > :: new ( 0.0 , 0.0 ) ; n ] ; 52let ref_fft = ref_planner . plan_fft_forward ( result_ref . len ()) ; 53 54c . bench_function ( format ! ( "size {}, reference" , n ) . as_str () , | b | { 55b . iter_batched_ref ( 56|| result_ref . clone () , 57| result_ref | { 58ref_fft . process_with_scratch ( result_ref , & mut scratch ) ; 59black_box ( result_ref ) ; 60} , 61BatchSize :: PerIteration , 62) ; 63}) ; 64} 65 66fn do_bench ( c : & mut Criterion , n : usize ) { 67//do_ref_benchmark_for_n(c, n); 68do_fft_v8_benchmark_for_n ( c , n ) ; 69} 70 71fn get_composites ( primes : & [ usize ] , min_number : usize , max_number : usize ) -> Vec < usize > { 72let mut composites = Vec :: new () ; 73composites . push ( 1 ) ; 74 75for prime in primes { 76let num_before = composites . len () ; 77for i in 0 .. num_before { 78let mut composite = composites [ i ] * prime ; 79while composite <= max_number { 80composites . push ( composite ) ; 81composite *= prime ; 82} 83} 84} 85 86composites . retain ( |& x | x >= min_number ) ; 87composites . dedup () ; 88composites . sort () ; 89composites 90} 91 92pub fn criterion_benchmark ( c : & mut Criterion ) { 93//let radices = [2, 3, 5, 7]; 94let radices = [ 2 ] ; 95const MIN_SIZE : usize = 16 ; 96const MAX_SIZE : usize = 8192 ; 97let composites = get_composites ( & radices , MIN_SIZE , MAX_SIZE ) ; 98for composite in & composites { 99do_bench ( c , * composite ) ; 100} 101} 102 103criterion_group ! ( benches , criterion_benchmark ) ; 104criterion_main ! ( benches ) ;