Skip to content

Commit

Permalink
Merge pull request #70 from Enet4/static-gpu
Browse files Browse the repository at this point in the history
Add GPU support to static linking
  • Loading branch information
Enet4 committed Aug 26, 2023
2 parents 374800f + 2f3e65b commit ea0bc97
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
This project provides Rust bindings to [Faiss](https://github.com/facebookresearch/faiss),
the state-of-the-art vector search and clustering library.

## Installing as a dependency
## Installing with dynamic linking

Currently, this crate does not build Faiss automatically for you. The dynamic library needs to be installed manually to your system.
By default, this crate is dynamically linked with the Faiss library installed in your system,
so it does not build Faiss automatically for you.
To build the library yourself:

1. Follow the instructions [here](https://github.com/Enet4/faiss/tree/c_api_head/INSTALL.md#step-1-invoking-cmake)
to build Faiss using CMake,
enabling the variables `FAISS_ENABLE_C_API` and `BUILD_SHARED_LIBS`.
The crate is currently only compatible with version v1.7.2.
Consider building Faiss from [this fork, `c_api_head` branch](https://github.com/Enet4/faiss/tree/c_api_head),
which will contain the latest bindings to the C interface.
which will contain the latest supported bindings to the C interface.
For example:

```sh
cmake -B . -DFAISS_ENABLE_C_API=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release
cmake -B build -DFAISS_ENABLE_C_API=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build
```

This will result in the dynamic library `faiss_c` ("c_api/libfaiss_c.so" on Linux),
which needs to be installed in a place where your system will pick up
(in Linux, try somewhere in the `LD_LIBRARY_PATH` environment variable, such as "/usr/lib",
Expand All @@ -32,16 +37,35 @@ Currently, this crate does not build Faiss automatically for you. The dynamic li
as well.
2. You are now ready to include this crate as a dependency:
```toml
[dependencies]
"faiss" = "0.11.0"
```
If you have built Faiss with GPU support, you can include the "gpu" Cargo feature:
```toml
[dependencies]
"faiss" = { version = "0.11.0", features = ["gpu"] }
```
## Installing with static linking
Alternatively to the above, enable the "static" Cargo feature to let Rust build Faiss for you.
You will still need the dependencies required to build and run Faiss
as described in their [INSTALL.md](https://github.com/Enet4/faiss/blob/c_api_head/INSTALL.md#building-from-source),
namely a compatible C++ compiler and a BLAS implementation.
```toml
[dependencies]
"faiss" = "0.11.0"
"faiss" = { version = "0.11.0", features = ["static"] }
```
If you have built Faiss with GPU support, you can include the "gpu" feature in the bindings:
Compiling Faiss with GPU support is also possible.
```toml
[dependencies]
"faiss" = {version = "0.11.0", features = ["gpu"]}
"faiss" = { version = "0.11.0", features = ["static", "gpu"] }
```
## Using
Expand Down
30 changes: 29 additions & 1 deletion faiss-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ fn static_link_faiss() {
cfg.define("FAISS_ENABLE_C_API", "ON")
.define("BUILD_SHARED_LIBS", "OFF")
.define("CMAKE_BUILD_TYPE", "Release")
.define("FAISS_ENABLE_GPU", "OFF")
.define("FAISS_ENABLE_GPU", if cfg!(feature = "gpu") {
"ON"
} else {
"OFF"
})
.define("FAISS_ENABLE_PYTHON", "OFF")
.define("BUILD_TESTING", "OFF")
.very_verbose(true);
Expand All @@ -32,6 +36,12 @@ fn static_link_faiss() {
println!("cargo:rustc-link-lib=gomp");
println!("cargo:rustc-link-lib=blas");
println!("cargo:rustc-link-lib=lapack");
if cfg!(feature = "gpu") {
let cuda_path = cuda_lib_path();
println!("cargo:rustc-link-search=native={}/lib64", cuda_path);
println!("cargo:rustc-link-lib=cudart");
println!("cargo:rustc-link-lib=cublas");
}
}

#[cfg(feature = "static")]
Expand All @@ -57,3 +67,21 @@ fn link_cxx() {
println!("cargo:rustc-link-lib={}", cxx);
}
}

#[cfg(feature = "static")]
fn cuda_lib_path() -> String {
// look for CUDA_PATH in environment,
// then CUDA_LIB_PATH,
// then CUDA_INCLUDE_PATH
if let Ok(cuda_path) = std::env::var("CUDA_PATH") {
return cuda_path;
}
if let Ok(cuda_lib_path) = std::env::var("CUDA_LIB_PATH") {
return cuda_lib_path;
}
if let Ok(cuda_include_path) = std::env::var("CUDA_INCLUDE_PATH") {
return cuda_include_path;
}

panic!("Could not find CUDA: environment variables `CUDA_PATH`, `CUDA_LIB_PATH`, or `CUDA_INCLUDE_PATH` must be set");
}
2 changes: 1 addition & 1 deletion faiss-sys/faiss
6 changes: 4 additions & 2 deletions faiss-sys/gen_bindings.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/sh
#!/usr/bin/env sh
# Generate Rust bindings to the Faiss C API
#
# Ensure that the submodule is updated and checked out in the intended revision
if ! which bindgen > /dev/null; then
echo "ERROR: `bindgen` not found. Please install using cargo:"
echo " cargo install bindgen"
Expand Down Expand Up @@ -35,5 +38,4 @@ echo ${cmd}
${cmd}

# clean up
rm -rf faiss
rm -f c_api.h

0 comments on commit ea0bc97

Please sign in to comment.