Struct ConcurrentWrapper

Source
pub struct ConcurrentWrapper<P: ProgressLog = ProgressLogger> { /* private fields */ }
Expand description

A ConcurrentProgressLog implementation that wraps a ProgressLog in an Arc/Mutex.

The methods update and update_with_count buffer the increment and add it to the underlying logger only when the buffer reaches a threshold; this prevents locking the underlying logger too often. The threshold is set at creation using the methods with_threshold and wrap_with_threshold, or by calling the method threshold.

The method light_update, as in the case of ProgressLogger, further delays updates using an even faster check.

§Examples

In this example, we manually spawn processes:

use dsi_progress_logger::prelude::*;
use std::thread;

let mut cpl = concurrent_progress_logger![item_name = "pumpkin"];
cpl.start("Smashing pumpkins (using many threads)...");

std::thread::scope(|s| {
    for i in 0..100 {
        let mut pl = cpl.clone();
        s.spawn(move || {
            for _ in 0..100000 {
                // do something on each pumpkin
                pl.update();
            }
        });
    }
});

cpl.done();

You can obtain the same behavior with rayon using methods such as for_each_with and map_with:

use dsi_progress_logger::prelude::*;
use rayon::prelude::*;

let mut cpl = concurrent_progress_logger![item_name = "pumpkin"];
cpl.start("Smashing pumpkins (using many threads)...");

(0..1000000).into_par_iter().
    with_min_len(1000). // optional, might reduce the amount of cloning
    for_each_with(cpl.clone(), |pl, i| {
        // do something on each pumpkin
        pl.update();
    }
);

cpl.done();

Note that you have to pass cpl.clone() to avoid a move that would make the call to done impossible. Also, since for_each_with might perform excessive cloning if jobs are too short, you can use with_min_len to reduce the amount of cloning.

Implementations§

Source§

impl ConcurrentWrapper

Source

pub fn new() -> Self

Create a new ConcurrentWrapper based on a default ProgressLogger, using the default threshold.

Source

pub fn with_threshold(threshold: u32) -> Self

Create a new ConcurrentWrapper wrapping a default ProgressLogger, using the given threshold.

Source§

impl<P: ProgressLog> ConcurrentWrapper<P>

Source

pub const DEFAULT_THRESHOLD: u32 = 32_768u32

The default threshold for updating the underlying logger.

Source

pub const LIGHT_UPDATE_MASK: u32 = 1_023u32

Calls to light_update will cause a call to update_with_count only if the current local count is a multiple of this mask plus one.

Note that this constant is significantly smaller than the one used in ProgressLogger, as updates will be further delayed by the threshold mechanism.

Source

pub fn threshold(&mut self, threshold: u32) -> &mut Self

Set the threshold for updating the underlying logger.

Note that concurrent loggers with the same underlying logger have independent thresholds.

Source

pub fn wrap(inner: P) -> Self

Wrap a given ProgressLog in a ConcurrentWrapper using the default threshold.

Source

pub fn wrap_with_threshold(inner: P, threshold: u32) -> Self

Wrap a given ProgressLog in a ConcurrentWrapper using a given threshold.

Source

pub fn flush(&mut self)

Force an update of the underlying logger with the current local count.

Source§

impl<P: ProgressLog + Clone> ConcurrentWrapper<P>

Source

pub fn dup(&self) -> Self

Duplicates the concurrent wrapper, obtaning a new one with the same threshold, with a local count of zero, and with an inner ProgressLog that is a clone of the original one.

Trait Implementations§

Source§

impl<P: ProgressLog + Clone> Clone for ConcurrentWrapper<P>

Source§

fn clone(&self) -> Self

Clone the concurrent wrapper, obtaning a new one with the same threshold, with a local count of zero, and with the same inner ProgressLog.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: ProgressLog + Clone + Send> ConcurrentProgressLog for ConcurrentWrapper<P>

Source§

type Duplicated = ConcurrentWrapper<P>

The type returned by dup.
Source§

fn dup(&self) -> Self

Duplicate the concurrent progress logger, obtaning a new one. Read more
Source§

impl Default for ConcurrentWrapper

Source§

fn default() -> Self

Create a new ConcurrentWrapper based on a default ProgressLogger, with a threshold of DEFAULT_THRESHOLD.

Source§

impl Display for ConcurrentWrapper

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: ProgressLog> Drop for ConcurrentWrapper<P>

This implementation just calls flush, type Concurrent = Option<P::Concurrent>;

to guarantee that all updates are correctly passed to the underlying logger.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<P: ProgressLog + Clone + Send> ProgressLog for ConcurrentWrapper<P>

Source§

type Concurrent = ConcurrentWrapper<P>

The type returned by concurrent.
Source§

fn log(&mut self, now: Instant)

Force a log of self assuming now is the current time. Read more
Source§

fn log_if(&mut self, now: Instant)

Log self if it is time to log. Read more
Source§

fn add_to_count(&mut self, count: usize)

Add a value to the counter. Read more
Source§

fn display_memory(&mut self, display_memory: bool) -> &mut Self

Set the display of memory information. Read more
Source§

fn item_name(&mut self, item_name: impl AsRef<str>) -> &mut Self

Set the name of an item.
Source§

fn log_interval(&mut self, log_interval: Duration) -> &mut Self

Set the log interval.
Source§

fn expected_updates(&mut self, expected_updates: Option<usize>) -> &mut Self

Set the expected number of updates. Read more
Source§

fn time_unit(&mut self, time_unit: Option<TimeUnit>) -> &mut Self

Set the time unit to use for speed. Read more
Source§

fn local_speed(&mut self, local_speed: bool) -> &mut Self

Set whether to display additionally the speed achieved during the last log interval.
Source§

fn log_target(&mut self, target: impl AsRef<str>) -> &mut Self

Set the log target. Read more
Source§

fn start(&mut self, msg: impl AsRef<str>)

Start the logger, displaying the given message. Read more
Source§

fn update(&mut self)

Increase the count and check whether it is time to log.
Source§

fn update_with_count_and_time(&mut self, count: usize, _now: Instant)

Set the count and check whether it is time to log, given the current time. Read more
Source§

fn update_with_count(&mut self, count: usize)

Set the count and check whether it is time to log.
Source§

fn light_update(&mut self)

Increase the count but checks whether it is time to log only after an implementation-defined number of calls. Read more
Source§

fn update_and_display(&mut self)

Increase the count and forces a log.
Source§

fn stop(&mut self)

Stop the logger, fixing the final time.
Source§

fn done(&mut self)

Stop the logger, print Completed., and display the final stats. The number of expected updates will be cleared.
Source§

fn done_with_count(&mut self, count: usize)

Stop the logger, sets the count, prints Completed., and displays the final stats. The number of expected updates will be cleared. Read more
Source§

fn elapsed(&self) -> Option<Duration>

Return the elapsed time since the logger was started, or None if the logger has not been started.
Source§

fn refresh(&mut self)

Refreshe memory information, if previously requested with display_memory. You do not need to call this method unless you display the logger manually.
Source§

fn trace(&self, args: Arguments<'_>)

Output the given message at the trace level. Read more
Source§

fn debug(&self, args: Arguments<'_>)

Output the given message at the debug level. Read more
Source§

fn info(&self, args: Arguments<'_>)

Output the given message at the info level. Read more
Source§

fn warn(&self, args: Arguments<'_>)

Output the given message at the warn level. Read more
Source§

fn error(&self, args: Arguments<'_>)

Output the given message at the error level. Read more
Source§

fn concurrent(&self) -> Self::Concurrent

Return a concurrent copy of the logger. Read more

Auto Trait Implementations§

§

impl<P> Freeze for ConcurrentWrapper<P>

§

impl<P> RefUnwindSafe for ConcurrentWrapper<P>

§

impl<P> Send for ConcurrentWrapper<P>
where P: Send,

§

impl<P> Sync for ConcurrentWrapper<P>
where P: Send,

§

impl<P> Unpin for ConcurrentWrapper<P>

§

impl<P> UnwindSafe for ConcurrentWrapper<P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.