Using Intel compilers and MKL with CMake and Make

There can be substantial speed boosts from using Intel compilers. Intel Parallel Studio gives advanced debuggers and performance measurements. Intel MKL can give a significant speed boost even to non-Intel compilers for certain math operations.

Ninja backend

On any OS and particularly Windows we generally use CMake Ninja generator. Visual Studio backend often causes additional difficulties, so unless you know you need Visual Studio we recommended making the default CMake generator Ninja by setting environment variable:

CMAKE_GENERATOR=Ninja

or do this one-time by:

cmake -G Ninja

Linux / MacOS

To build a CMake project, note that we specify the environment variables CC, CXX, FC to signal to the build system which compilers are desired.

export FC=ifort CC=icc CXX=icpc

cmake -B build

cmake --build build

If for some reason you do not use Ninja backend with CMake, another MacOS / Linux CMake backend is:

cmake -G "Unix Makefiles"

Windows

To build a CMake project, note that we specify the environment variables CC, CXX, FC to signal to the build system which compilers are desired.

set FC=ifort
set CC=icl
set CXX=icl

cmake -B build

cmake --build build

If for some reason you do not use Ninja backend with CMake, another Windows CMake backend is:

cmake -G "MinGW Makefiles"

Intel MKL with CMake

MKL can be used with any compiler, e.g. ifort or gfortran. An example CMakeLists.txt using the factory FindLAPACK.cmake:

project(MKLtest Fortran)

# allows selecting parallel, sequential, 32/64 bit
# This example is sequential 32 bit

set(BLA_VENDOR Intel10_64lp_seq)
find_package(LAPACK REQUIRED)

add_executable(mytest main.f90)
target_link_libraries(mytest ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})

verbose build output

In general to see the compiler commands CMake is issuing, use

cmake --build build -v

Refer to Intel Link Advisor.

Verify MKL is used

Get runtime confirmation that MKL is being used via MKL_VERBOSE.

  • Linux / MacOS:

    MKL_VERBOSE=1 ./mytest
    
  • Windows

    set MKL_VERBOSE=1
    mytest.exe
    

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.

Notes

An easier to use FindLAPACK.cmake that handles MKL and non-MKL LAPACK