use lamellar::active_messaging::prelude::*;
use std::time::{Duration, Instant};
#[lamellar::AmData(Clone)]
struct StdSleepAM {
secs: u64,
}
#[lamellar::am]
impl LamellarAM for StdSleepAM {
async fn exec(self) {
println!(
"\tsleeping for {:?} sec on pe: {:?}",
self.secs,
lamellar::current_pe
);
let timer = Instant::now();
std::thread::sleep(Duration::from_secs(self.secs));
println!(
"\tpe: {:?} actually slept for {:?}",
lamellar::current_pe,
timer.elapsed().as_secs_f64()
);
}
}
#[lamellar::AmData(Clone)]
struct AsyncSleepAM {
secs: u64,
}
#[lamellar::am]
impl LamellarAM for AsyncSleepAM {
async fn exec(self) {
println!(
"\tsleeping for {:?} sec on pe: {:?}",
self.secs,
lamellar::current_pe
);
let timer = Instant::now();
async_std::task::sleep(Duration::from_secs(self.secs)).await;
println!(
"\tpe: {:?} actually slept for {:?}",
lamellar::current_pe,
timer.elapsed().as_secs_f64()
);
}
}
#[lamellar::main]
fn main() {
match std::env::var("LAMELLAR_THREADS") {
Ok(num) => if num.parse::<usize>().expect(
"NOTE: to highlight the effect of async tasks please set LAMELLAR_THREADS env var to 1",
) > 1
{
println!(" NOTE: to highlight the effect of async tasks please set LAMELLAR_THREADS env var to 1");
},
Err(_) => {
println!(" NOTE: to highlight the effect of async tasks please set LAMELLAR_THREADS env var to 1");
}
}
let world = lamellar::LamellarWorldBuilder::new().build();
let my_pe = world.my_pe();
world.barrier();
if my_pe == 0 {
let std_am = StdSleepAM { secs: 1 };
let async_am = AsyncSleepAM { secs: 1 };
println!("---------------------------------------------------------------");
println!("Testing Std Sleep am");
let mut timer = Instant::now();
let mut std_am_group = AmGroup::new(&world);
for _i in 0..10 {
std_am_group.add_am_all(std_am.clone()); }
world.block_on(async move {
std_am_group.exec().await;
});
println!(
"time for std sleep tasks: {:?}",
timer.elapsed().as_secs_f64()
);
println!("-----------------------------------");
println!("Testing async Sleep am");
let mut async_am_group = AmGroup::new(&world);
timer = Instant::now();
for _i in 0..10 {
async_am_group.add_am_all(async_am.clone()); }
world.block_on(async move {
async_am_group.exec().await;
});
println!(
"time for async sleep tasks: {:?}",
timer.elapsed().as_secs_f64()
);
println!("---------------------------------------------------------------");
}
}