Skip to content

nuskey8/BlasSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BlasSharp

BLAS/LAPACK bindings for .NET (OpenBLAS, MKL, Apple Accelerate)

Releases GitHub license

English | 日本語

Overview

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.

Usage

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));
}

IBlasOperations / ILapackOperations

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);

    ...
}

OpenBLAS

NuGet

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.

.NET CLI

dotnet add package BlasSharp.OpenBlas

Package Manager

Install-Package BlasSharp.OpenBlas

MKL

NuGet

You can use the bindings for Intel's MKL by adding BlasSharp.Mkl to your project.

.NET CLI

dotnet add package BlasSharp.Mkl

Package Manager

Install-Package BlasSharp.Mkl

To 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.

Accelerate (Apple)

NuGet

You can use the bindings for Apple’s Accelerate framework by adding BlasSharp.AppleAccelerate to your project.

BlasSharp.AppleAccelerate currently supports only osx-arm64.

.NET CLI

dotnet add package BlasSharp.AppleAccelerate

Package Manager

Install-Package BlasSharp.AppleAccelerate

License

This library is released under the MIT License.

About

BLAS/LAPACK bindings for .NET (OpenBLAS, MKL, Apple Accelerate)

Resources

License

Stars

15 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors