Developer Guide

Developer Guide for Intel® oneAPI Math Kernel Library Linux*

ID 766690
Date 7/13/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

CMake Config for oneMKL

If you want to integrate oneMKL into your CMake projects, starting with the Intel® oneAPI Math Kernel Library (oneMKL) 2021.3 release, MKLConfig.cmake is provided as part of the package and installation. MKLConfig.cmake supports all oneMKL configurations, compilers, and runtimes, as the oneMKL product itself. Help/usage is provided in the top section of MKLConfig.cmake.

Example

NOTE:
The following example demonstrates how to link with oneMKL using the C/Fortran API. For the oneMKL SYCL functionality, use the MKL::MKL_DPCPP target instead of MKL::MKL. For more information about how to link cluster functionality, enable the OpenMP Offload feature, specify the link type and threading type, and so forth, refer to the input and output parameters listed in the comments at the top of the MKLConfig.cmake file.
my_test/
  |_ build/                <-- Out-of-source build directory
  |_ CMakeLists.txt        <-- User side project's CMakeLists.txt
  |_ app.c                 <-- Source file that uses oneMKL API

app.c

#include <stdio.h>
#include "mkl.h"

int main()
{
    MKLVersion mkl_version;
    mkl_get_version(&mkl_version);

    printf("You are using oneMKL %d.%d\n", mkl_version.MajorVersion, mkl_version.UpdateVersion);

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

project(oneMKL_Example LANGUAGES C)

find_package(MKL CONFIG REQUIRED PATHS $ENV{MKLROOT})

add_executable(myapp app.c)
target_link_libraries(myapp PUBLIC MKL::MKL)

Command line

# Source the oneAPI setvars.sh script beforehand
build$ cmake .. -DCMAKE_C_COMPILER=icx
build$ cmake --build .

If your application requires a customized setting, the following CMakeLists.txt demonstrates how to achieve a more fine-grained control.

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

project(oneMKL_Example LANGUAGES C)

find_package(MKL CONFIG REQUIRED PATHS $ENV{MKLROOT})
message(STATUS "Imported oneMKL targets: ${MKL_IMPORTED_TARGETS}")

add_executable(myapp app.c)
target_compile_options(myapp PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
target_include_directories(myapp PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(myapp PUBLIC $<LINK_ONLY:MKL::MKL>)
NOTE:

When the Ninja build system is in use, Ninja 1.10.2 or later is required for Fortran support.