Introduction
Few weeks ago, I was doing research and I needed a fast program for Singular Value Decomposition. I have SVD implementation in my open source project called Daany which is using the SVD implementation of Accord.NET great Machine Learning Framework. However, the decomposition is working fine and smooth for small
matrices with few hundreds rows/cols but for matrices with more than 500 rows and columns it is pretty slow. So I was forced to think about of using different library in order to speed up the SVD calculation.
I could use some of python libraries eg. TensorFlow
, PyTorch
or SciPy
or similar libraries from R
and c++
. I have used such libraries and I know how they are fast. But I still wanted to have approximately same speed on .NET as well.
Then I decided to look how can I use some of available c++ based libraries. Once I switch to c++ based project I would not be able to use .NET framework where other parts of my research are implemented. So only solution was to implement a wrapper around a c++ library and use pInvoke
in order to expose required methods in C# code.
The first idea was to use LAPACK/BLAS numerical library to calculate not only SVD but whole set of Linear Algebra routines. LAPACK/BLAS libraries have long history back to 70s of the 20th century. They are proved to be very fast and reliable. However they are not supported for GPU.
Then I came to MAGMA which is nothing but LAPACK for GPU. MAGMA is very complex and fast library which requires CUDA. However if the machine has no CUDA, the library cannot be used.
The I decided to use hybrid approach and use MAGMA whenever the machine has CUDA, otherwise use LAPACK as computation engine. This approach is the most complex and required advance skills in C++ and C#. So after a more than a month of the implementation the MagmaSharp
is published as GitHub open source project with the fist public release MagmaSharp 0.02.01
at Nuget.org.
MagmaSharp
v0.02.01
The first release of MagmaSharp
supports MAGMA Device routines:
Currently the library supports MAGMA driver routines for general rectangular matrix:
gesv
– solve linear system, AX = B, A is general non-symetric matrix,gels
– least square solve, AX = B, A is rectangular,geev
– eigen value solver for non-symetric matrix,gesvd
– singular value decomposition (SVD),.
The library supports float
and double
value types.
Software requirements
The project is build on .NET Core 3.1 and .NET Standard 2.1. It is built and tested on Windows 10 1909 only.
Software (Native Libraries) requirements
In order to compile, build and use the library the following native libraries are needed to be installed.
- CUDA Toolkit in case the machine supports one of NVIDIA graphics cards.
- Intel MKL which can be downloaded at https://software.intel.com/content/www/us/en/develop/tools/math-kernel-library/choose-download.html, (The MKL library can be replaced with some open-source Lapack library very easy).
- MAGMA runtime library which can be download from the Official site.
However, if you install the MagmaSharp
as Nuget package, both libraries are included, so you don’t have to install it.
How to use MagmaSharp
MagmaSharp
is packed as Nuget and can be added to your .NET project as ordinary .NET component. You don’t have to worry about native libraries and dependencies. Everything is included in the package.
The package can be installed from this link, or just search for MagmaSharp
.
How to Build MagmaSharp
from the source
-
Download the
MagmaSharp
source code from the GitHub page. -
Reference Magma static library and put it to folder MagmaLib. Magma static library can be downloaded and built from the Official site.
-
Open ‘MagmaSharp.sln’ with Visual Studio 2019.
-
Make sure the building architecture is x64.
-
Restore Nuget packages.
-
Build and run the Solution.
How to start with MagmaSharp
The best way to start with MahmaSharp
is to take a look at the MagmaSharp.XUnit
project, there is a small example how to use each of the implemented method with or without CUDA device.