use rustsy_engine::pipeline;
use sha2::{Digest, Sha256};
use std::{
env, fs,
io::{self, Read, Write},
path::Path,
time::Instant,
};
fn main() -> io::Result<()> {
let path = env::args_os()
.nth(1)
.ok_or_else(|| io::Error::other("supply a fresh output directory"))?;
let root = Path::new(&path);
fs::create_dir(root)?;
let block = 512 * 1024;
let mut content = vec![0; block];
let mut seed = 1u32;
for byte in &mut content {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
*byte = (seed >> 24) as u8;
}
for n in 0..256 {
content[0] = n as u8;
fs::File::create_new(root.join(format!("source-{n}")))?.write_all(&content)?;
}
for count in [32, 256] {
for (round, depth) in [0, 2, 3, 3, 2, 0].into_iter().enumerate() {
let load = |job: &usize, buf: &mut Vec<u8>| {
fs::File::open(root.join(format!("source-{job}")))?.read_exact(buf)
};
let transform = |_: &usize, buf: &mut Vec<u8>| {
let mut result = Sha256::digest(&*buf);
for _ in 0..3 {
result = Sha256::digest([result.as_slice(), &buf[..4096]].concat());
}
Ok::<_, io::Error>(result)
};
let store = |job: usize, digest: sha2::digest::Output<Sha256>| {
let path = root.join(format!("digest-{count}-{round}-{job}"));
fs::File::create_new(path)?.write_all(&digest)
};
let start = Instant::now();
if depth == 0 {
let mut buf = vec![0; block];
for job in 0..count {
load(&job, &mut buf)?;
store(job, transform(&job, &mut buf)?)?;
}
} else {
let mut jobs = 0..count;
let metrics = pipeline::run(
vec![vec![0; block]; depth],
|| jobs.next(),
load,
transform,
store,
)?;
eprintln!("{count}\t{round}\t{depth}\t{metrics:?}");
}
println!("{count}\t{round}\t{depth}\t{}", start.elapsed().as_micros());
if round > 0 {
for job in 0..count {
if fs::read(root.join(format!("digest-{count}-{round}-{job}")))?
!= fs::read(root.join(format!("digest-{count}-0-{job}")))?
{
return Err(io::Error::other("digest mismatch"));
}
}
}
}
}
Ok(())
}