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.

Select Intel Compiler for CMake

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

Intel MKL with CMake

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

To enable CMake find_package(MKL) you will need the file inside your project cmake/Modules/FindMKL.cmake

Fortran, MKL and CMake

Fortran with MKL MUST have the compiler options -fdefault-integer-8 -m64 for EACH code compiled into object files and executables at compile time, not just link time.

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

cmake_minimum_required(VERSION 2.8.12)
project(mytest Fortran)

# NOTE: -fdefault-integer-8 -m64  are crucial for MKL to avoid SIGSEGV at runtime!
add_compile_options(-fdefault-integer-8 -m64)

add_executable(inteld intel_dgemm.f90)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
# https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor 
find_package(MKL)
if(MKL_FOUND)
    include_directories(${MKL_INCLUDE_DIRS})
    target_link_libraries(inteld ${MKL_LIBRARIES} pthread dl)
else()
    find_package(BLAS REQUIRED)
    target_link_libraries(inteld blas)
endif()

That example also has a fallback to open-source BLAS such as OpenBLAS in the else() stanza.

Note: If you also have C/C++ in your project and the Fortran options are making your compiler fail, you can do:

set(FFLAGS ${FFLAGS} -fdefault-integer-8 -m64)
target_compile_options(inteld PUBLIC ${FFLAGS})

Verify you’re using MKL

If you’re doing things right, your CMake script falls back to non-MKL options if MKL is not installed. How can we get runtime confirmation that MKL is being used via MKL_VERBOSE?

MKL_VERBOSE=1 ./myprogram

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

Leave a Comment