This month in rustsim #10 (March 2020)

Welcome to the tenth edition of This month in rustsim! This newsletter will provide you with a summary of important update that occurred within the rustsim community. This includes in particular updates about the nphysics (physics engine), salva (fluid simulation), ncollide (for collision-detection), nalgebra (for linear algebra), simba, and alga (for abstract algebra) crates. This tenth edition will contain updates for the month of March 2020.

Join us on discord and on our user forum!

Say hello to Simba!

![simba logo](https://rustsim.org/img/logo_simba_wide_wide.svg)

I am thrilled to announce the release of the new simba crate! Simba is a crate that defines a set of traits for writing code that can be generic with regard to the number of lanes of the input numeric value. Those traits are implemented by f32, u32, i16, bool as well as SIMD types like f32x4, u32x8, i16x2, etc. Here is a diagram showing most of the traits of Simba:

![simba logo](https://rustsim.org/img/simba_trait_hierarchy.svg)

Each solid arrow illustrates trait inheritance, e.g., SimdRealField is a subtrait of SimdSigned. Dashed arrows illustrate blanket impls, e.g., any type implementing RealField also automatically implements SimdRealField. All the Simd* traits (as well as Field) are implemented for SIMD types like f32x8, f64x4 as well as scalar types like f32 and f64. Non-Simd traits on the other hand (except Field) are only implemented for scalar types. by scalar types like f32 and f64 (and the blanket impls make them implement the Simd* traits too). Simba is both much simpler and more easily extensible than our alga crate which has a much deeper and complex trait hierarchy.

One example of use-case applied by the nalgebra crate is to define generic methods like vector normalization that will work for Vector3<f32> as well as Vector3<f32x4>. This makes it easier leverage the power of SIMD Array-of-Struct-of-Array (AoSoA) with less code duplication.

Two optional cargo features can be enabled:

  • With the packed_simd feature enabled, the simba::simd module will export several SIMD types like f32x2, f64x4, i32i8, u16i16, etc. There types are wrappers around the SIMD types from the packed_simd crate. This requires a nightly compiler.
  • With the wide feature enabled, the simba::simd module will export the WideF32x4 and WideBoolF32x4 types. These types are wrapper around the wide::f32x4 type from the wide crate. This will work with both stable and nightly compilers.

If none of those features are enabled, simba will define all the scalar and SIMD traits (and will be compatible with both stable an nightly compilers). However, the SIMD traits won't be implemented for any SIMD types. Therefore library developers writing generic code are not required to enable those features. However, users who will pick concrete SIMD numeric types are recommended to:

  • Enable the packed_simd feature and use the types like simba::simd::{f32x4, i32x2, ...} if they want the most complete platform/numeric type coverage, and can afford to use a nightly compiler.
  • Enable the wide feature and use the simba::simd::{WideF32x4, WideBoolF32x4} if they only need 4-lanes 32-bits floats, and can't afford to use a nightly compiler.

Use of the Simba crate by nalgebra 0.21

The use of Simba in the new version of nalgebra is what makes it possible to leverage the strengths of SIMD AoSoA to obtain great performance boosts:

benchmarknalgebranalgebra_f32x4nalgebra_f32x8nalgebra_f32x16
euler 2d x100009.674 us3.05 us2.224 us2.076 us
euler 3d x1000018.18 us4.791 us2.809 us3.014 us
isometry transform point222.8197 ns7.8197 ns5.6563 ns5.7179 ns
isometry transform point360.0877 ns15.4410 ns10.1237 ns10.5417 ns
isometry2 mul isometry234.5250 ns10.1867 ns9.2351 ns8.1413 ns
isometry3 mul isometry397.8058 ns26.0439 ns16.5287 ns29.0822 ns
matrix2 mul matrix224.7601 ns10.3309 ns9.0379 ns10.6500 ns
matrix2 mul vector222.9934 ns6.7758 ns5.2159 ns5.7680 ns
matrix3 mul matrix383.2946 ns27.7722 ns19.6932 ns70.6877 ns
matrix3 mul vector346.1231 ns13.6117 ns10.7913 ns10.6031 ns
matrix4 mul matrix40.1247 us0.07657 us0.06835 us0.2354 us
matrix4 mul vector430.6785 ns20.7324 ns15.4389 ns39.0102 ns
quaternion mul quaternion30.0095 ns12.3669 ns9.4567 ns9.3504 ns
quaternion mul vector349.3278 ns13.3755 ns7.7526 ns8.5309 ns
vector3 cross26.0655 ns6.3479 ns4.5852 ns4.4663 ns
vector3 dot25.3941 ns6.4285 ns4.5232 ns4.4944 ns
vector3 length20.7343 ns5.4675 ns3.5936 ns3.5110 ns
vector3 normalize61.5877 ns15.4976 ns8.3383 ns7.8013 ns

Refer to the full benchmark for details about those numbers and comparison with other linear algebra crates.

  • Before the version 0.21, nalgebra relied on the traits from the alga crate (e.g. RealField, ComplexField, etc). Those traits have now been replaced by the traits from the simba crate which have similar names. For example the equivalent of alga::general::{RealField, ComplexField} is simba::scalar::{RealField, ComplexField}.
  • In many places, you will see the SimdRealField or SimdComplexField traits which are slightly more general than RealField and ComplexField because they are also implemented for SIMD types like f32x4, f64x2, etc.
  • The dependency to alga is now completely optional. All the implementations of alga traits are still present, though the alga cargo feature must be enabled to get them.

The linalg and sparse modules don't use the SimdRealField and SimdComplexField traits at all. This is because the algorithms they implement are full of branching, and thus are difficult to rewrite in an SIMD AoSoA friendly manner. Finally, note that:

  1. If you are not using any generics with nalgebra, chances are that your code will still compile as-is.
  2. If you are using some generics, chances are that simply replacing all occurrences of alga::general::{RealField, ComplexField} by simba::scalar::{RealField, ComplexField} will likely do the trick.

The status of alga

Starting today, the alga crate switches to passive maintenance mode. Unfortunately, the traits structure of alga is very complicated, and makes it hardly accessible to users without strong knowledge about set theory. Moreover it does not seem to be much used within the community. Thoses are other reasons why nalgebra is now relies on traits from simba instead of alga.

Thanks

We would like to thank the whole community and contributors. In particular, thanks to the contributors from the past month1:

  • Thanks to users reporting spelling mistakes on the documentation. This is always appreciated.
  • Thanks to users joining us on our discord server to provide feedbacks, discuss features, and get assistance!

Finally, thanks to all the former, current and new patrons supporting me, sebcrozet, the lead developer of the current crates part of this organization on patreon or GitHub sponsors! This help is greatly appreciated and allows me do spend a significant amount of time developing those crates.


  1. The list of contributors is automatically generated from the past months' github commit history. Don't hesitate to let us know if your name should have been mentioned here.