Make the most of your CPU — use the potential of SIMD architecture.

Ilia Gyrdymov
2 min readJun 30, 2022

--

Hi everyone!

I’m Ilia, and I’m a frontend developer loving the Dart programming language.

Once, I found a great possibility to improve the Dart code's performance drastically. There is a beautiful library in the SDK — typed_data. The library contains a bunch of classes based on the “Single Instructure Multiple Data” architecture.

But what is this architecture?

Say you have two collections of numbers and a goal to sum them up element by element. Ok, let’s do it in a regular iterative way:

The last instruction prints 6.0, 8.0, 10.0, 12.0 .

Elements of the collections sum up one by one in four subsequent operations:

Is there a possibility of improving the performance of the code? With Dart, Yes!

To fasten the code, we may utilize Float32x4 type from the typed_data library:

It took us just one operation to perform the summation, instead of four like in the previous example:

There is a separate lane for every pair of numbers. In total, we have four lanes. The summation of the lanes is done in parallel. This is possible because of a special usage of the CPU 128-bit register for a Float32x4 number representation: the register is divided into four parts, and each part acts as a separate 32-bit unit.

Potentially, this computation approach can give us x4 boost!

SIMD is a great example of “true” parallelism: there is no concurrency in such a paradigm.

In the same manner, we may perform addition, multiplication, division, and subtraction not just with Float32x4 objects but with collections of Float32x4 objects. The library typed_data has Float32x4List data type which makes it possible to perform the mentioned operations:

One may ask: “Why do we need a special list? Why not a regular List?”. The answer is that Float32x4List is way more optimized for iterating through a collection of Float32x4 objects.

Probably, you don’t need to utilize the GPU for your applications every time, the CPU can also be powerful enough.

As you may notice, SIMD in Dart gives us a lot of opportunities for developing great things. For instance, I developed a library ml_linalg which is based on SIMD. The library allows making really effective computations for linear algebra.

Another great example is the vector_math library by guys from Google.

So, don’t hesitate. Just use SIMD (where it’s applicable, of course) and speed up your code :)

--

--

Ilia Gyrdymov

Frontend engineer (Dart, Vue/Vuex/Nuxt, React/Redux + Typescript) with an interest in Machine Learning, living in Cyprus