Skip to content

Commit

Permalink
chunked batch inverse
Browse files Browse the repository at this point in the history
  • Loading branch information
ohad-starkware committed Jan 14, 2025
1 parent aa3c697 commit 1dff27d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions crates/prover/src/core/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,22 @@ pub fn batch_inverse_in_place<F: FieldExpOps>(column: &[F], dst: &mut [F]) {
dst[0..WIDTH].clone_from_slice(&tail_inverses);
}

// TODO(Ohad): chunks, parallelize.
pub fn batch_inverse<F: FieldExpOps>(column: &[F]) -> Vec<F> {
let mut dst = vec![unsafe { std::mem::zeroed() }; column.len()];
batch_inverse_in_place(column, &mut dst);
dst
}

// TODO(Ohad): parallelize.
pub fn batch_inverse_chunked<T: FieldExpOps>(column: &[T], chunk_size: usize) -> Vec<T> {
let mut dst = vec![unsafe { std::mem::zeroed() }; column.len()];
let iter = dst.chunks_mut(chunk_size).zip(column.chunks(chunk_size));
iter.for_each(|(dst, column)| {
batch_inverse_in_place(column, dst);
});
dst
}

pub trait Field:
NumAssign
+ Neg<Output = Self>
Expand Down Expand Up @@ -472,8 +481,8 @@ mod tests {
use rand::{Rng, SeedableRng};

use super::batch_inverse_in_place;
use crate::core::fields::batch_inverse;
use crate::core::fields::m31::M31;
use crate::core::fields::{batch_inverse, batch_inverse_chunked};

#[test]
fn test_batch_inverse() {
Expand All @@ -495,4 +504,16 @@ mod tests {

batch_inverse_in_place(&elements, &mut dst);
}

#[test]
fn test_batch_inverse_chunked() {
let mut rng = SmallRng::seed_from_u64(0);
let elements: [M31; 16] = rng.gen();
let chunk_size = 4;
let expected = batch_inverse(&elements);

let result = batch_inverse_chunked(&elements, chunk_size);

assert_eq!(expected, result);
}
}

0 comments on commit 1dff27d

Please sign in to comment.