How to use Intel compilers and MKL with CMake and Make

Using the GNU GCC family of compilers is typically easier than using the Intel compilers. However, there can be substantial speed boosts from using the Intel compilers, as well as the ability to use advanced debuggers and compile for supercomputers and laptops with the same command. Also, using the Intel Math libraries MKL can give a big speed boost to any compiler where the code is doing math operations.

Intel Compiler with CMake

FC=ifort CC=icc CXX=icpc cmake ..


On Windows, you may need to specify the Makefile type:

FC=ifort CC=icc CXX=icpc cmake -G "MinGW Makefiles" ..


Intel MKL with CMake

MKL can be used with any compiler, e.g. ifort or gfortran.

Fortran, MKL and CMake

In your CMakeLists.txt that means for a test file intel_dgemm.f90 you’d have

cmake_minimum_required(VERSION 3.0)
project(MKLtest Fortran)

# allows selecting parallel, sequential, 32/64 bit
# This example is sequential 32 bit
set(BLA_VENDOR Intel10_64lp_seq)
find_package(BLAS REQUIRED)

add_executable(inteld intel_dgemm.f90)
target_link_libraries(inteld ${BLAS_LIBRARIES})

For any issues, use make VERBOSE=1 and refer to Intel Link Advisor. This example also has a fallback to open-source BLAS such as OpenBLAS in the else() stanza.

Verify MKL is used

The CMake script falls back to non-MKL options if MKL is not installed. Get runtime confirmation that MKL is being used via MKL_VERBOSE.

MKL_VERBOSE=1 ./inteld

That gives verbose text output upon use of MKL functions. That runtime option does slow down MKL performance, so normally we don’t use it.