Math Kernel Library (MKL) - bwHPC Wiki Math Kernel Library (MKL) - bwHPC Wiki

Math Kernel Library (MKL)

From bwHPC Wiki
Jump to: navigation, search
Description Content
module load numlib/mkl
License Commercial. See EULA.
Citing n/a
Links Intel MKL Homepage | Online-Documentation
Graphical Interface No

1 Description

Intel MKL (Math Kernel Library) is a library of optimized math routines for numerical computations such as linear algebra (using BLAS, LAPACK, ScaLAPACK) and discrete Fourier Transformation. With its standard interface in matrix computation and the interface of the popular fast Fourier transformation library fftw, MKL can be used to replace other libraries with minimal code changes. In fact a program which uses FFTW without MPI doesn't need to be changed at all. Just recompile it with the MKL linker flags.

2 Versions and Availability

A list of currently available MKL modules can be obtained from the
Cluster Information System CIS
Show a list of available versions using 'module avail numlib/mkl' on any HPC-C5 cluster.

: EXAMPLE bwUniCluster
$ module avail numlib/mkl
------------------------------ /opt/bwhpc/common/modulefiles ------------------------------
numlib/mkl/10.3.12         numlib/mkl/11.1.4(default)
numlib/mkl/11.0.5          numlib/mkl/11.2.3

3 Local documentation

There is some information in the module help file accessible via 'module help numlib/mkl'- command.

: EXCERPT ONLY
$ module help numlib/mkl
----------- Module Specific Help for 'numlib/mkl/11.1.4' ----------
This module provides the Intel(R) Math Kernel Library (MKL)
version 11.1.4, a fast and reliable implementation
of BLAS/LAPACK/FFTW (see also 'http://software.intel.com/en-us/intel-mkl/').

The preferable compiler for this MKL version is 'compiler/intel/14.0'. Linking
with other compilers like GNU, PGI and SUN is possible. The desired compiler
module (exception system GNU compiler) has to be loaded before using MKL.

Local documentation:

  Man pages in '$MKL_MAN_DIR/man3', e.g. 'man dotc'.
  firefox  $MKL_DOC_DIR/mkl_documentation.htm
  acroread $MKL_DOC_DIR/l_mkl_11.1.4.211.mklman.pdf
  acroread $MKL_DOC_DIR/l_mkl_11.1.4.211.mkl_11.1.4_lnx_userguide.pdf

Linking examples (ifort compiler with support for blas and lapack):

* Dynamic linking of myprog.f and parallel MKL supporting the LP64 interface:

  ifort myprog.f -L${MKL_LIB_DIR} -I${MKL_INC_DIR}            \
        -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -liomp5 -lpthread
[... t.b.c. ...]

After loading the module, the environment variable $MKL_DOC_DIR points to the local documentation folder. Various examples can be found in $MKLROOT/examples.

4 MKL-Specific Environments

To see a list of all MKL environments set by the 'module load'-command use 'env | grep MKL'. Or use the command 'module display numlib/mkl/version'.
Example (bwUniCluster)

$ module load numlib/mkl
$ env | grep MKL
MKLROOT=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl
MKL_LIB_MIC_COM=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/lib/mic
MKL_DOC_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/composerxe/Documentation/en_US/mkl
MKL_NUM_THREADS=1
MKL_HOME=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl
MKL_LIB_MIC=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl/lib/mic
MKL_MAN_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/man/en_US
MKL_EXA_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/composerxe/Samples/en_US
MKL_STA_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl/lib/intel64_static
MKL_INC_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl/include
MKL_BIN_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl/bin
MKL_LIB_DIR=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/mkl/lib/intel64
MKL_VERSION=11.1.4
MKL_LIB_COM=/opt/bwhpc/common/compiler/intel/compxe.2013.sp1.4.211/lib/intel64

5 Compiling and linking

Compilation is possible with both GCC and Intel compilers but it is easier for Intel compilers, so this case is explained here. After loading the compiler and the library module with

$ module load compiler/intel
$ module load numlib/mkl

you can include the MKL header file in your program:

#include <mkl.h>

Compilation is simple:

$ icpc -c example_mkl.c

When linking the program you have to tell the compiler to link against the mkl library:

$ icpc example_mkl.o -mkl

With the -mkl switch the intel compiler automatically sets the correct linker flags but you can specify them explicitly for example to enable static linking or when non-intel compilers are used. Information about the different options can be found at https://software.intel.com/en-us/node/438568 and especially helpful is the MKL link line advisor at https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor. By default $MKL_NUM_THREADS is set to 1 and so only one thread will be created, but if you feel the need to run the computation on more cores (after benchmarking) you can set $MKL_NUM_THREADS to a higher number.

5.1 FFTW Interface to Intel Math Kernel Library (MKL)

Sometimes, FFTW is not available on your cluster. You can use the MKL library instead and include the FFTW functions, too.

Intel Math Kernel Library (MKL) offers FFTW2 and FFTW3 interfaces to Intel MKL Fast Fourier Transform and Trigonometric Transform functionality. The purpose of these interfaces is to enable applications using FFTW to gain performance with Intel MKL without changing the program source code. To include the proper header files use the compiler option

-I${MKL_INC_DIR}/fftw

If you want to link dynamically against the fftw functions you can just use the flag

-mkl

but when using static linking you have to link against the correct library in the directory

${MKL_HOME}/interfaces/

See the corresponding webpages:

6 Examples

To help getting started we provide two C++ examples. The first one computes the square of a 2x2 matrix:

#include <iostream>
#include <mkl.h>
using namespace std;

int main()
{
    double m[2][2] = {{2,1}, {0,2}};
    double c[2][2];

    for(int i = 0; i < 2; ++i)
    {
        for(int j = 0; j < 2; ++j)
            cout << m[i][j] << " ";

        cout << endl;
    }

    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 2, 2, 2, 1.0, &m[0][0], 2, &m[0][0], 2, 0.0, &c[0][0], 2);

    cout << endl;

    for(int i = 0; i < 2; ++i)
    {
        for(int j = 0; j < 2; ++j)
            cout << c[i][j] << " ";

        cout << endl;
    }

    return 0;
}

And the second one does a fast Fourier transformation using the Intel MKL interface (DFTI):

#include <iostream>
#include <complex>
#include <cmath>
#include <mkl.h>
using namespace std;

int main()
{
    const int N = 3;
    complex<double> x[N] = {2, -1, 0.5};

    cout << "Input: " << endl;

    for(int i = 0; i < N; i++)
        cout << x[i] << endl;

    DFTI_DESCRIPTOR_HANDLE desc;

    DftiCreateDescriptor(&desc, DFTI_DOUBLE, DFTI_COMPLEX, 1, N);
    DftiCommitDescriptor(desc);
    DftiComputeForward(desc, x);
    DftiFreeDescriptor(&desc);

    cout << "\nOutput: " << endl;

    for(int i = 0; i < N; i++)
        cout << x[i] << endl;

    cout << "\nTest the interpolation function f:" << endl;

    for(int i = 0; i < N; i++)
    {
        double t = i/(double)N;
        complex<double> u(0, 2*M_PI*t);
        complex<double> z = exp(u);
        complex<double> w = 1.0/N * (x[0] + x[1]*z + x[2]*z*z);

        cout << "f(" << t << ") = " << w << endl;
    }

    return 0;
}