use std::str::FromStr;
use std::time::Instant;
use flynnel::sched::par_iter::for_each_chunk_triple_min_leaf;
use flynnel::sched::trace;
use flynnel::{JobPlan, for_each_chunk};
#[inline(never)]
fn light(x: &mut f64) {
let mut v = *x;
for _ in 0..3 {
v = v.sqrt() * 1.0000001_f64;
}
*x = v;
}
fn argument<T: FromStr>(args: &[String], index: usize, default: T, name: &str) -> T
where
T::Err: std::fmt::Display,
{
match args.get(index) {
None => default,
Some(text) => match text.parse() {
Ok(value) => value,
Err(e) => {
eprintln!("argument {name} is {text:?}: {e}");
std::process::exit(2);
}
},
}
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let n: usize = argument(&args, 1, 10_000, "items");
let est: u32 = argument(&args, 2, 6, "estimate");
let kind = args.get(3).map(String::as_str).unwrap_or("light");
let gap_ms: u64 = argument(&args, 4, 0, "gap_ms");
if !trace::is_enabled() {
eprintln!("the trace switch is not set; the run will time the call but record nothing");
}
let p = flynnel::sched::par_iter::calibrate_host_dispatch();
println!(
"host profile: dispatch cost {} ns, collapse {} ns, wake {} ns",
p.dispatch_cost_ns, p.collapse_threshold_ns, p.jec_wake_threshold_ns
);
let plan = JobPlan::new(6, n as u32).with_estimated_per_item_ns(est);
let mut v = vec![1.5f64; n];
let a = vec![1.0f64; n];
let b = vec![2.0f64; n];
let mut out = vec![0f64; n];
let mut call = || match kind {
"add" => for_each_chunk_triple_min_leaf(&plan, &mut out, &a, &b, 64, |o, x, y| {
for ((o, x), y) in o.iter_mut().zip(x).zip(y) {
*o = x + y;
}
}),
_ => for_each_chunk(&plan, &mut v, |s| s.iter_mut().for_each(light)),
};
let gap = std::time::Duration::from_millis(gap_ms);
for _ in 0..200 {
call();
if gap_ms > 0 {
std::thread::sleep(gap);
}
}
trace::reset_current_thread();
if gap_ms > 0 {
std::thread::sleep(gap);
}
let t0 = Instant::now();
call();
let wall = t0.elapsed();
println!("traced call: {kind} n={n} est={est} ns/item, wall {wall:?}");
let waits = flynnel::total_latch_monitor_waits();
let arms = flynnel::total_latch_monitor_arms();
let per_wait = if waits == 0 {
0.0
} else {
arms as f64 / waits as f64
};
println!("latch monitor: waits={waits} arms={arms} arms_per_wait={per_wait:.1}");
trace::dump_to_stderr("caller");
trace::request_worker_flush();
let mut wide = vec![1.5f64; 1 << 20];
let wide_plan = JobPlan::new(6, wide.len() as u32).with_estimated_per_item_ns(50).with_smt();
let expected = std::thread::available_parallelism().map_or(1, |n| n.get()) as u64;
let deadline = Instant::now() + std::time::Duration::from_secs(30);
let done = loop {
for_each_chunk(&wide_plan, &mut wide, |s| {
for x in s.iter_mut() {
let mut y = *x;
for _ in 0..16 {
y = y.sqrt() * 1.0000001;
}
*x = y;
}
});
std::thread::sleep(std::time::Duration::from_millis(5));
let done = trace::worker_flushes_done();
if done >= expected {
break done;
}
if Instant::now() > deadline {
eprintln!("only {done} of {expected} worker dumps completed after 30 s");
break done;
}
};
std::thread::sleep(std::time::Duration::from_millis(500));
println!("worker dumps completed: {done} of {expected}");
trace::clear_worker_flush_request();
std::hint::black_box(&wide);
std::hint::black_box((&v, &out));
}