Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FindMKL.cmake
################################################################################
#
# \file cmake/FindMKL.cmake
# \author J. Bakosi
# \copyright 2012-2015, Jozsef Bakosi, 2016, Los Alamos National Security, LLC.
# \brief Find the Math Kernel Library from Intel
# \date Thu 26 Jan 2017 02:05:50 PM MST
#
################################################################################
# Find the Math Kernel Library from Intel
#
# MKL_FOUND - System has MKL
# MKL_INCLUDE_DIRS - MKL include files directories
# MKL_LIBRARIES - The MKL libraries
# MKL_INTERFACE_LIBRARY - MKL interface library
# MKL_SEQUENTIAL_LAYER_LIBRARY - MKL sequential layer library
# MKL_CORE_LIBRARY - MKL core library
#
# The environment variables MKLROOT and INTEL are used to find the library.
# Everything else is ignored. If MKL is found "-DMKL_ILP64" is added to
# CMAKE_C_FLAGS and CMAKE_CXX_FLAGS.
#
# Example usage:
#
# find_package(MKL)
# if(MKL_FOUND)
# target_link_libraries(TARGET ${MKL_LIBRARIES})
# endif()
# If already in cache, be silent
if (MKL_INCLUDE_DIRS AND MKL_LIBRARIES AND MKL_INTERFACE_LIBRARY AND
MKL_SEQUENTIAL_LAYER_LIBRARY AND MKL_CORE_LIBRARY)
set (MKL_FIND_QUIETLY TRUE)
endif()
if(NOT BUILD_SHARED_LIBS)
set(INT_LIB "libmkl_intel_ilp64.a")
set(SEQ_LIB "libmkl_sequential.a")
set(THR_LIB "libmkl_intel_thread.a")
set(COR_LIB "libmkl_core.a")
else()
set(INT_LIB "mkl_intel_ilp64")
set(SEQ_LIB "mkl_sequential")
set(THR_LIB "mkl_intel_thread")
set(COR_LIB "mkl_core")
endif()
find_path(MKL_INCLUDE_DIR NAMES mkl.h HINTS $ENV{MKLROOT}/include)
find_library(MKL_INTERFACE_LIBRARY
NAMES ${INT_LIB}
PATHS $ENV{MKLROOT}/lib
$ENV{MKLROOT}/lib/intel64
$ENV{INTEL}/mkl/lib/intel64
NO_DEFAULT_PATH)
find_library(MKL_SEQUENTIAL_LAYER_LIBRARY
NAMES ${SEQ_LIB}
PATHS $ENV{MKLROOT}/lib
$ENV{MKLROOT}/lib/intel64
$ENV{INTEL}/mkl/lib/intel64
NO_DEFAULT_PATH)
find_library(MKL_CORE_LIBRARY
NAMES ${COR_LIB}
PATHS $ENV{MKLROOT}/lib
$ENV{MKLROOT}/lib/intel64
$ENV{INTEL}/mkl/lib/intel64
NO_DEFAULT_PATH)
set(MKL_INCLUDE_DIRS ${MKL_INCLUDE_DIR})
set(MKL_LIBRARIES ${MKL_INTERFACE_LIBRARY} ${MKL_SEQUENTIAL_LAYER_LIBRARY} ${MKL_CORE_LIBRARY})
if (MKL_INCLUDE_DIR AND
MKL_INTERFACE_LIBRARY AND
MKL_SEQUENTIAL_LAYER_LIBRARY AND
MKL_CORE_LIBRARY)
if (NOT DEFINED ENV{CRAY_PRGENVPGI} AND
NOT DEFINED ENV{CRAY_PRGENVGNU} AND
NOT DEFINED ENV{CRAY_PRGENVCRAY} AND
NOT DEFINED ENV{CRAY_PRGENVINTEL})
set(ABI "-m64")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMKL_ILP64 ${ABI}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMKL_ILP64 ${ABI}")
else()
set(MKL_INCLUDE_DIRS "")
set(MKL_LIBRARIES "")
set(MKL_INTERFACE_LIBRARY "")
set(MKL_SEQUENTIAL_LAYER_LIBRARY "")
set(MKL_CORE_LIBRARY "")
endif()
# Handle the QUIETLY and REQUIRED arguments and set MKL_FOUND to TRUE if
# all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(MKL DEFAULT_MSG MKL_LIBRARIES MKL_INCLUDE_DIRS MKL_INTERFACE_LIBRARY MKL_SEQUENTIAL_LAYER_LIBRARY MKL_CORE_LIBRARY)
MARK_AS_ADVANCED(MKL_INCLUDE_DIRS MKL_LIBRARIES MKL_INTERFACE_LIBRARY MKL_SEQUENTIAL_LAYER_LIBRARY MKL_CORE_LIBRARY)
@vmiheer

This comment has been minimized.

Copy link

commented Sep 21, 2018

How can I use this cmake file to actually find mkl?
I have:

include(FetchContent)
project(matmul_double LANGUAGES CXX CUDA)
FetchContent_Declare(
  findmkl
  GIT_REPOSITORY https://gist.github.com/5108cf6ab1515f581a84cd9ad1ef72aa.git
)

FetchContent_GetProperties(findmkl)
if(NOT findmkl_POPULATED)
  FetchContent_Populate(findmkl)
  # add_subdirectory(${findmkl_SOURCE_DIR} ${findmkl_BINARY_DIR})
endif()

find_package(MKL HINTS ${findmkl_SOURCE_DIR} REQUIRED)

But I get error:

CMake Error at CMakeLists.txt:15 (find_package):
  Could not find a package configuration file provided by "MKL" with any of
  the following names:

    MKLConfig.cmake
    mkl-config.cmake

  Add the installation prefix of "MKL" to CMAKE_PREFIX_PATH or set "MKL_DIR"
  to a directory containing one of the above files.  If "MKL" provides a
  separate development package or SDK, be sure it has been installed.
@GillianGrayson

This comment has been minimized.

Copy link

commented Dec 6, 2018

I have the same problem:

Could not find a package configuration file provided by "MKL" with any of
the following names:

MKLConfig.cmake
mkl-config.cmake

Add the installation prefix of "MKL" to CMAKE_PREFIX_PATH or set "MKL_DIR"
to a directory containing one of the above files. If "MKL" provides a
separate development package or SDK, be sure it has been installed.

How can I use this module? Should I set MKLROOT?

@scivision

This comment has been minimized.

Copy link
Owner Author

commented Mar 5, 2019

Sorry I didn't get notification of your comments.
I don't actually use this script. I make my own package-specific FindMyLib.cmake that have options for MKL.

Can you tell me which package (e.g. Lapack) you're wanting to link using MKL?

@mwtoews

This comment has been minimized.

Copy link

commented Mar 19, 2019

I too get the error. However, I'm getting better success with https://github.com/openmeeg/findmkl_cmake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.