Giter VIP home page Giter VIP logo

pulp's Introduction

pulp is a safe abstraction over SIMD instructions, that allows you to write a function once and dispatch to equivalent vectorized versions based on the features detected at runtime.

Documentation Crate

Autovectorization example

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);
}

Manual vectorization example

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);
}

Less boilerplate using pulp::with_simd

Only available with the macro feature.

Requires the first non-lifetime generic parameter, as well as the function's first input parameter to be the SIMD type.

#[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);
}

pulp's People

Contributors

sarah-ek avatar icetdrinker avatar virtualritz avatar edfix avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.