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

chore(avm): Remove initialization for non-derived polynomials #10103

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A factory! :O ! !

:D

* 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
fcarreiro marked this conversation as resolved.
Show resolved Hide resolved
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
Loading