|
| 1 | +use criterion::Criterion; |
| 2 | +use lambdaworks_gpu::fft::fft_metal::*; |
| 3 | +use lambdaworks_math::{ |
| 4 | + fft::bit_reversing::in_place_bit_reverse_permute, |
| 5 | + field::{element::FieldElement, traits::IsTwoAdicField}, |
| 6 | + field::{test_fields::u32_test_field::U32TestField, traits::RootsConfig}, |
| 7 | +}; |
| 8 | +use rand::random; |
| 9 | + |
| 10 | +type F = U32TestField; |
| 11 | +type FE = FieldElement<F>; |
| 12 | + |
| 13 | +fn gen_coeffs(pow: usize) -> Vec<FE> { |
| 14 | + let mut result = Vec::with_capacity(1 << pow); |
| 15 | + for _ in 0..result.capacity() { |
| 16 | + result.push(FE::new(random())); |
| 17 | + } |
| 18 | + result |
| 19 | +} |
| 20 | + |
| 21 | +pub fn metal_fft_benchmarks(c: &mut Criterion) { |
| 22 | + let mut group = c.benchmark_group("metal_fft"); |
| 23 | + |
| 24 | + for order in 20..=24 { |
| 25 | + let coeffs = gen_coeffs(order); |
| 26 | + group.throughput(criterion::Throughput::Elements(1 << order)); // info for criterion |
| 27 | + |
| 28 | + // the objective is to bench ordered FFT, including twiddles generation and Metal setup |
| 29 | + group.bench_with_input( |
| 30 | + format!("parallel_nr_2radix_2^{order}_coeffs"), |
| 31 | + &coeffs, |
| 32 | + |bench, coeffs| { |
| 33 | + bench.iter(|| { |
| 34 | + // TODO: autoreleaspool hurts perf. by 2-3%. Search for an alternative |
| 35 | + objc::rc::autoreleasepool(|| { |
| 36 | + let coeffs = coeffs.clone(); |
| 37 | + let twiddles = |
| 38 | + F::get_twiddles(order as u64, RootsConfig::BitReverse).unwrap(); |
| 39 | + let fft_metal = FFTMetalState::new(None).unwrap(); |
| 40 | + let command_buff_encoder = fft_metal |
| 41 | + .setup_fft("radix2_dit_butterfly", &twiddles) |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + let mut result = fft_metal |
| 45 | + .execute_fft(&coeffs, command_buff_encoder) |
| 46 | + .unwrap(); |
| 47 | + |
| 48 | + in_place_bit_reverse_permute(&mut result); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + group.finish(); |
| 56 | +} |
| 57 | + |
| 58 | +pub fn metal_fft_twiddles_benchmarks(c: &mut Criterion) { |
| 59 | + let mut group = c.benchmark_group("metal_fft"); |
| 60 | + group.sample_size(10); // it becomes too slow with the default of 100 |
| 61 | + |
| 62 | + for order in 2..=4 { |
| 63 | + group.throughput(criterion::Throughput::Elements(1 << order)); // info for criterion |
| 64 | + |
| 65 | + // the objective is to bench ordered FFT, including twiddles generation and Metal setup |
| 66 | + group.bench_with_input( |
| 67 | + format!("parallel_twiddle_factors_2^({order}-1)_elems"), |
| 68 | + &order, |
| 69 | + |bench, order| { |
| 70 | + bench.iter(|| { |
| 71 | + // TODO: autoreleaspool hurts perf. by 2-3%. Search for an alternative |
| 72 | + objc::rc::autoreleasepool(|| { |
| 73 | + let metal_state = FFTMetalState::new(None).unwrap(); |
| 74 | + let _gpu_twiddles = metal_state.gen_twiddles::<F>(*order).unwrap(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + group.finish(); |
| 82 | +} |
0 commit comments