These are the steps to follow to prepare our package:
- The CMakeLists.txt file starts with the minimum version requirement, project name, and supported language:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-05 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
- We wish to build the dgemm-example executable, which is built from example.cpp:
add_executable(dgemm-example "")
target_sources(dgemm-example
PRIVATE
example.cpp
)
- We then need to locate MKL libraries installed via mkl-devel. We prepare an INTERFACE library called IntelMKL. This can be used as any other target and will set include directories, compiler options, and link libraries for any dependent target. The setup is made to mimic what is suggested by the Intel MKL link line advisor (https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor/). First, we set the compiler options:
add_library(IntelMKL INTERFACE)
target_compile_options(IntelMKL
INTERFACE
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:AppleClang>>:-m64>
)
- Next, we search for the mkl.h header file and set the include directories for the IntelMKL target:
find_path(_mkl_h
NAMES
mkl.h
HINTS
${CMAKE_INSTALL_PREFIX}/include
)
target_include_directories(IntelMKL
INTERFACE
${_mkl_h}
)
message(STATUS "MKL header file FOUND: ${_mkl_h}")
- Finally, we locate the libraries and set the link libraries for the IntelMKL target:
find_library(_mkl_libs
NAMES
mkl_rt
HINTS
${CMAKE_INSTALL_PREFIX}/lib
)
message(STATUS "MKL single dynamic library FOUND: ${_mkl_libs}")
find_package(Threads QUIET)
target_link_libraries(IntelMKL
INTERFACE
${_mkl_libs}
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:AppleClang>>:Threads::Threads>
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:AppleClang>>:m>
)
- We use the cmake_print_properties function to print out useful messages about the IntelMKL target:
include(CMakePrintHelpers)
cmake_print_properties(
TARGETS
IntelMKL
PROPERTIES
INTERFACE_COMPILE_OPTIONS
INTERFACE_INCLUDE_DIRECTORIES
INTERFACE_LINK_LIBRARIES
)
- We link the dgemm-example target against these libraries:
target_link_libraries(dgemm-example
PRIVATE
IntelMKL
)
- We conclude CMakeLists.txt by defining the install target:
install(
TARGETS
dgemm-example
DESTINATION
bin
)
- Now we can try to build the package:
$ conda build conda-recipe
- We will see lots of output on the screen, but once the build is complete, we can install the package. We will do this first locally:
$ conda install --use-local conda-example-dgemm
- Now we are ready to test it – open a new terminal (assuming Anaconda is activated) and type:
$ dgemm-example
MKL DGEMM example worked!
- After the successful test, we can remove the package again:
$ conda remove conda-example-dgemm