Introduction :
This paper discusses details of calling native dlls from the C# applications.
To call MKL from your C# code:
• In C# code declare MKL function with DllImport attribute and Marshal parameters if it is required;
• Build custom DLL with required MKL functions;
• Make this DLL ‘visible’ for the Microsoft .NET CLR. There are some ways to do this:
- From Command Window type: set path=<paths_to_libs>;%path%, then run application in this window. Here <paths_to_libs> are the names of the directories (separated by semicolon), where the custom and other required (as MKL ones) libraries are located;
- Add <paths_to_libs> to the system (or may be user) environment variable ‘path’;
- Copy the libraries, including dependents, to one of the system paths;
DLL dependencies
General coding rules you can find in the paper [1]. A short MKL C# suite [2] provides examples for 5 MKL domains:
General coding rules you can find in the paper [1]. A short MKL C# suite [2] provides examples for 5 MKL domains:
1. dgemm.cs – BLAS (CBLAS)
2. dgeev.cs – LAPACK
3. pardiso.cs – PARDISO
4. dfti_d1.cs – DFTI
5. vddiv.cs – VML
2. dgeev.cs – LAPACK
3. pardiso.cs – PARDISO
4. dfti_d1.cs – DFTI
5. vddiv.cs – VML
The BLAS, LAPACK and PARDISO examples illustrate general coding rules.
The DFTI example demonstrates the work with a descriptor (pointer) and with the variable number of arguments.
The VML example shows how to map a C structure to a C# one. It also demonstrates how to declare and work with asynchronous call-back function.
The detailed instruction how to build custom MKL DLL you can find in the MKL User Guide document (see Chapter 5 \ Building a Custom DLL). Custom DLL is also built by the MKL C# examples makefile.
Ensure that all of the MKL functions declared in the C# code are listed (see as an example MKLfunctions.lst file into [2]) when you build custom DLL.
The big misunderstand often connected with the fact that not only the custom DLL but all dependents libraries should be visible. You can use ‘dumpbin’ utility command to see dependents libraries. For instance, the DLL that is built by the MKL C# examples on the IA32 platform has the following dependencies:
$ dumpbin /DEPENDENTS mkl.dll
Microsoft ® COFF/PE Dumper Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file mkl.dll
File Type: DLL
Image has the following dependencies:
KERNEL32.dll
mkl_intel_thread.dll
libiomp5md.dll
mkl_intel_thread.dll
libiomp5md.dll
Summary
12000 .data
3000 .rdata
2000 .reloc
A000 .text
3000 .rdata
2000 .reloc
A000 .text
That’s mean that mkl_intel_thread.dll and libiomp5md.dll have to be visible in the path.
The other way to check DLL dependencies is to use Dependency Walker tool. The following picture shows some possible problems with dependent libraries. Remember, Microsoft reports “your DLL cannot be found” even if it found your library, but can’t find a dependent library.
The other way to check DLL dependencies is to use Dependency Walker tool. The following picture shows some possible problems with dependent libraries. Remember, Microsoft reports “your DLL cannot be found” even if it found your library, but can’t find a dependent library.
Fig.1

On the next picture ( Fig.2) all dependent libraries are accessible and there will not problems with call MKL functions.
Fig.2

How to use MKL C# examples
We consider running these examples in the Microsoft Visual studio environement.
Download file Intel_MKL_C#_Examples.zip from the page [2].
Unzip it into any folder. You will see the following 7 files:
Fig.3
We consider running these examples in the Microsoft Visual studio environement.
Download file Intel_MKL_C#_Examples.zip from the page [2].
Unzip it into any folder. You will see the following 7 files:
Fig.3

To run examples on the IA32 platform, go to the command window:
Start -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt
Start -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt
Fig.4

Change directory to the one where examples are located and type:
nmake ia32 MKLROOT=<path_to_mkl>
Where <path_to_mkl> should be for instance: C:\Program Files\Intel\MKL\10.2.5.035
To run examples on the intel64 platform, go to the command window:
Start -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 x64 Win64 Command Prompt
nmake ia32 MKLROOT=<path_to_mkl>
Where <path_to_mkl> should be for instance: C:\Program Files\Intel\MKL\10.2.5.035
To run examples on the intel64 platform, go to the command window:
Start -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 x64 Win64 Command Prompt

Change directory to those, where examples are located and type:
nmake em64t MKLROOT=<path_to_mkl>
Visual Studio 2008 project
nmake em64t MKLROOT=<path_to_mkl>
Visual Studio 2008 project
The code of the MKL C# examples could be also used in the Visual Studio project. Custom DLL should be still built outside of the projects. In the Visual Studio set correct path for include, library and binaries files. If you want to use 64-bit MKL - switch to Intel64 platform as shown below.

References:
[1] Using Intel® Math Kernel Library and Intel® Integrated Performance Primitives in the Microsoft* .NET* Framework http://software.intel.com/en-us/articles/using-intel-math-kernel-library-and-intel-integrated-performance-primitives-in-the-microsoft-net-framework/
[2] “Using Intel® MKL in your C# program” http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/
