Skip to content

Commit

Permalink
Create row struct (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Jul 2, 2024
1 parent ac6499c commit bb44126
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 208 deletions.
2 changes: 1 addition & 1 deletion executor/src/witgen/block_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod tests {

for &(i, name, expected) in asserted_values.iter() {
let poly_id = poly_ids[name];
let actual: T = data[i][&poly_id].value_or_zero();
let actual: T = data[i].value_or_zero(&poly_id);
assert_eq!(actual, T::from(expected));
}
},
Expand Down
29 changes: 26 additions & 3 deletions executor/src/witgen/data_structures/column_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<V, T: PolynomialTypeTrait> ColumnMap<V, T> {
let mut values: Vec<V> = (0..len).map(|_| V::default()).collect();
for (poly, value) in items {
values[poly.id as usize] = value;
assert_eq!(poly.ptype, T::P_TYPE);
debug_assert_eq!(poly.ptype, T::P_TYPE);
}

ColumnMap {
Expand Down Expand Up @@ -90,25 +90,48 @@ impl<V, T: PolynomialTypeTrait> ColumnMap<V, T> {
self.values.iter()
}

pub fn values_into_iter(self) -> impl Iterator<Item = V> {
self.values.into_iter()
}

pub fn values_iter_mut(&mut self) -> impl Iterator<Item = &mut V> {
self.values.iter_mut()
}

pub fn len(&self) -> usize {
self.values.len()
}
}

impl<V, T: PolynomialTypeTrait> Default for ColumnMap<V, T> {
fn default() -> Self {
ColumnMap {
values: Vec::new(),
_ptype: PhantomData,
}
}
}

impl<V: PartialEq, T: PolynomialTypeTrait> PartialEq for ColumnMap<V, T> {
fn eq(&self, other: &Self) -> bool {
self.values == other.values
}
}

impl<V, T: PolynomialTypeTrait> Index<&PolyID> for ColumnMap<V, T> {
type Output = V;

#[inline]
fn index(&self, poly_id: &PolyID) -> &Self::Output {
assert!(poly_id.ptype == T::P_TYPE);
debug_assert!(poly_id.ptype == T::P_TYPE);
&self.values[poly_id.id as usize]
}
}

impl<V, T: PolynomialTypeTrait> IndexMut<&PolyID> for ColumnMap<V, T> {
#[inline]
fn index_mut(&mut self, poly_id: &PolyID) -> &mut Self::Output {
assert!(poly_id.ptype == T::P_TYPE);
debug_assert!(poly_id.ptype == T::P_TYPE);
&mut self.values[poly_id.id as usize]
}
}
6 changes: 3 additions & 3 deletions executor/src/witgen/data_structures/finalizable_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bit_vec::BitVec;
use powdr_ast::analyzed::PolyID;
use powdr_number::FieldElement;

use crate::witgen::rows::{finalize_row, Row};
use crate::witgen::rows::Row;

/// A row entry in [FinalizableData].
#[derive(Clone)]
Expand Down Expand Up @@ -125,8 +125,8 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> {
}

pub fn finalize(&mut self, i: usize) -> bool {
if let Entry::InProgress(row) = &self.data[i] {
self.data[i] = Entry::Finalized(finalize_row(row, &self.column_ids));
if let Entry::InProgress(row) = &mut self.data[i] {
self.data[i] = Entry::Finalized(row.finalize(&self.column_ids));
true
} else {
false
Expand Down
6 changes: 3 additions & 3 deletions executor/src/witgen/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use crate::witgen::data_structures::finalizable_data::FinalizableData;
use crate::witgen::machines::profiling::{record_end, record_start};
use crate::witgen::processor::OuterQuery;
use crate::witgen::rows::merge_row_with;
use crate::witgen::EvalValue;
use crate::Identity;

Expand Down Expand Up @@ -216,7 +215,8 @@ impl<'a, T: FieldElement> Generator<'a, T> {
is_main_run: bool,
) -> ProcessResult<'a, T> {
log::trace!(
"Running main machine from row {row_offset} with the following initial values in the first row:\n{}", first_row.render_values(false, None)
"Running main machine from row {row_offset} with the following initial values in the first row:\n{}",
first_row.render_values(false, None, self.fixed_data)
);
let data = FinalizableData::with_initial_rows_in_progress(
&self.witnesses,
Expand Down Expand Up @@ -244,6 +244,6 @@ impl<'a, T: FieldElement> Generator<'a, T> {
assert_eq!(self.data.len() as DegreeType, self.fixed_data.degree + 1);

let last_row = self.data.pop().unwrap();
merge_row_with(&mut self.data[0], &last_row).unwrap();
self.data[0].merge_with(&last_row).unwrap();
}
}
21 changes: 11 additions & 10 deletions executor/src/witgen/machines/block_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::witgen::block_processor::BlockProcessor;
use crate::witgen::data_structures::finalizable_data::FinalizableData;
use crate::witgen::identity_processor::IdentityProcessor;
use crate::witgen::processor::{OuterQuery, Processor};
use crate::witgen::rows::{merge_row_with, Row, RowIndex, RowPair, UnknownStrategy};
use crate::witgen::rows::{Row, RowIndex, RowPair, UnknownStrategy};
use crate::witgen::sequence_iterator::{
DefaultSequenceIterator, ProcessingSequenceCache, ProcessingSequenceIterator,
};
Expand Down Expand Up @@ -644,15 +644,16 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
// 1. Ignore the first row of the next block:
new_block.pop();
// 2. Merge the last row of the previous block
merge_row_with(
new_block.get_mut(0).unwrap(),
self.get_row(self.last_row_index()),
)
.map_err(|_| {
EvalError::Generic(
"Block machine overwrites existing value with different value!".to_string(),
)
})?;

new_block
.get_mut(0)
.unwrap()
.merge_with(self.get_row(self.last_row_index()))
.map_err(|_| {
EvalError::Generic(
"Block machine overwrites existing value with different value!".to_string(),
)
})?;
// 3. Remove the last row of the previous block from data
self.data.pop();

Expand Down
28 changes: 20 additions & 8 deletions executor/src/witgen/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use powdr_ast::analyzed::PolynomialType;
use powdr_ast::analyzed::{AlgebraicExpression as Expression, AlgebraicReference, PolyID};
use powdr_number::{DegreeType, FieldElement};

use crate::witgen::rows::set_cell_unknown;
use crate::witgen::{query_processor::QueryProcessor, util::try_to_simple_poly, Constraint};
use crate::Identity;

use super::rows::value_is_known;
use super::{
affine_expression::AffineExpression,
data_structures::{
Expand Down Expand Up @@ -225,14 +223,22 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> Processor<'a, 'b, 'c, T,
Known values in current row (local: {row_index}, global {global_row_index}):
{}
",
self.data[row_index].render_values(false, Some(self.witness_cols))
self.data[row_index].render_values(
false,
Some(self.witness_cols),
self.fixed_data,
)
);
if identity.contains_next_ref() {
error += &format!(
"Known values in next row (local: {}, global {}):\n{}\n",
row_index + 1,
global_row_index + 1,
self.data[row_index + 1].render_values(false, Some(self.witness_cols))
self.data[row_index + 1].render_values(
false,
Some(self.witness_cols),
self.fixed_data,
)
);
}
error += &format!(" => Error: {e}");
Expand Down Expand Up @@ -319,7 +325,7 @@ Known values in current row (local: {row_index}, global {global_row_index}):
pub fn set_inputs_if_unset(&mut self, row_index: usize) -> bool {
let mut input_updates = EvalValue::complete(vec![]);
for (poly_id, value) in self.inputs.iter() {
if !value_is_known(&self.data[row_index], poly_id) {
if !self.data[row_index].value_is_known(poly_id) {
input_updates.combine(EvalValue::complete(vec![(
&self.fixed_data.witness_cols[poly_id].poly,
Constraint::Assignment(*value),
Expand All @@ -335,7 +341,7 @@ Known values in current row (local: {row_index}, global {global_row_index}):
self.fixed_data.column_name(poly_id)
);
for row_index in start_row..row_index {
set_cell_unknown(&mut self.data[row_index], poly_id);
self.data[row_index].set_cell_unknown(poly_id);
}
}
}
Expand Down Expand Up @@ -514,8 +520,14 @@ Known values in current row (local: {row_index}, global {global_row_index}):
.process_identity(identity, &row_pair)
.is_err()
{
log::debug!("Previous {:?}", &self.data[row_index - 1]);
log::debug!("Proposed {:?}", proposed_row);
log::debug!(
"Previous {}",
self.data[row_index - 1].render_values(true, None, self.fixed_data)
);
log::debug!(
"Proposed {:?}",
proposed_row.render_values(true, None, self.fixed_data)
);
log::debug!("Failed on identity: {}", identity);

return false;
Expand Down
2 changes: 1 addition & 1 deletion executor/src/witgen/query_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a, 'b, T: FieldElement, QueryCallback: super::QueryCallback<T>>
) -> Option<EvalResult<'a, T>> {
let column = &self.fixed_data.witness_cols[poly_id];

if rows.get_value(&column.poly).is_none() {
if !rows.value_is_known(&column.poly) {
Some(self.process_witness_query(column.query.unwrap(), &column.poly, rows))
} else {
None
Expand Down
Loading

0 comments on commit bb44126

Please sign in to comment.