From Rayon to Tokyo

⚓ rust    📅 2025-04-26    👤 surdeus    👁️ 1      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: From Rayon to Tokyo

Please consider this exercise:

use rayon::prelude::*;
fn rosza(m: u32, n: u32) -> u32 {
    match (m, n) {
        (0, n) => n + 1,
        (m, 0) => rosza(m - 1, 1),
        (m, n) => rosza(m - 1, rosza(m, n - 1)),
    }
}
async fn reckon() {
    rayon::spawn(move || {
        (1..=10).into_par_iter().for_each(|_| {
            let result = rosza(3, 5);
            let idx = rayon::current_thread_index().unwrap();
            println!("{} {}", idx, result);
        });
    });
}
#[tokio::main]
async fn main() {
    println!("{:?}",reckon().await);
}

This performs not so bad and it can be seen nicely how it is spread over the pool:

()                                                                                            
0 253                                                                                         
5 253                                                                                         
1 253                                                                                         
0 253                                                                                         
5 253                                                                                         
1 253                                                                                         
0 253                                                                                         
4 253                                                                                         
5 253                                                                                         
0 253  

What i couldn’t manage yet is to send the result over a channel back to Tokyo.
I tried desperately to guess - without success so far:

// let (tx, mut rx) = mpsc::channel::<u32>(10);                                                  
// tx.send(result);                                                                              
// while let Some(thing) = rx.recv().await {                                                     
//    println!("{}", thing);                                                                     
// }                                                                                             

This and some other unfortunate variants yield None.

Thanks for any hint.

P.S.: I wanted something similar like in the blog from Alice:

async fn psum_iter(nums: Vec<i32>) -> i32 {
    let (tx, rx) = tokio::sync::oneshot::channel();
    rayon::spawn(move || {
        let sum = nums.par_iter().sum();
        let _ = tx.send(sum);
    });
    rx.await.expect("Panic in rayon::spawn")

5 posts - 2 participants

Read full topic

🏷️ rust_feed