Linking Intel MKL is easy

Linking Intel MKL is easy

icl prog.c /Qmkl

or,

ifort prog.f /Qmkl

That's the easiest way if you are using one of the latest Intel compilers on Windows*. There are similar compiler options for Linux* and Mac OS* X as well.

Another easy way is to use our new dynamic linking model which requires a link to just one library. Add mkl_rt.lib to your Windows* link line or add -lmkl_rt to your Linux* or Mac OS* X link line.

These new options willwork for the cases usedby most users. Those who use less common interfaces or threading models may still want to visit the Link Line Advisor to find the right set of libraries.

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

If it would be so easy, no one would have problems....

The easiness claim was made with two explicit conditions:

1. "These new options will work for the cases used by most users."

AND

2. The current Intel compilers are used.

I have found no example for which the /Qmkl (-mkl on Linux/OSX) option did not work. If you have such examples I am sure that they will be given adequate attention if you divulge them here.

Quoting mecej4
The easiness claim was made with two explicit conditions:

1. "These new options willwork for the cases usedby most users."

AND

2. The current Intel compilers are used.

I have found no example for which the /Qmkl (-mkl on Linux/OSX) option did not work. If you have such examples I am sure that they will be given adequate attention if you divulge them here.

As a matter of fact, I think I found an example (literally from the mkl/examples directory) where these options were unsuccessful.

I'm using (ifort -V):

Intel Fortran Intel 64 Compiler XE for applications running on Intel 64, Version 12.1.4.319 Build 20120410

which I think is the latest version.

Take a look at my post here:

http://software.intel.com/en-us/forums/showthread.php?t=105667&o=a&s=lr

This thread was about using MKL with simple C and Fortran-77 codes.

Using the Fortran 95 interfaces requires the MKL Lapack95 and Blas95 libraries to be specified at link time, and requires specifying the path for the module files when calling from Fortran-9X.

Earlier releases of MKL came with no prebuilt Lapack95 and Blas95 libraries.

Can anyone tell me how to use command line argument for compilation of c program.

at the very top of this thread Todd already gave two simpliest examples for windows and linux

Hi

I used the Link line advisor, to build a link line for my application, with PARDISO, it gave this line

 -L$(MKLROOT)/lib/intel64 -lmkl_scalapack_ilp64 -lmkl_cdft_core -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -lmkl_blacs_intelmpi_ilp64 -lpthread -lm

With this compiler option :

 -openmp -DMKL_ILP64 -I$(MKLROOT)/include

I created this makefile :

CFLAGS = -Wall -O3  -I ../../../SPOOLES-2.2_Intel -I /opt/intel/mkl/include -DMKL_ILP64 -DARCH="Linux" -DSPOOLES -DARPACK -DPARDISO -DMATRIXSTORAGE -DUSE_MT=1
FFLAGS = -O3 -nofor-main -openmp

CC=icc
FC=ifort

.c.o :
        $(CC) $(CFLAGS) -c $<
.f.o :
        $(FC) $(FFLAGS) -c $<

include Makefile.inc

SCCXMAIN = ccx_2.5.c

OCCXF = $(SCCXF:.f=.o)
OCCXC = $(SCCXC:.c=.o)
OCCXMAIN = $(SCCXMAIN:.c=.o)

DIR=/home/alex/Desktop/Buildfactory/Calculix/SPOOLES-2.2_Intel
MKLROOT=/opt/intel/mkl
MKLPATH=/opt/intel/mkl/lib/intel64
MKLINCLUDE=/opt/intel/mkl/include

LIBS = \
       $(DIR)/libspooles.a \
        $(DIR)/MT/src/spoolesMT.a \
        ../../../ARPACK_Intel/libarpack_Linux.a \
        ../../../Pardiso/libpardiso412-INTEL120-X86-64.so \
       -lm -lc -lutil -ldl -lpthread

ccx_2.5: $(OCCXMAIN) ccx_2.5.a -L$(MKLROOT)/lib/intel64 -lmkl_scalapack_ilp64 -lmkl_cdft_core -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -lmkl_blacs_intelmpi_ilp64 -lpthread -lm  $(LIBS)
        ./date.pl; $(CC) $(CFLAGS) -c ccx_2.5.c; $(FC) $(FFLAGS) -o $@ $(OCCXMAIN) ccx_2.5.a  -L$(MKLROOT)/lib/intel64 -lmkl_scalapack_ilp64 -lmkl_cdft_core -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -lmkl_blacs_intelmp$

ccx_2.5.a: $(OCCXF) $(OCCXC)
        ar vr $@ $?

when i type Make this gives me an error :

alex@iskandhar:~/Desktop/Buildfactory/Calculix/CalculiX/ccx_2.5_Intel_Pa/src$ make
make: *** No rule to make target `-L/opt/intel/mkl/lib/intel64', needed by `ccx_2.5'.  Stop.
alex@iskandhar:~/Desktop/Buildfactory/Calculix/CalculiX/ccx_2.5_Intel_Pa/src$

Where is my mistake..?

I understand nothing of makefiles, all i know is that i have to link with Intel compiled Spooles, Arpack, and Pardiso, all the sources have been compiled with icc and ifort

Can someone help me in this..?

BR

Alex

Hi,

In the makefile you need the only two `$(OCCXMAIN) ccx_2.5.a' dependencies for target ccx_2.5. So the line should be as follows:

ccx_2.5: $(OCCXMAN) ccx_2.5.a

Also please double check used MKL libraris. E.g. there is incorrect  -lmkl_blacs_intelmp$ in your post

 

Thanks, -- Victor

How to reference these from a Fortran program?

I looked under PROJECT PREPERTIES, under FORTRAN,

and I jet FOUR possible choices. How would I know which one ?

Pr does it matter?

In some C/C++ project ( still in R&D state ) I decided to use runtime binding of MKL functions instead of dynamic linking. Here is a small example how I do it:
...
hMklRtDll = ::LoadLibrary( RTU("mkl_rt.dll") );
if( hMklRtDll == NULL )
break;

CrtPrintf( RTU("Dynamic library mkl_rt.dll loaded\n") );

pfMklGetVersionString = ( void ( * )( char *, int ) )::GetProcAddress( hMklRtDll, "MKL_Get_Version_String" );
if( pfMklGetVersionString == NULL )
break;
...
MKLVersion Ver = { 0x0 };
int iLenData = 256;
char szVerData[256] = { 0x0 };

pfMklGetVersionString( szVerData, iLenData );
CrtPrintfA( "\t%s\n", szVerData );
...
The most important things are that at a compilation phase I don't use any C++ compiler options related to MKL and at a linker phase I don't use any MKL libraries.

I just can't compile/link, with the lastest software, an old program that I made in 2006, and I need to continue.

Having linking problems, I started trying to compile/link spsv.f90 example in different ways -mkl option, link advisor and other. But I allways have problems.

In my application (several modules) the only problem listed in compiling time is about the line:

call spgvx(mesh%K, aux, eigen(1:15) ,itype=1 ,uplo='U' ,il=1 ,iu=15 ,m=inf1 ,info=inf2) !I've tryed changing mesh%K from pointer array to allocatable array, but it didn't make any difference (the working original was pointer array).

At the head I've called the correct "use" statement, the worked in the past

program axis_fem
    
!library callings
use IFPORT !similar to dflib
use mkl95_PRECISION, ONLY: WP=>SP
use mkl95_LAPACK !similar to IMSL

-mkl doesn't work to spsv.f90 neither to my application

the following compiling failed both my application and the example (changes in file names were needed to the example)

#Multi thread lp64
ifort -w $MKLROOT/lib/intel64/libmkl_blas95_lp64.a $MKLROOT/lib/intel64/libmkl_lapack95_lp64.a -Wl,--start-group  $MKLROOT/lib/intel64/libmkl_intel_lp64.a $MKLROOT/lib/intel64/libmkl_intel_thread.a $MKLROOT/lib/intel64/libmkl_core.a $MKLROOT/lib/intel64/libmkl_blacs_intelmpi_lp64.a -Wl,--end-group -liomp5 -lpthread -lm aux_math.f90 isoemat.f90 enodea.f90 TransfInterp.f90 equad9a.f90 emesha.f90 bondariesa.f90 dynamics.f90 postprocessing.f90 axis_condini.f90 -o axis.exe -i8 -I$MKLROOT/include/intel64/lp64 -I$MKLROOT/include

#single thread
#ifort $MKLROOT/lib/intel64/libmkl_blas95_lp64.a $MKLROOT/lib/intel64/libmkl_lapack95_lp64.a -Wl,--start-group  $MKLROOT/lib/intel64/libmkl_intel_lp64.a $MKLROOT/lib/intel64/libmkl_sequential.a $MKLROOT/lib/intel64/libmkl_core.a $MKLROOT/lib/intel64/libmkl_blacs_intelmpi_lp64.a -Wl,--end-group -lpthread -lm aux_math.f90 isoemat.f90 enodea.f90 TransfInterp.f90 equad9a.f90 emesha.f90 bondariesa.f90 dynamics.f90 postprocessing.f90 axis_condini.f90 -o axis.exe  -I$MKLROOT/include/intel64/lp64 -I$MKLROOT/include

#single dynamic
#ifort -L$MKLROOT/lib/intel64 $MKLROOT/lib/intel64/libmkl_blas95_lp64.a $MKLROOT/lib/intel64/libmkl_lapack95_lp64.a -lmkl_rt -lpthread -lm aux_math.f90 isoemat.f90 enodea.f90 TransfInterp.f90 equad9a.f90 emesha.f90 bondariesa.f90 dynamics.f90 postprocessing.f90 axis_condini.f90 -o axis.exe -I$MKLROOT/include/intel64/lp64 -I$MKLROOT/include

Can somebody help me please.

I know that there is a makefile to buid the examples, but it doesn't allow me to find out the problem I'm having.

Thanks

Attachments: 

AttachmentSize
Download spsv.f903.18 KB
Ifort on i7 CPU 860 @ 2.80GHz × 8 running Ubuntu 12.04 LTS

ifort -mkl spsv.f90 -lmkl_lapack95

Thanks mecej4, but it haven't worked yet.

"arantes@arantes:~/PROGRAMACAO/mestrado$ ifort -mkl spsv.f90 -o spsv.exe -lmkl_lapack95
ld: cannot find -lmkl_lapack95
arantes@arantes:~/PROGRAMACAO/mestrado$ echo $MKLROOT
/opt/intel/composer_xe_2013.3.163/mkl

arantes@arantes:~/PROGRAMACAO/mestrado$ ifort -mkl -lmkl_lapack95 spsv.f90 -o spsv.exe
ld: cannot find -lmkl_lapack95 "

"I can compile and run some lapack routines. I've used a mkl lapack routine (DGEMM) in the attached source code, and I can compile it in a easy way.

"

arantes@arantes:~/PROGRAMACAO$ ifort -mkl testelapack.f90 -o teste.exe -liomp5 -lpthread -lm
arantes@arantes:~/PROGRAMACAO$ ./teste.exe
 Top left corner of matrix A:
          1.           2.           3.           4.           5.           6.
       2001.        2002.        2003.        2004.        2005.        2006.
       4001.        4002.        4003.        4004.        4005.        4006.
       6001.        6002.        6003.        6004.        6005.        6006.
       8001.        8002.        8003.        8004.        8005.        8006.
      10001.       10002.       10003.       10004.       10005.       10006.
 
 Top left corner of matrix B:
         -1.          -2.          -3.          -4.          -5.          -6.
     -10001.      -10002.      -10003.      -10004.      -10005.      -10006.
     -20001.      -20002.      -20003.      -20004.      -20005.      -20006.
     -30001.      -30002.      -30003.      -30004.      -30005.      -30006.
     -40001.      -40002.      -40003.      -40004.      -40005.      -40006.
     -50001.      -50002.      -50003.      -50004.      -50005.      -50006.
 
 Top left corner of matrix C:
 -2.6667E+13  -2.6667E+13  -2.6667E+13  -2.6667E+13  -2.6667E+13  -2.6667E+13
 -6.6647E+13  -6.6647E+13  -6.6647E+13  -6.6647E+13  -6.6647E+13  -6.6647E+13
 -1.0663E+14  -1.0663E+14  -1.0663E+14  -1.0663E+14  -1.0663E+14  -1.0663E+14
 -1.4661E+14  -1.4661E+14  -1.4661E+14  -1.4661E+14  -1.4661E+14  -1.4661E+14
 -1.8659E+14  -1.8659E+14  -1.8659E+14  -1.8659E+14  -1.8659E+14  -1.8659E+14
 -2.2657E+14  -2.2657E+14  -2.2657E+14  -2.2657E+14  -2.2657E+14  -2.2657E+14
 
 Example completed.
arantes@arantes:~/PROGRAMACAO$"

I can just type:

"arantes@arantes:~/PROGRAMACAO$ ifort -mkl testelapack.f90 -o teste.exe
arantes@arantes:~/PROGRAMACAO$"

And get the same results (it also retuns a threaded executable for lapack routine, not the openmp part).

I belive that there is some problem related to configuration, that is avoiding me to use the lapack95.

If you have more suggestions I'll be happy to try them.

Attachments: 

AttachmentSize
Download testelapack.f901.07 KB
Ifort on i7 CPU 860 @ 2.80GHz × 8 running Ubuntu 12.04 LTS

If you are building a 64-bit target, there are two versions of the Lapack95 libraries, depending on whether you have chosen the LP64 or the ILP64 models. The MKL Link Line Advisor would have given you the correct command line arguments, if you had followed it faithfully. For the simple example that you gave above which uses Lapack95, the command line for LP64 would be

ifort -mkl spsv.f90 -lmkl_lapack95_lp64

The second example that you gave, testelapack.f90, does not use Lapack95 and that is why your build succeeded.

Dear mecej4

It worked, I'll try to find what is happening to my original program (written and working in 2006), There must be a integer*8 transfered to a routine that spects a integer*4.

Thanks

Ifort on i7 CPU 860 @ 2.80GHz × 8 running Ubuntu 12.04 LTS

>>...Those who use less common interfaces or threading models may still want to visit the Link Line Advisor to find the right
>>set of libraries...

The article at:

Web-link: software.intel.com/en-us/articles/intel-mkl-link-line-advisor

doesn't specify what executable needs to be executed depending on a platform.

Dear mecej4

Your tip worked to spsv.f90 example. I cant undestand way -lmkl_lapack95 is needed after -mkl that worked fine to a simple gemm test.

But when compiling my old program I get this message:

arantes@arantes:~/PROGRAMACAO/mestrado$ ifort -mkl aux_math.f90 isoemat.f90 enodea.f90 TransfInterp.f90 equad9a.f90 emesha.f90 bondariesa.f90 dynamics.f90 postprocessing.f90 axis_condini.f90 -o axis.exe -lmkl_lapack95_lp64
/tmp/ifortgvLH55.o: In function `dynamics_mp_newmark_':
dynamics.f90:(.text+0x6fb): undefined reference to `sspmv_mkl95_'
dynamics.f90:(.text+0x725): undefined reference to `sspmv_mkl95_'

The original was with spgvx, but the error is the same. All integers were defined as integer*4 to avoid the lp64 vs ilp64 thing.

The commented is the original and the other is the current:

...

        !call spgvx(mesh%K, aux, eigen(1:15) ,itype=1 ,uplo='U' ,il=1 ,iu=15 ,m=inf1 ,info=inf2)
        call spgv(mesh%K, aux, eigen, itype=1, uplo='U', info=inf2)

...

mesh is a composite type created in one of my modules, that were compiled ok, I can see the *.mod files in the folder. mesh%K is a ponter array with form mesh%K(:).

"type(E_mesh_a),pointer::mesh" as declared in the main program.

Kind Regards.

Arantes

Ifort on i7 CPU 860 @ 2.80GHz × 8 running Ubuntu 12.04 LTS

Dear mecej4

Please forget my last post. Ive forgot to use "-lmkl_blas95_lp64". After typed this, everything worked fine.

Thanks

Ifort on i7 CPU 860 @ 2.80GHz × 8 running Ubuntu 12.04 LTS

Quote:

arantesb wrote:
I can't understand way -lmkl_lapack95 is needed after -mkl; that worked fine for a simple gemm test.

The Fortran-95 routines are wrappers around the Fortran-77 routines. As the MKL documentation states, the former have shorter argument lists, may have optional arguments, and a single generic procedure name covers arguments of various real and complex types. Some glue code is needed to decode the input arguments, fill in default values of optional arguments that are not present, allocate work arrays as needed, select and call the Fortran-77 routines that are appropriate for the argument types, and map the results back to the Fortran-95 arguments. This glue code is present in the mkl_lapack95 and mkl_blas95 libraries. If your code makes any calls to routines in these libraries, you must explicitly specify them in the linking command.

Here is an article about custom static linkage for those people who cannot employ the the begin-group / end-group linker options e.g., due to a legacy build system.

Quote:

mecej4 wrote:

Quote:

arantesb wrote:I can't understand way -lmkl_lapack95 is needed after -mkl; that worked fine for a simple gemm test.

The Fortran-95 routines are wrappers around the Fortran-77 routines. As the MKL documentation states, the former have shorter argument lists, may have optional arguments, and a single generic procedure name covers arguments of various real and complex types. Some glue code is needed to decode the input arguments, fill in default values of optional arguments that are not present, allocate work arrays as needed, select and call the Fortran-77 routines that are appropriate for the argument types, and map the results back to the Fortran-95 arguments. This glue code is present in the mkl_lapack95 and mkl_blas95 libraries. If your code makes any calls to routines in these libraries, you must explicitly specify them in the linking command.

I am using MKL 10.3 in Visual Studio 2010 with Composer XE 2011. I am able to use the F77 interfaces to lapack, but could not figure out how to use the F95 interfaces. What shoould I do in Visual Studio so the linker finds the F95 interface?

In order to use the Lapack95 library, you need to

  1. Add appropriate INCLUDE and/or USE statements in your source code,
  2. Add the directory containing the Lapack95 module (.MOD) files to the compiler's include path list, or to the INCLUDE environment variable
  3. Add the directory containing the Lapack95 libraries to the linker's search path, or to the LIB environment variable.
  4. Add the Lapack95 libraries to the list of additional libraries to search at link time.

I am using Visual Studio 2008, C++.  I have followed the instructions to set environment variables.  When I use the Link Advisor

cl -D"%MKLROOT%\include" dgemm_example.cpp mkl_intel_ilp64.lib mkl_core.lib mkl_sequential.lib

I receive the following link errors:

dgemm_example.obj : error LNK2019: unresolved external symbol _MKL_free referenced in function _main
dgemm_example.obj : error LNK2019: unresolved external symbol _MKL_malloc referenced in function _main

 

Hi Emmet, 

I guess, the possible problem is that cl.exe is for 32bit application and mkl library is for 64bit application.  You can check it by  enter command > cl

If it show something like  Microsoft (R) 32-bit C/C++ Optimizing

Then you can try the command: 

C:\MKL_issue>"C:\Program Files (x86)\Intel\Composer XE 2013_SP1\mkl\bin\mklvars.bat" ia32

>cl dgemm_example.cpp mkl_intel_c.lib mkl_core.lib mkl_sequential.lib

Best Regards,

Ying 

P.S : Some on-line article about the IA32 architecture and 64 architecture. Please see

1. http://software.intel.com/en-us/articles/how-to-build-mkl-application-in...  

2.  http://software.intel.com/en-us/articles/ia-32-intelr-64-ia-64-architect...

3.  http://software.intel.com/en-us/forums/showthread.php?t=105918

 

Emmett B: Try using

cl -I"%MKLROOT%include"
instead of
cl -D"%MKLROOT%include" 
In other words, specify the directory for additional include files, rather than define a symbol that is never invoked in the program source.

I wrote a Console App in VS 2010, Fortran XE, Win32, Debug. The MKL are installed together w/XE. 

In VS2010 I set Project> Properties> Fortran> Libraries> Use MKL> yes.

I get: 

Error    1     error LNK2019: unresolved external symbol _SBGVX referenced in function _MAIN__    Console1.obj    

What am I missing?

 

 

Hello Ever,

What Fortran XE version are you installed?  Not sure if there is integration issue.  I checked the latest Visual Fortran Composer XE 2013 update 2 and VS2010.  It seems work fine.

The error itself hints the MKL library is not linked in your program or in wrong path.

You can fix the problem by set the library and library path manually, like the step 3.2 and 3.3.

http://software.intel.com/en-us/articles/how-to-build-mkl-application-in-intel-visual-fotran-msvc2005

and check why the Use MKL  Parallel /Qmkl:Parallel ( you mentioned it is Use MKL > yes, could you please attach one screenshot to us?)  as my reply in http://software.intel.com/en-us/forums/topic/506620

Best Regards,

Ying

Ever B.: The symbol _SBGVX is not present in the MKL_Lapack95 library, because that is only an F95 interface name. If you apply Dumpbin to the .OBJ file that the compiler produced, you will find that the "specific" symbol name is _DSBGVX_MKL95 (this is for Windows-32; the name decoration varies with the target architecture and the OS).

When the documentation names a subroutine or function with "Syntax ... Fortran 95", especially when that subroutine or function takes optional arguments, take that as a hint that an implicit interface will not be sufficient. The necessary explicit interface may be provided conveniently by adding a USE statement. In this particular case, use mkl95_lapack is what you need. Please refer to my response to your other post, MKL LAPACK SBGVX example.

The problem can be solved like this: The heading of the program unit should contain this

    include 'lapack.f90'    ! interfaces for .f90, .f95 are here
    program EigenVal
    use mkl95_lapack        ! variables needed are defined here
    implicit none
    ! Variables
    ! http://software.intel.com/sites/products/documentation/hpc/mkl/mklman/index.htm#GUID-659297EE-D35B-428C-ABBE-1A7EBE2B0F6E.htm#GUID-659297EE-D35B-428C-ABBE-1A7EBE2B0F6E 

Band storage is defined here:

    !copy to band form using 
    !http://software.intel.com/sites/products/documentation/hpc/mkl/mklman/index.htm#GUID-659297EE-D35B-428C-ABBE-1A7EBE2B0F6E.htm#GUID-659297EE-D35B-428C-ABBE-1A7EBE2B0F6E
    KGB = 0 !band storage
    KMB = 0 !band storage
    do i=1,N
        do j=1,N
            if (i.ge.max(1,j-ku).and.i.le.min(N,j+kl)) KGB(ku+1+i-j,j) = KG(i,j)    !from full to band storage
            if (i.ge.max(1,j-ku).and.i.le.min(N,j+kl)) KMB(ku+1+i-j,j) = KM(i,j)    !from full to band storage
        enddo
    enddo

Then, use the F95 calling syntax:

    call sbgvx(KGB, KMB, w)     ! .f95 syntax calling. Optional arguments can be added as needed. 

Thank you all for your help !

Login to leave a comment.