### Index Factory Example Source: https://github.com/facebookresearch/faiss/blob/main/c_api/INSTALL.md C code demonstrating how to use the faiss_index_factory function. ```c FaissIndex* index = NULL; int c = faiss_index_factory(&index, 64, "Flat", METRIC_L2); if (c) { // operation failed } ``` -------------------------------- ### Free Index Example Source: https://github.com/facebookresearch/faiss/blob/main/c_api/INSTALL.md C code demonstrating manual memory management for a Faiss index. ```c faiss_Index_free(index); ``` -------------------------------- ### Python Search Example Source: https://github.com/facebookresearch/faiss/wiki/Getting-started Demonstrates how to perform k-nearest neighbor search using Faiss in Python, including a sanity check and the actual search. ```python k = 4 # we want to see 4 nearest neighbors D, I = index.search(xb[:5], k) # sanity check print(I) print(D) D, I = index.search(xq, k) # actual search print(I[:5]) # neighbors of the 5 first queries print(I[-5:]) # neighbors of the 5 last queries ``` -------------------------------- ### PQFastScan Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the Product Quantization Fast Scan Faiss example and links it to the faiss library. ```cmake add_executable(7-PQFastScan EXCLUDE_FROM_ALL 7-PQFastScan.cpp) target_link_libraries(7-PQFastScan PRIVATE faiss) ``` -------------------------------- ### PQFastScanRefine Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the Product Quantization Fast Scan Refine Faiss example and links it to the faiss library. ```cmake add_executable(8-PQFastScanRefine EXCLUDE_FROM_ALL 8-PQFastScanRefine.cpp) target_link_libraries(8-PQFastScanRefine PRIVATE faiss) ``` -------------------------------- ### C++ Search Example Source: https://github.com/facebookresearch/faiss/wiki/Getting-started Provides a C++ code snippet for performing k-nearest neighbor search, including a sanity check and the actual search. Note: This is an excerpt, refer to the full code in the Faiss tutorial. ```cpp int k = 4; { idx_t *I = new idx_t[k * 5]; float *D = new float[k * 5]; index.search(5, xb, k, D, I); printf("I=\n"); for(int i = 0; i < 5; i++) { for(int j = 0; j < k; j++) printf("%5ld ", I[i * k + j]); printf("\n"); } ... delete [] I; delete [] D; } { idx_t *I = new idx_t[k * nq]; float *D = new float[k * nq]; index.search(nq, xq, k, D, I); ... } ``` -------------------------------- ### IVFFlat Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the IVFFlat Faiss example and links it to the faiss library. ```cmake add_executable(2-IVFFlat EXCLUDE_FROM_ALL 2-IVFFlat.cpp) target_link_libraries(2-IVFFlat PRIVATE faiss) ``` -------------------------------- ### Get Total Number of Elements Source: https://github.com/facebookresearch/faiss/blob/main/c_api/INSTALL.md C code showing how to get the total number of elements in an index. ```c idx_t ntotal = faiss_Index_ntotal(index); ``` -------------------------------- ### Per-SIMD translation unit example (AVX2) Source: https://github.com/facebookresearch/faiss/blob/main/faiss/docs/simd_dynamic_dispatch_migration.md Example of a per-SIMD translation unit for AVX2, compiled with -mavx2. It includes the necessary header and defines the specialized function within a COMPILE_SIMD_AVX2 guard. ```cpp #ifdef COMPILE_SIMD_AVX2 #include "functions.h" #include template <> void fvec_madd( size_t n, const float* a, float bf, const float* b, float* c) { // AVX2 intrinsics implementation } #endif ``` -------------------------------- ### Basic Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for a basic Faiss example (e.g., 1-Flat) and links it to the faiss library. ```cmake add_executable(1-Flat EXCLUDE_FROM_ALL 1-Flat.cpp) target_link_libraries(1-Flat PRIVATE faiss) ``` -------------------------------- ### Error Handling Example Source: https://github.com/facebookresearch/faiss/blob/main/c_api/INSTALL.md C code illustrating error checking and retrieving error messages. ```c int c = faiss_Index_add(index, nb, xb); if (c) { printf("%s", faiss_get_last_error()); exit(-1); } ``` -------------------------------- ### IVFPQ Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the IVFPQ Faiss example and links it to the faiss library. ```cmake add_executable(3-IVFPQ EXCLUDE_FROM_ALL 3-IVFPQ.cpp) target_link_libraries(3-IVFPQ PRIVATE faiss) ``` -------------------------------- ### Multiple GPUs Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the multiple GPUs Faiss example and links it to the faiss library. ```cmake add_executable(5-Multiple-GPUs EXCLUDE_FROM_ALL 5-Multiple-GPUs.cpp) target_link_libraries(5-Multiple-GPUs PRIVATE faiss) ``` -------------------------------- ### Build Demo: IVFPQ Indexing Source: https://github.com/facebookresearch/faiss/blob/main/INSTALL.md Compiles the C++ demo for IVFPQ indexing. ```shell make -C build demo_ivfpq_indexing ``` -------------------------------- ### Common translation unit example (Scalar) Source: https://github.com/facebookresearch/faiss/blob/main/faiss/docs/simd_dynamic_dispatch_migration.md Example of the common translation unit retaining only the scalar (NONE) specialization and the dispatch wrapper. It includes the base header and defines the scalar implementation and the dispatch function. ```cpp #include "functions.h" #include namespace faiss { template <> void fvec_madd( size_t n, const float* a, float bf, const float* b, float* c) { for (size_t i = 0; i < n; i++) c[i] = a[i] + bf * b[i]; } void fvec_madd(size_t n, const float* a, float bf, const float* b, float* c) { with_simd_level([&]() { fvec_madd(n, a, bf, b, c); }); } } // namespace faiss ``` -------------------------------- ### GPU Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the GPU-accelerated Faiss example and links it to the faiss library. ```cmake add_executable(4-GPU EXCLUDE_FROM_ALL 4-GPU.cpp) target_link_libraries(4-GPU PRIVATE faiss) ``` -------------------------------- ### Monolithic SIMD (legacy) - Before Conversion Source: https://github.com/facebookresearch/faiss/blob/main/faiss/docs/simd_dynamic_dispatch_migration.md Example of a function using monolithic SIMD with preprocessor directives for AVX2. ```cpp // functions.h void fvec_madd(size_t n, const float* a, float bf, const float* b, float* c); // functions.cpp void fvec_madd(size_t n, const float* a, float bf, const float* b, float* c) { #ifdef __AVX2__ // AVX2 implementation #else for (size_t i = 0; i < n; i++) c[i] = a[i] + bf * b[i]; #endif } ``` -------------------------------- ### Build environment setup and compilation Source: https://github.com/facebookresearch/faiss/blob/main/demos/rocksdb_ivf/README.md Steps to create a conda environment, install dependencies, and compile the RocksDB IVF demo. ```bash conda create -n rocksdb_ivf conda activate rocksdb_ivf conda install pytorch::faiss-cpu conda-forge::rocksdb cmake make gxx_linux-64 sysroot_linux-64 cd ~/faiss/demos/rocksdb_ivf cmake -B build . make -C build -j$(nproc) ``` -------------------------------- ### CMake configuration for SIMD source files Source: https://github.com/facebookresearch/faiss/blob/main/faiss/docs/simd_dynamic_dispatch_migration.md Example of how to add per-SIMD source files to the build system in faiss/CMakeLists.txt. ```cmake set(FAISS_SIMD_AVX2_SRC # ... existing entries ... path/to/functions_avx2.cpp # <-- add ) set(FAISS_SIMD_AVX512_SRC # ... existing entries ... path/to/functions_avx512.cpp # <-- add ) set(FAISS_SIMD_NEON_SRC # ... existing entries ... path/to/functions_neon.cpp # <-- add ) set(FAISS_SIMD_SVE_SRC # ... existing entries ... path/to/functions_sve.cpp # <-- add (if SVE implementation exists) ) set(FAISS_SIMD_RVV_SRC # ... existing entries ... path/to/functions_rvv.cpp # <-- add (if RVV implementation exists) ) ``` -------------------------------- ### RefineComparison Example Build Target Source: https://github.com/facebookresearch/faiss/blob/main/tutorial/cpp/CMakeLists.txt Defines a build target for the Refine Comparison Faiss example and links it to the faiss library. ```cmake add_executable(9-RefineComparison EXCLUDE_FROM_ALL 9-RefineComparison.cpp) target_link_libraries(9-RefineComparison PRIVATE faiss) ```