Skip to content

Commit

Permalink
mkl support in cmake (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adeykin authored and mdouze committed May 31, 2017
1 parent d3c8456 commit c056f1d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
16 changes: 12 additions & 4 deletions CMakeLists.txt
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions 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)
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Expand Up @@ -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)
Expand Down

0 comments on commit c056f1d

Please sign in to comment.