This project shows how new java.foreign
API (project Panama) can be used to
call C/C++ library - Faiss - from Java.
-
Download faiss
MacOS and Ubuntu:
git clone https://github.com/facebookresearch/faiss.git cd faiss
-
Install dependencies
MacOS:
brew install libomp
Ubuntu:
sudo apt-get install libomp-dev libblas-dev liblapack-dev sudo snap install cmake --classic # the version in apt is too old
-
Create build configuration
MacOS:
cmake -B build \ -DFAISS_ENABLE_GPU=OFF \ -DFAISS_ENABLE_PYTHON=OFF \ -DBUILD_TESTING=OFF \ -DBUILD_SHARED_LIBS=ON \ -DCMAKE_BUILD_TYPE=Release \ -DFAISS_OPT_LEVEL=avx2 \ -DFAISS_ENABLE_C_API=ON \ -DOpenMP_C_FLAGS=-fopenmp=lomp \ -DOpenMP_CXX_FLAGS=-fopenmp=lomp \ -DOpenMP_C_LIB_NAMES="libomp" -DOpenMP_CXX_LIB_NAMES="libomp" \ -DOpenMP_libomp_LIBRARY="/opt/homebrew/opt/libomp/lib/libomp.dylib" \ -DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp /opt/homebrew/opt/libomp/lib/libomp.dylib \ -I/opt/homebrew/opt/libomp/include" -DOpenMP_CXX_LIB_NAMES="libomp" \ -DOpenMP_C_FLAGS="-Xpreprocessor -fopenmp --I/opt/homebrew/opt/libomp/include" .
Ubuntu:
cmake . -B build \ -DFAISS_ENABLE_GPU=OFF \ -DFAISS_ENABLE_PYTHON=OFF \ -DFAISS_OPT_LEVEL=avx2 \ -DFAISS_ENABLE_C_API=ON \ -DBUILD_SHARED_LIBS=ON
-
Build faiss
MacOS and Ubuntu:
cmake --build build --config Release -j
-
build & install faiss C API
MacOS:
cd build/c_api && make && sudo make install sudo cp libfaiss_c.dylib /usr/local/lib/
Ubuntu:
cd build/c_api && make && sudo make install sudo cp libfaiss_c.so /usr/local/lib/
-
Download & extract jextract
MacOS:
wget https://download.java.net/java/early_access/jextract/1/openjdk-20-jextract+1-2_macos-x64_bin.tar.gz && tar -xzf openjdk-20-jextract+1-2_macos-x64_bin.tar.gz
Ubuntu:
wget https://download.java.net/java/early_access/jextract/1/openjdk-20-jextract+1-2_linux-x64_bin.tar.gz && tar -xzf openjdk-20-jextract+1-2_linux-x64_bin.tar.gz
-
Generate faiss C bindings (they will be created in
faiss
directory)MacOS:
cd scripts && ./create_bindings.sh
Ubuntu:
cd scripts && ./create_bindings_ubuntu.sh