faiss icon indicating copy to clipboard operation
faiss copied to clipboard

how to Compile Faiss using static MKL

Open wangsouc opened this issue 9 years ago • 6 comments

i try to use static MKL, like this:

BLASLDFLAGS= \
	$(MKLROOT)/lib/libmkl_intel_lp64.a  \
	$(MKLROOT)/lib/libmkl_intel_thread.a \
	$(MKLROOT)/lib/libmkl_core.a  \
	-lpthread -lrt -ldl 

but something wrong, like below, awk for help!


/home/users/wangsijiang01/.jumbo/opt/gcc48/bin/g++ -o tests/demo_ivfpq_indexing -fPIC -m64 -Wall -g -O3  -msse4 -mpopcnt -fopenmp -Wno-sign-compare -Dnullptr=NULL -Doverride= -fopenmp -I/home/users/wangsijiang01/bc_out/baidu/third-party/mkl/output/include tests/demo_ivfpq_indexing.cpp libfaiss.a -g -fPIC  -fopenmp /home/users/wangsijiang01/bc_out/baidu/third-party/mkl/output/lib/libmkl_intel_lp64.a /home/users/wangsijiang01/bc_out/baidu/third-party/mkl/output/lib/libmkl_intel_thread.a /home/users/wangsijiang01/bc_out/baidu/third-party/mkl/output/lib/libmkl_core.a -lpthread -lrt -ldl
/home/users/wangsijiang01/bc_out/baidu/third-party/mkl/output/lib/libmkl_intel_thread.a(sorgqr_par.o): In function `mkl_lapack_sorgqr':
sorgqr_omp_gen.f:(.text+0x3f7): undefined reference to `__kmpc_global_thread_num'
sorgqr_omp_gen.f:(.text+0x460): undefined reference to `__kmpc_ok_to_fork'

wangsouc avatar Apr 26 '17 09:04 wangsouc

Hi

Setting MKL flags is a frustrating process of trial and errors. Please post here if you manage to fix this compile error, we will incorporate it in the documentation.

mdouze avatar Apr 28 '17 08:04 mdouze

For what it's worth, this tool helped me get me get static linking working on OSX

https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor

bakercp avatar Apr 28 '17 12:04 bakercp

successful compile MKL in stitac link, using conf blow:

CFLAGS=-fPIC -m64 -Wall -g -O3  -msse4 -mpopcnt -fopenmp -Wno-sign-compare -Dnullptr=NULL -Doverride= -fopenmp \
 -DMKL_ILP64 -m64 -I${MKLROOT}/include

BLASLDFLAGS= -Wl,--start-group ${MKLROOT}/lib/libmkl_intel_ilp64.a ${MKLROOT}/lib/libmkl_gnu_thread.a \
${MKLROOT}/lib/libmkl_core.a -Wl,--end-group -lgomp -lpthread -lm -ldl

BLASCFLAGS=-DFINTEGER=long

wangsouc avatar May 04 '17 06:05 wangsouc

Thanks @wangsouc! Closing issue.

mdouze avatar Jun 19 '17 11:06 mdouze

With the migration of FAISS build system to CMake successful recipe for building it with static link to MKL looks in my case like this:

cmake -B build \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=ON \
    -DFAISS_ENABLE_GPU=OFF \
    -DFAISS_ENABLE_PYTHON=OFF \
    -DFAISS_ENABLE_C_API=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DBLA_VENDOR=Intel10_64lp \
    "-DMKL_LIBRARIES=-Wl,--start-group;${MKL_PATH}/libmkl_intel_lp64.a;${MKL_PATH}/libmkl_gnu_thread.a;${MKL_PATH}/libmkl_core.a;-Wl,--end-group" \
    .

make -C build faiss faiss_test
make -C build test

Note that these not obvious -Wl,--start-group and -Wl,--end-group pseudo libraries are required because of cyclic dependencies in MKL modules.

Perhaps this should be noted in INSTALL.md. Or even better handled by faiss build scripts somehow. Maybe update faiss/CMakeLists.txt like this:

find_package(MKL)
if(MKL_FOUND)
  # not sure about exact condition here
  if(NOT BLA_VENDOR STREQUAL "Intel10_64_dyn" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_libraries(faiss PRIVATE "-Wl,--start-group" ${MKL_LIBRARIES} "-Wl,--end-group")
    target_link_libraries(faiss_avx2 PRIVATE "-Wl,--start-group" ${MKL_LIBRARIES} "-Wl,--end-group")
  else()
    target_link_libraries(faiss PRIVATE ${MKL_LIBRARIES})
    target_link_libraries(faiss_avx2 PRIVATE ${MKL_LIBRARIES})
  endif()
...

@mdouze What do you think?

AndrewKostousov avatar Feb 16 '22 13:02 AndrewKostousov

With the migration of FAISS build system to CMake successful recipe for building it with static link to MKL looks in my case like this:

cmake -B build \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=ON \
    -DFAISS_ENABLE_GPU=OFF \
    -DFAISS_ENABLE_PYTHON=OFF \
    -DFAISS_ENABLE_C_API=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DBLA_VENDOR=Intel10_64lp \
    "-DMKL_LIBRARIES=-Wl,--start-group;${MKL_PATH}/libmkl_intel_lp64.a;${MKL_PATH}/libmkl_gnu_thread.a;${MKL_PATH}/libmkl_core.a;-Wl,--end-group" \
    .

make -C build faiss faiss_test
make -C build test

Note that these not obvious -Wl,--start-group and -Wl,--end-group pseudo libraries are required because of cyclic dependencies in MKL modules.

Perhaps this should be noted in INSTALL.md. Or even better handled by faiss build scripts somehow. Maybe update faiss/CMakeLists.txt like this:

find_package(MKL)
if(MKL_FOUND)
  # not sure about exact condition here
  if(NOT BLA_VENDOR STREQUAL "Intel10_64_dyn" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_link_libraries(faiss PRIVATE "-Wl,--start-group" ${MKL_LIBRARIES} "-Wl,--end-group")
    target_link_libraries(faiss_avx2 PRIVATE "-Wl,--start-group" ${MKL_LIBRARIES} "-Wl,--end-group")
  else()
    target_link_libraries(faiss PRIVATE ${MKL_LIBRARIES})
    target_link_libraries(faiss_avx2 PRIVATE ${MKL_LIBRARIES})
  endif()
...

@mdouze What do you think?

Thank you so much, I spent 2 days trying to install faiss from source in Arch linux.

Inside the Arch User Repository (AUR), all the faiss implementations with MKL have -DMKL_LIBRARIES=/opt/intel/mkl in their PKGBUILD. But this gives the error:


CMake Warning at faiss/CMakeLists.txt:225 (target_link_libraries):
  Target "faiss" requests linking to directory "/opt/intel/mkl".
  Targets may link only to libraries.  CMake is dropping the item.

I finally found that $MKL_PATH for arch users is:

MKL_PATH="/opt/intel/oneapi/mkl/2022.1.0/lib/intel64"

then I put the libraries you mentioned in my cmake:

"-DMKL_LIBRARIES=-Wl,--start-group;${MKL_PATH}/libmkl_intel_lp64.a;${MKL_PATH}/libmkl_gnu_thread.a;${MKL_PATH}/libmkl_core.a;-Wl,--end-group"

I found this very important due to the original documentation for building FAISS from source is not very clear at saying: -DMKL_LIBRARIES=/path/to/mkl/libs

javiabellan avatar Nov 05 '22 00:11 javiabellan