killer addition works
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 1m30s Details

This commit is contained in:
Christoph J. Scherr 2024-02-01 15:24:31 +01:00
parent 13c19c1831
commit bc7eb41675
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
1 changed files with 50 additions and 17 deletions

View File

@ -1,10 +1,42 @@
use std::io::{prelude::*, Sink};
use std::sync::{Arc, Mutex};
use rayon::prelude::*;
use tokio::time::{interval, Instant};
// if we make these larger, our computer can be used as a heater🔥
type Danum = u16;
const CAP: usize = 1 << 14;
const M: u128 = CAP as u128 * Danum::MAX as u128;
fn status(start: &Instant, range: &Vec<Danum>) -> bool {
let sum: u128 = { range.par_iter().map(|n| *n as u128).sum::<u128>() };
if sum < 1 {
return false;
}
let progress = sum as f64 / M as f64;
let eq = sum == M;
println!(
r#"
done: {}
current threads: {}
progress: {}%
log_2(capacity): {}
log_2(sum): {}
cap: {}
sum: {}
took: {:?}
"#,
eq,
rayon::current_num_threads(),
progress * 100.0,
CAP.ilog2(),
sum.ilog2(),
CAP,
sum,
start.elapsed(),
);
eq
}
#[tokio::main]
async fn main() {
@ -14,22 +46,23 @@ async fn main() {
unsafe {
range.set_len(range.capacity());
}
let now = std::time::Instant::now();
range.par_iter_mut().for_each(|num| {
let mut sink = Sink::default();
while *num < Danum::MAX {
*num += 1; // cannot use `+=` on type `&u8`
let _ = write!(sink, "{num}"); // just to disable the compiler from calculating it all
// beforehand
let start = Instant::now();
let lock = Arc::new(Mutex::new(range));
let lock2 = lock.clone();
rayon::spawn(move || {
for n in 0..CAP {
let mut range = lock.lock().unwrap();
for _ in 0..Danum::MAX {
range[n] += 1;
}
}
});
let sum: u128 = { range.par_iter().map(|n| *n as u128).sum::<u128>() };
let eq = sum == CAP as u128 * Danum::MAX as u128;
println!(
"log cap: {}\nit worked: {eq}\nsum: {sum}\nlog_2(sum): {}\ntook: {:?}\nused threads: {}",
CAP.ilog2(),
sum.ilog2(),
now.elapsed(),
rayon::current_num_threads()
);
let mut ticker = interval(tokio::time::Duration::from_millis(500));
loop {
ticker.tick().await;
if status(&start, &lock2.lock().unwrap()) {
break;
};
}
}