-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
59 lines (54 loc) · 1.8 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use futures::executor::block_on;
use futures::future::join_all;
use async_std::channel::{bounded, Sender, Receiver};
use std::env;
use std::time::Instant;
fn main() {
let argv : Vec<String> = env::args().collect();
let n : usize = argv[1].parse().unwrap(); /* Cycle length. */
let r : usize = argv[2].parse().unwrap(); /* Number of cycles. */
let m : usize = argv[3].parse().unwrap(); /* Number of rounds. */
let tobe = f(n, r, m);
let start = Instant::now();
block_on(tobe);
println!("{:?}", start.elapsed().as_secs_f64());
}
struct R {
rx : Receiver<()>,
tx : Sender<()>
}
static mut CYCLES : Vec::<Vec::<R>> = Vec::<Vec::<R>>::new();
const DEPTH: usize = 1000;
async fn f(n : usize, r : usize, m : usize) {
let _pad : [u8; DEPTH] = [50; DEPTH];
for _ in 0 .. r {
let mut c = Vec::<R>::new();
for _ in 0 .. n {
let (tx, rx) = bounded::<()>(10);
c.push(R { tx : tx.clone(), rx : rx.clone() });
}
unsafe { CYCLES.push(c); }
}
join_all((0 .. r)
.flat_map(|i| (0 .. n)
.map(|j| (i, j))
.collect::<Vec<_>>())
.map(|(i, j)| lop(n, m, i, j))
.collect::<Vec<_>>())
.await;
}
async fn lop(n : usize, m : usize, cnr : usize, idx : usize) {
unsafe {
let left = &CYCLES[cnr][idx];
let right = &CYCLES[cnr][(idx + 1) % n];
for round in 0 .. m {
if idx == round % n {
right.tx.send(()).await.expect("send failed");
left.rx.recv().await.expect("recv failed");
} else {
left.rx.recv().await.expect("recv failed");
right.tx.send(()).await.expect("send failed");
}
}
}
}