-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89ec9e9
commit fe76a48
Showing
13 changed files
with
229 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,146 @@ | ||
pub mod bit_reverse; | ||
|
||
use std::ops::Index; | ||
|
||
use bytemuck::{cast_slice, cast_slice_mut, Pod, Zeroable}; | ||
use num_traits::Zero; | ||
|
||
use self::bit_reverse::bit_reverse_m31; | ||
use crate::core::fields::m31::BaseField; | ||
use crate::core::fields::{Column, FieldOps}; | ||
use crate::core::utils; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct AVX512Backend; | ||
|
||
// BaseField. | ||
// TODO(spapini): Unite with the M31AVX512 type. | ||
pub const K_ELEMENTS: usize = 16; | ||
|
||
#[repr(align(64))] | ||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] | ||
pub struct PackedBaseField([BaseField; K_ELEMENTS]); | ||
unsafe impl Pod for PackedBaseField {} | ||
unsafe impl Zeroable for PackedBaseField { | ||
fn zeroed() -> Self { | ||
unsafe { core::mem::zeroed() } | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct BaseFieldVec { | ||
pub data: Vec<PackedBaseField>, | ||
length: usize, | ||
} | ||
|
||
impl BaseFieldVec { | ||
pub fn as_slice(&self) -> &[BaseField] { | ||
let data: &[BaseField] = cast_slice(&self.data[..]); | ||
&data[..self.length] | ||
} | ||
pub fn as_mut_slice(&mut self) -> &mut [BaseField] { | ||
let data: &mut [BaseField] = cast_slice_mut(&mut self.data[..]); | ||
&mut data[..self.length] | ||
} | ||
} | ||
|
||
impl FieldOps<BaseField> for AVX512Backend { | ||
type Column = BaseFieldVec; | ||
|
||
fn bit_reverse_column(column: &mut Self::Column) { | ||
// Fallback to cpu bit_reverse. | ||
if column.data.len().ilog2() < bit_reverse::MIN_LOG_SIZE { | ||
utils::bit_reverse(column.as_mut_slice()); | ||
return; | ||
} | ||
bit_reverse_m31(&mut column.data); | ||
} | ||
} | ||
|
||
impl Column<BaseField> for BaseFieldVec { | ||
fn zeros(len: usize) -> Self { | ||
Self { | ||
data: vec![PackedBaseField::default(); len.div_ceil(K_ELEMENTS)], | ||
length: len, | ||
} | ||
} | ||
fn to_vec(&self) -> Vec<BaseField> { | ||
self.data | ||
.iter() | ||
.flat_map(|x| x.0) | ||
.take(self.length) | ||
.collect() | ||
} | ||
fn len(&self) -> usize { | ||
self.length | ||
} | ||
} | ||
|
||
impl Index<usize> for BaseFieldVec { | ||
type Output = BaseField; | ||
fn index(&self, index: usize) -> &Self::Output { | ||
&self.data[index / K_ELEMENTS].0[index % K_ELEMENTS] | ||
} | ||
} | ||
|
||
impl FromIterator<BaseField> for BaseFieldVec { | ||
fn from_iter<I: IntoIterator<Item = BaseField>>(iter: I) -> Self { | ||
let mut chunks = iter.into_iter().array_chunks(); | ||
let mut res: Vec<_> = (&mut chunks).map(PackedBaseField).collect(); | ||
let mut length = res.len() * K_ELEMENTS; | ||
|
||
if let Some(remainder) = chunks.into_remainder() { | ||
if !remainder.is_empty() { | ||
length += remainder.len(); | ||
let pad_len = 16 - remainder.len(); | ||
let last = PackedBaseField( | ||
remainder | ||
.chain(std::iter::repeat(BaseField::zero()).take(pad_len)) | ||
.collect::<Vec<_>>() | ||
.try_into() | ||
.unwrap(), | ||
); | ||
res.push(last); | ||
} | ||
} | ||
|
||
Self { data: res, length } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::core::fields::{Col, Column}; | ||
|
||
type B = AVX512Backend; | ||
|
||
#[test] | ||
fn test_column() { | ||
for i in 0..100 { | ||
let col = Col::<B, BaseField>::from_iter((0..i).map(BaseField::from)); | ||
assert_eq!( | ||
col.to_vec(), | ||
(0..i).map(BaseField::from).collect::<Vec<_>>() | ||
); | ||
for j in 0..i { | ||
assert_eq!(col[j], BaseField::from(j)); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_bit_reverse() { | ||
for i in 1..16 { | ||
let len = 1 << i; | ||
let mut col = Col::<B, BaseField>::from_iter((0..len).map(BaseField::from)); | ||
B::bit_reverse_column(&mut col); | ||
assert_eq!( | ||
col.to_vec(), | ||
(0..len) | ||
.map(|x| BaseField::from(utils::bit_reverse_index(x, i as u32))) | ||
.collect::<Vec<_>>() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.