3 unstable releases

0.2.0 Nov 12, 2022
0.1.1 Nov 11, 2022
0.1.0 Nov 11, 2022

#989 in Concurrency

Download history 60/week @ 2024-02-11 1/week @ 2024-02-18 22/week @ 2024-02-25

83 downloads per month

AGPL-3.0-or-later

17KB
354 lines

木綿(momen) is low overhead thread pool library. 木綿(momen) means cotton in Japanese.

Usage

use momen::prelude::*;
fn daxpy(alpha: f64, x: &[f64], y: &mut [f64]) {
    y.iter_mut().zip(x.iter()).for_each(|(y, x)| *y += alpha * *x);
}
let thread_pool = ThreadPoolDyn::new();
let n = thread_pool.max_len();
let mut x = Vec::with_capacity(1000);
let mut y = vec![0f64; 1000];
for i in 0..1000 {
    x.push(i as f64);
}
let chunck_size = (1000 + n - 1) / n;
let alpha = std::f64::consts::PI;
x.chunks(chunck_size)
 .zip(y.chunks_mut(chunck_size))
 .par_for_each_dyn(&|(x, y)| daxpy(alpha, x, y), &thread_pool);
for i in 0..1000 {
    assert_eq!(alpha * x[i], y[i]);
}

Benchmark

  • OS : Ubuntu
  • CPU : Ryzen 9 5950X
  • MEM : DDR4 3600MHz 128GB
  • momen = "0.2.0"
  • rayon = "1.5.3"

lib.rs:

木綿(momen) is low overhead thread pool library.

use momen::prelude::*;
fn daxpy(alpha: f64, x: &[f64], y: &mut [f64]) {
    y.iter_mut().zip(x.iter()).for_each(|(y, x)| *y += alpha * *x);
}
let thread_pool = ThreadPoolDyn::new();
let n = thread_pool.max_len();
let mut x = Vec::with_capacity(1000);
let mut y = vec![0f64; 1000];
for i in 0..1000 {
    x.push(i as f64);
}
let chunck_size = (1000 + n - 1) / n;
let alpha = std::f64::consts::PI;
x.chunks(chunck_size)
 .zip(y.chunks_mut(chunck_size))
 .par_for_each_dyn(&|(x, y)| daxpy(alpha, x, y), &thread_pool);
for i in 0..1000 {
    assert_eq!(alpha * x[i], y[i]);
}

Dependencies

~0.1–11MB
~61K SLoC