From bc7eb4167579cd599a9e4fab0a88e933bfee5af3 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Thu, 1 Feb 2024 15:24:31 +0100 Subject: [PATCH] killer addition works --- members/tokryon/src/main.rs | 67 +++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/members/tokryon/src/main.rs b/members/tokryon/src/main.rs index b3c1aca..fb67d9b 100644 --- a/members/tokryon/src/main.rs +++ b/members/tokryon/src/main.rs @@ -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) -> bool { + let sum: u128 = { range.par_iter().map(|n| *n as u128).sum::() }; + 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::() }; - 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; + }; + } }