Skip to content

Commit 8f8ed02

Browse files
Merge pull request #56 from MChatzakis/feature/sing-dtw
Feature/sing dtw
2 parents baf3dbe + 0a9b1d8 commit 8f8ed02

16 files changed

Lines changed: 1619 additions & 76 deletions

CMakeLists.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -274,53 +274,53 @@ if(BUILD_PYTHON)
274274
message(STATUS "Subdirectory 'extern/pybind11' added.")
275275
endif()
276276

277-
# Build the Python module
278-
pybind11_add_module(daisy pybinds/setup.cpp)
277+
# Build the Python module as daisy._core
278+
pybind11_add_module(daisy_core pybinds/setup.cpp)
279+
set_target_properties(daisy_core PROPERTIES
280+
OUTPUT_NAME "_core"
281+
PREFIX "" # no 'lib' prefix, produce _core.cpython-*.so
282+
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/daisy"
283+
)
279284
if(DEBUG_MSG)
280-
message(STATUS "Python module 'daisy' created from pybinds/setup.cpp.")
285+
message(STATUS "Python module 'daisy._core' created from pybinds/setup.cpp.")
281286
endif()
282287

283-
target_link_libraries(daisy PRIVATE dino_lib)
288+
target_link_libraries(daisy_core PRIVATE dino_lib)
284289
if(DEBUG_MSG)
285-
message(STATUS "Python module 'daisy' linked to 'dino_lib'.")
290+
message(STATUS "Python module 'daisy._core' linked to 'dino_lib'.")
286291
endif()
287292

288293
# Pass BUILD_SING_ENABLED definition to Python module
289294
if (BUILD_SING_AVAILABLE)
290-
target_compile_definitions(daisy PRIVATE SING_CUDA_ENABLED=1)
295+
target_compile_definitions(daisy_core PRIVATE SING_CUDA_ENABLED=1)
291296
if(DEBUG_MSG)
292-
message(STATUS "Python module 'daisy' compiled with SING_CUDA_ENABLED=1.")
297+
message(STATUS "Python module 'daisy._core' compiled with SING_CUDA_ENABLED=1.")
293298
endif()
294299
else()
295-
target_compile_definitions(daisy PRIVATE SING_CUDA_ENABLED=0)
300+
target_compile_definitions(daisy_core PRIVATE SING_CUDA_ENABLED=0)
296301
if(DEBUG_MSG)
297-
message(STATUS "Python module 'daisy' compiled with SING_CUDA_ENABLED=0.")
302+
message(STATUS "Python module 'daisy._core' compiled with SING_CUDA_ENABLED=0.")
298303
endif()
299304
endif()
300305

306+
# Link Python module 'daisy._core' with MPI when Odyssey is available
301307
if (BUILD_ODYSSEY_AVAILABLE)
302308
if(DEBUG_MSG)
303-
message(STATUS "Linking Python module 'daisy' with MPI.")
309+
message(STATUS "Linking Python module 'daisy._core' with MPI.")
304310
endif()
305-
target_link_libraries(daisy PRIVATE ${MPI_CXX_LIBRARIES})
306-
target_include_directories(daisy PRIVATE ${MPI_CXX_INCLUDE_DIRS})
311+
target_link_libraries(daisy_core PRIVATE ${MPI_CXX_LIBRARIES})
312+
target_include_directories(daisy_core PRIVATE ${MPI_CXX_INCLUDE_DIRS})
307313
if(DEBUG_MSG)
308314
message(DEBUG "MPI Libraries: ${MPI_CXX_LIBRARIES}")
309315
message(DEBUG "MPI Includes: ${MPI_CXX_INCLUDE_DIRS}")
310316
endif()
311317
else()
312318
if(DEBUG_MSG)
313-
message(STATUS "BUILD_ODYSSEY_AVAILABLE is FALSE. Python module 'daisy' will NOT be linked with MPI.")
319+
message(STATUS "BUILD_ODYSSEY_AVAILABLE is FALSE. Python module 'daisy._core' will NOT be linked with MPI.")
314320
endif()
315-
endif()
316-
317-
# Set output directory for the .so file (demos/ so that import daisy finds daisy.*.so when run from demos)
318-
set_target_properties(daisy PROPERTIES
319-
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/demos"
320-
)
321-
if(DEBUG_MSG)
322-
message(STATUS "Python module 'daisy' output directory set to: ${CMAKE_SOURCE_DIR}/demos")
323321
endif()
322+
323+
# (Python module 'daisy._core' already configured to output into ${PROJECT_SOURCE_DIR}/daisy above)
324324
else()
325325
if(DEBUG_MSG)
326326
message(STATUS "BUILD_PYTHON is OFF. Skipping Python bindings.")

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,3 @@ The logo of DaiSy was designed by [Eva Chamilaki](https://evachamilaki.github.io
191191

192192

193193

194-

demos/CMakeLists.txt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,28 @@ if(BUILD_DEMO)
202202
message(STATUS "Include directories added for demo_Messi_DTW.")
203203
endif()
204204

205+
# ////// SING DTW //////
206+
if(BUILD_SING_AVAILABLE)
207+
if(DEBUG_MSG)
208+
message(STATUS "---")
209+
message(STATUS "## Demo: Sing DTW")
210+
message(STATUS "Attempting to add executable: demo_Sing_DTW")
211+
endif()
212+
add_executable(demo_Sing_DTW demo_Sing_DTW.cpp)
213+
target_link_libraries(demo_Sing_DTW PRIVATE dino_lib commons_lib)
214+
target_include_directories(demo_Sing_DTW PRIVATE
215+
${CMAKE_CURRENT_SOURCE_DIR}/../lib
216+
${CMAKE_CURRENT_SOURCE_DIR}/../commons
217+
)
218+
if(DEBUG_MSG)
219+
message(STATUS "Executable demo_Sing_DTW added and configured (CUDA/Sing available).")
220+
endif()
221+
else()
222+
if(DEBUG_MSG)
223+
message(STATUS "BUILD_SING_AVAILABLE is FALSE. demo_Sing_DTW will NOT be built.")
224+
endif()
225+
endif()
226+
205227
# ////// ODYSSEY L2Square //////
206228
if(DEBUG_MSG)
207229
message(STATUS "---")
@@ -325,25 +347,23 @@ if(BUILD_DEMO)
325347
# ////// SING L2Square //////
326348
if(DEBUG_MSG)
327349
message(STATUS "---")
328-
message(STATUS "## Demo: Sing")
350+
message(STATUS "## Demo: Sing L2Square")
329351
endif()
330352

331-
if(SING_CUDA_AVAILABLE)
353+
if(BUILD_SING_AVAILABLE)
332354
if(DEBUG_MSG)
333-
message(STATUS "SING_CUDA_AVAILABLE is TRUE. Building demo_Sing_L2Square.")
355+
message(STATUS "BUILD_SING_AVAILABLE is TRUE. Building demo_Sing_L2Square.")
334356
endif()
335357

336358
add_executable(demo_Sing_L2Square demo_Sing_L2Square.cpp)
337-
338-
target_link_libraries(demo_Sing_L2Square PRIVATE dino_lib commons_lib CUDA::cudart)
339-
359+
target_link_libraries(demo_Sing_L2Square PRIVATE dino_lib commons_lib)
340360
target_include_directories(demo_Sing_L2Square PRIVATE
341361
${CMAKE_CURRENT_SOURCE_DIR}/../lib
342362
${CMAKE_CURRENT_SOURCE_DIR}/../commons
343363
)
344364
else()
345365
if(DEBUG_MSG)
346-
message(STATUS "SING_CUDA_AVAILABLE is FALSE. demo_Sing_L2Square will NOT be built.")
366+
message(STATUS "BUILD_SING_AVAILABLE is FALSE. demo_Sing_L2Square will NOT be built.")
347367
endif()
348368
endif()
349369

demos/demo_Messi_DTW.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int main(){
1616
printf("Loaded %llu database points and %llu query points with dimension %llu\n", n_database, n_query, dim);
1717

1818
daisy::Messi messi_search(daisy::DistanceType::DTW);
19-
messi_search.setNumThreads(4);
19+
messi_search.setNumThreads(1);
2020

2121
int warp_window = std::max(1, static_cast<int>(dim * 0.1));
2222
messi_search.setWarpingWindow(warp_window);

demos/demo_Messi_DTW.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
def main():
1010

11-
n_database = 200000
12-
dim = 96
13-
n_query = 10
11+
n_database = 840000
12+
dim = 128
13+
n_query = 20
1414
k = 5
1515

1616
np.random.seed(100)

demos/demo_Sing_DTW.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "../commons/dataloaders.hpp"
2+
#include "../lib/daisy.hpp"
3+
#include <chrono>
4+
#include <algorithm>
5+
6+
int main() {
7+
daisy::idx_t n_database = 200000;
8+
unsigned long long dim = 96;
9+
unsigned long long n_query = 10;
10+
daisy::idx_t k = 1; // current Sing DTW implementation supports only k = 1
11+
12+
float *database = loadRandomData(n_database, dim, 100, true);
13+
float *query = loadRandomData(n_query, dim, 50, true);
14+
15+
daisy::Sing sing_search(daisy::DistanceType::DTW);
16+
sing_search.setNumThreads(1);
17+
18+
// Sing::buildIndex expects a DataSource*, use InMemoryDataSource as in other components.
19+
daisy::InMemoryDataSource data_source(database, n_database, dim);
20+
printf("indexing\n");
21+
sing_search.buildIndex(&data_source);
22+
printf("finished indexing\n");
23+
24+
daisy::idx_t *I = new daisy::idx_t[n_query * k];
25+
float *D = new float[n_query * k];
26+
printf("querying\n");
27+
sing_search.searchIndex(query, n_query, k, I, D);
28+
printf("finished querying\n");
29+
30+
for (daisy::idx_t i = 0; i < n_query; i++) {
31+
printf("Query %llu:\n", i);
32+
printf(" Distances: ");
33+
for (daisy::idx_t j = 0; j < k; j++) {
34+
printf("%f ", D[i * k + j]);
35+
}
36+
}
37+
38+
delete[] database;
39+
delete[] query;
40+
delete[] I;
41+
delete[] D;
42+
43+
return 0;
44+
}
45+

demos/demo_Sing_DTW.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Demo Sing DTW — build index from NumPy array and run DTW search.
3+
Requires Sing to be built with CUDA (SING_CUDA_ENABLED).
4+
Note: current Sing DTW implementation only supports k = 1.
5+
"""
6+
import sys
7+
import os
8+
import time
9+
import numpy as np
10+
11+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
12+
13+
from daisy import DistanceType, Sing
14+
15+
16+
def main():
17+
# Small-ish sizes so the demo runs quickly
18+
n_database = 50000
19+
dim = 128
20+
n_query = 5
21+
k = 1 # DTW implementation in Sing currently supports only k = 1
22+
23+
np.random.seed(100)
24+
db = np.random.randn(n_database, dim).astype(np.float32)
25+
np.random.seed(50)
26+
query = np.random.randn(n_query, dim).astype(np.float32)
27+
print(f"Loaded {n_database} database points and {n_query} query points with dimension {dim}")
28+
29+
sing_search = Sing(DistanceType.DTW)
30+
# Optional: use multiple threads
31+
sing_search.setNumThreads(4)
32+
33+
sing_search.buildIndex(db)
34+
print(f"buildIndex (DTW) done")
35+
36+
print(f"Starting DTW search (n_query={n_query}, k={k})...")
37+
I, D = sing_search.searchIndex(query, k)
38+
print(f"Search done)")
39+
40+
print("\nPython Sing DTW Search Results:")
41+
for i in range(n_query):
42+
print(f"Query {i}:")
43+
print("Distances:", D[i])
44+
print("Indices:", I[i])
45+
print()
46+
47+
return 0
48+
49+
50+
if __name__ == "__main__":
51+
sys.exit(main())
52+

lib/algos/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ if(DEBUG_MSG)
128128
endif()
129129

130130
# ////// SING //////
131-
if (SING_CUDA_AVAILABLE)
131+
if (BUILD_SING_AVAILABLE)
132132
if(DEBUG_MSG)
133-
message(STATUS "SING_CUDA_AVAILABLE is TRUE. Configuring CUDA for SING components.")
133+
message(STATUS "BUILD_SING_AVAILABLE is TRUE. Configuring CUDA for SING components.")
134134
message(STATUS "Adding CUDA Toolkit include directories to dino_lib...")
135135
endif()
136136

@@ -171,7 +171,7 @@ if (SING_CUDA_AVAILABLE)
171171
endif()
172172
else()
173173
if(DEBUG_MSG)
174-
message(STATUS "SING_CUDA_AVAILABLE is FALSE. CUDA configuration for SING skipped.")
174+
message(STATUS "BUILD_SING_AVAILABLE is FALSE. CUDA configuration for SING skipped.")
175175
endif()
176176
target_compile_definitions(dino_lib PUBLIC SING_CUDA_ENABLED=0)
177177
endif()

0 commit comments

Comments
 (0)