use crate::batch::{HomologicalBatch, HomologicalBatchError, HomologicalBatchLimits};
use crate::resolution::{ProjectiveResolution, ResolutionEnd};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HomologicalBatchStreamLimits {
pub max_live_sources: usize,
pub max_pairs: usize,
pub max_ext_cells: usize,
}
impl Default for HomologicalBatchStreamLimits {
fn default() -> Self {
let batch = HomologicalBatchLimits::default();
Self {
max_live_sources: 1,
max_pairs: batch.max_pairs,
max_ext_cells: batch.max_ext_cells,
}
}
}
impl HomologicalBatchStreamLimits {
pub(super) fn batch(self) -> HomologicalBatchLimits {
HomologicalBatchLimits {
max_pairs: self.max_pairs,
max_ext_cells: self.max_ext_cells,
}
}
}
#[derive(Clone, Debug)]
pub enum HomologicalBatchStreamError {
InvalidMaxLiveSources,
DegreeOverflow { degree: usize },
NoPairCapacity {
max_pairs: usize,
max_ext_cells: usize,
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: usize,
pub sources: usize,
pub resolutions: usize,
pub target_covers: usize,
pub hom_spaces: usize,
pub projective_factor_spaces: usize,
pub ext_tables: usize,
pub peak_live_sources: usize,
}
impl HomologicalBatchStreamWork {
pub(crate) fn add_chunk(
&mut self,
work: crate::batch::HomologicalBatchWork,
sources: usize,
) -> Result<(), HomologicalBatchStreamError> {
self.chunks = self
.chunks
.checked_add(1)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.sources = self
.sources
.checked_add(sources)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.resolutions = self
.resolutions
.checked_add(work.resolutions)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.target_covers = self
.target_covers
.checked_add(work.target_covers)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.hom_spaces = self
.hom_spaces
.checked_add(work.hom_spaces)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.projective_factor_spaces = self
.projective_factor_spaces
.checked_add(work.projective_factor_spaces)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.ext_tables = self
.ext_tables
.checked_add(work.ext_tables)
.ok_or(HomologicalBatchStreamError::WorkOverflow)?;
self.peak_live_sources = self.peak_live_sources.max(sources);
Ok(())
}
}
#[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(),
},
}
}
}