#simd #generic #pulp

pulp-macro

安全的泛型SIMD

2个版本

0.1.1 2023年11月19日
0.1.0 2023年11月19日

#197 in #simd

Download history 1233/week @ 2024-03-14 1156/week @ 2024-03-21 1179/week @ 2024-03-28 1548/week @ 2024-04-04 1178/week @ 2024-04-11 1394/week @ 2024-04-18 1975/week @ 2024-04-25 2337/week @ 2024-05-02 2070/week @ 2024-05-09 1515/week @ 2024-05-16 1068/week @ 2024-05-23 1403/week @ 2024-05-30 2089/week @ 2024-06-06 1870/week @ 2024-06-13 2567/week @ 2024-06-20 2264/week @ 2024-06-27

9,119 个月下载量
用于 pulp

MIT 许可证

10KB
197 代码行

pulp 是SIMD指令的安全抽象,允许您编写一次函数,并根据在运行时检测到的功能调用等效的向量化版本。

Documentation Crate

自动向量化示例

use pulp::Arch;

let mut v = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
let arch = Arch::new();

arch.dispatch(|| {
    for x in &mut v {
        *x *= 2.0;
    }
});

for (i, x) in v.into_iter().enumerate() {
    assert_eq!(x, 2.0 * i as f64);
}

手动向量化示例

use pulp::{Arch, Simd, WithSimd};

struct TimesThree<'a>(&'a mut [f64]);
impl<'a> WithSimd for TimesThree<'a> {
    type Output = ();

    #[inline(always)]
    fn with_simd<S: Simd>(self, simd: S) -> Self::Output {
        let v = self.0;
        let (head, tail) = S::f64s_as_mut_simd(v);

        let three = simd.f64s_splat(3.0);
        for x in head {
            *x = simd.f64s_mul(three, *x);
        }

        for x in tail {
            *x = *x * 3.0;
        }
    }
}

let mut v = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
let arch = Arch::new();

arch.dispatch(TimesThree(&mut v));

for (i, x) in v.into_iter().enumerate() {
    assert_eq!(x, 3.0 * i as f64);
}

使用 pulp::with_simd 减少模板代码

仅当启用 功能时可用。

需要第一个非生命周期泛型参数以及函数的第一个输入参数是SIMD类型。

#[pulp::with_simd(sum = pulp::Arch::new())]
#[inline(always)]
fn sum_with_simd<'a, S: Simd>(simd: S, v: &'a mut [f64]) {
    let (head, tail) = S::f64s_as_mut_simd(v);
    let three = simd.f64s_splat(3.0);
    for x in head {
        *x = simd.f64s_mul(three, *x);
    }
    for x in tail {
        *x = *x * 3.0;
    }
}

let mut v = (0..1000).map(|i| i as f64).collect::<Vec<_>>();
sum(&mut v);

for (i, x) in v.into_iter().enumerate() {
    assert_eq!(x, 3.0 * i as f64);
}

依赖项

~280–730KB
~17K SLoC