|
| 1 | +//! Exhaustive tests for `f16` and `f32`, high-iteration for `f64` and `f128`. |
| 2 | +
|
| 3 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use indicatif::{ProgressBar, ProgressStyle}; |
| 7 | +use libm_test::gen::extensive::{self, ExtensiveInput}; |
| 8 | +use libm_test::mpfloat::MpOp; |
| 9 | +use libm_test::{ |
| 10 | + CheckBasis, CheckCtx, CheckOutput, EXTENSIVE_ENV, GeneratorKind, MathOp, TestAction, |
| 11 | + TestResult, TupleCall, get_iterations, |
| 12 | +}; |
| 13 | +use libtest_mimic::{Arguments, Completion, Trial}; |
| 14 | +use rayon::prelude::*; |
| 15 | + |
| 16 | +/// Template for `indicatif` progress bars. |
| 17 | +const PB_TEMPLATE: &str = "[{elapsed:3} {percent:3}%] {bar:20.cyan/blue} NAME \ |
| 18 | + {human_pos:>13}/{human_len:13} {per_sec:18} eta {eta:8} {msg}"; |
| 19 | +const PB_TEMPLATE_FINAL: &str = "[{elapsed:3} {percent:3}%] {bar:20.cyan/blue} NAME \ |
| 20 | + {human_pos:>13}/{human_len:13} {per_sec:18} done in {elapsed_precise}"; |
| 21 | + |
| 22 | +pub fn run() { |
| 23 | + let mut args = Arguments::from_args(); |
| 24 | + // Prevent multiple tests from running in parallel, each test gets parallized internally. |
| 25 | + args.test_threads = Some(1); |
| 26 | + let tests = register_tests(); |
| 27 | + |
| 28 | + // With default parallelism, the CPU doesn't saturate. We don't need to be nice to |
| 29 | + // other processes, so do 1.5x to make sure we use all available resources. |
| 30 | + let threads = std::thread::available_parallelism().map(Into::into).unwrap_or(0) * 3 / 2; |
| 31 | + rayon::ThreadPoolBuilder::new().num_threads(threads).build_global().unwrap(); |
| 32 | + |
| 33 | + libtest_mimic::run(&args, tests).exit(); |
| 34 | +} |
| 35 | + |
| 36 | +macro_rules! mp_extensive_tests { |
| 37 | + ( |
| 38 | + fn_name: $fn_name:ident, |
| 39 | + extra: [$push_to:ident], |
| 40 | + ) => { |
| 41 | + register_one::<libm_test::op::$fn_name::Routine>(&mut $push_to, stringify!($fn_name)); |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +fn register_tests() -> Vec<Trial> { |
| 46 | + let mut all_tests = Vec::new(); |
| 47 | + libm_macros::for_each_function! { |
| 48 | + callback: mp_extensive_tests, |
| 49 | + extra: [all_tests], |
| 50 | + skip: [ |
| 51 | + // TODO |
| 52 | + jn, |
| 53 | + jnf, |
| 54 | + |
| 55 | + // FIXME: MPFR tests needed |
| 56 | + frexp, |
| 57 | + frexpf, |
| 58 | + ilogb, |
| 59 | + ilogbf, |
| 60 | + ldexp, |
| 61 | + ldexpf, |
| 62 | + modf, |
| 63 | + modff, |
| 64 | + remquo, |
| 65 | + remquof, |
| 66 | + scalbn, |
| 67 | + scalbnf, |
| 68 | + |
| 69 | + // FIXME: test needed, see |
| 70 | + // https://github.com/rust-lang/libm/pull/311#discussion_r1818273392 |
| 71 | + nextafter, |
| 72 | + nextafterf, |
| 73 | + ], |
| 74 | + } |
| 75 | + all_tests |
| 76 | +} |
| 77 | + |
| 78 | +/// Add a single test to the list. |
| 79 | +fn register_one<Op>(all: &mut Vec<Trial>, name: &'static str) |
| 80 | +where |
| 81 | + Op: MathOp + MpOp, |
| 82 | + Op::RustArgs: ExtensiveInput<Op> + Send, |
| 83 | +{ |
| 84 | + let test_name = format!("mp_extensive_{name}"); |
| 85 | + all.push(Trial::skippable_test(test_name, move || { |
| 86 | + let ctx = CheckCtx::new(Op::IDENTIFIER, CheckBasis::Mpfr); |
| 87 | + let action = get_iterations(&ctx, GeneratorKind::Extensive, 0); |
| 88 | + match action { |
| 89 | + TestAction::Run => (), |
| 90 | + TestAction::Iterations(_) => panic!("extensive tests disregard iteration counts"), |
| 91 | + TestAction::Skip => { |
| 92 | + return Ok(Completion::Ignored { |
| 93 | + reason: format!("extensive tests are only run if specified in {EXTENSIVE_ENV}"), |
| 94 | + }); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + if !cfg!(optimizations_enabled) { |
| 99 | + panic!("extensivetests should be run with --release"); |
| 100 | + } |
| 101 | + |
| 102 | + test_one::<Op>(name).map(|()| Completion::Completed).map_err(Into::into) |
| 103 | + })); |
| 104 | +} |
| 105 | + |
| 106 | +/// Test runner for a signle routine. |
| 107 | +fn test_one<Op>(name: &str) -> TestResult |
| 108 | +where |
| 109 | + Op: MathOp + MpOp, |
| 110 | + Op::RustArgs: ExtensiveInput<Op> + Send, |
| 111 | +{ |
| 112 | + // How may iterations have been completed. |
| 113 | + static COMPLETED: AtomicU64 = AtomicU64::new(0); |
| 114 | + let ctx = CheckCtx::new(Op::IDENTIFIER, CheckBasis::Mpfr); |
| 115 | + |
| 116 | + let expected_checks = Op::RustArgs::count(); |
| 117 | + let name_padded = format!("{name:9}"); |
| 118 | + let pb_style = ProgressStyle::with_template(&PB_TEMPLATE.replace("NAME", &name_padded)) |
| 119 | + .unwrap() |
| 120 | + .progress_chars("##-"); |
| 121 | + |
| 122 | + // Just delay a bit before printing anything so other output (ignored tests list) has a |
| 123 | + // chance to flush. |
| 124 | + std::thread::sleep(Duration::from_millis(500)); |
| 125 | + |
| 126 | + eprintln!("starting extensive tests for `{name}`"); |
| 127 | + let pb = ProgressBar::new(expected_checks); |
| 128 | + COMPLETED.store(0, Ordering::Relaxed); |
| 129 | + pb.set_style(pb_style); |
| 130 | + |
| 131 | + let run_single_input = |mp_vals: &mut Op::MpTy, input: Op::RustArgs| -> TestResult { |
| 132 | + let mp_res = Op::run(mp_vals, input); |
| 133 | + let crate_res = input.call(Op::ROUTINE); |
| 134 | + |
| 135 | + crate_res.validate(mp_res, input, &ctx)?; |
| 136 | + let completed = COMPLETED.fetch_add(1, Ordering::Relaxed) + 1; |
| 137 | + |
| 138 | + // Infrequently update the progress bar. |
| 139 | + if completed % 20_000 == 0 { |
| 140 | + pb.set_position(completed); |
| 141 | + } |
| 142 | + if completed % 500_000 == 0 { |
| 143 | + pb.set_message(format!("input: {input:?}")); |
| 144 | + } |
| 145 | + Ok(()) |
| 146 | + }; |
| 147 | + |
| 148 | + let cases = &mut extensive::get_test_cases::<Op>(&ctx); |
| 149 | + |
| 150 | + // Chunk the cases so Rayon doesn't switch threads between each iterator item. 50k seems to be |
| 151 | + // a performance sweet spot. |
| 152 | + // |
| 153 | + // This allocates then discards a vector for each chunk. These should be reusable |
| 154 | + let chunk_size = 50_000; |
| 155 | + let chunks = std::iter::from_fn(move || { |
| 156 | + let v = Vec::from_iter(cases.take(chunk_size)); |
| 157 | + (!v.is_empty()).then_some(v) |
| 158 | + }); |
| 159 | + |
| 160 | + let res = chunks.par_bridge().try_for_each_init( |
| 161 | + || Op::new_mp(), |
| 162 | + |mp_vals, input_vec| -> TestResult { |
| 163 | + for x in input_vec { |
| 164 | + run_single_input(mp_vals, x)?; |
| 165 | + } |
| 166 | + Ok(()) |
| 167 | + }, |
| 168 | + ); |
| 169 | + |
| 170 | + let total_run = COMPLETED.load(Ordering::Relaxed); |
| 171 | + |
| 172 | + let pb_style = ProgressStyle::with_template(&PB_TEMPLATE_FINAL.replace("NAME", &name_padded)) |
| 173 | + .unwrap() |
| 174 | + .progress_chars("##-"); |
| 175 | + pb.set_style(pb_style); |
| 176 | + pb.set_position(total_run); |
| 177 | + pb.abandon(); |
| 178 | + |
| 179 | + if total_run != expected_checks { |
| 180 | + // Provide a warning if our estimate needs to be updated. |
| 181 | + eprintln!("WARNING: total run {total_run} does not match expected {expected_checks}"); |
| 182 | + } |
| 183 | + |
| 184 | + eprintln!(); |
| 185 | + res |
| 186 | +} |
0 commit comments