Skip to content
aka-nse edited this page Aug 28, 2024 · 1 revision

Quickstart

SmartVectorDotNet provides simple vectorizations for your calculation.

To vectorize,

  1. prepare all argument set as ReadOnlySpan<T>
  2. prepare answer destination as Span<T>
  3. use methods in SmartVectorDotNet.Vectorization.SIMD
using SmartVectorDotNet;

var a = new double[]{ 0, 1, 2, 3, 4, 5, 6, 7, };
var b = new double[]{ 1, -1, 1, -1, 1, -1, 1, -1, };
var ans = new double[a.Length];
Vectorization.SIMD.Multiply<double>(a, b, ans);
Console.WriteLine(string.Join(", ", ans));
// 0, -1, 2, -3, 4, -5, 6, -7

Concepts

This library has 3 layers:

  1. ScalarOp / ScalarMath
    provides generalized and backward-compatibility-enhanced operators and Math/MathF functions.

  2. VectorOp / VectorMath
    provides SIMD parallelized APIs which are corresponding with each method in ScalarOp/ScalarMath.

  3. Vectorization
    provides span based sequential operation. Their APIs are declared as strategy pattern, you can select simple loop or SIMD vectorized operation.

The APIs in their layers are defined on consistent rule:

  • if ScalarOp class has T XXXX<T>(T arg1, T arg2, ... , T argN) method, VectorOp class has Vector<T> XXXX<T>(in Vector<T> arg1, in Vector<T> arg2, ... , in Vector<T> argN) method which is simply replaced original calculation with SIMD operation.

  • there is same relation between ScalarMath and VectorMath.

  • if VectorOp or VectorMath class has Vector<T> XXXX<T>(in Vector<T> arg1, in Vector<T> arg2, ... , in Vector<T> argN) method, Vectorization.Emulated and Vectorization.SIMD have void XXXX<T>(ReadOnlySpan<T> arg1, ReadOnlySpan<T> arg2, ... , ReadOnlySpan<T> argN, Span<T> ans) method which is generalized for arbitrary length sequence.

These rules enables you to find required method easily.

Clone this wiki locally