Expand description
§Wide - Cross Architecture SIMD
This crate attempts to provide (mostly) Miri-compatible, cross-platform SIMD with support for light-weight architecture dispatching.
§Traits
SIMD vectors are weird types as they behave both like scalars and containers. Primary
traits exposed by wide are:
-
SIMDVector: General trait for working with a SIMD vector, including creation and data access. -
SIMDMask: Basically a SIMD boolean. Comparisons betweenSIMDVectorsare done lanewise, with the mask containing the results for each lane. EachSIMDVectorhas an associated mask. -
Architecture: SIMD instructions are architecture specific. Some server CPUs like new(ish) x86 models support AVX512, while most consumer CPUs do not yet support that instruction set extension.To allow compilation of single binaries that support multiple architectures,
widehas taken the position that theArchitectureis largely explicit when it comes to SIMD types.Generic, cross-architecture algorithms are still supported by using an
Architectures associated SIMD types.
A host of secondary SIMD related traits are also exported, all prefixed with SIMD.
Refer to the documentation on each trait for more information.
§Structs
Types implementing SIMDMask can take a variety of architecture specific shapes.
To that end, each architecture-specific SIMDMask is associated with a BitMask,
where bit i is set to 1 if the corresponding lane in the full mask representation
evaluates to a logic true, and 0 otherwise.
Masks can be converted to and from their corresponding BitMask as needed.
§Safety
One source of unsafety in SIMD is the accidental use of an intrinsic that is not supported
by the current runtime CPU. This is made safe in wide by using the following strategy:
-
Each
SIMDVectorandSIMDMasktype is uniquely associated with anArchitecture. -
Construction of a new
SIMDVectororSIMDMaskrequires either an instance of its associated architecture, or aSIMDVector/SIMDMaskof the sameArchitecture. -
Architectureinstances can only be obtained:- From an instance of a
SIMDVector/SIMDMaskassociated with thatArchitecture. - From one of the safe constructors like
arch::dispatchornew_checkedwhich perform runtime checks necessary to ensure the compatibility. - Through an
unsafeconstructor, on which case all bets are off.
- From an instance of a
So an Architecture is needed to bootstrap the use of SIMD, but from then on, the
existence of SIMD types for a given Architecture serve as proof-of-safety.
§Special Architectures
Some Architectures are special and always available to use safely:
-
arch::Scalar: An architecture that uses emulation via loops to implement SIMD-like operations. This architecture is safe because no special hardware intrinsics are invoked. -
arch::Current: TheArchitecturethat is the closest fit to the current compilation target. This is not alwaysarch::Scalar. For example, if compiling forx86-64-v3, then thearch::Currentwill bearch::x86_64::V3. This is safe because it only uses intrinsics that are already available for the compiler to use.The current architecture can be obtained using with
arch::current()or the constantcrate::ARCH.
§Dev Docs
§Adding a new TxN vector type.
-
Implement the type for the backends in
arch(you can usually follow and slightly modify the existing examples). -
Implement for
Emulatedfor the implementations that require macro instantiation. -
Add the type to the
Architecturetrait.
At each step, be sure to include tests, which should be fairly straight forward.
§Adding a New Implementation to an Existing Trait
Basically do steps 2-4 of the above list.
§Adding a New Trait
-
If needed, provide a reference implementation in the
referencemodule. -
If it’s a relatively simple op, adding a new macro in
test_utils/ops.rsthat invokes the reference implementation may be all that’s needed.More complicated operations may require their own test harness (see
test_utils/dot_product.rs).Tests should go through the utilities in
test_utils::driverto ensure adequate coverage and low compile time. -
Implement the trait for the needed types, implementing for
Emulated, architecture-specific types,Architecture.
§Testing and Architectural Levels
By default, wide will only run tests supported by the current runtime hardware. This
allows the tests to pass on a wide variety of machines during development.
However, this can mean that tests targeting architecture not supported by the runtime hardware will silently succeed.
To ensure all tests either run, or generate an error if the runtime hardware does not support a test, set the environment variable
WIDE_TEST_MIN_ARCH="all"Various back-end specific values are supported. Note that this variable sets the minimum level of tests that are required to run. Tests for higher architecture levels will still be run if supported by the runtime hardware.
§x86_64
x86-64-v4: Target Wide’sarch::x86_64::V4architecture.x86-64-v3: Target Wide’sarch::x86_64::V3architecture.scalar: Target the scalar architecture.
Re-exports§
pub use arch::Architecture;
Modules§
- arch
- Traits and functions supporting multi-architecture applications.
- lifetime
- Tools to pass objects with lifetimes across the function pointer API.
Macros§
- alias
- Convenience aliases for aliasing SIMD types.
Structs§
- BitMask
- A lane-wise mask represented as a bit-mask.
- Const
- Move a const-generic into the type domain to work around issues with the use and compile-time computation involving const-generic parameters.
- Emulated
- An emulated SIMD vector.
- LoHi
- Representation of the low and high halves associated with an implementation of
SplitJoinorcrate::ZipUnzip.
Constants§
- ARCH
- The current architecture that is the closest fit for the current compilation target.
Traits§
- AsSIMD
- Convert
Selfto the SIMD typeT. This is mainly useful when implementing fallback operations throughcrate::Emulatedto restore the original SIMD type. - Constant
- A trait to model compile-time constants.
- FromInt
- Perform a potentially lossy conversion from a raw integer.
- SIMDAbs
- Take the absolute value of each lane.
- SIMD
Cast - Perform a numeric cast on the scalar type.
- SIMD
DotProduct - Optimized dot-product style accumulation.
- SIMD
Float - A roll-up of traits required for SIMD floating point types.
- SIMD
Mask - A logical mask for SIMD operations.
- SIMD
MinMax - Efficiently retrieve the pairwise minimum or maximum for the two arguments.
- SIMD
MulAdd - Efficiently perform the operation
- SIMD
Partial Eq - A SIMD equivalent of
std::cmp::PartialEq. - SIMD
Partial Ord - A SIMD equivalent of
std::cmp::PartialOrd. - SIMD
Reinterpret - Perform a bit-cast from one SIMD type to another.
- SIMD
Select - A vectorized “if else”.
- SIMD
Signed - SIMD
SumTree - Perform a pairwise reducing sum of all lanes in the vector and return the result as a scalar.
- SIMD
Unsigned - A roll-up of traits required for SIMD integer types.
- SIMD
Vector - A trait representing minimal behavior for a SIMD-like vector.
- Split
Join - Split a type into or join from two halves.
- Supported
Lane Count - Mapping from a number of lanes to the underlying type for a bitmask.
- ZipUnzip
- Element-wise zip and unzip of two half-width SIMD vectors.
Functions§
- cast_
f16_ to_ f32 - Perform a Miri-safe conversion from
f16tof32. - cast_
f32_ to_ f16 - Perform a Miri-safe cast from
f32tof16.