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