| June 16, 2009 2:00 PM PDT | |
Introduction
Several users have asked how to call and link the Intel® Math Kernel Library (Intel® MKL) functions from their C# programs. While the standard way of interfacing with third party software libraries from C# is well documented, some of the steps in interfacing with Intel MKL specifically may still be confusing. The attached source code package is intended to show how to navigate the whole process for Intel MKL users. These examples show how to create a DLL from Intel MKL libraries, call those functions from their C# source, and interface with that DLL.
Examples are provided for calling the BLAS, LAPACK, DFTI (the FFT interface), the PARDISO direct sparse solver, and the vector math library (VML).
Building the Examples
Follow these steps to build the example programs:
- unzip the contents of the attached zip file
- Open a Microsoft Visual Studio command prompt or add the Microsoft.NET Framework to the PATH environment variable in another command prompt
- Run the build script (makefile) using nmake
- Example:
nmake ia32 MKLROOT="c:\program files\intel\mkl\10.1.1.015" - The makefile provides further explanation of the parameters in comments
- Example:
This will create an executable for each of the example programs.
Example files: Intel_MKL_C#_Examples.zip
Building the Examples with Intel® MKL 10.3
Dynamic interface libraries have been added in Intel MKL 10.3 for improved linkage from C#. It is not required to build a custom DLL. The Intel MKL library named mkl_rt.dll can be called directly from C# code. Below is an updated version of the examples. Follow the same steps and run the nmake command.
Example: nmake ia32 MKLREDIST="c:\Program Files\Intel\ComposerXE-2011\redist"
Example files: Intel_MKL_C#_Examples_02.zip
Take our C++/C# interface survey for Intel® Math Kernel Library
For more complete information about compiler optimizations, see our Optimization Notice.
Comments (18) 
| April 9, 2009 10:09 PM PDT
Vladimir Koldakov (Intel)
|
Thank you, Oleg! Of course, MKL root directory should be in command line. -Vladimir |
| July 16, 2009 7:32 AM PDT
michasspamgmx.de
|
Hi, the example does not work on my system. As you can see in the errorcode below I am working with MKL 10.0.1.015. I am using Visual Studio 2005 Framework 2.0. Are there any known solutions to this? Best regards Micha |
| July 16, 2009 7:33 AM PDT
michasspamgmx.de
|
Sorry, here comes the error code: D:CsharpTestUmgebungMKL_Einbinden>nmake ia32 MKLROOT="C:ProgrammeIntelMKL10.0.1.015" Microsoft (R) Program Maintenance Utility, Version 8.00.50727.42 Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten. Add path of the MKL libraries to the lib environment variable set lib=%MKLROOT%ia32lib;%lib% MKL entries for custom dll Workaround for pardiso Microsoft (R) 32-Bit C/C++-Optimierungscompiler Version 14.00.50727.42 für 80x86 Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten. _fseeki64.c Build MKL custom dll nmake mkl.dll MACHINE=IX86 MKL_LIB="mkl_intel_c_dll.lib mkl_intel_thread_dll.lib mkl_core_dll.lib" MSL Microsoft (R) Program Maintenance Utility, Version 8.00.50727.42 Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten. link /DLL /MACHINE:IX86 /def:user.def _fseeki64.obj mkl_intel_c_dll.lib mkl_intel_thread_dll.lib mkl_cor Microsoft (R) Incremental Linker Version 8.00.50727.42 Copyright (C) Microsoft Corporation. All rights reserved. Bibliothek "mkl.lib" und Objekt "mkl.exp" werden erstellt. mkl_intel_c_dll.lib(_pardiso.obj) : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_mkl_solver_pa mkl.dll : fatal error LNK1120: 1 nicht aufgelöste externe Verweise. NMAKE : fatal error U1077: ""C:ProgrammeMicrosoft Visual Studio 8VCBINlink.EXE"": Rückgabe-Code "0x460" Stop. NMAKE : fatal error U1077: ""C:ProgrammeMicrosoft Visual Studio 8VCBINnmake.EXE"": Rückgabe-Code "0x2" Stop. |
| July 17, 2009 6:00 AM PDT
Vladimir Koldakov (Intel)
|
Hello, Micha, Thank you for the interest to these examples. For MKL 10.0 you need add one more library: 44c44 < MKL_LIB="mkl_intel_c_dll.lib mkl_intel_thread_dll.lib mkl_core_dll.lib" --- > MKL_LIB="mkl_intel_c_dll.lib mkl_intel_thread_dll.lib mkl_core_dll.lib mkl_solver.lib" And replace OMP library: 99c99 < libiomp5md.lib $(MSLIB) /out:mkl.dll --- > libguide40.lib $(MSLIB) /out:mkl.dll It’s better also to run examples on the drive C: The security permission problem can be appeared otherwise: Run dgemm example <…skipped…> Unhandled Exception: System.Security.SecurityException: System.Security.Permissions.SecurityPermission at mkl.CBLAS.dgemm(Int32 Order, Int32 TransA, Int32 TransB, Int32 M, Int32 N, Int32 K, Double alpha, Double[] A, Int32 lda, Double[] B, Int32 ldb, Double beta, Double[] C, Int32 ldc) at test_dgemm.Main(String[] args) Best regards, -Vladimir |
| September 24, 2009 10:04 AM PDT
Tommy |
Hi, Is there going to be an official wrapper release for C#? Thanks, Tommy |
| September 30, 2009 4:24 AM PDT
Jo Cotterell |
Hi MKL gurus, We have successfully linked a C# program to MKL thanks to your examples, but a problem remain: It seems that MKL can't span multiple threads when being called from our managed environment. When calling cblas_dgemm from a freshly built DLL, the linkage is successfully done (pinning, ...) and the Matrix-Matrix multiplication is processed, but on an Intel Xeon 5130, we attained only 25% of CPU Usage, even after setting some environment variables (MKL_DYNAMIC=FALSE, MKL_NUM_THREADS=x) which seemed to have no impact on this issue... Of course we were expecting some overhead coming from the C#->C transition, but such a behavior seems strange. Is it expected or have we gone wrong on something? Does C# constrain the use of unmanaged code to only one thread? Best Regards. |
| November 2, 2009 3:18 AM PST
Vladimir Koldakov (Intel)
|
Hi, Jo. I've checked on an 8 core Xeon - it is ok with MKL_NUM_THREADS=2 or 4 or (default) 8. I used the makefile attached to the examples to build custom dll and run example with N=9000, K=8000, M=6000. Could you please provide more details how do you build dll? And a small test if it is possible. Thanks, Vladimir |
| November 11, 2009 2:26 PM PST
Shivesh Wangrungvichaisri | If I had a C# program running on mono/linux, can I use the MKL Linux version with my program since this solution is relying on a DLL? |
| November 11, 2009 2:28 PM PST
shachris23
|
I have a C# program running on Mono/Linux platform. Does Intel MKL Linux version allow me to integrate the MKL into the Mono implementation as well? If so, is the instruction pretty similar? Thanks. |
| November 12, 2009 12:54 AM PST
Vladimir Koldakov (Intel)
|
Hi, We do not support Mono at the moment. I believe it will work if you build custom dynamic library (<mkl_root>/tools/builder) and call it via P/Invoke. Thanks, Vladimir |
| November 13, 2009 2:43 AM PST
to work this sample |
only for this version: nmake ia32 MKLROOT="C:ProgramFileIntelCompile11.1 .51mkl" and i need to coy libiomp5md.dll & libiomp5md.lib to de folder IntelCompiler11.1 51mklia32lib |
| November 16, 2009 7:00 PM PST
shachris23
| Just a feedback that as Mono platform grows, it would be real nice if MKL supports that platform. Thanks!! |
| April 26, 2010 12:26 PM PDT
fritzfranz
|
I downloaded MKL for evaluation, and I also downloaded the examples above. The source code is pretty clear - but I do NOT manage to properly set up the links from the C# project to the MKL DLLs Using the Visual studio command for project links, I can find the DLLs, but setting up links is not accepted. What is the nature of the DLLs? COM or .NET? Is there anything else I have to do to set up the links ?? The makefile was not helpful at all, I have a Windows 7 64-bit computer, and I can not find nmake on my machine .... |
| December 27, 2010 11:45 AM PST
Janene Pappas-mccrillis
|
I'm attempting to run the examples on my machine, Windows-7 VS 2010. I need to eventually integrate a good FFT library into my product and this one was recommended, but I can't get the examples to run. I've set up nmake, added paths of the MKL redistributable to my main path environment variable, etc. The makefile execution fails: C:projectsMKL Examples>nmake ia32 mklredist="C:Program Files (x86)IntelComposerXE-2011redist" Microsoft (R) Program Maintenance Utility Version 10.00.30319.01 Copyright (C) Microsoft Corporation. All rights reserved. Add path of the MKL redistributable to the path environment variable set path=%MKLREDIST%ia32mkl;%MKLREDIST%ia32compiler;%path% Build and run examples nmake /a dgemm.exe dgeev.exe dfti_d1.exe pardiso.exe vddiv.exe Microsoft (R) Program Maintenance Utility Version 10.00.30319.01 Copyright (C) Microsoft Corporation. All rights reserved. Compile dgemm.cs csc .dgemm.cs Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved. Run dgemm example dgemm.exe MKL cblas_dgemm example alpha=1 beta=-1 Matrix A 1 2 3 4 5 6 Matrix B 0 1 0 1 1 0 0 1 1 0 1 0 Initial C 5 1 3 3 11 4 6 9 MKL FATAL ERROR: Cannot load mkl_intel_thread.dll NMAKE : fatal error U1077: '.dgemm.exe' : return code '0x1' Stop. NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio 10.0VCBINnmake.EXE"' : return code '0x2' Stop. I am totally unable to add references to the MKL DLL's in VS2010. The standard "Add Reference" simply ignores my attempt. How does one use this library with a C# project? Janene |
| April 12, 2011 3:03 AM PDT
Eric |
Hi, I am using eigen values/vectors decomposition DSYEV in mkl for a university project and I need results to be reproductible. I wrote a little wrapper (according to examples) to call DSYEV from C# :
namespace Front_toolbox_v2
{
class Solve
{
public double[,] decompo(double[,] A)
{
int lwork = -1;
int n = A.GetLength(0);
int lda = n;
double[] A_bis = new double[n * n];
double[] w = new double[n];
for (int i = 0; i < n; i++)
{
int cte = i * n;
for (int j = 0; j < n; j++)
{
A_bis[cte + j] = A[j, i];
}
}
lwork = -1;
double[] work_1 = new double[1];
int info = LAPACK_mkl.dsyev('V', 'U', n, A_bis, lda, w, work_1, lwork);
lwork = (int)work_1[0];
double[] work = new double[lwork];
info = LAPACK_mkl.dsyev('V', 'U', n, A_bis, lda, w, work, lwork);
double[,] res2 = new double[n, n + 1];
for (int i = 0; i < n; i++)
{
res2[i, 0] = w[i];
for (int j = 0; j < n; j++)
{
res2[i, j + 1] = A_bis[i + j * n];
}
}
GC.Collect(0);
return res2;
}
}
}
namespace mkl
{
public sealed class LAPACK_mkl
{
private LAPACK_mkl() { }
public static int dsyev(char jobz, char uplo, int N, double[] A, int LDA, double[] w, double[] work, int lwork)
{
LAPACKNative.kmp_set_warnings_off();
int num = Environment.ProcessorCount;
LAPACKNative.omp_set_num_threads(ref num);
int info = -1;
LAPACKNative.dsyev(ref jobz, ref uplo, ref N, A, ref LDA, w, work, ref lwork, ref info);
return info;
}
}
/** LAPACK native declarations */
[SuppressUnmanagedCodeSecurity]
internal sealed class LAPACKNative
{
[DllImport("libiomp5md", EntryPoint = "omp_set_num_threads")]
internal static extern void omp_set_num_threads(ref int num);
[DllImport("libiomp5md", EntryPoint = "kmp_set_warnings_off")]
internal static extern void kmp_set_warnings_off();
[DllImport("mkl", EntryPoint = "DSYEV", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
internal static extern void dsyev(ref char jobz, ref char uplo, ref int n, double[] A, ref int lda, double[] w, double[] work, ref int lwork, ref int info);
private LAPACKNative()
{
kmp_set_warnings_off();
int num = Environment.ProcessorCount;
omp_set_num_threads(ref num);
}
}
}
I red something about memory alignment and 16-byte boundaries... does this apply here ? Thank you for any answer or comment on my code |
| April 12, 2011 3:10 AM PDT
trombif
|
Hi, I am using eigen values/vectors decomposition DSYEV in mkl for a university project and I need results to be reproductible. I wrote a little wrapper (according to examples) to call DSYEV from C# :
namespace Front_toolbox_v2
{
class Solve
{
public double[,] decompo(double[,] A)
{
int lwork = -1;
int n = A.GetLength(0);
int lda = n;
double[] A_bis = new double[n * n];
double[] w = new double[n];
for (int i = 0; i < n; i++)
{
int cte = i * n;
for (int j = 0; j < n; j++)
{
A_bis[cte + j] = A[j, i];
}
}
lwork = -1;
double[] work_1 = new double[1];
int info = LAPACK_mkl.dsyev('V', 'U', n, A_bis, lda, w, work_1, lwork);
lwork = (int)work_1[0];
double[] work = new double[lwork];
info = LAPACK_mkl.dsyev('V', 'U', n, A_bis, lda, w, work, lwork);
double[,] res2 = new double[n, n + 1];
for (int i = 0; i < n; i++)
{
res2[i, 0] = w[i];
for (int j = 0; j < n; j++)
{
res2[i, j + 1] = A_bis[i + j * n];
}
}
GC.Collect(0);
return res2;
}
}
}
namespace mkl
{
public sealed class LAPACK_mkl
{
private LAPACK_mkl() { }
public static int dsyev(char jobz, char uplo, int N, double[] A, int LDA, double[] w, double[] work, int lwork)
{
LAPACKNative.kmp_set_warnings_off();
int num = Environment.ProcessorCount;
LAPACKNative.omp_set_num_threads(ref num);
int info = -1;
LAPACKNative.dsyev(ref jobz, ref uplo, ref N, A, ref LDA, w, work, ref lwork, ref info);
return info;
}
}
/** LAPACK native declarations */
[SuppressUnmanagedCodeSecurity]
internal sealed class LAPACKNative
{
[DllImport("libiomp5md", EntryPoint = "omp_set_num_threads")]
internal static extern void omp_set_num_threads(ref int num);
[DllImport("libiomp5md", EntryPoint = "kmp_set_warnings_off")]
internal static extern void kmp_set_warnings_off();
[DllImport("mkl", EntryPoint = "DSYEV", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
internal static extern void dsyev(ref char jobz, ref char uplo, ref int n, double[] A, ref int lda, double[] w, double[] work, ref int lwork, ref int info);
private LAPACKNative()
{
kmp_set_warnings_off();
int num = Environment.ProcessorCount;
omp_set_num_threads(ref num);
}
}
}
I red something about memory alignment and 16-byte boundaries... does this apply here ? Thank you for any answer or comment on my code |
| July 6, 2011 1:46 AM PDT
Ying H (Intel)
|
Hello trombif, Your question look not about how to call MKL functioon in C#, but the run result is not inconsistant in run and run on same platform, right? Could you submit the problem to MKL forum <http://software.intel.com/en-us/forums/intel-math-kernel-library/>? where many users may be share their ideas. Regards, Ying H. |
Trackbacks (1)
- .NET numerical libraries - Programmers Goodies
October 31, 2011 2:45 PM PDT
Leave a comment 
Vladimir Koldakov (Intel)
| ||
Todd Rosenquist (Intel)
|



Oleg Golubovsky
value of MKLROOT environment variable should be reduced to "C:\Program Files\Intel\MKL\RR.r.y.xxx", where RR.r is the version number, y is the release-update number, and xxx is the package number, for
example, "C:\Program Files\Intel\MKL\10.1.0.004", otherwise you will get compilation and linkage errors.