Coder Social home page Coder Social logo

compute.net's Introduction

Compute.NET: .NET bindings for native numerical computing

bind program screenshot

Get the latest release from the Compute.NET package feed.

About

Compute.NET provides auto-generated bindings for native numerical computing libraries like Intel Math Kernel Library, AMD Core Math Library (and its successors), NVIDIA CUDA, AMD clBLAS, cl* and others. The bindings are auto-generated from the library's C headers using the excellent CppSharp library. The generator is a CLI program that be can used to generate individual modules of each library as well as customize key aspects of the generated code, such as the use of .NET structs instead of classes for complex data types, and marshalling array parameters in native code functions (either as managed arrays or pointers.)

Status

  • CLI Bindings Generator: Works on Windows.

  • Bindings:

    • Compute.Bindings.IntelMKL package available on Myget feed. This library is not Windows-specific but I haven't tested it on Linux or other platforms yet. The following modules are available:
      • BLAS, CBLAS, SpBLAS, and PBLAS
      • LAPACK and SCALAPACK
      • VML
      • VSL
    • Compute.Bindings.CUDA package available on NuGet and MyGet. This library is not Windows-specific but I haven't tested it on Linux or other platforms yet. The entire runtime API is bound together with the following modules:
      • cuBLAS
  • Native Library Packages:

    • Compute.Winx64.IntelMKL package available on MyGet feed.
    • Compute.Winx64.CUDA package available on MyGet and NuGet.

Usage

Intel MKL Bindings

  1. Add the Compute.NET package feed to your NuGet package sources: https://www.myget.org/F/computedotnet/api/v2
  2. Install the bindings package into your project: Install-Package Compute.Bindings.IntelMKL.
  3. (Optional) Install the native library package into your project: Install-Package Compute.Winx64.IntelMKL.

Without step 2 you will need to make sure the .NET runtime can locate the native MKL library DLLs or shared library files. You can set your path to include the directory where the library files are located (typically %MKLROOT%\redist). Or you can copy the needed files into your project output directory with a build task.

With the packages installed you can use the MKL BLAS or vector math or other routines in your code. E.g the following code is translated from the Intel MKL examples for CBLAS:

using IntelMKL.ILP64;
public class BlasExamples
{
	public const int GENERAL_MATRIX = 0;
	public const int UPPER_MATRIX = 1;
	public const int LOWER_MATRIX = -1;

	public void RunBlasExample1()
	{
	    int m = 3, n = 2, i, j;
	    int lda = 3, ldb = 3, ldc = 3;
	    int rmaxa, cmaxa, rmaxb, cmaxb, rmaxc, cmaxc;
	    float alpha = 0.5f, beta = 2.0f;
	    float[] a, b, c;
	    CBLAS_LAYOUT layout = CBLAS_LAYOUT.CblasRowMajor;
	    CBLAS_SIDE side = CBLAS_SIDE.CblasLeft;
	    CBLAS_UPLO uplo = CBLAS_UPLO.CblasUpper;
	    int ma, na, typeA;
	    if (side == CBLAS_SIDE.CblasLeft)
	    {
		rmaxa = m + 1;
		cmaxa = m;
		ma = m;
		na = m;
	    }
	    else
	    {
		rmaxa = n + 1;
		cmaxa = n;
		ma = n;
		na = n;
	    }
	    rmaxb = m + 1;
	    cmaxb = n;
	    rmaxc = m + 1;
	    cmaxc = n;
	    a = new float[rmaxa * cmaxa];
	    b = new float[rmaxb * cmaxb];
	    c = new float[rmaxc * cmaxc];
	    if (layout == CBLAS_LAYOUT.CblasRowMajor)
	    {
		lda = cmaxa;
		ldb = cmaxb;
		ldc = cmaxc;
	    }
	    else
	    {
		lda = rmaxa;
		ldb = rmaxb;
		ldc = rmaxc;
	    }
	    if (uplo == CBLAS_UPLO.CblasUpper)
		typeA = UPPER_MATRIX;
	    else
		typeA = LOWER_MATRIX;
	    for (i = 0; i < m; i++)
	    {
		for (j = 0; j < m; j++)
		{
		    a[i + j * lda] = 1.0f;
		}
	    }
	    for (i = 0; i < m; i++)
	    {
		for (j = 0; j < n; j++)
		{
		    c[i + j * ldc] = 1.0f;
		    b[i + j * ldb] = 2.0f;
		}
	    } 
	    CBlas.Ssymm(layout, side, uplo, m, n, alpha, ref a[0], lda, ref b[0], ldb, beta, ref c[0], ldc);
	}
}

Enums like CBLAS_UPLO are generated from the CBLAS header file. You pass double[] and float[] arrays to the BLAS functions using a ref alias to the first element of the array which is converted to a pointer and passed to the native function. You can use either LP64 or ILP64 array indexing depending on the namespace you import.

Bindings Generator

The basic syntax is bind LIBRARY MODULE [OPTIONS] e.g bind mkl --vml --ilp64 -n IntelMKL -o .\Compute.Bindings.IntelMKL -c Vml --file vml.ilp64.cs will create bindings for the Intel MKL VML routines, with ILP64 array indexing, in the .NET class Vml and namespace IntelMKL and in the file vmk.ilp64.cs in the .\Compute.Bindings.IntelMKL output directory.

compute.net's People

Contributors

allisterb 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

compute.net's Issues

How to use SpBlas

I'm trying to use the SpBlas API but get error

static void Main(string[] args)
{
    double[] values_A = { 10, 11, 12, 13, 15, 14, 16, 17, 18, 19 };
    int[] columns_A = { 0, 1, 2, 3, 0, 4, 1, 2, 3, 4 };
    int[] rowIndex_A1 = { 0, 2, 4, 6, 8, 10 };
    int[] rowIndex_A2 = { 2, 4, 6, 8, 10 };

    double[] values_B = { 5, 6, 7, 8, 9 };
    int[] columns_B = { 0, 1, 2, 3, 4 };
    int[] rowIndex_B1 = { 0, 1, 2, 3, 4, 5 };
    int[] rowIndex_B2 = { 1, 2, 3, 4, 5 };

    var a = new SparseMatrix();
    var b = new SparseMatrix();

    var res = SpBlas.MklSparseDCreateCsr(a, SparseIndexBaseT.SPARSE_INDEX_BASE_ZERO, 5, 5, ref rowIndex_A1[0], ref rowIndex_A2[0], ref columns_A[0], ref values_A[0]);
    res = SpBlas.MklSparseDCreateCsr(b, SparseIndexBaseT.SPARSE_INDEX_BASE_ZERO, 5, 5, ref rowIndex_B1[0], ref rowIndex_B2[0], ref columns_B[0], ref values_B[0]);

    var c = new SparseMatrix();
    res = SpBlas.MklSparseSpmm(SparseOperationT.SPARSE_OPERATION_NON_TRANSPOSE, a, b, c); // res is IntelMKL.LP64.SparseStatusT.SPARSE_STATUS_NOT_SUPPORTED
    res = SpBlas.MklSparseDSetValue(a, 1, 2, 10); // Exception: System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

}

What is wrong here?

StackOverflowExeption at Binding

Thank you so amazing useful tool!

When I bind the IntelMKL, the below error occurs.

 C:\Users\dqm2k\source\repos\Compute.NET>bind mkl --vml --ilp64 -n IntelMKL -o .\Compute.Bindings.IntelMKL -c Vml --file vml.ilp64.cs --root "C:\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2020.4.311\\windows\\mkl"
16:28:12<01> [INF] Binding library module blas.
16:28:12<01> [INF] Using C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020.4.311\windows\mkl as library directory.
16:28:12<01> [INF] Using C:\Users\dqm2k\source\repos\Compute.NET\test as output directory.
16:28:12<01> [INF] Using IntelMKL as library namespace.
16:28:12<01> [INF] Module file is C:\Users\dqm2k\source\repos\Compute.NET\test\blas.cs.
16:28:12<01> [INF] Using Intel64 architecture.
16:28:12<01> [INF] Using default Intel threading library.
16:28:12<01> [INF] Using ILP64 (Int64) array indexing
16:28:12<01> [INF] Creating bindings for BLAS routines...

Process is terminated due to StackOverflowException.

I have confirmed that the include files and libraries are successfully loaded from the MKL root directory.
Do you know any idea to treat this error?

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.