Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
trace_container.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <functional>
7#include <memory>
8#include <shared_mutex>
9#include <span>
10#include <unordered_map>
11#include <utility>
12
19
20namespace bb::avm2::tracegen {
21
22// This container is thread-safe.
23// Contention can only happen when concurrently accessing the same column.
25 public:
27
28 const FF& get(Column col, uint32_t row) const;
29 // Returns a tuple of const references to the values in the specified columns.
30 template <size_t N> auto get_multiple(const std::array<ColumnAndShifts, N>& cols, uint32_t row) const
31 {
32 return [&]<size_t... Is>(std::index_sequence<Is...>) {
35 }
36 // Extended version of get that works with shifted columns. More expensive.
38
39 void set(Column col, uint32_t row, const FF& value);
40 // Bulk setting for a given row.
41 void set(uint32_t row, std::span<const std::pair<Column, FF>> values);
42 // Reserve column size. Useful for precomputed columns.
43 void reserve_column(Column col, size_t size);
44
45 // Visits non-zero values in a column.
46 void visit_column(Column col, const std::function<void(uint32_t, const FF&)>& visitor) const;
47 // Returns the number of rows in a column. That is, the maximum non-zero row index + 1.
49 // Maximum number of rows in any column.
50 uint32_t get_num_rows() const;
51 // Maximum number of rows in any column (ignoring clk which is always 2^21).
53 // Number of columns (without shifts).
54 static constexpr size_t num_columns() { return NUM_COLUMNS_WITHOUT_SHIFTS; }
55
56 // Batch inverts a set of columns.
58
59 // Free column memory.
61
62 private:
63 // We use a mutex per column to allow for concurrent writes.
64 // Observe that therefore concurrent write access to different columns is cheap.
65 struct SparseColumn {
66 std::shared_mutex mutex;
67 int64_t max_row_number = -1; // We use -1 to indicate that the column is empty.
68 bool row_number_dirty; // Needs recalculation.
69 // Future memory optimization notes: we can do the same trick as in Operand.
70 // That is, store a variant with a unique_ptr. However, we should benchmark this.
71 // (see serialization.hpp).
73 };
74 // We store the trace as a sparse matrix.
75 // We use a unique_ptr to allocate the array in the heap vs the stack.
76 // Even if the _content_ of each unordered_map is always heap-allocated, if we have 3k columns
77 // we could unnecessarily put strain on the stack with sizeof(unordered_map) * 3k bytes.
79
81};
82
83} // namespace bb::avm2::tracegen
static constexpr size_t num_columns()
const FF & get(Column col, uint32_t row) const
void reserve_column(Column col, size_t size)
const FF & get_column_or_shift(ColumnAndShifts col, uint32_t row) const
std::unique_ptr< std::array< SparseColumn, NUM_COLUMNS_WITHOUT_SHIFTS > > trace
void invert_columns(std::span< const Column > cols)
auto get_multiple(const std::array< ColumnAndShifts, N > &cols, uint32_t row) const
void visit_column(Column col, const std::function< void(uint32_t, const FF &)> &visitor) const
uint32_t get_column_rows(Column col) const
void set(Column col, uint32_t row, const FF &value)
constexpr auto NUM_COLUMNS_WITHOUT_SHIFTS
Definition columns.hpp:40
ColumnAndShifts
Definition columns.hpp:34
AvmFlavorSettings::FF FF
Definition field.hpp:10
::ankerl::unordered_dense::map< Key, T > unordered_flat_map
Definition map.hpp:15
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
TUPLET_INLINE constexpr auto forward_as_tuple(T &&... a) noexcept
Definition tuplet.hpp:1067