From c056f1d32031fea1b884628245f627a1a59998a3 Mon Sep 17 00:00:00 2001 From: Adeykin Date: Wed, 31 May 2017 16:41:42 +0300 Subject: [PATCH] mkl support in cmake (#123) --- CMakeLists.txt | 16 ++++++-- cmake/Modules/FindMKL.cmake | 74 +++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 cmake/Modules/FindMKL.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index af12d02fd6..ee707e6b5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,15 +6,23 @@ project(faiss C CXX) option(BUILD_TUTORIAL "Build tutorials" ON) option(BUILD_TEST "Build tests" ON) option(BUILD_WITH_GPU "Build faiss with gpu (cuda) support" ON) +option(WITH_MKL "Build with MKL if ON (OpenBLAS if OFF)" OFF) list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) # OpenMP find_package(OpenMP REQUIRED) -# openblas -find_package(OpenBLAS REQUIRED) -include_directories(${OpenBLAS_INCLUDE_DIR}) +# BLAS (MKL os OpenBLAS) +if(WITH_MKL) + find_package(MKL REQUIRED) + include_directories(${MKL_INCLUDE_DIRS}) + set(BLAS_LIB ${MKL_LIBRARIES}) +else() + find_package(OpenBLAS REQUIRED) + include_directories(${OpenBLAS_INCLUDE_DIR}) + set(BLAS_LIB ${OpenBLAS_LIB}) +endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -m64 -Wall -g -O3 -msse4 -mpopcnt -fopenmp -Wno-sign-compare") add_definitions(-DFINTEGER=int) @@ -30,7 +38,7 @@ file(GLOB faiss_cpu_cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) set(faiss_lib faiss) add_library(${faiss_lib} STATIC ${faiss_cpu_headers} ${faiss_cpu_cpp}) -target_link_libraries(${faiss_lib} ${OpenMP_CXX_FLAGS} ${OpenBLAS_LIB}) +target_link_libraries(${faiss_lib} ${OpenMP_CXX_FLAGS} ${BLAS_LIB}) # build gpu lib if(BUILD_WITH_GPU) diff --git a/cmake/Modules/FindMKL.cmake b/cmake/Modules/FindMKL.cmake new file mode 100644 index 0000000000..de93d5ccf9 --- /dev/null +++ b/cmake/Modules/FindMKL.cmake @@ -0,0 +1,74 @@ +# defines: +# MKL_INCLUDE_DIRS +# MKL_LIBRARIES +# MKL_COMPILER_LIBRARIES - a list of compiler libraries (file names) required for MKL + +#unset(MKL_LIB_DIR CACHE) +#unset(MKL_COMPILER_LIB_DIR CACHE) +#unset(MKL_COMPILER_REDIST_PATH CACHE) + +if(NOT HAVE_MKL) + find_path(MKL_INCLUDE_DIRS "mkl.h" PATHS ${MKL_INCLUDE_DIR} DOC "The path to MKL headers") + + if(MKL_INCLUDE_DIRS) + + get_filename_component(_MKL_LIB_PATH "${MKL_INCLUDE_DIRS}/../lib" ABSOLUTE) + + if(APPLE) + # MKL 2017 for mac has only 64 bit libraries without directory prefix + set(_MKL_COMPILER_LIB_PATH ${MKL_INCLUDE_DIRS}/../../compiler/lib) + else() + if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(_MKL_LIB_PATH "${_MKL_LIB_PATH}/intel64") + set(_MKL_COMPILER_LIB_PATH ${MKL_INCLUDE_DIRS}/../../compiler/lib/intel64) + if(WIN32) + set(_MKL_COMPILER_REDIST_PATH ${MKL_INCLUDE_DIRS}/../../redist/intel64/compiler) + endif() + else() + set(_MKL_LIB_PATH "${_MKL_LIB_PATH}/ia32") + set(_MKL_COMPILER_LIB_PATH ${MKL_INCLUDE_DIRS}/../../compiler/lib/ia32) + if(WIN32) + set(_MKL_COMPILER_REDIST_PATH ${MKL_INCLUDE_DIRS}/../../redist/ia32/compiler) + endif() + endif() + endif() + + # On Linux and Apple take libraries for redistribution from the same location that is used for linking + if(UNIX) + set(_MKL_COMPILER_REDIST_PATH ${_MKL_COMPILER_LIB_PATH}) + endif() + + if(WIN32) + set(MKL_COMPILER_LIBRARIES libiomp5md.dll) + set(MKL_LIBRARIES ${MKL_LIBRARIES} mkl_intel_lp64 mkl_core mkl_intel_thread libiomp5md) + elseif(APPLE) + set(MKL_COMPILER_LIBRARIES libiomp5.dylib) + # generated by https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor + # with the following options: OSX; Clang; Intel64; static; 32 bit integer; OpenMP; Intel OpenMP + set(MKL_LIBRARIES ${MKL_LIBRARIES} libmkl_intel_lp64.a libmkl_intel_thread.a libmkl_core.a iomp5 pthread m dl) + else() + set(MKL_COMPILER_LIBRARIES libiomp5.so) + # a --start-group / --end-group pair is required when linking with static MKL on GNU. + # see https://software.intel.com/en-us/forums/topic/280974#comment-1478780 + # and https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor + set(MKL_LIBRARIES ${MKL_LIBRARIES} + "-Wl,--start-group" + libmkl_intel_lp64.a libmkl_core.a libmkl_intel_thread.a + "-Wl,--end-group" + "-Wl,--exclude-libs,libmkl_intel_lp64.a,--exclude-libs,libmkl_core.a,--exclude-libs,libmkl_intel_thread.a,--exclude-libs,iomp5" + iomp5 dl pthread m) + endif() + + set(MKL_LIB_DIR "${_MKL_LIB_PATH}" + CACHE PATH "Full path of MKL library directory") + set(MKL_COMPILER_LIB_DIR "${_MKL_COMPILER_LIB_PATH}" + CACHE PATH "Full path of MKL compiler library directory") + set(MKL_COMPILER_REDIST_PATH "${_MKL_COMPILER_REDIST_PATH}" + CACHE PATH "Full path of MKL compiler redistributable library directory") + + link_directories(${MKL_LIB_DIR} ${MKL_COMPILER_LIB_DIR}) + + set(HAVE_MKL 1) + + endif(MKL_INCLUDE_DIRS) +endif(NOT HAVE_MKL) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b3876ec6e3..57005e78ac 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,7 +12,7 @@ foreach(source ${srcs}) # target add_executable(${name} ${source}) - target_link_libraries(${name} ${faiss_lib} ${OpenBLAS_LIB} ${GTEST_BOTH_LIBRARIES}) + target_link_libraries(${name} ${faiss_lib} ${BLAS_LIB} ${GTEST_BOTH_LIBRARIES}) # Install install(TARGETS ${name} DESTINATION test)