Skip to content

Commit

Permalink
CompositeBackend: Print maximum degree per machine
Browse files Browse the repository at this point in the history
  • Loading branch information
georgwiese committed Jul 17, 2024
1 parent 4f873e6 commit ba9a076
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ast/src/analyzed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,10 @@ impl<T> Identity<SelectedExpressions<AlgebraicExpression<T>>> {
a => (a, None),
}
}

pub fn degree(&self) -> usize {
self.children().map(|e| e.degree()).max().unwrap_or(0)
}
}

impl<R> Identity<parsed::SelectedExpressions<parsed::Expression<R>>> {
Expand Down Expand Up @@ -1120,6 +1124,22 @@ impl<T> AlgebraicExpression<T> {
}
}
}

/// Returns the degree of the expressions
pub fn degree(&self) -> usize {
match self {
// One for each column
AlgebraicExpression::Reference(_) => 1,
// Multiplying two expressions adds their degrees
AlgebraicExpression::BinaryOperation(AlgebraicBinaryOperation {
op: AlgebraicBinaryOperator::Mul,
left,
right,
}) => left.degree() + right.degree(),
// In all other cases, we take the maximum of the degrees of the children
_ => self.children().map(|e| e.degree()).max().unwrap_or(0),
}
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, JsonSchema)]
Expand Down Expand Up @@ -1337,6 +1357,8 @@ impl Display for PolynomialType {
mod tests {
use powdr_parser_util::SourceRef;

use crate::analyzed::{AlgebraicReference, PolyID, PolynomialType};

use super::{AlgebraicExpression, Analyzed};

#[test]
Expand Down Expand Up @@ -1376,4 +1398,35 @@ mod tests {
assert_eq!(pil.identities, pil_result.identities);
assert_eq!(pil.source_order, pil_result.source_order);
}

#[test]
fn test_degree() {
let column = AlgebraicExpression::<i32>::Reference(AlgebraicReference {
name: "column".to_string(),
poly_id: PolyID {
id: 0,
ptype: PolynomialType::Committed,
},
next: false,
});
let one = AlgebraicExpression::Number(1);

let expr = one.clone() + one.clone() * one.clone();
assert_eq!(expr.degree(), 0);

let expr = column.clone() + one.clone() * one.clone();
assert_eq!(expr.degree(), 1);

let expr = column.clone() + one.clone() * column.clone();
assert_eq!(expr.degree(), 1);

let expr = column.clone() + column.clone() * column.clone();
assert_eq!(expr.degree(), 2);

let expr = column.clone() + column.clone() * (column.clone() + one.clone());
assert_eq!(expr.degree(), 2);

let expr = column.clone() * column.clone() * column.clone();
assert_eq!(expr.degree(), 3);
}
}
7 changes: 7 additions & 0 deletions backend/src/composite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl<F: FieldElement, B: BackendFactory<F>> BackendFactory<F> for CompositeBacke
for (machine_name, pil) in pils.iter() {
let num_witness_columns = pil.committed_polys_in_source_order().len();
let num_fixed_columns = pil.constant_polys_in_source_order().len();
let max_identity_degree = pil
.identities_with_inlined_intermediate_polynomials()
.iter()
.map(|i| i.degree())
.max()
.unwrap_or(0);
let num_identities_by_kind = pil
.identities
.iter()
Expand All @@ -95,6 +101,7 @@ impl<F: FieldElement, B: BackendFactory<F>> BackendFactory<F> for CompositeBacke
log::info!("* {}:", machine_name);
log::info!(" * Number of witness columns: {}", num_witness_columns);
log::info!(" * Number of fixed columns: {}", num_fixed_columns);
log::info!(" * Maximum identity degree: {}", max_identity_degree);
log::info!(" * Number of identities:");
for (kind, count) in num_identities_by_kind {
log::info!(" * {:?}: {}", kind, count);
Expand Down

0 comments on commit ba9a076

Please sign in to comment.