Skip to content

Commit

Permalink
reduce divisions
Browse files Browse the repository at this point in the history
  • Loading branch information
kali committed Jan 11, 2022
1 parent cbf5420 commit d3d266f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ log = "0.4.14"
num-traits = "0.2.14"
tract-data = { path = "../data" }
paste = "1.0.5"
strength_reduce = "0.2.3"

[build-dependencies]
cc = "1.0.69"
Expand Down
44 changes: 30 additions & 14 deletions linalg/src/frame/pack.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
use std::fmt::Debug;
use std::marker::PhantomData;
use strength_reduce::StrengthReducedUsize;
use tract_data::internal::*;

#[derive(Clone, Debug, Eq, PartialEq, Educe)]
#[educe(Hash)]
#[derive(Clone, Debug, Educe)]
#[educe(Hash, PartialEq, Eq)]
pub struct Packer {
k: usize,
r: usize,
alignment: usize,
end_padding_record: usize,
#[educe(PartialEq(ignore))]
#[educe(Hash(ignore))]
r_reduced: StrengthReducedUsize,
}

impl Packer {
pub fn new(k: usize, nr: usize, alignment: usize, end_padding_record: usize) -> Packer {
Packer { k, r: nr, alignment, end_padding_record }
}

pub fn k(&self) -> usize {
self.k
}

pub fn new(k: usize, nr: usize, alignment: usize, end_padding_record: usize) -> Packer {
Packer { k, r: nr, alignment, end_padding_record, r_reduced: StrengthReducedUsize::new(nr) }
}

pub fn alignment(&self) -> usize {
self.alignment
}
Expand Down Expand Up @@ -99,15 +103,15 @@ impl Packer {
pb: &'p mut [T],
mn: usize,
) -> KOutWriter<'p, T> {
KOutWriter::new(pb, self.r, mn, self.k)
KOutWriter::new(pb.as_mut_ptr(), self.r, self.r_reduced, mn, self.k)
}

pub fn write_with_k_inner<'p, T: Copy + Debug>(
&self,
pb: &'p mut [T],
mn: usize,
) -> KInWriter<'p, T> {
KInWriter::new(pb, self.r, mn, self.k)
KInWriter::new(pb.as_mut_ptr(), self.r, self.r_reduced, mn, self.k)
}
}

Expand All @@ -131,11 +135,17 @@ impl<'p, T> KOutWriter<'p, T>
where
T: Copy + std::fmt::Debug,
{
pub fn new(data: &'p mut [T], panel_width: usize, mn: usize, k: usize) -> KOutWriter<'p, T> {
let panels = (mn + panel_width - 1) / panel_width;
pub fn new(
ptr: *mut T,
panel_width: usize,
panel_width_reduced: StrengthReducedUsize,
mn: usize,
k: usize,
) -> KOutWriter<'p, T> {
let panels = (mn + panel_width - 1) / panel_width_reduced;
let last_panel_width = mn - (panels - 1) * panel_width;
KOutWriter {
ptr: data.as_mut_ptr(),
ptr,
panels,
panel_width,
last_panel_width,
Expand Down Expand Up @@ -194,11 +204,17 @@ impl<'p, T> KInWriter<'p, T>
where
T: Copy + Debug,
{
pub fn new(data: &'p mut [T], panel_width: usize, mn: usize, k: usize) -> KInWriter<'p, T> {
let panels = (mn + panel_width - 1) / panel_width;
pub fn new(
ptr: *mut T,
panel_width: usize,
panel_width_reduced: StrengthReducedUsize,
mn: usize,
k: usize,
) -> KInWriter<'p, T> {
let panels = (mn + panel_width - 1) / panel_width_reduced;
let last_panel_width = mn - (panels - 1) * panel_width;
KInWriter {
ptr: data.as_mut_ptr(),
ptr,
k,
panels,
panel_width,
Expand Down

0 comments on commit d3d266f

Please sign in to comment.