#![feature(test)]
#![feature(portable_simd)]
extern crate test;
#[macro_use]
mod helpers;
mod abort_strategy;
mod api;
mod distances;
mod inits;
mod memory;
mod variants;
pub use abort_strategy::AbortStrategy;
pub use api::{DistanceFunction, KMeans, KMeansConfig, KMeansConfigBuilder, KMeansState};
pub use distances::{EuclideanDistance, HistogramDistance};
pub use memory::Primitive;
#[cfg(test)]
mod tests {
use super::*;
use distances::EuclideanDistance;
use memory::SupportedSimdArray;
use rand::prelude::*;
use std::simd::Simd;
use test::Bencher;
#[bench]
fn complete_benchmark_lloyd_small_f64x8(b: &mut Bencher) { complete_benchmark_lloyd::<f64, 8>(b, 200, 2000, 10, 32); }
#[bench]
fn complete_benchmark_lloyd_mid_f64x8(b: &mut Bencher) { complete_benchmark_lloyd::<f64, 8>(b, 2000, 200, 10, 32); }
#[bench]
fn complete_benchmark_lloyd_big_f64x8(b: &mut Bencher) { complete_benchmark_lloyd::<f64, 8>(b, 10000, 8, 10, 32); }
#[bench]
fn complete_benchmark_lloyd_huge_f64x8(b: &mut Bencher) { complete_benchmark_lloyd::<f64, 8>(b, 20000, 256, 1, 32); }
#[bench]
fn complete_benchmark_lloyd_small_f32x8(b: &mut Bencher) { complete_benchmark_lloyd::<f32, 8>(b, 200, 2000, 10, 32); }
#[bench]
fn complete_benchmark_lloyd_mid_f32x8(b: &mut Bencher) { complete_benchmark_lloyd::<f32, 8>(b, 2000, 200, 10, 32); }
#[bench]
fn complete_benchmark_lloyd_big_f32x8(b: &mut Bencher) { complete_benchmark_lloyd::<f32, 8>(b, 10000, 8, 10, 32); }
#[bench]
fn complete_benchmark_lloyd_huge_f32x8(b: &mut Bencher) { complete_benchmark_lloyd::<f32, 8>(b, 20000, 256, 1, 32); }
fn complete_benchmark_lloyd<T: Primitive, const LANES: usize>(
b: &mut Bencher, sample_cnt: usize, sample_dims: usize, max_iter: usize, k: usize,
) where
T: Primitive,
Simd<T, LANES>: SupportedSimdArray<T, LANES>,
{
let mut rnd = rand::rngs::StdRng::seed_from_u64(1337);
let mut samples = vec![T::zero(); sample_cnt * sample_dims];
samples.iter_mut().for_each(|v| *v = rnd.gen_range(T::zero()..T::one()));
let kmean: KMeans<T, LANES, _> = KMeans::new(&samples, sample_cnt, sample_dims, EuclideanDistance);
let conf = KMeansConfig::build().random_generator(rnd).build();
b.iter(|| kmean.kmeans_lloyd(k, max_iter, KMeans::init_kmeanplusplus, &conf));
}
#[bench]
fn complete_benchmark_minibatch_small_f64x8(b: &mut Bencher) { complete_benchmark_minibatch::<f64, 8>(b, 30, 200, 2000, 100, 32); }
#[bench]
fn complete_benchmark_minibatch_mid_f64x8(b: &mut Bencher) { complete_benchmark_minibatch::<f64, 8>(b, 200, 2000, 200, 100, 32); }
#[bench]
fn complete_benchmark_minibatch_big_f64x8(b: &mut Bencher) { complete_benchmark_minibatch::<f64, 8>(b, 1000, 10000, 8, 100, 32); }
#[bench]
fn complete_benchmark_minibatch_huge_f64x8(b: &mut Bencher) { complete_benchmark_minibatch::<f64, 8>(b, 2000, 20000, 256, 30, 32); }
#[bench]
fn complete_benchmark_minibatch_small_f32x8(b: &mut Bencher) { complete_benchmark_minibatch::<f32, 8>(b, 30, 200, 2000, 100, 32); }
#[bench]
fn complete_benchmark_minibatch_mid_f32x8(b: &mut Bencher) { complete_benchmark_minibatch::<f32, 8>(b, 200, 2000, 200, 100, 32); }
#[bench]
fn complete_benchmark_minibatch_big_f32x8(b: &mut Bencher) { complete_benchmark_minibatch::<f32, 8>(b, 1000, 10000, 8, 100, 32); }
#[bench]
fn complete_benchmark_minibatch_huge_f32x8(b: &mut Bencher) { complete_benchmark_minibatch::<f32, 8>(b, 2000, 20000, 256, 30, 32); }
fn complete_benchmark_minibatch<T: Primitive, const LANES: usize>(
b: &mut Bencher, batch_size: usize, sample_cnt: usize, sample_dims: usize, max_iter: usize, k: usize,
) where
T: Primitive,
Simd<T, LANES>: SupportedSimdArray<T, LANES>,
{
let mut rnd = rand::rngs::StdRng::seed_from_u64(1337);
let mut samples = vec![T::zero(); sample_cnt * sample_dims];
samples.iter_mut().for_each(|v| *v = rnd.gen_range(T::zero()..T::one()));
let kmean = KMeans::new(&samples, sample_cnt, sample_dims, EuclideanDistance);
let conf = KMeansConfig::build().random_generator(rnd).build();
b.iter(|| kmean.kmeans_minibatch(batch_size, k, max_iter, KMeans::init_random_sample, &conf));
}
}