Skip to content

Commit

Permalink
bindings/rust/src/pippenger.rs: parallelize MSM for N<32.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Jul 23, 2024
1 parent 0aa918c commit 1f49aa0
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion bindings/rust/src/pippenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ macro_rules! pippenger_mult_impl {
$add:ident,
$is_inf:ident,
$in_group:ident,
$from_affine:ident,
) => {
pub struct $points {
points: Vec<$point_affine>,
Expand Down Expand Up @@ -139,7 +140,7 @@ macro_rules! pippenger_mult_impl {

let pool = mt::da_pool();
let ncpus = pool.max_count();
if ncpus < 2 || npoints < 32 {
if ncpus < 2 {
let p: [*const $point_affine; 2] = [&self[0], ptr::null()];
let s: [*const u8; 2] = [&scalars[0], ptr::null()];

Expand All @@ -161,6 +162,53 @@ macro_rules! pippenger_mult_impl {
}
}

if npoints < 32 {
let (tx, rx) = channel();
let counter = Arc::new(AtomicUsize::new(0));
let n_workers = core::cmp::min(ncpus, npoints);

for _ in 0..n_workers {
let tx = tx.clone();
let counter = counter.clone();

pool.joined_execute(move || {
let mut acc = <$point>::default();
let mut tmp = <$point>::default();
let mut first = true;

loop {
let work =
counter.fetch_add(1, Ordering::Relaxed);
if work >= npoints {
break;
}

unsafe {
$from_affine(&mut tmp, &self[work]);
let scalar = &scalars[nbytes * work];
if first {
$mult(&mut acc, &tmp, scalar, nbits);
first = false;
} else {
$mult(&mut tmp, &tmp, scalar, nbits);
$add_or_double(&mut acc, &acc, &tmp);
}
}
}

tx.send(acc).expect("disaster");
});
}

let mut ret = rx.recv().expect("disaster");
for _ in 1..n_workers {
let p = rx.recv().expect("disaster");
unsafe { $add_or_double(&mut ret, &ret, &p) };
}

return ret;
}

let (nx, ny, window) =
breakdown(nbits, pippenger_window_size(npoints), ncpus);

Expand Down Expand Up @@ -426,6 +474,7 @@ pippenger_mult_impl!(
blst_p1s_add,
blst_p1_affine_is_inf,
blst_p1_affine_in_g1,
blst_p1_from_affine,
);

pippenger_mult_impl!(
Expand All @@ -444,6 +493,7 @@ pippenger_mult_impl!(
blst_p2s_add,
blst_p2_affine_is_inf,
blst_p2_affine_in_g2,
blst_p2_from_affine,
);

fn num_bits(l: usize) -> usize {
Expand Down

0 comments on commit 1f49aa0

Please sign in to comment.