Skip to content

Commit

Permalink
10096: Remove initialization for non-derived polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Nov 21, 2024
1 parent 698cd3d commit 34878d6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
15 changes: 8 additions & 7 deletions barretenberg/cpp/src/barretenberg/polynomials/polynomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,11 @@ void Polynomial<Fr>::allocate_backing_memory(size_t size, size_t virtual_size, s
*
* @param size The size of the polynomial.
*/
template <typename Fr>
Polynomial<Fr>::Polynomial(size_t size, size_t virtual_size, size_t start_index, bool disable_parallelisation)
template <typename Fr> Polynomial<Fr>::Polynomial(size_t size, size_t virtual_size, size_t start_index)
{
PROFILE_THIS_NAME("polynomial allocation with zeroing");

allocate_backing_memory(size, virtual_size, start_index);
if (disable_parallelisation) {
// In AVM polynomials are small and already constructed in parallel
memset(static_cast<void*>(coefficients_.backing_memory_.get()), 0, sizeof(Fr) * size);
return;
}

size_t num_threads = calculate_num_threads(size);
size_t range_per_thread = size / num_threads;
Expand Down Expand Up @@ -293,6 +287,13 @@ template <typename Fr> Polynomial<Fr>& Polynomial<Fr>::operator*=(const Fr scali
return *this;
}

template <typename Fr> Polynomial<Fr> Polynomial<Fr>::create_non_parallel_zero_init(size_t size, size_t virtual_size)
{
Polynomial p(size, virtual_size, Polynomial<Fr>::DontZeroMemory::FLAG);
memset(static_cast<void*>(p.coefficients_.backing_memory_.get()), 0, sizeof(Fr) * size);
return p;
}

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1113): Optimizing based on actual sizes would involve using
// expand, but it is currently unused.
template <typename Fr>
Expand Down
10 changes: 9 additions & 1 deletion barretenberg/cpp/src/barretenberg/polynomials/polynomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <typename Fr> class Polynomial {
using FF = Fr;
enum class DontZeroMemory { FLAG };

Polynomial(size_t size, size_t virtual_size, size_t start_index = 0, bool disable_parallelisation = false);
Polynomial(size_t size, size_t virtual_size, size_t start_index = 0);
// Intended just for plonk, where size == virtual_size always
Polynomial(size_t size)
: Polynomial(size, size){};
Expand Down Expand Up @@ -284,6 +284,14 @@ template <typename Fr> class Polynomial {
return p;
}

/**
* @brief A factory to construct a polynomial where parallel initialization is
* not possible (e.g. AVM code).
*
* @return a polynomial initialized with zero on the range defined by size
*/
static Polynomial create_non_parallel_zero_init(size_t size, size_t virtual_size);

/**
* @brief Expands the polynomial with new start_index and end_index
* The value of the polynomial remains the same, but defined memory region differs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,17 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
bb::parallel_for(num_unshifted, [&](size_t i) {
auto& poly = unshifted[i];
const auto col_idx = polys_to_cols_unshifted_idx[i];
size_t col_size = 0;

// We fully allocate the inverse polynomials. We leave this potential memory optimization for
// later.
if (derived_labels_set.contains(labels[i])) {
col_size = num_rows;
} else {
col_size = col_nonzero_size[col_idx];
}
const bool is_derived = derived_labels_set.contains(labels[i]);

if (poly.is_empty()) {
// Not set above
poly = Polynomial{ /*memory size*/
col_size,
/*largest possible index as virtual size*/ circuit_subgroup_size,
/*start_index=*/0,
/*/*disable parallel initialisation=*/true
};
// Not set above. Non-derived unshifted polynomials are initialized below. The
// non-derived ones do need to be initialized with zeros and are fully allocated (size ==
// num_rows).
poly = is_derived
? Polynomial::create_non_parallel_zero_init(num_rows, circuit_subgroup_size)
: Polynomial{ col_nonzero_size[col_idx],
circuit_subgroup_size,
Polynomial::DontZeroMemory::FLAG };
}
});
}));
Expand Down
20 changes: 6 additions & 14 deletions bb-pilcom/bb-pil-backend/templates/circuit_builder.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,14 @@ namespace bb {
bb::parallel_for(num_unshifted, [&](size_t i) {
auto& poly = unshifted[i];
const auto col_idx = polys_to_cols_unshifted_idx[i];
size_t col_size = 0;

// We fully allocate the inverse polynomials. We leave this potential memory optimization for later.
if (derived_labels_set.contains(labels[i])) {
col_size = num_rows;
} else {
col_size = col_nonzero_size[col_idx];
}
const bool is_derived = derived_labels_set.contains(labels[i]);

if (poly.is_empty()) {
// Not set above
poly = Polynomial{ /*memory size*/ col_size,
/*largest possible index as virtual size*/ circuit_subgroup_size,
/*start_index=*/0,
/*disable parallel initialization=*/true
}; }
// Not set above. Non-derived unshifted polynomials are initialized below. The non-derived ones
// do need to be initialized with zeros and are fully allocated (size == num_rows).
poly = is_derived ? Polynomial::create_non_parallel_zero_init(num_rows, circuit_subgroup_size)
: Polynomial{ col_nonzero_size[col_idx], circuit_subgroup_size, Polynomial::DontZeroMemory::FLAG };
}
});
}));

Expand Down

0 comments on commit 34878d6

Please sign in to comment.