Skip to content

Instantly share code, notes, and snippets.

@sundy-li
Last active January 16, 2021 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sundy-li/01a02ef5fcd98bce564a307f31ff850b to your computer and use it in GitHub Desktop.
Save sundy-li/01a02ef5fcd98bce564a307f31ff850b to your computer and use it in GitHub Desktop.
memory_bench.rs
use std::sync::{Mutex, Arc};
use std::env;
use std::time::Instant;
use std::sync::mpsc::channel;
use threadpool::ThreadPool;
// test memory bandwidth
fn main() {
let args: Vec<String> = env::args().collect();
let threads: usize = args[1].parse().unwrap();
let block_size: usize = args[2].parse().unwrap();
let counter = Arc::new(Mutex::new(0));
let jobs = (10000000000u64 / block_size as u64) as usize;
let pool = ThreadPool::new(threads);
let start = Instant::now();
let (tx, rx) = channel();
for _ in 0..jobs {
let counter = counter.clone();
let tx = tx.clone();
pool.execute(move || {
let arr: Vec<u64> = (0..(block_size as u64)).collect();
let mut num = counter.lock().unwrap();
*num += arr.len();
tx.send(1).expect("channel will be there waiting for the pool");
});
}
assert_eq!(rx.iter().take(jobs).fold(0, |a, b| a + b), jobs);
let cost = start.elapsed();
println!("Memory Bandwidth: {:?} GB/s, cost: {:?}", (8.0*1e10/1e6) as u128 / cost.as_millis(), cost);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment