BLAS/LAPACK bindings for .NET (OpenBLAS, MKL, Apple Accelerate)
English | 日本語
BlasSharp is a library that provides bindings to BLAS/LAPACK for .NET.
BLAS/LAPACK libraries optimized for various CPUs are provided by Intel, Apple, and others. By bringing these libraries into C#, you can perform linear algebra computations with optimal performance.
You can use the bindings by installing the package that corresponds to the desired library. Below is a sample using OpenBLAS:
using BlasSharp;
using static BlasSharp.OpenBlas.NativeMethods;
unsafe
{
double[] A = [
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0
];
double[] B = [
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
7.0, 8.0, 9.0,
];
double[] C = new double[9];
fixed (double* a = A, b = B, c = C)
{
cblas_dgemm(CBLAS_ORDER.CblasColMajor,
CBLAS_TRANSPOSE.CblasNoTrans,
CBLAS_TRANSPOSE.CblasNoTrans,
3, 3, 3, 1.0, a, 3, b, 3, 0.0, c, 3);
}
Console.WriteLine(string.Join(',', C));
}The BlasSharp.Shared package provides IBlasOperations / ILapackOperations, which abstracts BLAS/LAPACK routines. Each package provides its own implementation.
public interface IBlasOperations : IBlasLevel1, IBlasLevel2, IBlasLevel3
{
}
public unsafe interface IBlasLevel1
{
void Saxpy(int n, float alpha, float* x, int incX, float* y, int incY);
void Daxpy(int n, double alpha, double* x, int incX, double* y, int incY);
void Caxpy(int n, void* alpha, void* x, int incX, void* y, int incY);
void Zaxpy(int n, void* alpha, void* x, int incX, void* y, int incY);
...
}public interface ILapackOperations : ILapackDriverRoutines, ILapackComputationalRoutines, ILapackAuxiliaryRoutines
{
}
public unsafe interface ILapackDriverRoutines
{
int Sgesv(int* n, int* nrhs, float* a, int* lda, int* ipiv, float* b, int* ldb, int* info);
int Cgesv(int* n, int* nrhs, void* a, int* lda, int* ipiv, void* b, int* ldb, int* info);
int Dgesv(int* n, int* nrhs, double* a, int* lda, int* ipiv, double* b, int* ldb, int* info);
int Zgesv(int* n, int* nrhs, void* a, int* lda, int* ipiv, void* b, int* ldb, int* info);
...
}You can use the OpenBLAS binaries and bindings by adding BlasSharp.OpenBlas to your project.
BlasSharp.OpenBlas supports multiple platforms:
win-x64, osx-x64, osx-arm64, linux-x64, and linux-arm64.
dotnet add package BlasSharp.OpenBlasInstall-Package BlasSharp.OpenBlasYou can use the bindings for Intel's MKL by adding BlasSharp.Mkl to your project.
dotnet add package BlasSharp.MklInstall-Package BlasSharp.MklTo use MKL, you also need to install the official Intel MKL NuGet package (e.g., intelmkl.devel.win-x64).
See the MKL download page for more details.
You can use the bindings for Apple’s Accelerate framework by adding BlasSharp.AppleAccelerate to your project.
BlasSharp.AppleAccelerate currently supports only osx-arm64.
dotnet add package BlasSharp.AppleAccelerateInstall-Package BlasSharp.AppleAccelerateThis library is released under the MIT License.