#![allow(clippy::needless_range_loop)]
use std::hint::black_box;
use std::time::Duration;
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion, Throughput};
use multidouble::dd::DD;
use multidouble::dv::{DDotAcc, DV};
use multidouble::ov::{ODotAcc, OV};
use multidouble::pack::{self, Packed};
use multidouble::simd::{self, Kernel, Simd};
use multidouble::v8::V8;
fn values<const N: usize>(n: usize, seed: u64) -> Vec<[f64; N]> {
let mut s = seed;
let mut sym = move || {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
(s >> 11) as f64 / (1u64 << 53) as f64 - 0.5
};
(0..n)
.map(|_| {
let mut x = [0.0; N];
x[0] = 1.0 + sym();
for k in 1..N {
x[k] = x[k - 1] * 2f64.powi(-53) * sym();
}
x
})
.collect()
}
#[inline(always)]
fn planar<const N: usize>(v: &[[f64; N]], b: usize) -> [[f64; 8]; N] {
let mut out = [[0.0; 8]; N];
for j in 0..(v.len() - 8 * b).min(8) {
for k in 0..N {
out[k][j] = v[8 * b + j][k];
}
}
out
}
#[inline(always)]
fn two_sum<S: Simd>(a: V8<S>, b: V8<S>) -> (V8<S>, V8<S>) {
let s = a.add(b);
let z = s.sub(a);
(s, a.sub(s.sub(z)).add(b.sub(z)))
}
#[inline(always)]
fn two_prod<S: Simd>(a: V8<S>, b: V8<S>) -> (V8<S>, V8<S>) {
let p = a.mul(b);
(p, a.fms(b, p))
}
#[inline(always)]
fn qtw_add_prod<S: Simd>(s: [V8<S>; 3], a: [V8<S>; 3], b: [V8<S>; 3]) -> [V8<S>; 3] {
let (c1, e1) = two_prod(a[0], b[0]);
let (t2, e2) = two_prod(a[0], b[1]);
let (t3, e3) = two_prod(a[1], b[0]);
let (c2, e4) = two_sum(t2, t3);
let (c2, e5) = two_sum(c2, e1);
let c3 = a[2]
.fma(b[0], e2)
.add(a[1].fma(b[1], e3))
.add(a[0].fma(b[2], e4))
.add(e5);
let (d1, f1) = two_sum(s[0], c1);
let (d2, f2) = two_sum(s[1], c2);
let (d2, f3) = two_sum(d2, f1);
[d1, d2, s[2].add(c3).add(f2).add(f3)]
}
#[derive(Clone, Copy)]
enum Src<'a, const N: usize> {
Raw(&'a [[f64; N]], &'a [[f64; N]]),
Soa(&'a [Vec<f64>], &'a [Vec<f64>]),
Packed(&'a Packed<N>, &'a Packed<N>),
}
trait Acc<S: Simd, const N: usize> {
fn step(&mut self, x: [V8<S>; N], y: [V8<S>; N]);
fn out(self) -> f64;
}
fn lanes<S: Simd>(v: &[V8<S>]) -> f64 {
v.iter().map(|x| x.to_array().iter().sum::<f64>()).sum()
}
struct Dd<S: Simd>(DDotAcc<S>);
impl<S: Simd> Acc<S, 2> for Dd<S> {
#[inline(always)]
fn step(&mut self, x: [V8<S>; 2], y: [V8<S>; 2]) {
self.0 = self.0.add_prod(DV { hi: x[0], lo: x[1] }, DV { hi: y[0], lo: y[1] });
}
fn out(self) -> f64 {
let f = self.0.finish();
lanes(&[f.hi, f.lo])
}
}
struct Qtw<S: Simd>([V8<S>; 3]);
impl<S: Simd> Acc<S, 3> for Qtw<S> {
#[inline(always)]
fn step(&mut self, x: [V8<S>; 3], y: [V8<S>; 3]) {
self.0 = qtw_add_prod(self.0, x, y);
}
fn out(self) -> f64 {
lanes(&self.0)
}
}
struct Od<S: Simd>(ODotAcc<S>);
impl<S: Simd> Acc<S, 8> for Od<S> {
#[inline(always)]
fn step(&mut self, x: [V8<S>; 8], y: [V8<S>; 8]) {
self.0.add_prod_mut(&OV { c: x }, &OV { c: y });
}
fn out(self) -> f64 {
lanes(&self.0.finish().c)
}
}
#[inline(always)]
fn each<const N: usize, S: Simd, A: Acc<S, N>>(src: Src<'_, N>, mut acc: A) -> f64 {
match src {
Src::Raw(a, b) => {
for blk in 0..a.len().div_ceil(8) {
let (x, y) = (planar(a, blk), planar(b, blk));
let (mut u, mut v) = ([V8::zero(); N], [V8::zero(); N]);
for k in 0..N {
u[k] = V8::load(&x[k]);
v[k] = V8::load(&y[k]);
}
acc.step(u, v);
}
}
Src::Soa(a, b) => {
for blk in 0..a[0].len() / 8 {
let (mut u, mut v) = ([V8::zero(); N], [V8::zero(); N]);
for k in 0..N {
u[k] = V8::load(&a[k][8 * blk..]);
v[k] = V8::load(&b[k][8 * blk..]);
}
acc.step(u, v);
}
}
Src::Packed(a, b) => {
for (x, y) in a.blocks::<S>().zip(b.blocks::<S>()) {
acc.step(x, y);
}
}
}
acc.out()
}
struct Dot2<'a>(Src<'a, 2>);
struct Dot3<'a>(Src<'a, 3>);
struct Dot8<'a>(Src<'a, 8>);
impl Kernel for Dot2<'_> {
type Output = f64;
#[inline(always)]
fn run<S: Simd>(self) -> f64 {
each(self.0, Dd(DDotAcc::<S>::zero()))
}
}
impl Kernel for Dot3<'_> {
type Output = f64;
#[inline(always)]
fn run<S: Simd>(self) -> f64 {
each(self.0, Qtw([V8::<S>::zero(); 3]))
}
}
impl Kernel for Dot8<'_> {
type Output = f64;
#[inline(always)]
fn run<S: Simd>(self) -> f64 {
each(self.0, Od(ODotAcc::<S>::zero()))
}
}
fn dot2(src: Src<'_, 2>) -> f64 {
simd::dispatch(Dot2(src))
}
fn dot3(src: Src<'_, 3>) -> f64 {
simd::dispatch(Dot3(src))
}
fn dot8(src: Src<'_, 8>) -> f64 {
simd::dispatch(Dot8(src))
}
fn group<const N: usize>(c: &mut Criterion, n: usize, label: &str, dot: fn(Src<'_, N>) -> f64) {
let a = values::<N>(n, 0x9e37_79b9_7f4a_7c15);
let b = values::<N>(n, 0x1234_5678_9abc_def1);
let (pa, pb) = (Packed::<N>::pack(&a), Packed::<N>::pack(&b));
let bytes = (n * 8 * N) as u64;
let mut g: BenchmarkGroup<_> = c.benchmark_group(format!("pack {N} parts {label}"));
g.sample_size(10)
.warm_up_time(Duration::from_millis(500))
.measurement_time(Duration::from_secs(2));
g.throughput(Throughput::Bytes(bytes));
let mut dst = vec![[0.0; N]; n];
g.bench_function("copy raw", |bn| {
bn.iter(|| {
dst.copy_from_slice(black_box(&a));
black_box(&dst);
})
});
g.bench_function("unpack", |bn| {
bn.iter(|| {
black_box(&pa).unpack_into(0, &mut dst);
black_box(&dst);
})
});
g.throughput(Throughput::Bytes(2 * bytes));
g.bench_function("dot raw", |bn| bn.iter(|| dot(Src::Raw(black_box(&a), black_box(&b)))));
let soa = |v: &[[f64; N]]| -> Vec<Vec<f64>> { (0..N).map(|k| v.iter().map(|x| x[k]).collect()).collect() };
let (sa, sb) = (soa(&a), soa(&b));
assert_eq!(n % 8, 0, "whole blocks for the raw parts' arrays");
g.bench_function("dot raw parts", |bn| {
bn.iter(|| dot(Src::Soa(black_box(&sa), black_box(&sb))))
});
g.bench_function("dot packed", |bn| {
bn.iter(|| dot(Src::Packed(black_box(&pa), black_box(&pb))))
});
if N == 2 {
let a2: Vec<DD> = a.iter().map(|x| DD { hi: x[0], lo: x[1] }).collect();
let b2: Vec<DD> = b.iter().map(|x| DD { hi: x[0], lo: x[1] }).collect();
let (qa, qb) = (pack::PackedDD::pack(&a2), pack::PackedDD::pack(&b2));
g.bench_function("dot_raw (DD)", |bn| {
bn.iter(|| pack::dot_raw(black_box(&a2), black_box(&b2)))
});
g.bench_function("Packed::dot (DD)", |bn| bn.iter(|| black_box(&qa).dot(black_box(&qb))));
}
g.finish();
}
fn bench(c: &mut Criterion) {
println!("pack_simd on {}", simd::isa());
group::<2>(c, 1 << 13, "in cache", dot2);
group::<3>(c, 5456, "in cache", dot3);
group::<8>(c, 1 << 11, "in cache", dot8);
group::<2>(c, 1 << 24, "out of cache", dot2);
group::<3>(c, (1 << 28) / 24 / 8 * 8, "out of cache", dot3);
group::<8>(c, 1 << 22, "out of cache", dot8);
}
criterion_group!(benches, bench);
criterion_main!(benches);