Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix matrix balancing #33

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/ahp/constraint_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ fn to_matrix_helper<F: Field>(
new_matrix
}

fn balance_matrices<F: Field>(a_matrix: &mut Matrix<F>, b_matrix: &mut Matrix<F>) {
let mut a_density: usize = a_matrix.iter().map(|row| row.len()).sum();
let mut b_density: usize = b_matrix.iter().map(|row| row.len()).sum();
let mut max_density = std::cmp::max(a_density, b_density);
let mut a_is_denser = a_density == max_density;
// TODO: write a test to check that `balance_matrices` improves the balancing of the matrices
// A and B by distributing the non-zero elements (more or less) evenly between the two.
fn balance_matrices<F: Field>(a_matrix: &mut Vec<Vec<(F, VarIndex)>>, b_matrix: &mut Vec<Vec<(F, VarIndex)>>) {
let mut a_weight: usize = a_matrix.iter().map(|row| row.len()).sum();
let mut b_weight: usize = b_matrix.iter().map(|row| row.len()).sum();
for (a_row, b_row) in a_matrix.iter_mut().zip(b_matrix) {
if a_is_denser {
let a_row_size = a_row.len();
let b_row_size = b_row.len();
let a_row_weight = a_row.len();
let b_row_weight = b_row.len();
if (a_weight < b_weight && a_row_weight < b_row_weight) ||
(a_weight > b_weight && a_row_weight > b_row_weight)
{
std::mem::swap(a_row, b_row);
a_density = a_density - a_row_size + b_row_size;
b_density = b_density - b_row_size + a_row_size;
max_density = std::cmp::max(a_density, b_density);
a_is_denser = a_density == max_density;
a_weight = a_weight - a_row_weight + b_row_weight;
b_weight = b_weight - b_row_weight + a_row_weight;
}
}
}
Expand Down Expand Up @@ -108,10 +108,10 @@ pub(crate) struct IndexerConstraintSystem<F: Field> {

impl<F: Field> IndexerConstraintSystem<F> {
pub(crate) fn process_matrices(&mut self) {
let mut a = to_matrix_helper(&self.a, self.num_input_variables);
let mut b = to_matrix_helper(&self.b, self.num_input_variables);
balance_matrices(&mut self.a, &mut self.b);
let a = to_matrix_helper(&self.a, self.num_input_variables);
let b = to_matrix_helper(&self.b, self.num_input_variables);
let c = to_matrix_helper(&self.c, self.num_input_variables);
balance_matrices(&mut a, &mut b);
self.a_matrix = Some(a);
self.b_matrix = Some(b);
self.c_matrix = Some(c);
Expand Down