Calling MKL from C# - how to create proper references to the MKL DLLs ??

Calling MKL from C# - how to create proper references to the MKL DLLs ??

I have downloaded MKL for 30-day evaluation, and I want to use it from C#.
I found some examples (C# code) how to use it, I also found the installed MKL DLLs on my hard disk, but I am NOT able to add references from my C# project to the MKL Dlls I want to use.
I use Windows 7 on a 64 bit computer (I7).

Please provide some hints.

14 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi,

MKL dlls are unmanaged (code), so you can't reference them in your managed (C#) code/project directly. You need to call them using DllImport directive, providing interface for the function(s) you intend to use.
As far as I can remember in MKL examples, internal sealed classes are used to "wrapp" access MKL functions/routines.

Example would be something like

[DllImport("mkl.dll", CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, SetLastError=false)]
(Interface goes here)

A.

Hello,

You can't use MKL dll's directly in the current MKL version.Please, build custom dll first.
See Building a Custom DLL section in the userguide.pdf document or run nmake in the MKL\tools\builder directory for thehelp. Example of building custom dll you can find also here: Using Intel MKL in your C# program(see makefile).
Briefly: put list of all required MKL functions into a file, say: "my_func_list.txt" and run:

nmake em64t export=my_func_list.txtname=my_dll

Thenimport 'my_dll.dll' in your C# code.
-Vladimir

I use Visual Studio 2008 (mainly with C#, but I do not have NMAKE on my computer at all
(OS = Windows 7 64 bit). (I extended Visual studio to also support C++, but this did not help to make NMAKE available)

I looked at the example code before and I can compile it, but I can not run it, since mkl.dll does not exist
(looking at the MKLEM64t bin directories, there is no mkl.dll, but rather things like mkl.core.dll and others which are not mentioned in the C# example code

Questions:
- do I really need nmake? if yes, where do I findor get it?
- how do the various DLLs mkl.core.dll mkl.cdft.core.dll etc relate? Even after reading the chapter un the MKL structure and the layered model in the user guide, it does not become clear to me how to relate this to the C# environment
- is there any more detailed description for C# beyond the article "Using Intel MKL in your C# environment"?

Please provide more help.
Thanks - Fanz

Hello,

You need to build custom dll for the current MKL version.

Try to use default builder settings for a start:

nmake em64t export=your_file

where 'your_file' contains a list of MKL functions.

Run mnake in the Visual Studio 2008 Command Prompt window. Standard nmakelocation is:

32-bit: "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\nmake"

64-bit: "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\nmake"

-Vladimir

Thanks - that helped for a first step.
I could locate nmake and copy it to a directory where all the MKL examples are located.

However the next step fails: nmake comes up with a lot of errors which are probably related to the provided "makefile" sample script (see below).

output of nmake is this:
=== beginning of output ====

Microsoft Program Maintenance Utility Version 9.00.30729.01

Copyright (C) Microsoft Corporation. All rights reserved.

Add path of the MKL libraries to the lib environment variable

set lib=%MKLROOT%\em64t\lib;%lib%

MKL entries for custom dll

Workaround for pardiso

Der Befehl "cl" ist entweder falsch geschrieben oder

konnte nicht gefunden werden.

NMAKE : fatal error U1077: 'cl' : return code '0x1'

Stop.

=== end of output ===

However it is really not clear to me what I have to do to adjust that script so that it will run successfully on my computer.
Can you provide some more hints please?

Thanks - Franz

copy of makefile script below (everything except the copyright comment at the beginning"
====================================================================

help:
@echo Usage: nmake {ia32/em64t/ia64} MKLROOT=path [CC=C++compiler]
@echo. Default CC=cl
@echo. Microsoft .NET Framework 2.0 or higher (vddiv example only)

example = dgemm.cs dgeev.cs dfti_d1.cs pardiso.cs vddiv.cs

!ifndef CC
CC = cl
!endif
CSC = csc
RES = $(example:.cs=.exe)

ia32:
@echo Add path of the MKL libraries to the lib environment variable
set lib=%%MKLROOT%%\$@\lib;%%lib%%

@echo MKL entries for custom dll
@echo Workaround for pardiso
@echo int _fseeki64() {return 1;} > _fseeki64.c
@$(CC) /c _fseeki64.c
@echo EXPORTS > user.def
@type MKLfunctions.lst >> user.def

@echo 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" \
MSLIB=user32.lib

@echo Add path of the mkl.dll and path of the MKL binaries to the path environment variable
set path=$(MAKEDIR);%%MKLROOT%%\$@\bin;%%path%%

@echo Build and run examples
nmake /a $(RES)

em64t:
@echo Add path of the MKL libraries to the lib environment variable
set lib=%%MKLROOT%%\$@\lib;%%lib%%

@echo MKL entries for custom dll
@echo Workaround for pardiso
@echo int _fseeki64() {return 1;} > _fseeki64.c
@$(CC) /c _fseeki64.c
@echo EXPORTS > user.def
@type MKLfunctions.lst >> user.def

@echo Build MKL custom dll
nmake mkl.dll MACHINE=AMD64 \
MKL_LIB="mkl_intel_lp64_dll.lib mkl_intel_thread_dll.lib mkl_core_dll.lib" \
MSLIB=user32.lib

@echo Add path of the mkl.dll and path of the MKL binaries to the path environment variable
set path=$(MAKEDIR);%%MKLROOT%%\$@\bin;%%path%%

@echo Build and run examples
nmake /a $(RES)

ia64:
@echo Add path of the MKL libraries to the lib environment variable
set lib=%%MKLROOT%%\$@\lib;%%lib%%

@echo MKL entries for custom dll
@echo Workaround for pardiso
@echo int _fseeki64() {return 1;} > _fseeki64.c
@$(CC) /c _fseeki64.c
@echo EXPORTS > user.def
@type MKLfunctions.lst >> user.def

@echo Build MKL custom dll
nmake mkl.dll MACHINE=IA64 \
MKL_LIB="mkl_intel_lp64_dll.lib mkl_intel_thread_dll.lib mkl_core_dll.lib" \
MSLIB="msvcrt.lib user32.lib"

@echo Add path of the mkl.dll and path of the MKL binaries to the path environment variable
set path=$(MAKEDIR);%%MKLROOT%%\$@\bin;%%path%%

@echo Build and run examples
nmake /a $(RES)

mkl.dll:
link /DLL /MACHINE:$(MACHINE) /def:user.def _fseeki64.obj $(MKL_LIB) \
libiomp5md.lib $(MSLIB) /out:mkl.dll
@-del /f _fseeki64.c _fseeki64.obj user.def

.SUFFIXES:: .cs .exe

{.}.cs.exe:
@echo Compile $($(CSC) $<
@echo Run $(@B) example
$(@B).exe

Did you set up the VC command line environment? You shouldn't have to move nmake or CL from their normal positions. e.g.
start>all programs>microsoft visual studio>visual studio tools>x64 command prompt (for win7 64-bit)
(or run the vcvarsall.bat in your own command line environment)

There are 2 different places for the command prompt which I found and to which i was directed so far:
1. the one you describe: start>all programs>microsoft visual studio>visual studio tools>x64 command prompt
- here nmake is available (and cl as well) , but it is not clear to me at all how to provide the sample input and where the makefile goes
(I really do not want to type in the lengthy multi-level VS project directory path ...)

2. thecommand prompt reachable from inside a Visual Studio project (here the project directory is set, but nmake is not available, therefore I copied it ... . Now i noticed that cl also is not available ...
I would have hoped that all standard tools are also visible / executable in this subdirectory

So I still need help how to run nmake in the generic prompt and how to make the project specific information available ...

Hello,

It doesnt really need to copy system commands. Use option 1.

  • Go to the x64 command prompt window.
  • Change directory to the examples location: cd
  • Type nmake em64t MKLROOT=

is the directory, where you can see the following names:

benchmarks doc em64t examples help ia32 ia64 include interfaces tests tools

-Vladimir

I did all that, but I get a fatal error U1073 and it is not clear to me where I could look for details / reasons of this error.
Also it is not clear to me how nmake would find the specific source and project files ...

Please provide some help.
So far I still was NOT able to make use of the MKL, and the trial period comes to an and soon.

Hello,

Provide please the command line that you run in the command window.

Please execute nmake command in your directory.

Please execute dir command in your directory.

Pleaseprovideyour output.

See my results. If Im in the directory where the examples are located:

C:\wrk\webC#>dir

Directory of C:\wrk\webC#

05/20/2010 04:48 PM .
05/20/2010 04:48 PM ..
05/27/2009 03:16 PM 9,676 dfti_d1.cs
05/27/2009 03:16 PM 5,117 dgeev.cs
05/27/2009 03:15 PM 4,412 dgemm.cs
05/27/2009 03:18 PM 3,858 makefile
01/26/2009 05:55 AM 213 MKLfunctions.lst
05/27/2009 03:16 PM 9,213 pardiso.cs
05/27/2009 03:16 PM 5,683 vddiv.cs
7 File(s) 38,172 bytes
2 Dir(s) 19,039,469,568 bytes free

C:\wrk\webC#>nmake

Microsoft Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: nmake ia32/em64t/ia64 MKLROOT=path [CC=C++compiler]
Default CC=cl
Microsoft .NET Framework 2.0 or higher (vddiv example only)

If Im somewhere else (empty directory):

C:\wrk\tmp>dir
Directory of C:\wrk\tmp

05/20/2010 04:43 PM .
05/20/2010 04:43 PM ..
0 File(s) 0 bytes
2 Dir(s) 19,039,465,472 bytes free

C:\wrk\tmp>nmake

Microsoft Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

NMAKE : fatal error U1064: MAKEFILE not found and no target specified
Stop.

-Vladimir

Some weeks and many iterations later (with the help of Vladimir), I could make MKL work and call it from C# console and Windows Forms applications.
Here is the solution and summary:

1. X64; whenever a command prompt window is required, it needs to be the X64 window (the standard command prompt is 32 bit)

2. Language settings: I have a Visual Studio 2008 installation with language "Germain". At least in some cases this has impact for path names. The MKLROOT parameter of nmake had to be "Programme/Intel/MKL/...." and NOT "Program Files/Intel/MKL...".

3. Dependent libraries: it was NOT sufficient to make the generated custom lib accessbile. the whole MKL binary directory (in my case the em64t part) needs to be in the system path. Without having this MKL library in the system path, I got all kinds of very strange "not found" messages and return code 1 (which could not be explained ...)

4. Setting the system path in Windows 7 is an awkward procedure. The best way to find the tool is to go to Control Panel and then search for"system path"
In the popup, you need to choose "environment variables", scroll system variables down to "path" and press "edit" which then lets you edit the (pretty long) system path variable in a tiny text field :-(

Franz

Here is a bit more information. Hope it helps someone else.

To call MKL from your C# code you need:

  • Just declare MKL function with the DllImport attribute as it is shown in the MKL C# examples;
  • Build custom dll with required MKL functions (see MKL user guide for details);
  • This dll should be visible for Microsoft .NET CLR. There are some ways to do this:
    • In Command window, type: set path=paths_to_libs;%path% then run your application in this window.
      Here paths_to_libs are names of the directories (separated by semicolon), where the custom and dependents 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;
  • Remember: Microsoft reports your DLL cannot be found even if it found your library, but cant find one of thedependents libraries.

One can find MKL C# examples following the link Using Intel MKL in your C# program.

Its easy to run them from a proper command window. In case of Microsoft Visual studio 2008 on Intel64 architecture just type in the directory with theunzipped examples:

  • nmake em64t MKLROOT=path_to_mkl in the Visual Studio 2008 x64 Win64 Command Prompt window

WBR,
Vladimir

Leave a Comment

Please sign in to add a comment. Not a member? Join today