use std::{
num::NonZeroUsize,
sync::Arc,
thread,
time::Duration,
};
use qubit_function::{
ArcConsumer,
Consumer,
};
use qubit_progress::{
Progress,
reporter::{
NoOpProgressReporter,
ProgressReporter,
},
};
use crate::process::{
BatchProcessError,
BatchProcessResult,
BatchProcessState,
BatchProcessor,
};
use crate::utils::run_scoped_parallel;
pub struct ParallelBatchProcessor<Item> {
consumer: ArcConsumer<Item>,
thread_count: NonZeroUsize,
report_interval: Duration,
reporter: Arc<dyn ProgressReporter>,
}
impl<Item> ParallelBatchProcessor<Item> {
pub const DEFAULT_REPORT_INTERVAL: Duration = Duration::from_secs(5);
#[inline]
pub fn new<C>(consumer: C) -> Self
where
C: Consumer<Item> + Send + Sync + 'static,
{
Self {
consumer: consumer.into_arc(),
thread_count: NonZeroUsize::new(Self::default_thread_count())
.expect("default parallel processor thread count should be non-zero"),
report_interval: Self::DEFAULT_REPORT_INTERVAL,
reporter: Arc::new(NoOpProgressReporter),
}
}
#[inline]
pub fn default_thread_count() -> usize {
thread::available_parallelism()
.map(usize::from)
.unwrap_or(1)
}
#[inline]
pub const fn with_thread_count(mut self, thread_count: NonZeroUsize) -> Self {
self.thread_count = thread_count;
self
}
#[inline]
pub fn with_reporter<R>(self, reporter: R) -> Self
where
R: ProgressReporter + 'static,
{
self.with_reporter_arc(Arc::new(reporter))
}
#[inline]
pub fn with_reporter_arc(self, reporter: Arc<dyn ProgressReporter>) -> Self {
Self { reporter, ..self }
}
#[inline]
pub fn with_report_interval(self, report_interval: Duration) -> Self {
Self {
report_interval,
..self
}
}
#[inline]
pub const fn thread_count(&self) -> usize {
self.thread_count.get()
}
#[inline]
pub const fn report_interval(&self) -> Duration {
self.report_interval
}
#[inline]
pub fn reporter(&self) -> &Arc<dyn ProgressReporter> {
&self.reporter
}
#[inline]
pub const fn consumer(&self) -> &ArcConsumer<Item> {
&self.consumer
}
#[inline]
pub fn into_consumer(self) -> ArcConsumer<Item> {
self.consumer
}
}
impl<Item> BatchProcessor<Item> for ParallelBatchProcessor<Item>
where
Item: Send,
{
type Error = BatchProcessError;
fn process<I>(&mut self, items: I, count: usize) -> Result<BatchProcessResult, Self::Error>
where
I: IntoIterator<Item = Item>,
{
let state = Arc::new(BatchProcessState::new(count));
let progress = Progress::new(self.reporter.as_ref(), self.report_interval);
progress.report_started(state.progress_counters());
if count > 0 {
self.process_non_empty(items, count, Arc::clone(&state), &progress);
} else if items.into_iter().next().is_some() {
state.record_item_observed();
}
if state.observed_count() < count {
let failed = progress.report_failed(state.progress_counters());
let result = state.to_direct_result(failed.elapsed());
Err(BatchProcessError::CountShortfall {
expected: count,
actual: state.observed_count(),
result,
})
} else if state.observed_count() > count {
let failed = progress.report_failed(state.progress_counters());
let result = state.to_direct_result(failed.elapsed());
Err(BatchProcessError::CountExceeded {
expected: count,
observed_at_least: state.observed_count(),
result,
})
} else {
let finished = progress.report_finished(state.progress_counters());
let result = state.to_direct_result(finished.elapsed());
Ok(result)
}
}
}
impl<Item> ParallelBatchProcessor<Item>
where
Item: Send,
{
fn process_non_empty<I>(
&self,
items: I,
count: usize,
state: Arc<BatchProcessState>,
progress: &Progress<'_>,
) where
I: IntoIterator<Item = Item>,
{
thread::scope(|scope| {
let reporter_state = Arc::clone(&state);
let running_progress =
progress.spawn_running_reporter(scope, move || reporter_state.progress_counters());
let running_point_handle = running_progress.point_handle();
let worker_count = self.thread_count.get().min(count);
let observer_state = Arc::clone(&state);
let worker_state = Arc::clone(&state);
let consumer = self.consumer.clone();
run_scoped_parallel(
items,
count,
worker_count,
move || observer_state.record_item_observed(),
move |_index, item| {
worker_state.record_item_started();
consumer.accept(&item);
worker_state.record_item_processed();
running_point_handle.report();
},
);
running_progress.stop_and_join();
});
}
}