Coder Social home page Coder Social logo

mkl-net / mkl.net Goto Github PK

View Code? Open in Web Editor NEW
161.0 14.0 18.0 1.17 MB

A simple cross platform .NET API for Intel MKL

License: Apache License 2.0

C# 68.18% PowerShell 0.56% F# 29.78% CMake 0.07% C 1.41%
mkl cross-platform matrix linear-algebra differentiation integration math root-finding optimization minimum

mkl.net's Introduction

MKL.NET

build

A simple cross platform .NET API for Intel MKL.

Exposing functions from MKL keeping the syntax as close to the c developer reference as possible.

Reference the MKL.NET package and required runtime packages and use the static MKL functions. The correct native libraries will be included and loaded at runtime.

If you use your own copy of MKL native libraries please make sure that mkl_rt.dll/libmkl_rt.so/libmkl_rt.dylib is on the path. Newer MKL builds include a version number in the library name, so you will have to create a link or rename it. The names are already adjusted in the MKL.NET packages which work out of the box.

MKL.NET MKL.NET
runtimes:
MKL.NET.win-x64 MKL.NET
MKL.NET.win-x86 MKL.NET
MKL.NET.linux-x64 MKL.NET
MKL.NET.linux-x86 MKL.NET
MKL.NET.osx-x64 MKL.NET
libraries:
MKL.NET.Matrix MKL.NET
MKL.NET.Optimization MKL.NET
MKL.NET.Statistics MKL.NET

Rationale

  • Use freely available Intel MKL packages repackaged to work for each runtime.
  • The MKL.NET API is just a thin .NET wrapper around the native API keeping the syntax as close as possible.
  • The project is well defined, with an open design, and no business logic and could benefit from external input.
  • Cross platform testing is easy and free using Github actions.
  • MKL.NET native packages can just be referenced for needed runtimes at library or application level.

MKL.NET.Matrix

  • Performance and memory optimised matrix algebra library.
  • Matrix expressions are optimised to perform intermediate calculations inplace and reuse memory.
  • Operations such as scale, transpose, +, * are combined into single MKL calls.
  • Intermediate matrices are disposed (or reused) automatically.
  • ArrayPool underlying memory model using IDisposable and Finalizers.
  • Uses the Pinned Object Heap for net6.0.
  • All these combined result in it being much faster than other matrix libraries.

The following example only results in one new matrix r (using ArrayPool) without mutating inputs.

public static matrix Example(matrix ma, matrix mb, vector va, vector vb)
{
    using matrix r = 0.5 * Matrix.Abs(1.0 - ma) * mb.T + Math.PI * va.T * Vector.Sin(vb);
    ...
}

Example statistics matrix function:

public static (vector, matrix) MeanAndCovariance(matrix samples, vector weights)
{
    if (samples.Rows != weights.Length) ThrowHelper.ThrowIncorrectDimensionsForOperation();
    var mean = new vector(samples.Cols);
    var cov = new matrix(samples.Cols, samples.Cols);
    var task = Vsl.SSNewTask(samples.Cols, samples.Rows, VslStorage.ROWS, samples.Array, weights.Array);
    ThrowHelper.Check(Vsl.SSEditCovCor(task, mean.Array, cov.Array, VslFormat.FULL, null, VslFormat.FULL));
    ThrowHelper.Check(Vsl.SSCompute(task, VslEstimate.COV, VslMethod.FAST));
    ThrowHelper.Check(Vsl.SSDeleteTask(task));
    return (mean, cov);
}

Note: arrays need to be pinned across all MKL function calls when there are multiple as above as MKL stores native pointers and the arrays could be moved between calls. MKL.NET handles pinning automatically, unpinning when the task is deleted. This is a common seen bug when using MKL directly from .NET which causes occasional crashes.

MKL.NET.Optimization

Simple and high performance optimization and root finding library loosely based on the scipy.optimize API.

The aim is to include the latest algorithms such as Toms748, robustly tested with CsCheck. Full use of MKL.NET will be made to improve performance. Algorithms will be performance tested and default to the best for given inputs.

  • Root - root finding algorithms. Default algorithm has 20% fewer function calls than Brent, Toms748, Newton and Halley. Further details here.
  • Calculus - derivative and integral numeric calculations and check to any precision using Richardson extrapolation.
  • Minimum - minimum finding algorithms in one dimension. Default algorithm has 50% fewer function calls than Brent.
  • Minimum - in N dimensions. Intuative tolerance parameters. Optimised no array allocation in main loop using in place symmetric MKL rank-k, rank-2k functions.
    Should scale well with the number of dimensions. ~ 50-70% fewer function calls than other BFGS algorithms.
  • CurveFit and non-linear LeastSquares - helper functions based on Minimum.
  • Minimum_Global - global minimum algorithm in N dimensions returning a sequence of parallel grid BFGS searches ever reducing the spacing between prior searches. Further details here.

MKL.NET.Statistics

Simple and high performance statistics functions.

  • Summary - Sum, Mean, Median, MAD, Raw/Central/Standard Moments, Quartiles, Quantiles, Covariance, Correlation. All can be weighted.
  • Estimator - Running high performance, low memory estimators for Quantile, Quartiles, Quantiles, Histogram, Central/Standard Moments. Further details here.

mkl.net's People

Contributors

alloncm avatar anthonylloyd avatar atlemann avatar delreluca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mkl.net's Issues

Cant find mkl_malloc

Hi just found this awesome library and I cant find some mkl functions (especially mkl_malloc).
Does this library exposes all mkl functionality?
If so how does I find this function?
And if not can I access the native function and wrap in myself using this library (for the installation of mkl).

Thanks in advance.

Cannot use dfti functions with windows-x64 runtime

Hi, I am having the following issue where one of the DLLs is not being built.

Using MKL.NET with the following versions:

MKL.NET 1.4.0
MKL.NET.Matrix 1.0.0
MKL.NET.win-x64 2022.0.0.115

I've been able to do some matrix and vector operations with no worries, but calling in to the C API does not work.

System.DllNotFoundException: Unable to load DLL 'MKL.NET.Native.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)
   at MKLNET.Dfti.ComputeForwardInplace(Int32 n, Complex[] x_inout)
   at MKLNET.Dfti.ComputeForward(Complex[] x_inout)
   at Filtering.Fft.Resample(Double[] xs, Int32 num)

I have tried building Debug & Release for x64 & any-cpu targets.

I'm sorry if this turns out to be a stupid question, I just can't find any documentation for how to fix it myself.

Thanks.

Size of runtime packages

Hi,

First of all: thank you for your work on this great library! I am currently toying around with it and so far I do really enjoy using it!

I am currently working on some .Net CLI tools which reference the MKL.Net core package as well as the windows and/or linux runtime and I noticed that their size (runtimes) is rather large. This has the effect, that any CLI tool using the linux runtime is >250mb in size and can therefore not be hosted on nuget.org.

Unfortunately, dependencies can not be included into cli tools by reference, but are included upon build, so the only workaround I can come up with is to shrink the size of the referenced runtimes. Do you think it would be possible to do so?

Other than that, I wondered whats the rational behind MKL.NET.linux-x64.b, which is referenced by the linux runtime.

Cheers

Spline

Would it be possible to access to Spline1D in the future ?

Help calling Lapack.gesvd from C#

I am trying to use your package to do SVD on a large matrix. However, I am having trouble getting a simple matrix to execute. Here is the code:

            int m = 3; // Number of rows
            int n = 3; // Number of columns
            double[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Input matrix
            double[] s = new double[Math.Min(m, n)]; // Singular values
            double[] u = new double[m * m]; // Left singular vectors
            double[] vt = new double[n * n]; // Right singular vectors
            double[] superb = new double[Math.Min(m, n) - 1]; // Array for additional information

            int info = MKLNET.Lapack.gesvd( MKLNET.Layout.ColMajor, 'A', 'A', m, n, a, n, s, u, m, vt, n, superb);

When the code executes the gesvd line - the program crashes. No error - just stops running.

Any idea on what I am doing wrong?
Thanks for the help!

mkl_rt.2.dll

The current DLL name is mkl_rt.2.dll. Theres no way to update the DLL variable that holds this without rebuilding.

#if LINUX
internal const string DLL = "libmkl_rt.so";
#elif OSX
internal const string DLL = "libmkl_rt.dylib";
#else
internal const string DLL = "mkl_rt.dll";
#endif

And its a FREAKING CONST So its baked in to the compile. Cant change it at runtime!!! whyyyyyy????

Your runtime nuget packages are old (2022) so I want to use the current MKL, so I would rather just be able to have path set up to it, but i cant change the damned DLL name in your library.

Memory leak when using MatrixSolve with direct matrix input

We seen a constant increasing memory usage when used MatrixSolve from our code. The below example show how we used the library which cause an extra Matrix allocated and never freed (Ea in the evaluate method as that got a copy of A which never freed).

int n = 400;
matrix ax = new matrix(n, n);
matrix b = new matrix(n, 1);
for (int i = 0; i < 10000; i++)
{
      MatrixSolve solve = new MatrixSolve(ax, b);
      matrix result = solve.Evaluate();
      result.Dispose();
}
ax.Dispose();
b.Dispose();

After this point we have 10000 extra 400*400 double array in the pool which never returned.
Is there a better way to use this method with direct matrix input?

Cant execute buildNative.ps1 script

Hi,
I tried to build the project and in order to do so I executed the buildNative.ps1 script locally.
It seems like there is a problem unzipping some .nupkg files in the script.

The error massage:

PS D:\Documnets\projects\csharp\MKL.NET> .\buildNative.ps1
Expand-Archive : .nupkg is not a supported archive file format. .zip is the only supported archive file format.
At D:\Documnets\projects\csharp\MKL.NET\buildNative.ps1:11 char:5
+     Expand-Archive $filename "packages/$name"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (.nupkg:String) [Expand-Archive], IOException
    + FullyQualifiedErrorId : NotSupportedArchiveFileExtension,Expand-Archive

Im using:
Powershell version - 5.1.19041.906
Windows 10 Pro version 2004

My system info -

OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19041 N/A Build 19041
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Standalone Workstation
OS Build Type:             Multiprocessor Free
Registered Organization:
Original Install Date:     8/21/2020, 9:05:38 PM
System Boot Time:          5/27/2021, 7:40:14 PM
System Manufacturer:       ASUS
System Model:              All Series
System Type:               x64-based PC
Processor(s):              1 Processor(s) Installed.
                           [01]: Intel64 Family 6 Model 60 Stepping 3 GenuineIntel ~4001 Mhz
BIOS Version:              American Megatrends Inc. 2205, 2/12/2015
Windows Directory:         C:\WINDOWS
System Directory:          C:\WINDOWS\system32
Boot Device:               \Device\HarddiskVolume10
Input Locale:              en-us;English (United States)
Total Physical Memory:     16,323 MB
Available Physical Memory: 8,527 MB
Virtual Memory: Max Size:  25,027 MB
Virtual Memory: Available: 11,551 MB
Virtual Memory: In Use:    13,476 MB
Page File Location(s):     C:\pagefile.sys
Hotfix(s):                 12 Hotfix(s) Installed.
                           [01]: KB4601554
                           [02]: KB4559309
                           [03]: KB4561600
                           [04]: KB4570334
                           [05]: KB4577266
                           [06]: KB4577586
                           [07]: KB4580325
                           [08]: KB4586864
                           [09]: KB4593175
                           [10]: KB4598481
                           [11]: KB5003173
                           [12]: KB5003242
Hyper-V Requirements:      VM Monitor Mode Extensions: Yes
                           Virtualization Enabled In Firmware: Yes
                           Second Level Address Translation: Yes
                           Data Execution Prevention Available: Yes

Provide MKL functions for pointers

Hi,

First of all thanks for this library, it's really helpful to have the P/Invoke definitions available like this.

I wanted to ask whether it is possible to provide the MKL functions with double*,int signatures instead of double[]. Actually just making the original extern declarations public would be enough.

It would be really useful for example with pointers from memory-mapping or when I want to access only parts of an array.

Thanks and cheers

Unable to load DLL 'mkl_rt.dll'

Hi,

I'm probably missing something obvious but for example MKLNET.Vml.Asin(Values, Result1); results in:

System.DllNotFoundException
HResult=0x80131524
Message=Unable to load DLL 'mkl_rt.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)
Source=MKL.NET
StackTrace:
at MKLNET.Vml.vdAsin(Int32 n, Double[] a, Double[] r)
at MKLNET.Vml.Asin(Double[] a, Double[] r)

I've add MKL.NET + Matrix + Optimization + Statistics and the only native dependency seems to be MKL.NET.Native.dll.
Should mkl_rt.dll be distributed with your package or is it something I have to install separately?

Support 2D FFT

As far as I can tell there is only 1D fft support.

Any chance of 2D being supported in a future release?

Intel's IPP library

Just an idea, since you already have a build pipeline in place for packaging cross-platform libraries (and maybe for generating the P/Invoke boilerplate, although maybe that is handcrafted?)

We've had good experiences with Intel's other library IPP (Integrated Performance Primitives), it offers some functionality that MKL lacks like vector-scalar binary operations (e.g. add constant to vector), threshold operations and more.

It might be out of scope for this project, just tell me what you think. I might be able to contribute some P/Invoke definitions if you're interested, subject to my employer's permission.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.