# MKL.NET
**Repository Path**: wdhust/MKL.NET
## Basic Information
- **Project Name**: MKL.NET
- **Description**: No description available
- **Primary Language**: C#
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2021-04-27
- **Last Updated**: 2024-09-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# MKL.NET
A simple cross platform .NET API for Intel MKL.
Exposing functions from MKL keeping the syntax as close to the
[c developer reference](https://software.intel.com/content/www/us/en/develop/documentation/mkl-developer-reference-c/top.html) 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.
| MKL.NET |  |
| runtimes: |
| MKL.NET.win-x64 |  |
| MKL.NET.win-x86 |  |
| MKL.NET.linux-x64 |  |
| MKL.NET.linux-x86 |  |
| MKL.NET.osx-x64 |  |
| libraries: |
| MKL.NET.Matrix |  |
## 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 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 net5.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.
```csharp
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:
```csharp
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.