console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.22k stars 238 forks source link

Rayon and indicatif #573

Closed jimouris closed 11 months ago

jimouris commented 11 months ago

Will something like the following cause pb to lock and eventually run sequentially?

    let pb = ProgressBar::new(size as u64);
    let found_r = (0..size).into_par_iter().map(BigUint::from).find_any(|r| {
        pb.inc(1);
        // Check if g^r == a
        if g.modpow(r, &p) == a {
            true
        } else {
            false
        }
    });
    pb.finish_with_message("done");
djc commented 11 months ago

indicatif has native support for rayon iterators, maybe check that out?

jimouris commented 11 months ago

Of course I tried it before asking. I saw 100% utilization from all the cores but my question was mostly focusing on whether this is efficient. What does it mean to have built in support for rayon? Doesn't it still need to be locked and unlocked all the time in order to be updated? In my understanding this would result in a lot of overhead. I'm asking to try to understand how it works.

djc commented 11 months ago

It means you can call progress_with() on a ParallelIterator:

https://github.com/console-rs/indicatif/blob/main/src/rayon.rs

chris-laplante commented 11 months ago

Of course I tried it before asking. I saw 100% utilization from all the cores but my question was mostly focusing on whether this is efficient. What does it mean to have built in support for rayon? Doesn't it still need to be locked and unlocked all the time in order to be updated? In my understanding this would result in a lot of overhead. I'm asking to try to understand how it works.

I think the 100% utilization is to be expected and is not necessarily an issue. There are no delays in the code.

jimouris commented 11 months ago

Thank you, that makes sense.