Some more additional tips "How to call MKL from your C# code"

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:
  1. 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;
  2. Add <paths_to_libs> to the system (or may be user) environment variable ‘path’;
  3. 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:
1. dgemm.cs – BLAS (CBLAS)
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 Intel64 platform has the following dependencies:
$ dumpbin /DEPENDENTS mkl.dll

Microsoft (R) COFF/PE Dumper Version 11.00.50727.1
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

  Summary

        4000 .data
        1000 .pdata
        4000 .rdata
        1000 .reloc
        F000 .text

That’s mean that mkl_intel_thread.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. for example, the MKL.DLL depends on MKL_INTEL_THREAD.DLL and MKL_INTEL_THREADS.DLL depend on MKL_CORE.DLL and LIBIOMP5MD.DLL, which is Intel OpenMP Runtime LIbrary from Intel Compiler. 
 
All of DLLS (MKL.DLL and Dependency DLL) have to be visible in the path.  You can make all required libraries visible in the path by calling Intel MKL environment bat file
>"C:\Program Files (x86)\Intel_sw_development_tools\compilers_and_libraries\windows\mkl\bin\mklvars.bat" [ia32|intel64]
 
Fig.1
 
 
 
How to use MKL C# examples

We consider running these examples in the Microsoft Visual studio environment.
Download file Intel_MKL_C#_Examples.zip from the page [2].
Unzip it into any folder. You will see the following 7 files:
 
To  run examples on the IA32 platform, go to the command window:
Start -> All Programs -> Microsoft Visual Studio 2012 -> Visual Studio Tools -> VS2012 x86 Native Tools Command Prompt
 
Change directory to the one where examples are located and type:
>"C:\Program Files (x86)\Intel_sw_development_tools\compilers_and_libraries\windows\mkl\bin\mklvars.bat" ia32
>nmake ia32

To run examples on the intel64 platform, go to the command window:
Start -> All Programs -> Microsoft Visual Studio 2012 -> Visual Studio Tools -> VS2012 x64 Win64 Command Prompt
 
Change directory to those, where examples are located and type:
>"C:\Program Files (x86)\Intel_sw_development_tools\compilers_and_libraries\windows\mkl\bin\mklvars.bat" intel64
>nmake intel64
 
Fig 2
 


Microsoft* Visual Studio 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 "X64" platform.
 
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/
 
Per informazioni più dettagliate sulle ottimizzazioni basate su compilatore, vedere il nostro Avviso sull'ottimizzazione.