Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ option(ODYSSEY_DEBUG_RESULTS "Print Odyssey search results (query_result) for de
option(BUILD_SING "Enable CUDA support" OFF)
option(BUILD_SOFA "Build SOFA with FFTW3 support" OFF)
option(BUILD_COCONUT "Build the COCONUT sortable-SAX algorithm (static + streaming)" ON)
option(BUILD_FAISS "Build the FAISS-backed baseline algorithm (static + streaming)" OFF)

# Backward compatibility: ENABLE_MPI and ODYSSEY_MPI map to BUILD_ODYSSEY
if(DEFINED ENABLE_MPI)
Expand Down Expand Up @@ -76,6 +77,7 @@ message(STATUS " BUILD_ODYSSEY: ${BUILD_ODYSSEY}")
message(STATUS " ODYSSEY_DEBUG_RESULTS: ${ODYSSEY_DEBUG_RESULTS}")
message(STATUS " BUILD_SING: ${BUILD_SING}")
message(STATUS " BUILD_COCONUT: ${BUILD_COCONUT}")
message(STATUS " BUILD_FAISS: ${BUILD_FAISS}")

# Define PROJECT_ROOT_DIR for all targets (paramSetup.cpp is compiled by commons_lib and by benchmark executables)
get_filename_component(PROJECT_ROOT_DIR "${CMAKE_SOURCE_DIR}" ABSOLUTE)
Expand Down Expand Up @@ -249,6 +251,49 @@ if(BUILD_TESTS)
enable_testing()
endif()

# ====== FAISS (CPU-only) ======
# FAISS ships as a submodule under benchmark/faiss. Two things need it: the FAISS
# benchmark competitors, and the daisy::FaissFlat algorithm when BUILD_FAISS is ON.
# It is added here, once, so both consumers share a single 'faiss' target.
#
# FAISS requires CMake >= 3.24, above this project's floor of 3.16. That is checked
# explicitly: otherwise configuring with BUILD_BENCHMARK=ON aborts inside the
# submodule with an error that does not say which dependency asked for 3.24.
set(BUILD_FAISS_AVAILABLE FALSE CACHE INTERNAL "Internal flag indicating if FAISS is available.")

if(BUILD_FAISS OR BUILD_BENCHMARK)
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/benchmark/faiss/CMakeLists.txt")
message(WARNING
"FAISS sources not found at benchmark/faiss. FAISS targets will NOT be built. "
"Fetch them with: git submodule update --init benchmark/faiss")
elseif(CMAKE_VERSION VERSION_LESS 3.24)
message(WARNING
"FAISS requires CMake >= 3.24 but this is CMake ${CMAKE_VERSION}. "
"FAISS targets will NOT be built. Install a newer CMake to enable them.")
else()
set(FAISS_ENABLE_GPU OFF CACHE BOOL "" FORCE)
set(FAISS_ENABLE_PYTHON OFF CACHE BOOL "" FORCE)
set(FAISS_ENABLE_EXTRAS OFF CACHE BOOL "" FORCE)
set(FAISS_ENABLE_C_API OFF CACHE BOOL "" FORCE)

# FAISS gates its own test suite on BUILD_TESTING. Shadow it for the
# submodule only, then restore it so DaiSy's CTest registration is intact.
set(_daisy_saved_build_testing "${BUILD_TESTING}")
set(BUILD_TESTING OFF)
add_subdirectory(benchmark/faiss ${CMAKE_BINARY_DIR}/faiss)
set(BUILD_TESTING "${_daisy_saved_build_testing}")

set(BUILD_FAISS_AVAILABLE TRUE CACHE INTERNAL "Internal flag indicating if FAISS is available." FORCE)
if(DEBUG_MSG)
message(STATUS "FAISS found and configured (CPU-only). BUILD_FAISS_AVAILABLE=TRUE")
endif()
endif()
endif()

if(BUILD_FAISS AND NOT BUILD_FAISS_AVAILABLE)
message(WARNING "BUILD_FAISS is ON but FAISS is unavailable. daisy::FaissFlat will NOT be built.")
endif()


# Add subdirectories
if(DEBUG_MSG)
Expand Down
18 changes: 16 additions & 2 deletions CONTINUITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,29 @@ From the current source tree and public includes, the main algorithm set include
- `Hercules`
- `DumpyOS`
- `Fresh`
- `FaissFlat` (optional, `BUILD_FAISS`)

These algorithms do not all share the same dependency profile or feature set.

## Important Behavioral Conventions

- Top-k search is the baseline capability.
- Range search is modeled through `SearchConfig` and is not universally implemented.
- Streaming insert is implemented by `BruteForceSearch`, `LbBruteforce`, `Messi`, and `Coconut`;
the base implementation still throws for algorithms that do not support it.
- Streaming insert is implemented by `BruteForceSearch`, `LbBruteforce`, `Messi`, `Coconut`, and
`FaissFlat`; the base implementation still throws for algorithms that do not support it.
- Bruteforce streaming grows the owned in-memory database incrementally. LbBruteforce also
computes SAX summaries incrementally, using the breakpoint set fixed by the initial build.
- MESSI uses those fixed breakpoints to route stable SAX/position records into its live iSAX
tree, including new-root creation and leaf splitting. Callers must serialize inserts and
queries; simultaneous update/search is not supported.
- `FaissFlat` wraps `faiss::IndexFlatL2` and is the streaming baseline: `add()` appends with no
training and no rebuild, so it has no build-time state that can drift from the live data and
stays exact after every insert. It is `L2_SQUARED` only and rejects DTW in its constructor.
Its inherited `database` pointer is a non-owning view into FAISS storage, refreshed after every
build and insert; it must never be freed by DaiSy.
- FAISS range search keeps only `dist < radius` while DaiSy's convention is `dist <= r`, so
`FaissFlat` widens the radius by one float and filters the result to restore the inclusive
boundary. FAISS also returns range hits unsorted; DaiSy sorts them by distance.
- `setNormalized(bool)` is a declaration about the input data, not a preprocessing step.
- Data can come from in-memory arrays or file-backed sources via `DataSource`.

Expand All @@ -91,13 +100,18 @@ Important options:
- `BUILD_SING`
- `BUILD_SOFA`
- `BUILD_COCONUT`
- `BUILD_FAISS`

Current optional dependency model:

- MPI gates `Odyssey`
- CUDA gates `Sing`
- FFTW3 gates `Sofa`
- Coconut can be fully toggled off
- The `benchmark/faiss` submodule gates `FaissFlat` and the FAISS benchmarks. FAISS requires
CMake >= 3.24, above this project's floor of 3.16; the root `CMakeLists.txt` checks the version
and warns instead of letting the submodule abort configuration. FAISS is added exactly once,
at the root, and shared by `dino_lib` and the benchmarks via `BUILD_FAISS_AVAILABLE`.

Backward-compatibility option aliases exist for some old names such as MPI and CUDA toggles.

Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ The following table summarizes the key features of each algorithm:
| **[DumpyOS](https://helios2.mi.parisdescartes.fr/~themisp/publications/vldbj24-dumpyos.pdf)** | In-memory scalable data series similarity search using an adaptive multi-ary iSAX index |
| **[FreSH](http://publications.ics.forth.gr/tech-reports/2023/2023.TR489_FreSh_A_LockFree_Data_Series_Index.pdf)** | In-memory lock-free parallel similarity search using an iSAX index (SRDS 2023) |
| **[COCONUT](http://www.vldb.org/pvldb/vol11/p677-kondylakis.pdf)** | Sortable-SAX index built bottom-up; supports both static datasets and **streaming** (incremental) inserts (PVLDB 2018) |
| **[FAISS](https://github.com/facebookresearch/faiss) flat** | Exact flat baseline backed by `faiss::IndexFlatL2`; static build plus streaming inserts with no retraining. Optional: build with `-DBUILD_FAISS=ON` |

### Incremental streaming inserts

`BruteForceSearch`, `LbBruteforce`, `Messi`, and `Coconut` implement the common streaming API. Build
the initial index once, then append one series or a contiguous batch without rebuilding:
`BruteForceSearch`, `LbBruteforce`, `Messi`, `Coconut`, and `FaissFlat` implement the common
streaming API. Build the initial index once, then append one series or a contiguous batch without
rebuilding:

```cpp
daisy::Messi search(daisy::DistanceType::L2_SQUARED);
Expand All @@ -76,6 +78,10 @@ SAX summary for each insert using the breakpoints established during the initial
routes each new summary into the live iSAX tree and splits full leaves without rebuilding the
index. Its first insert copies a borrowed initial in-memory database into owned growable storage.

`FaissFlat` is the streaming baseline: `faiss::IndexFlatL2::add()` appends into flat storage with
no training step and no rebuild, so nothing is frozen at build time that could drift from the live
data and the index stays exact after every insert. It supports `L2_SQUARED` only.

Streaming updates are not concurrent with queries. Inserts can reallocate the owned database,
so callers should not retain a pointer returned by `getDatabase()` across them.

Expand Down Expand Up @@ -133,6 +139,7 @@ Based on the available hardware, you can specify the below arguments to enable/d
| `BUILD_ODYSSEY` | Enable MPI for distributed computing | `OFF` | OpenMPI/MPICH |
| `BUILD_SING` | Enable CUDA for GPU acceleration | `OFF` | CUDA Toolkit |
| `BUILD_COCONUT` | Enable the COCONUT sortable-SAX algorithm | `ON` | Core library |
| `BUILD_FAISS` | Enable the `FaissFlat` baseline algorithm | `OFF` | FAISS submodule, CMake 3.24+, BLAS/LAPACK |
| `DEBUG_MSG` | Enable debug output | `OFF` | None |
| `BUILD_SOFA` | Enable SOFA with SFA-Based indexing | `OFF` | FFTW3 |

Expand Down Expand Up @@ -220,6 +227,10 @@ cd build
./benchmark/bm_Messi_L2Square
./benchmark/bm_Messi_Streaming

# FAISS baselines (CMake 3.24+ required by the FAISS submodule)
./benchmark/bm_FaissFlat_L2Square
./benchmark/bm_Faiss_Streaming

# Advanced algorithms (if available)
./benchmark/bm_Odyssey_L2Square # MPI required
./benchmark/bm_Sing_L2Square # CUDA required
Expand Down
40 changes: 32 additions & 8 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ if(DEBUG_MSG)
message(STATUS "FetchContent module included.")
endif()

# Google Benchmark's own test suite wants a googletest checkout nested inside its
# source tree, which FetchContent does not provide. We only need the library, so
# keep its self-tests off or configuration fails before FAISS is even reached.
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE)

FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
Expand Down Expand Up @@ -42,13 +48,12 @@ if(DEBUG_MSG)
message(STATUS "GoogleTest fetched and available.")
endif()

# ====== FAISS (CPU-only) for benchmarks ======
set(FAISS_ENABLE_GPU OFF CACHE BOOL "" FORCE)
set(FAISS_ENABLE_PYTHON OFF CACHE BOOL "" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(FAISS_ENABLE_EXTRAS OFF CACHE BOOL "" FORCE)
set(FAISS_ENABLE_C_API OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/faiss ${CMAKE_CURRENT_BINARY_DIR}/faiss)
# FAISS is configured once by the root CMakeLists (it is shared with the
# daisy::FaissFlat algorithm) and exposes BUILD_FAISS_AVAILABLE. The FAISS
# benchmarks below are skipped when it could not be configured.
if(NOT BUILD_FAISS_AVAILABLE AND DEBUG_MSG)
message(STATUS "BUILD_FAISS_AVAILABLE is FALSE. Skipping FAISS benchmarks.")
endif()

# ////// BRUTEFORCE //////
if(DEBUG_MSG)
Expand Down Expand Up @@ -221,7 +226,9 @@ target_include_directories(bm_Messi_Streaming
${CMAKE_CURRENT_SOURCE_DIR}/../lib
)

# ////// FAISS FLAT //////
# ////// FAISS FLAT + FAISS STREAMING //////
if(BUILD_FAISS_AVAILABLE)

if(DEBUG_MSG)
message(STATUS "---")
message(STATUS "## Benchmark: FAISS Flat L2Square")
Expand Down Expand Up @@ -249,6 +256,23 @@ target_include_directories(bm_FaissFlat_L2Square
${CMAKE_CURRENT_SOURCE_DIR}/../commons
)

# ////// FAISS STREAMING //////
if(DEBUG_MSG)
message(STATUS "---")
message(STATUS "## Benchmark: FAISS Streaming")
endif()

add_executable(bm_Faiss_Streaming bm_Faiss_Streaming.cpp)

target_link_libraries(bm_Faiss_Streaming
PRIVATE
benchmark::benchmark
benchmark::benchmark_main
faiss
)

endif() # BUILD_FAISS_AVAILABLE

# ////// PARIS //////
if(DEBUG_MSG)
message(STATUS "---")
Expand Down
114 changes: 114 additions & 0 deletions benchmark/bm_Faiss_Streaming.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// FAISS streaming baseline.
//
// faiss::IndexFlatL2::add() is a pure append into the flat code storage: no
// training, no rebuild, and the index stays exact. That makes it the natural
// lower bound on what an incremental insert can cost, and the reference point
// for the DaiSy streaming algorithms (Bruteforce, LbBruteforce, Messi, Coconut).
//
// The series generator, DIM, INITIAL and the batch sizes mirror
// bm_Messi_Streaming.cpp so the insert numbers are directly comparable.
//
// Insert-only numbers alone are misleading: a flat index buys its cheap insert
// by paying O(n) per query. BM_Faiss_StreamingMixed therefore interleaves
// inserts and queries, which is where an indexed competitor earns its keep.

#include <benchmark/benchmark.h>

#include <faiss/IndexFlat.h>

#include <cmath>
#include <vector>

namespace
{
constexpr int DIM = 96;
constexpr int INITIAL = 4096;
constexpr int N_QUERY = 16;
constexpr int K = 10;

std::vector<float> makeSeries(int n, int phase_offset)
{
std::vector<float> data(static_cast<size_t>(n) * DIM);
for (int i = 0; i < n; ++i)
{
float *series = data.data() + static_cast<size_t>(i) * DIM;
const float phase = static_cast<float>(phase_offset + i) * 0.013f;
for (int j = 0; j < DIM; ++j)
series[j] = std::sin(0.07f * j + phase) +
0.5f * std::cos(0.19f * j - phase);
}
return data;
}
}

static void BM_Faiss_InsertBatch(benchmark::State &state)
{
const faiss::idx_t batch_size = static_cast<faiss::idx_t>(state.range(0));
const auto initial = makeSeries(INITIAL, 0);
const auto batch = makeSeries(static_cast<int>(batch_size), INITIAL);

faiss::IndexFlatL2 index(DIM);
index.add(INITIAL, initial.data());

// One untimed insert absorbs the reallocation that the first add() after a
// bulk build triggers (~2ms at INITIAL=4096, measured). Inside the loop that
// single spike dominates: it alone accounts for ~108us of an apparent 113us
// mean over 20 iterations, while steady-state add(1) is ~0.2us.
index.add(batch_size, batch.data());

for (auto _ : state)
{
index.add(batch_size, batch.data());
benchmark::ClobberMemory();
}

state.SetItemsProcessed(
static_cast<int64_t>(state.iterations()) * static_cast<int64_t>(batch_size));
}

BENCHMARK(BM_Faiss_InsertBatch)
->Arg(1)
->Arg(64)
->Arg(1024)
->Iterations(20)
->Unit(benchmark::kMicrosecond);

// One insert batch followed by one top-k query round, repeated. This is the
// workload a streaming index actually serves, and it exposes the O(n) scan that
// the flat index pays back on every query.
static void BM_Faiss_StreamingMixed(benchmark::State &state)
{
const faiss::idx_t batch_size = static_cast<faiss::idx_t>(state.range(0));
const auto initial = makeSeries(INITIAL, 0);
const auto batch = makeSeries(static_cast<int>(batch_size), INITIAL);
const auto queries = makeSeries(N_QUERY, INITIAL / 2);

faiss::IndexFlatL2 index(DIM);
index.add(INITIAL, initial.data());

std::vector<faiss::idx_t> I(static_cast<size_t>(N_QUERY) * K);
std::vector<float> D(static_cast<size_t>(N_QUERY) * K);

index.add(batch_size, batch.data());
index.search(N_QUERY, queries.data(), K, D.data(), I.data());

for (auto _ : state)
{
index.add(batch_size, batch.data());
index.search(N_QUERY, queries.data(), K, D.data(), I.data());
benchmark::ClobberMemory();
}

state.SetItemsProcessed(
static_cast<int64_t>(state.iterations()) * static_cast<int64_t>(batch_size));
state.counters["n_database_final"] = static_cast<double>(index.ntotal);
}

BENCHMARK(BM_Faiss_StreamingMixed)
->Arg(1)
->Arg(64)
->Arg(1024)
->Iterations(20)
->Unit(benchmark::kMicrosecond);

BENCHMARK_MAIN();
Loading
Loading