Created
October 4, 2020 10:43
-
-
Save lu-zero/6774feae6e57369140b38005fe6f999f to your computer and use it in GitHub Desktop.
testcase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use rayon::prelude::*; | |
use std::{thread, time}; | |
fn process(data: usize) -> usize { | |
(0..100usize) | |
.into_par_iter() | |
.map(|v| { | |
thread::sleep(time::Duration::from_millis(1)); | |
data + v | |
}) | |
.sum() | |
} | |
fn main() { | |
let num_threads = rayon::current_num_threads(); | |
let (send, recv) = crossbeam::channel::bounded(num_threads); | |
let mut aggregate = Vec::new(); | |
rayon::scope(|s| { | |
s.spawn(move |_| { | |
for v in 0..num_threads * 10 { | |
thread::sleep(time::Duration::from_millis(10)); | |
send.send(v).unwrap(); | |
if v % 100 == 0 { | |
println!("sending {}", v); | |
} | |
} | |
println!("Out!"); | |
}); | |
loop { | |
let working_set: Vec<_> = (0..num_threads) | |
.into_par_iter() | |
.filter_map(|_w| { | |
let r = recv.recv().map(|v| process(v)).ok(); | |
if r.is_none() { | |
println!("No more elements"); | |
} else { | |
println!("Processing {}", r.unwrap()); | |
} | |
r | |
}) | |
.collect(); | |
if working_set.is_empty() { | |
println!("Empty!"); | |
break; | |
} else { | |
println!("Appending {}", working_set[0]); | |
aggregate.extend(working_set); | |
} | |
} | |
}); | |
println!("{}", aggregate.len()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment