Skip to content

Commit

Permalink
use subset of columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Jul 3, 2024
1 parent c3742a6 commit c32e179
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
5 changes: 3 additions & 2 deletions executor/src/witgen/block_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> BlockProcessor<'a, 'b, 'c

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use std::{collections::BTreeMap, iter::repeat};

use powdr_ast::analyzed::PolyID;
use powdr_number::{FieldElement, GoldilocksField};
Expand Down Expand Up @@ -163,9 +163,10 @@ mod tests {
let mut machines = [];

let columns = fixed_data.witness_cols.keys().collect();
let basic_row = Row::fresh(&fixed_data, fixed_data.witness_cols.keys());
let data = FinalizableData::with_initial_rows_in_progress(
&columns,
(0..fixed_data.degree).map(|i| Row::fresh(&fixed_data, fixed_data.witness_cols.keys())),
repeat(basic_row).take(fixed_data.degree as usize),
);

let mut mutable_state = MutableState {
Expand Down
10 changes: 10 additions & 0 deletions executor/src/witgen/data_structures/column_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ impl<V, T: PolynomialTypeTrait> ColumnMap<V, T> {
pub fn column_id_range(&self) -> Range<usize> {
self.column_id_range.clone()
}

#[inline]
pub fn get(&self, poly_id: &PolyID) -> Option<&V> {
debug_assert!(poly_id.ptype == T::P_TYPE);
if self.column_id_range.contains(&(poly_id.id as usize)) {
Some(&self.values[poly_id.id as usize - self.column_id_range.start])
} else {
None
}
}
}

impl<V, T: PolynomialTypeTrait> Default for ColumnMap<V, T> {
Expand Down
20 changes: 12 additions & 8 deletions executor/src/witgen/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,20 @@ enum CellValue<T: FieldElement> {
}

impl<T: FieldElement> CellValue<T> {
pub fn is_known(&self) -> bool {
fn is_known(&self) -> bool {
matches!(self, CellValue::Known(_))
}

/// Returns the value if known, otherwise zero.
pub fn unwrap_or_zero(&self) -> T {
fn unwrap_or_zero(&self) -> T {
match self {
CellValue::Known(v) => *v,
_ => Default::default(),
}
}

/// Returns Some(value) if known, otherwise None.
pub fn value(&self) -> Option<T> {
fn value(&self) -> Option<T> {
match self {
CellValue::Known(v) => Some(*v),
_ => None,
Expand All @@ -146,7 +146,7 @@ impl<T: FieldElement> CellValue<T> {
///
/// # Panics
/// Panics if the update is not an improvement.
pub fn apply_update(&mut self, c: &Constraint<T>) {
fn apply_update(&mut self, c: &Constraint<T>) {
match (&self, c) {
(CellValue::Known(_), _) => {
// Note that this is a problem even if the value that was set is the same,
Expand Down Expand Up @@ -186,18 +186,19 @@ pub struct Row<T: FieldElement> {

impl<T: FieldElement> Row<T> {
pub fn value_or_zero(&self, poly_id: &PolyID) -> T {
// This should not return zero for columns outside the set of columns, because they might be known.
self.values[poly_id].unwrap_or_zero()
}

pub fn value(&self, poly_id: &PolyID) -> Option<T> {
self.values[poly_id].value()
self.values.get(poly_id).and_then(|v| v.value())
}

pub fn range_constraint(&self, poly_id: &PolyID) -> Option<RangeConstraint<T>> {
match &self.values[poly_id] {
self.values.get(poly_id).and_then(|v| match v {
CellValue::RangeConstraint(c) => Some(c.clone()),
_ => None,
}
})
}

/// Merges two rows, updating the first.
Expand Down Expand Up @@ -262,8 +263,9 @@ impl<T: FieldElement> Row<T> {
.map(|poly_id| poly_id.id as usize)
.minmax()
.into_option()
.map(|(min, max)| min..(max - 1))
.map(|(min, max)| min..(max + 1))
.unwrap_or_default();
println!("Creating row with columns: {:?}", column_id_range);

let values = WitnessColumnMap::from(
column_id_range.clone(),
Expand Down Expand Up @@ -486,6 +488,7 @@ impl<'row, 'a, T: FieldElement> RowPair<'row, 'a, T> {
pub fn get_value(&self, poly: &AlgebraicReference) -> Option<T> {
let row = self.get_row(poly.next);
if self.unknown_strategy == UnknownStrategy::Zero {
// TODO this should panic if the column is out of range.
Some(row.value_or_zero(&poly.poly_id))
} else {
row.value(&poly.poly_id)
Expand All @@ -507,6 +510,7 @@ impl<'row, 'a, T: FieldElement> RowPair<'row, 'a, T> {

impl<T: FieldElement> WitnessColumnEvaluator<T> for RowPair<'_, '_, T> {
fn value<'b>(&self, poly: &'b AlgebraicReference) -> AffineResult<&'b AlgebraicReference, T> {
// TODO this should not panic if the column is out of range.
Ok(match self.get_value(poly) {
Some(v) => v.into(),
None => AffineExpression::from_variable_id(poly),
Expand Down

0 comments on commit c32e179

Please sign in to comment.