Skip to content

Instantly share code, notes, and snippets.

@lu-zero
Created October 4, 2020 10:43
Show Gist options
  • Select an option

  • Save lu-zero/6774feae6e57369140b38005fe6f999f to your computer and use it in GitHub Desktop.

Select an option

Save lu-zero/6774feae6e57369140b38005fe6f999f to your computer and use it in GitHub Desktop.

Revisions

  1. lu-zero created this gist Oct 4, 2020.
    53 changes: 53 additions & 0 deletions main.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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());
    }