Kolbe.LitMath 0.5.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package Kolbe.LitMath --version 0.5.7                
NuGet\Install-Package Kolbe.LitMath -Version 0.5.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kolbe.LitMath" Version="0.5.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Kolbe.LitMath --version 0.5.7                
#r "nuget: Kolbe.LitMath, 0.5.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Kolbe.LitMath as a Cake Addin
#addin nuget:?package=Kolbe.LitMath&version=0.5.7

// Install Kolbe.LitMath as a Cake Tool
#tool nuget:?package=Kolbe.LitMath&version=0.5.7                

LitMath

A collection of AVX-256 accelerated mathematical functions for .NET

I rewrote Exp, Log, Sin and a few other useful functions using pure AVX intrinsics, so instead of doing one calculation per core, you can now do 4 doubles or 8 floats per core. I added the Sqrt, ERF function and a Normal Distribution CDF as well. On doubles, the following accuracies apply:

  • Exp and Sqrt run at double precision limits
  • ERF at 1e-13
  • Sin and Cos at 1e-15
  • Tan in $[0,\pi/4]$ at 2e-16
  • ATan at 1e-10 (working on it)

There are examples in the benchmark and tests. But here is one to get you started anyway.

Calculate n $e^x$'s in chunks of 4 and store the result in y.

int n = 40;
Span<double> x = new Span<double>(Enumerable.Range(0 , n).Select(z => (double)z/n).ToArray());
Span<double> y = new Span<double>(new double[n]);
Lit.Exp(ref x, ref y);

Parallel Processing

LitMath leverages SIMD for instruction level parallelism, but not compute cores. For array sizes large enough, it would be a really good idea to do multicore processing. There's an example called LitExpDoubleParallel in the ExpBenchmark.cs file to see one way to go about this.

Non-Math Things

Making a library like this involves reinventing the wheel so to speak on very basic concepts. The Util class includes methods like Max and Min and IfElse, which are key to many programming problems in AVX programming, because it needs to be branch-free.

FAQ

Why does this exist?

The reasons I hear most for why this library is pointless is that the Intel MKL can do everything here better than I or C# can, and that if you want such extreme optimization, you shouldn't be using C# to begin with. I wholeheartedly disagree with both. C# is a great language with increasingly great compilers, and the performance I've been getting in this library is close to what you'd find in the MKL. Choosing C# to do some serious back end math with is a totally fine choice, and if you do math, then you might care about performance, or about cloud computing fees. And for these reasons, optimization to its fullest extent can matter.

The MKL is great. But marshaling objects out of C# into C in order to use the MKL is not great. I have benchmarks that compare the exp function running on an array in LitMath and the MKL, and for <2000, LitMath wins on my Zen 3 processor. And it wins by a lot (10x) when you're talking about 256 bit sized arrays. This is the most interesting thing to me. Because with LitMath you can chain the Vector256 interfaced functions together to make whatever ComplexFunction(Vector256 x) you would like, then instead of thrashing your cache by running each n sized array through the MKL over and over to get to your complex function, simply run the full function over each x to get y. That is, the MKL would require you to have intermediate results for each basic function run, and these intermediate results would be run over the entire array each time. But by making your entire function a single Vector256 to Vector256, you only run though the array once. The ERF is a good example of this.

Why do you have such erratic deployments?

This library is an essential part of the code base I use at my job. When I need a new function, I add/deploy as I go, and sometimes that involves several deployments within a few hours.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.7.0 552 11/16/2023
0.6.4 150 10/13/2023
0.6.3 361 1/5/2023
0.6.2 305 1/3/2023
0.6.1 310 1/3/2023
0.6.0 294 12/8/2022
0.5.8 329 12/4/2022
0.5.7 334 11/27/2022
0.5.6 313 11/26/2022
0.5.5 330 11/26/2022
0.5.4 345 11/26/2022
0.5.3 341 11/26/2022
0.5.2 345 11/22/2022
0.5.1 320 11/22/2022
0.5.0 412 9/24/2022
0.4.1 430 9/21/2022
0.4.0 435 9/21/2022
0.3.8 443 9/15/2022
0.3.7 464 9/14/2022
0.3.6 462 9/14/2022
0.3.5 468 9/14/2022
0.3.4 428 7/22/2022
0.3.3 432 7/21/2022
0.3.2 428 7/20/2022
0.3.1 454 7/20/2022
0.3.0 458 7/18/2022
0.2.1 458 7/9/2022
0.2.0 468 6/5/2022
0.1.2 440 6/3/2022
0.1.1 406 6/2/2022
0.1.0 441 6/2/2022