- For general advice on beginning use of Intel MKL visit the Getting Started Tutorial
This article is intended to help users get started with intel® MKL quickly. Please click the topic you are interested in from the below table.
#1: Set build environment variable
#2: Write your first intel® MKL program
#3: Use intel® MKL Source Code Examples
• Set run time environment variable and executing
• Multi-threading (Multi-Core) Support
• Building a Custom Dynamic (Shared) Library
Before getting started, it may be helpful to take a look at the following links available online for the latest information regarding the Intel® MKL library:
Please register your product using your preferred email address. This helps Intel recognize you as a valued customer in the support forum and insures that you will be notified of product updates. You can read Intel's Online Privacy Notice Summary if you have any questions regarding the use of your email address for software product registration.
Implementation stage #1: Environment Variables
A script (batch file on Windows, shell script on other platforms) located in the <mkl install directory>>\ compilers_and_libraries_2019\windows\mkl\bin\ directory (by default, it is C:\Program Files (x86)\IntelSWTools\ compilers_and_libraries_2019\windows\mkl\bin\) can be used to set the MKLROOT, LIB, INCLUDE and any required system-specific environment variables to point to the appropriate MKL library directories. Use of this script is an optional, but convenient, means by which to configure your development system for compiling and linking with Intel® MKL.
You run the script with a single input that specifies the primary processor architecture of interest. For example, mklvars.bat ia32 configures the environment variables for compiling and linking a 32-bit Intel MKL application. Likewise, mklvars.bat intel64 configures your development environment to build 64-bit intel® MKL applications.
Note: in the text that follows, <arch> refers to the primary processor architecture, such as ia32 or intel64 and <MKLROOT> refers to the Intel MKL installation directory. Additionally, '\' and '/' are used interchangeably as directory separators.
An alternate architecture-specific script file is located in the <MKLROOT>\bin\<arch> directory that can be run without any input parameters. It calls the mklvars script mentioned above with the appropriate architecture as an input parameter.
Once defined, you can reference the MKLROOT variable within your makefiles and/or project files to locate the header and library files necessary to compile and link applications using Intel MKL.
Before running the build scripts provided with the Intel MKL example programs you must first be sure to set the MKLROOT environment variable to point to the MKL installation directory on your system. MKLROOT must point to that directory which contains the Intel MKL bin, lib, include and tools directories. The sample build scripts will reference the MKLROOT environment variable in order to locate Intel MKL and any additional tools required.
On a Windows system the following batch files are available to configure the environment for building Intel MKL applications:
On a Linux* system the following shell scripts are available to configure the environment for building Intel MKL applications:
Implementation stage #2: Write your first MKL program
Step1: Include Files
Intel MKL functions and structures are defined within several header files for C and interface files and modules for Fortran. These can be found in the <MKLROOT>\include directory and for Fortran several nested directories depending on architecture and interface. The "mkl.h" and "mkl.fi" files include all of these. The environment variable scripts can be used to point to the appropriate directories.
Step 2: Calling Intel MKL Functions
Calling Intel MKL function is as simple as calling any C function or FORTRAN function in your program. MKL provides both FORTRAN and C interface for most routines. Please see the MKL manual to find the function list you hope to use.
For example. compute C=A*B, A is mxn matrix, B is nxk matrix.
sgemm(transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc)
Step 3: Linking Intel® MKL library with your program
The dynamic library dispatcher and static library mechanisms in Intel MKL are designed to make the process of calling Intel MKL functions as simple as calling any library. Multiple SIMD-optimized versions of each function are concealed behind a single entry point. But you don't need to worry about the optimized code for get started, just make sure link the right libraries for your application.
Please click the intel® MKL link line advisor to find the needed libraries, example
The command line is like below:
> ..\compilers_and_libraries_2019\windows\mkl\bin\mklvars.bat" ia32
> cl.exe mkl_lab_solution.c mkl_intel_c.lib mkl_core.lib mkl_intel_thread.lib libiomp5md.lib
If with Intel(R) Parallel Studio XE 2019, as intel mkl is integrated to it. The command line is simpler,
> icl.exe mkl_lab_solution.c /Qmkl ( Please see more details in MKL's Developer Guide - Linking Your Application with the Intel® Math Kernel Library - Quick start )
If with Microsoft* Visual Studio, please refer to Compile and Link MKL with Microsoft* Visual C/C++* or Automatically Linking Your Intel® Visual Fortran Project with Intel® MKL
1) if with gcc
$source /opt/intel/compilers_and_libraries_2019/linux/mkl/bin/mklvars.sh ia32
$gcc -m32 mkl_lab_solution.c -lmkl_intel -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm
2) If with Intel(R) Parallel Studio XE 2019, the command line is easy
$ source /opt/intel/bin/iccvars.sh intel64 ( or ia32 )
$ icc mkl_lab_solution.c -mkl
$ icc mkl_lab_solution.c -mkl
Implementation stage #3: Intel® MKL Source Code Sample
1. Intel® MKL provide source code samples, almost for each function, like BLAS, LAPACK, FFT etc, both C and Fortran source code. You may find them under the <mkl install directory>\ compilers_and_libraries\windows\mkl\examples.
2. Intel® MKL knowledge base also provide many source code sample, please explore sample there, for example,
Run Stage: Executing program
You can set run-time environment as above description. If you have done with script, you can run the executable program directly.
$ source /opt/intel/compilers_and_libraries_2019/linux/mkl/bin/mklvars.sh intel64
Enter matrix size N=1024
Dgemm_multiply(). Elapsed time = 0.49 seconds
or set LD_LIBRARY_PATH under linux and PATH under windows.
$ export LD_LIBRARY_PATH=/opt/intel/compilers_and_libraries_2019/linux/mkl/lib/intel64:$LD_LIBRARY_PATH
Advantage Stage: Multi-threading (Multi-Core) Support
Intel MKL 2019 uses OpenMP/TBB threading internally to support parallelism and leverage the performance benefits of multicore architectures.
- In some cases you can use OpenMP environment variables and APIs to control OpenMP threading behavior within Intel MKL. In addition, dedicated functions have been introduced to control threading behavior of Intel MKL functions independent of the OpenMP controls. For detailed information regarding OpenMP and Intel MKL, please refer to the reference manuals and the following knowledge base articles: Intel® MKL 10.x Threading Note: You must include the appropriate OpenMP shared-library file in your PATH. There are known incompatibilities between various versions of the OpenMP libraries. If you encounter problems, make sure that there is only one version of OpenMP located in your PATH environment variable.
- In the case of TBB Threading, please refer to the MKL User's Guide and Using Intel MKL and Intel TBB in the same application knowledge base article to get more details
Advantage Stage: Building a Custom Dynamic (Shared) Library
Please see the following Intel MKL documentation for information on building a custom dynamic library: Intel® MKL User's Guide - Building Custom Dynamic-link Libraries
Product and Performance Information
Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.