Ignore:
Timestamp:
Dec 18, 2020, 2:53:20 PM (13 months ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
5d369c7
Parents:
49ce636
Message:

Added bench.rs for common benchmark rust code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/readyQ/locality.rs

    r49ce636 r751e2eb  
    1 use std::io::{self, Write};
    21use std::ptr;
    32use std::sync::Arc;
    4 use std::sync::atomic::{AtomicU64, AtomicBool, Ordering};
    5 use std::time::{Instant,Duration};
     3use std::sync::atomic::{AtomicU64, Ordering};
     4use std::time::Instant;
    65use std::thread::{self, ThreadId};
    76
    87use tokio::runtime::Builder;
    98use tokio::sync;
    10 use tokio::time;
    11 
    12 use clap::{Arg, App, ArgMatches};
    13 use isatty::stdout_isatty;
     9
     10use clap::{App, Arg};
    1411use num_format::{Locale, ToFormattedString};
    1512use rand::Rng;
    1613
    17 // ==================================================
    18 struct BenchData {
    19         clock_mode: bool,
    20         stop: AtomicBool,
    21         stop_count: u64,
    22         duration: f64,
    23         threads_left: AtomicU64,
    24 }
    25 
    26 impl BenchData {
    27         fn new(options: ArgMatches, nthreads: usize) -> BenchData {
    28                 let (clock_mode, stop_count, duration) = if options.is_present("iterations") {
    29                         (false,
    30                         options.value_of("iterations").unwrap().parse::<u64>().unwrap(),
    31                         -1.0)
    32                 } else {
    33                         (true,
    34                         std::u64::MAX,
    35                         options.value_of("duration").unwrap().parse::<f64>().unwrap())
    36                 };
    37 
    38                 BenchData{
    39                         clock_mode: clock_mode,
    40                         stop: AtomicBool::new(false),
    41                         stop_count: stop_count,
    42                         duration: duration,
    43                         threads_left: AtomicU64::new(nthreads as u64),
    44                 }
    45         }
    46 }
    47 
    48 async fn wait(bench: &BenchData, start: &Instant, is_tty: bool) {
    49         loop {
    50                 time::sleep(Duration::from_micros(100000)).await;
    51                 let delta = start.elapsed();
    52                 if is_tty {
    53                         print!(" {:.1}\r", delta.as_secs_f32());
    54                         io::stdout().flush().unwrap();
    55                 }
    56                 if bench.clock_mode && delta >= Duration::from_secs_f64(bench.duration)  {
    57                         break;
    58                 }
    59                 else if !bench.clock_mode && bench.threads_left.load(Ordering::Relaxed) == 0 {
    60                         break;
    61                 }
    62         }
    63 }
     14#[path = "../bench.rs"]
     15mod bench;
    6416
    6517// ==================================================
     
    240192}
    241193
    242 async fn local(start: Arc<sync::Barrier>, idata: MyDataPtr, spots : Arc<Vec<MySpot>>, cnt: u64, share: bool, id: usize, bench: Arc<BenchData>) -> Result{
     194async fn local(start: Arc<sync::Barrier>, idata: MyDataPtr, spots : Arc<Vec<MySpot>>, cnt: u64, share: bool, id: usize, exp: Arc<bench::BenchData>) -> Result{
    243195        let mut state = rand::thread_rng().gen::<u64>();
    244196        let mut data = idata;
     
    267219                // Check if the experiment is over
    268220                if closed { break }                                                   // yes, spot was closed
    269                 if  bench.clock_mode && bench.stop.load(Ordering::Relaxed) { break }  // yes, time's up
    270                 if !bench.clock_mode && r.count >= bench.stop_count { break }         // yes, iterations reached
     221                if  exp.clock_mode && exp.stop.load(Ordering::Relaxed) { break }  // yes, time's up
     222                if !exp.clock_mode && r.count >= exp.stop_count { break }         // yes, iterations reached
    271223
    272224                assert_ne!(data.ptr as *const MyData, ptr::null());
     
    284236        }
    285237
    286         bench.threads_left.fetch_sub(1, Ordering::SeqCst);
     238        exp.threads_left.fetch_sub(1, Ordering::SeqCst);
    287239        r
    288240}
     
    292244fn main() {
    293245        let options = App::new("Locality Tokio")
    294                 .arg(Arg::with_name("duration")  .short("d").long("duration")  .takes_value(true).default_value("5").help("Duration of the experiments in seconds"))
    295                 .arg(Arg::with_name("iterations").short("i").long("iterations").takes_value(true).conflicts_with("duration").help("Number of iterations of the experiments"))
    296                 .arg(Arg::with_name("nthreads")  .short("t").long("nthreads")  .takes_value(true).default_value("1").help("Number of threads to use"))
    297                 .arg(Arg::with_name("nprocs")    .short("p").long("nprocs")    .takes_value(true).default_value("1").help("Number of processors to use"))
     246                .args(&bench::args())
    298247                .arg(Arg::with_name("size")      .short("w").long("worksize")  .takes_value(true).default_value("2").help("Size of the array for each threads, in words (64bit)"))
    299248                .arg(Arg::with_name("work")      .short("c").long("workcnt")   .takes_value(true).default_value("2").help("Number of words to touch when working (random pick, cells can be picked more than once)"))
     
    315264        assert_eq!(&s, "1,000,000");
    316265
    317         let bench = Arc::new(BenchData::new(options, nprocs));
     266        let exp = Arc::new(bench::BenchData::new(options, nprocs));
    318267        let mut results = Result::new();
    319268
     
    342291                                        share,
    343292                                        i,
    344                                         bench.clone(),
     293                                        exp.clone(),
    345294                                ))
    346295                        }).collect();
     
    349298                        println!("Starting");
    350299
    351                         let is_tty = stdout_isatty();
    352300                        let start = Instant::now();
    353301                        barr.wait().await;
    354302
    355                         wait(&bench, &start, is_tty).await;
    356 
    357                         bench.stop.store(true, Ordering::SeqCst);
    358                         elapsed = start.elapsed();
     303                        elapsed = exp.wait(&start).await;
    359304
    360305                        println!("\nDone");
Note: See TracChangeset for help on using the changeset viewer.