Skip to content

Commit

Permalink
use uninitialized memory for allocation of master quotient table
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Aug 3, 2023
1 parent 13dfb28 commit b172482
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 59 deletions.
77 changes: 40 additions & 37 deletions triton-vm/src/table/extension_table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::Display;
use std::mem::MaybeUninit;

use itertools::Itertools;
use ndarray::parallel::prelude::*;
Expand Down Expand Up @@ -142,7 +143,7 @@ pub trait Quotientable: Evaluable<BFieldElement> {
fn fill_initial_quotients(
master_base_table: ArrayView2<BFieldElement>,
master_ext_table: ArrayView2<XFieldElement>,
quot_table: &mut ArrayViewMut2<XFieldElement>,
quot_table: &mut ArrayViewMut2<MaybeUninit<XFieldElement>>,
zerofier_inverse: ArrayView1<BFieldElement>,
challenges: &Challenges,
) {
Expand All @@ -153,22 +154,22 @@ pub trait Quotientable: Evaluable<BFieldElement> {
.axis_iter_mut(Axis(0))
.into_par_iter()
.enumerate()
.for_each(|(row_index, quot_row)| {
let mut quotient_table_row = Array1::from(Self::evaluate_initial_constraints(
master_base_table.row(row_index),
master_ext_table.row(row_index),
challenges,
));
let z_inv = zerofier_inverse[row_index];
quotient_table_row.mapv_inplace(|a| a * z_inv);
quotient_table_row.move_into(quot_row);
.for_each(|(row_index, quotient_table_row)| {
let base_row = master_base_table.row(row_index);
let ext_row = master_ext_table.row(row_index);
Self::evaluate_initial_constraints(base_row, ext_row, challenges)
.into_iter()
.map(|numerator| numerator * zerofier_inverse[row_index])
.map(MaybeUninit::new)
.collect::<Array1<_>>()
.move_into(quotient_table_row);
});
}

fn fill_consistency_quotients(
master_base_table: ArrayView2<BFieldElement>,
master_ext_table: ArrayView2<XFieldElement>,
quot_table: &mut ArrayViewMut2<XFieldElement>,
quot_table: &mut ArrayViewMut2<MaybeUninit<XFieldElement>>,
zerofier_inverse: ArrayView1<BFieldElement>,
challenges: &Challenges,
) {
Expand All @@ -179,22 +180,22 @@ pub trait Quotientable: Evaluable<BFieldElement> {
.axis_iter_mut(Axis(0))
.into_par_iter()
.enumerate()
.for_each(|(row_index, quot_row)| {
let mut quotient_table_row = Array1::from(Self::evaluate_consistency_constraints(
master_base_table.row(row_index),
master_ext_table.row(row_index),
challenges,
));
let z_inv = zerofier_inverse[row_index];
quotient_table_row.mapv_inplace(|a| a * z_inv);
quotient_table_row.move_into(quot_row);
.for_each(|(row_index, quotient_table_row)| {
let base_row = master_base_table.row(row_index);
let ext_row = master_ext_table.row(row_index);
Self::evaluate_consistency_constraints(base_row, ext_row, challenges)
.into_iter()
.map(|numerator| numerator * zerofier_inverse[row_index])
.map(MaybeUninit::new)
.collect::<Array1<_>>()
.move_into(quotient_table_row);
});
}

fn fill_transition_quotients(
master_base_table: ArrayView2<BFieldElement>,
master_ext_table: ArrayView2<XFieldElement>,
quot_table: &mut ArrayViewMut2<XFieldElement>,
quot_table: &mut ArrayViewMut2<MaybeUninit<XFieldElement>>,
zerofier_inverse: ArrayView1<BFieldElement>,
challenges: &Challenges,
trace_domain: ArithmeticDomain,
Expand All @@ -212,28 +213,30 @@ pub trait Quotientable: Evaluable<BFieldElement> {
.axis_iter_mut(Axis(0))
.into_par_iter()
.enumerate()
.for_each(|(current_row_index, quot_row)| {
.for_each(|(current_row_index, quotient_table_row)| {
// bitwise logical and `domain_length_bit_mask` performs the modulo operation:
// `domain.length - 1` is a bit-mask with all 1s because `domain.length` is 2^k
// for some k.
let next_row_index = (current_row_index + unit_distance) & domain_length_bit_mask;
let mut quotient_table_row = Array1::from(Self::evaluate_transition_constraints(
Self::evaluate_transition_constraints(
master_base_table.row(current_row_index),
master_ext_table.row(current_row_index),
master_base_table.row(next_row_index),
master_ext_table.row(next_row_index),
challenges,
));
let z_inv = zerofier_inverse[current_row_index];
quotient_table_row.mapv_inplace(|a| a * z_inv);
quotient_table_row.move_into(quot_row);
)
.into_iter()
.map(|numerator| numerator * zerofier_inverse[current_row_index])
.map(MaybeUninit::new)
.collect::<Array1<_>>()
.move_into(quotient_table_row);
});
}

fn fill_terminal_quotients(
master_base_table: ArrayView2<BFieldElement>,
master_ext_table: ArrayView2<XFieldElement>,
quot_table: &mut ArrayViewMut2<XFieldElement>,
quot_table: &mut ArrayViewMut2<MaybeUninit<XFieldElement>>,
zerofier_inverse: ArrayView1<BFieldElement>,
challenges: &Challenges,
) {
Expand All @@ -244,15 +247,15 @@ pub trait Quotientable: Evaluable<BFieldElement> {
.axis_iter_mut(Axis(0))
.into_par_iter()
.enumerate()
.for_each(|(row_index, quot_row)| {
let mut quotient_table_row = Array1::from(Self::evaluate_terminal_constraints(
master_base_table.row(row_index),
master_ext_table.row(row_index),
challenges,
));
let z_inv = zerofier_inverse[row_index];
quotient_table_row.mapv_inplace(|a| a * z_inv);
quotient_table_row.move_into(quot_row);
.for_each(|(row_index, quotient_table_row)| {
let base_row = master_base_table.row(row_index);
let ext_row = master_ext_table.row(row_index);
Self::evaluate_terminal_constraints(base_row, ext_row, challenges)
.into_iter()
.map(|numerator| numerator * zerofier_inverse[row_index])
.map(MaybeUninit::new)
.collect::<Array1<_>>()
.move_into(quotient_table_row);
});
}

Expand Down
32 changes: 10 additions & 22 deletions triton-vm/src/table/master_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,31 +945,19 @@ pub fn all_quotients(
);

prof_start!(maybe_profiler, "malloc");
let mut quotient_table = Array2::zeros([quotient_domain.length, num_quotients()]);
let mut quotient_table = Array2::uninit([quotient_domain.length, num_quotients()]);
prof_stop!(maybe_profiler, "malloc");

let init_section_start = 0;
let init_section_end = init_section_start + MasterExtTable::num_initial_quotients();
let cons_section_start = init_section_end;
let cons_section_end = cons_section_start + MasterExtTable::num_consistency_quotients();
let tran_section_start = cons_section_end;
let tran_section_end = tran_section_start + MasterExtTable::num_transition_quotients();
let term_section_start = tran_section_end;
let term_section_end = term_section_start + MasterExtTable::num_terminal_quotients();

let (mut init_quot_table, mut cons_quot_table, mut tran_quot_table, mut term_quot_table) =
quotient_table.multi_slice_mut((
s![.., init_section_start..init_section_end],
s![.., cons_section_start..cons_section_end],
s![.., tran_section_start..tran_section_end],
s![.., term_section_start..term_section_end],
));
let init_section_end = MasterExtTable::num_initial_quotients();
let cons_section_end = init_section_end + MasterExtTable::num_consistency_quotients();
let tran_section_end = cons_section_end + MasterExtTable::num_transition_quotients();
let term_section_end = tran_section_end + MasterExtTable::num_terminal_quotients();

prof_start!(maybe_profiler, "initial");
MasterExtTable::fill_initial_quotients(
quotient_domain_master_base_table,
quotient_domain_master_ext_table,
&mut init_quot_table,
&mut quotient_table.slice_mut(s![.., ..init_section_end]),
initial_quotient_zerofier_inverse(quotient_domain).view(),
challenges,
);
Expand All @@ -979,7 +967,7 @@ pub fn all_quotients(
MasterExtTable::fill_consistency_quotients(
quotient_domain_master_base_table,
quotient_domain_master_ext_table,
&mut cons_quot_table,
&mut quotient_table.slice_mut(s![.., init_section_end..cons_section_end]),
consistency_quotient_zerofier_inverse(trace_domain, quotient_domain).view(),
challenges,
);
Expand All @@ -989,7 +977,7 @@ pub fn all_quotients(
MasterExtTable::fill_transition_quotients(
quotient_domain_master_base_table,
quotient_domain_master_ext_table,
&mut tran_quot_table,
&mut quotient_table.slice_mut(s![.., cons_section_end..tran_section_end]),
transition_quotient_zerofier_inverse(trace_domain, quotient_domain).view(),
challenges,
trace_domain,
Expand All @@ -1001,13 +989,13 @@ pub fn all_quotients(
MasterExtTable::fill_terminal_quotients(
quotient_domain_master_base_table,
quotient_domain_master_ext_table,
&mut term_quot_table,
&mut quotient_table.slice_mut(s![.., tran_section_end..term_section_end]),
terminal_quotient_zerofier_inverse(trace_domain, quotient_domain).view(),
challenges,
);
prof_stop!(maybe_profiler, "terminal");

quotient_table
unsafe { quotient_table.assume_init() }
}

pub fn num_quotients() -> usize {
Expand Down

0 comments on commit b172482

Please sign in to comment.