use crate::batch::{HomologicalBatch, HomologicalBatchError, HomologicalBatchLimits};
use crate::resolution::{ProjectiveResolution, ResolutionEnd};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HomologicalBatchStreamLimits {
pub max_live_sources: u64,
pub max_pairs: u64,
pub max_ext_cells: u64,
}
impl Default for HomologicalBatchStreamLimits {
fn default() -> Self {
let batch = HomologicalBatchLimits::default();
Self {
max_live_sources: 1,
max_pairs: batch.max_pairs as u64,
max_ext_cells: batch.max_ext_cells as u64,
}
}
}
impl HomologicalBatchStreamLimits {
pub(super) fn batch(self) -> HomologicalBatchLimits {
HomologicalBatchLimits {
max_pairs: usize::try_from(self.max_pairs).unwrap_or(usize::MAX),
max_ext_cells: usize::try_from(self.max_ext_cells).unwrap_or(usize::MAX),
}
}
pub(crate) fn capacity(self, degree_steps: usize) -> u64 {
self.max_live_sources
.min(self.max_pairs)
.min(self.max_ext_cells / degree_steps as u64)
}
}
#[derive(Clone, Debug)]
pub enum HomologicalBatchStreamError {
InvalidMaxLiveSources,
DegreeOverflow { degree: usize },
NoPairCapacity {
max_pairs: u64,
max_ext_cells: u64,
degree_steps: usize,
},
DifferentAlgebra { source: usize },
Batch(HomologicalBatchError),
MissingResolution { source: usize },
IndexOverflow { source: usize },
WorkOverflow,
}
display_error! { error HomologicalBatchStreamError {
Self::InvalidMaxLiveSources => "max_live_sources must be positive";
Self::DegreeOverflow { degree } => "Ext degree {degree} has no representable successor";
Self::NoPairCapacity { max_pairs, max_ext_cells, degree_steps } => "stream limits cannot fit one source: max_pairs {max_pairs}, max_ext_cells {max_ext_cells}, degree steps {degree_steps}";
Self::DifferentAlgebra { source } => "source {source} does not share the stream algebra";
Self::Batch(error) => "self-pair chunk failed: {error}";
Self::MissingResolution { source } => "self-pair chunk has no resolution for source {source}";
Self::IndexOverflow { source } => "global source index {source} has no representable successor";
Self::WorkOverflow => "stream work counters overflowed";
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HomologicalBatchStreamCutReason {
Cancelled,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HomologicalBatchStreamRow {
pub(super) source: usize,
pub(super) target: usize,
pub(super) hom_dim: usize,
pub(super) stable_hom_dim: usize,
pub(super) ext_dimensions: Vec<usize>,
pub(super) resolution_end: ResolutionEnd,
}
impl HomologicalBatchStreamRow {
pub(crate) fn from_parts(
source: usize,
target: usize,
hom_dim: usize,
stable_hom_dim: usize,
ext_dimensions: Vec<usize>,
resolution_end: ResolutionEnd,
) -> Self {
Self {
source,
target,
hom_dim,
stable_hom_dim,
ext_dimensions,
resolution_end,
}
}
accessor_methods! {
pub source() -> usize = |this| this.source;
pub target() -> usize = |this| this.target;
pub hom_dim() -> usize = |this| this.hom_dim;
pub stable_hom_dim() -> usize = |this| this.stable_hom_dim;
pub ext_dimensions() -> &[usize] = |this| &this.ext_dimensions;
pub resolution_end() -> ResolutionEnd = |this| this.resolution_end;
}
}
#[derive(Clone)]
pub struct HomologicalBatchStreamChunk {
pub(super) first_source: usize,
pub(super) batch: HomologicalBatch,
pub(super) rows: Vec<HomologicalBatchStreamRow>,
pub(super) work: crate::batch::HomologicalBatchWork,
}
impl HomologicalBatchStreamChunk {
accessor_methods! {
pub first_source() -> usize = |this| this.first_source;
pub rows() -> &[HomologicalBatchStreamRow] = |this| &this.rows;
pub work() -> crate::batch::HomologicalBatchWork = |this| this.work;
pub batch() -> &HomologicalBatch = |this| &this.batch;
}
pub fn source_count(&self) -> usize {
self.batch.modules().len()
}
pub fn resolution(&self, row: usize) -> Option<&ProjectiveResolution> {
self.batch.resolution(row)
}
pub fn verify(&self) -> bool {
self.batch.verify()
&& self.rows.len() == self.batch.pairs().len()
&& self.rows.iter().enumerate().all(|(local, row)| {
let Some(pair) = self.batch.pairs().get(local) else {
return false;
};
let Some(resolution) = self.batch.resolution(local) else {
return false;
};
let Some(global) = self.first_source.checked_add(local) else {
return false;
};
row.source == global
&& row.target == global
&& row.hom_dim == pair.hom_dim()
&& row.stable_hom_dim == pair.stable_hom_dim()
&& row.ext_dimensions == pair.ext_dimensions()
&& row.resolution_end == resolution.end
})
}
}
debug_fields! { HomologicalBatchStreamChunk |this| {
"first_source" => this.first_source;
"sources" => this.source_count();
"rows" => this.rows.len();
"work" => this.work;
} }
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct HomologicalBatchStreamWork {
pub chunks: u64,
pub sources: u64,
pub resolutions: u64,
pub target_covers: u64,
pub hom_spaces: u64,
pub projective_factor_spaces: u64,
pub ext_tables: u64,
pub peak_live_sources: u64,
}
impl HomologicalBatchStreamWork {
pub(crate) fn add_chunk(
&mut self,
work: crate::batch::HomologicalBatchWork,
sources: usize,
) -> Result<(), HomologicalBatchStreamError> {
let add = |total: &mut u64, value: usize| {
*total = total
.checked_add(value as u64)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
Ok(())
};
add(&mut self.chunks, 1)?;
add(&mut self.sources, sources)?;
add(&mut self.resolutions, work.resolutions)?;
add(&mut self.target_covers, work.target_covers)?;
add(&mut self.hom_spaces, work.hom_spaces)?;
add(
&mut self.projective_factor_spaces,
work.projective_factor_spaces,
)?;
add(&mut self.ext_tables, work.ext_tables)?;
self.peak_live_sources = self.peak_live_sources.max(sources as u64);
Ok(())
}
pub(crate) fn primitive_units(self) -> Option<u64> {
[
self.resolutions,
self.target_covers,
self.hom_spaces,
self.projective_factor_spaces,
self.ext_tables,
]
.into_iter()
.try_fold(0u64, u64::checked_add)
}
}
#[derive(Clone, Debug)]
pub enum HomologicalBatchStreamStatus {
Active,
Complete,
Cut(HomologicalBatchStreamCutReason),
Failed {
source: usize,
error: HomologicalBatchStreamError,
},
}
#[derive(Debug)]
pub enum HomologicalBatchStreamStep {
Chunk(HomologicalBatchStreamChunk),
Complete {
next_source: usize,
work: HomologicalBatchStreamWork,
},
Cut {
next_source: usize,
reason: HomologicalBatchStreamCutReason,
work: HomologicalBatchStreamWork,
},
Failed {
source: usize,
error: HomologicalBatchStreamError,
work: HomologicalBatchStreamWork,
},
}
impl HomologicalBatchStreamStep {
pub fn status(&self) -> HomologicalBatchStreamStatus {
match self {
Self::Chunk(_) => HomologicalBatchStreamStatus::Active,
Self::Complete { .. } => HomologicalBatchStreamStatus::Complete,
Self::Cut { reason, .. } => HomologicalBatchStreamStatus::Cut(*reason),
Self::Failed { source, error, .. } => HomologicalBatchStreamStatus::Failed {
source: *source,
error: error.clone(),
},
}
}
}