Using Intel oneAPI and MKL with CMake

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

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

Specify the environment variables CC, CXX, FC to indicate desired compilers.

export FC=ifx CC=icx CXX=icpx

cmake -B build

cmake --build build

For Windows, the procedure is similar:

set FC=ifx
set CC=icx
set CXX=icx

cmake -B build

cmake --build build

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::LAPACK)

We have created an easier to use FindLAPACK.cmake that handles MKL and non-MKL LAPACK.


To see the compiler commands CMake is issuing, use

cmake --build build -v

Refer to Intel Link Advisor.


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.