Minor tweaks: parallel lookup & FloatOrd impl

This commit is contained in:
Cian Hughes
2023-11-13 14:02:17 +00:00
parent a9b671986e
commit 1793be1439
17 changed files with 569 additions and 206 deletions
+54
View File
@@ -1,6 +1,7 @@
#![allow(unused_imports)]
extern crate fastmath;
use rayon::prelude::*;
use fastmath::*;
use criterion::{Criterion, BenchmarkGroup, measurement::WallTime};
use criterion::{black_box, criterion_group, criterion_main};
@@ -44,21 +45,70 @@ fn cos_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[
group.bench_function("f64_fast", |b| {
b.iter(|| x_f64.iter().map(|&x| black_box(x).fast_cos()).collect::<Vec<f64>>())
});
group.bench_function("f64_fast_par", |b| {
b.iter(|| x_f64.par_iter().map(|&x| black_box(x).fast_cos()).collect::<Vec<f64>>())
});
group.bench_function("f64_lookup", |b| {
b.iter(|| x_f64.iter().map(|&x| black_box(x).lookup_cos()).collect::<Vec<f64>>())
});
group.bench_function("f64_lookup_map", |b| {
let inputs = x_f64.par_iter().map(|&x| black_box(x)).collect::<Vec<f64>>();
b.iter(|| inputs.clone().lookup_cos())
});
group.bench_function("f64_lookup_par_map", |b| {
let inputs = x_f64.par_iter().map(|&x| black_box(x)).collect::<Vec<f64>>();
b.iter(|| inputs.clone().par_lookup_cos())
});
group.bench_function("f64_builtin", |b| {
b.iter(|| x_f64.iter().map(|&x| exact::f64::cos(black_box(x))).collect::<Vec<f64>>())
});
group.bench_function("f64_builtin_par", |b| {
b.iter(|| x_f64.par_iter().map(|&x| exact::f64::cos(black_box(x))).collect::<Vec<f64>>())
});
group.bench_function("f32_fast", |b| {
b.iter(|| x_f32.iter().map(|&x| black_box(x).fast_cos()).collect::<Vec<f32>>())
});
group.bench_function("f32_fast_par", |b| {
b.iter(|| x_f32.par_iter().map(|&x| black_box(x).fast_cos()).collect::<Vec<f32>>())
});
group.bench_function("f32_lookup", |b| {
b.iter(|| x_f32.iter().map(|&x| black_box(x).lookup_cos()).collect::<Vec<f32>>())
});
group.bench_function("f32_lookup_map", |b| {
let inputs = x_f32.par_iter().map(|&x| black_box(x)).collect::<Vec<f32>>();
b.iter(|| inputs.clone().lookup_cos())
});
group.bench_function("f32_lookup_par_map", |b| {
let inputs = x_f32.par_iter().map(|&x| black_box(x)).collect::<Vec<f32>>();
b.iter(|| inputs.clone().par_lookup_cos())
});
group.bench_function("f32_builtin", |b| {
b.iter(|| x_f32.iter().map(|&x| exact::f32::cos(black_box(x))).collect::<Vec<f32>>())
});
group.bench_function("f32_builtin_par", |b| {
b.iter(|| x_f32.par_iter().map(|&x| exact::f32::cos(black_box(x))).collect::<Vec<f32>>())
});
}
fn sin_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[f32]) {
group.bench_function("f64_fast", |b| {
b.iter(|| x_f64.iter().map(|&x| black_box(x).fast_sin()).collect::<Vec<f64>>())
});
group.bench_function("f64_lookup", |b| {
b.iter(|| x_f64.iter().map(|&x| black_box(x).lookup_sin()).collect::<Vec<f64>>())
});
group.bench_function("f64_builtin", |b| {
b.iter(|| x_f64.iter().map(|&x| exact::f64::sin(black_box(x))).collect::<Vec<f64>>())
});
group.bench_function("f32_fast", |b| {
b.iter(|| x_f32.iter().map(|&x| black_box(x).fast_sin()).collect::<Vec<f32>>())
});
group.bench_function("f32_lookup", |b| {
b.iter(|| x_f32.iter().map(|&x| black_box(x).lookup_sin()).collect::<Vec<f32>>())
});
group.bench_function("f32_builtin", |b| {
b.iter(|| x_f32.iter().map(|&x| exact::f32::sin(black_box(x))).collect::<Vec<f32>>())
});
}
fn sigmoid_benchmarks(group: &mut BenchmarkGroup<WallTime>, x_f64: &[f64], x_f32: &[f32]) {
@@ -93,6 +143,10 @@ fn criterion_benchmark(c: &mut Criterion) {
cos_benchmarks(&mut group, &X_F64, &X_F32);
group.finish();
let mut group = c.benchmark_group("sin");
sin_benchmarks(&mut group, &X_F64, &X_F32);
group.finish();
let mut group = c.benchmark_group("sigmoid");
sigmoid_benchmarks(&mut group, &X_F64, &X_F32);
group.finish();
+3 -4
View File
@@ -13,11 +13,10 @@ pub mod exact {
}
include!("../src/tests/accuracy/x.rs");
#[inline]
fn dev_cos(x: f64) -> f64 {
const ONE: f64 = 1.0;
let v = ((((x + f64_consts::PI).abs()) % f64_consts::TAU) - f64_consts::PI).abs();
let qpprox = ONE - f64_consts::FRAC_2_PI * v;
qpprox + f64_consts::FRAC_PI_6 * qpprox * (ONE - qpprox * qpprox)
let qpprox = 1.0 - f64_consts::FRAC_2_PI * (((x + f64_consts::PI) % f64_consts::TAU) - f64_consts::PI).abs();
(qpprox * (1.0 + f64_consts::FRAC_PI_6)) - (qpprox.powi(3) * f64_consts::FRAC_PI_6)
}
fn devbench(group: &mut BenchmarkGroup<WallTime>) {