Skip to content

Commit 483e5a5

Browse files
committed
simplify apc tracegen
1 parent 3b751da commit 483e5a5

File tree

1 file changed

+73
-89
lines changed
  • openvm/src/powdr_extension/trace_generator/cpu

1 file changed

+73
-89
lines changed

openvm/src/powdr_extension/trace_generator/cpu/mod.rs

Lines changed: 73 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -175,107 +175,91 @@ impl PowdrTraceGeneratorCpu {
175175
let height = next_power_of_two_or_zero(num_apc_calls);
176176
let mut values = <BabyBear as FieldAlgebra>::zero_vec(height * width);
177177

178-
// get an iterator over the apc rows. This is what we write to
179-
let mut apc_rows = values
180-
// a record is `width` values
181-
.chunks_mut(width);
182-
183178
let mut rejected: HashMap<String, Vec<usize>> = dummy_trace_by_air_name
184179
.keys()
185180
.cloned()
186181
.map(|key| (key, vec![]))
187182
.collect();
188183

189-
let mut row_slice = apc_rows.next().unwrap();
190-
191-
for (dummy_values, is_last) in dummy_values
192-
.iter()
193-
.enumerate()
194-
.map(|(index, dummy_values)| (dummy_values, index == dummy_values.len() - 1))
195-
{
196-
// map the dummy rows to the autoprecompile row
197-
198-
use powdr_autoprecompiles::expression::MappingRowEvaluator;
199-
for (dummy_row, dummy_trace_index_to_apc_index) in dummy_values
200-
.iter()
201-
.map(|r| &r.data[r.start()..r.start() + r.length])
202-
.zip_eq(&dummy_trace_index_to_apc_index_by_instruction)
203-
{
204-
for (dummy_trace_index, apc_index) in dummy_trace_index_to_apc_index {
205-
row_slice[*apc_index] = dummy_row[*dummy_trace_index];
206-
}
207-
}
208-
209-
// Fill in the columns we have to compute from other columns
210-
// (these are either new columns or for example the "is_valid" column).
211-
for (column, computation_method) in columns_to_compute {
212-
let col_index = apc_poly_id_to_index[&column.id];
213-
row_slice[col_index] = match computation_method {
214-
ComputationMethod::Constant(c) => *c,
215-
ComputationMethod::InverseOrZero(expr) => {
216-
use powdr_number::ExpressionConvertible;
217-
218-
let expr_val = expr.to_expression(&|n| *n, &|column_ref| {
219-
row_slice[apc_poly_id_to_index[&column_ref.id]]
220-
});
221-
if expr_val.is_zero() {
222-
BabyBear::ZERO
223-
} else {
224-
expr_val.inverse()
225-
}
226-
}
227-
};
228-
}
229-
230-
let evaluator = MappingRowEvaluator::new(row_slice, &apc_poly_id_to_index);
231-
232-
// check the constraints and bus interactions
233-
let row_is_valid = true;
234-
235-
if row_is_valid {
236-
// replay the side effects of this row on the main periphery
237-
self.apc
238-
.machine()
239-
.bus_interactions
184+
// go through the final table and fill in the values
185+
values
186+
// a record is `width` values
187+
// TODO: optimize by parallelizing on chunks of rows, currently fails because `dyn AnyChip<MatrixRecordArena<Val<SC>>>` is not `Send`
188+
.chunks_mut(width)
189+
.zip(dummy_values)
190+
.for_each(|(row_slice, dummy_values)| {
191+
// map the dummy rows to the autoprecompile row
192+
193+
use powdr_autoprecompiles::expression::MappingRowEvaluator;
194+
for (dummy_row, dummy_trace_index_to_apc_index) in dummy_values
240195
.iter()
241-
.for_each(|interaction| {
242-
use powdr_autoprecompiles::expression::{
243-
AlgebraicEvaluator, ConcreteBusInteraction,
244-
};
245-
246-
let ConcreteBusInteraction { id, mult, args } =
247-
evaluator.eval_bus_interaction(interaction);
248-
self.periphery.real.apply(
249-
id as u16,
250-
mult.as_canonical_u32(),
251-
args.map(|arg| arg.as_canonical_u32()),
252-
&self.periphery.bus_ids,
253-
);
254-
});
255-
// move to the next row
256-
if !is_last {
257-
row_slice = apc_rows.next().unwrap();
196+
.map(|r| &r.data[r.start()..r.start() + r.length])
197+
.zip_eq(&dummy_trace_index_to_apc_index_by_instruction)
198+
{
199+
for (dummy_trace_index, apc_index) in dummy_trace_index_to_apc_index {
200+
row_slice[*apc_index] = dummy_row[*dummy_trace_index];
201+
}
258202
}
259-
} else {
260-
// for each original row
261-
for original_row_reference in dummy_values {
262-
// TODO replay the side effects of this row on the real periphery
263-
264-
// add the row index to the rejected set
265-
rejected
266-
.get_mut(original_row_reference.air_id)
267-
.unwrap()
268-
.push(original_row_reference.row_index);
203+
204+
// Fill in the columns we have to compute from other columns
205+
// (these are either new columns or for example the "is_valid" column).
206+
for (column, computation_method) in columns_to_compute {
207+
let col_index = apc_poly_id_to_index[&column.id];
208+
row_slice[col_index] = match computation_method {
209+
ComputationMethod::Constant(c) => *c,
210+
ComputationMethod::InverseOrZero(expr) => {
211+
use powdr_number::ExpressionConvertible;
212+
213+
let expr_val = expr.to_expression(&|n| *n, &|column_ref| {
214+
row_slice[apc_poly_id_to_index[&column_ref.id]]
215+
});
216+
if expr_val.is_zero() {
217+
BabyBear::ZERO
218+
} else {
219+
expr_val.inverse()
220+
}
221+
}
222+
};
269223
}
270224

271-
// keep the same row_slice for the next iteration unless we're in the last iteration
272-
if !is_last {
273-
for v in row_slice.iter_mut() {
274-
*v = BabyBear::ZERO;
225+
let evaluator = MappingRowEvaluator::new(row_slice, &apc_poly_id_to_index);
226+
227+
// check the constraints and bus interactions
228+
let row_is_valid = true;
229+
230+
if row_is_valid {
231+
// replay the side effects of this row on the main periphery
232+
self.apc
233+
.machine()
234+
.bus_interactions
235+
.iter()
236+
.for_each(|interaction| {
237+
use powdr_autoprecompiles::expression::{
238+
AlgebraicEvaluator, ConcreteBusInteraction,
239+
};
240+
241+
let ConcreteBusInteraction { id, mult, args } =
242+
evaluator.eval_bus_interaction(interaction);
243+
self.periphery.real.apply(
244+
id as u16,
245+
mult.as_canonical_u32(),
246+
args.map(|arg| arg.as_canonical_u32()),
247+
&self.periphery.bus_ids,
248+
);
249+
});
250+
} else {
251+
// for each original row
252+
for original_row_reference in dummy_values {
253+
// TODO replay the side effects of this row on the real periphery
254+
255+
// add the row index to the rejected set
256+
rejected
257+
.get_mut(original_row_reference.air_id)
258+
.unwrap()
259+
.push(original_row_reference.row_index);
275260
}
276261
}
277-
}
278-
}
262+
});
279263

280264
// merge the rejected indices with the traces
281265
let rejected = rejected

0 commit comments

Comments
 (0)