Skip to content

Commit 408d9c9

Browse files
robertmaynardenp1s0
authored andcommitted
Refactor libcuvs_c header and source locations (rapidsai#1357)
In support of building a stable C interface for libcuvs I have moved all `C` code into a separate directory for both the headers and sources. This will make it easier to install just 'C' language headers, make it easier to apply code rules for C headers, and in the future add more CI checks for our C API. Authors: - Robert Maynard (https://github.com/robertmaynard) - MithunR (https://github.com/mythrocks) - Ben Frederickson (https://github.com/benfred) Approvers: - Kyle Edwards (https://github.com/KyleFromNVIDIA) - Ben Frederickson (https://github.com/benfred) URL: rapidsai#1357
1 parent 2b3383c commit 408d9c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+669
-503
lines changed

c/CMakeLists.txt

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# =============================================================================
2+
# Copyright (c) 2025, NVIDIA CORPORATION.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
# in compliance with the License. You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software distributed under the License
10+
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
# or implied. See the License for the specific language governing permissions and limitations under
12+
# the License.
13+
# =============================================================================
14+
15+
cmake_minimum_required(VERSION 3.30.4 FATAL_ERROR)
16+
include(../cmake/rapids_config.cmake)
17+
include(rapids-cmake)
18+
include(rapids-cpm)
19+
include(rapids-export)
20+
include(rapids-find)
21+
22+
project(
23+
CUVS_C
24+
VERSION "${RAPIDS_VERSION}"
25+
LANGUAGES CXX C
26+
)
27+
set(CMAKE_INSTALL_MESSAGE LAZY)
28+
29+
# ##################################################################################################
30+
# * User Options ------------------------------------------------------------
31+
32+
option(BUILD_SHARED_LIBS "Build cuvs_c shared libraries" ON)
33+
option(BUILD_TESTS "Build cuvs unit-tests" ON)
34+
35+
option(BUILD_CAGRA_HNSWLIB "Build CAGRA+hnswlib interface" ON)
36+
option(BUILD_MG_ALGOS "Build with multi-GPU support" ON)
37+
38+
option(CUVSC_STATIC_CUVS_LIBRARY "Link against statical version of libcuvs" OFF)
39+
40+
# Check if cuVS is already available. If so, it is the user's responsibility to ensure that the
41+
# CMake package is also available at build time of the Python cuvs package.
42+
if(NOT TARGET cuvs::cuvs)
43+
find_package(cuvs "${RAPIDS_VERSION}" REQUIRED)
44+
else()
45+
set(BUILDING_FROM_CUVS ON)
46+
endif()
47+
48+
# ##################################################################################################
49+
# * build type ---------------------------------------------------------------
50+
51+
# Set a default build type if none was specified
52+
rapids_cmake_build_type(Release)
53+
54+
# this is needed for clang-tidy runs
55+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
56+
57+
if(NOT DEFINED CUVS_CXX_FLAGS)
58+
include(../cpp/cmake/modules/ConfigureCUDA.cmake)
59+
endif()
60+
61+
# ##################################################################################################
62+
# * Requirements -------------------------------------------------------------
63+
64+
# add third party dependencies using CPM
65+
rapids_cpm_init()
66+
67+
# add third party dependencies using CPM
68+
include(../cpp/cmake/thirdparty/get_dlpack.cmake)
69+
70+
if(BUILD_CAGRA_HNSWLIB)
71+
include(../cpp/cmake/thirdparty/get_hnswlib.cmake)
72+
endif()
73+
74+
# ##################################################################################################
75+
# * cuvs_c -------------------------------------------------------------------------------
76+
add_library(
77+
cuvs_c SHARED
78+
src/core/c_api.cpp
79+
src/cluster/kmeans.cpp
80+
src/neighbors/brute_force.cpp
81+
src/neighbors/ivf_flat.cpp
82+
src/neighbors/ivf_pq.cpp
83+
src/neighbors/cagra.cpp
84+
$<$<BOOL:${BUILD_CAGRA_HNSWLIB}>:src/neighbors/hnsw.cpp>
85+
$<$<BOOL:${BUILD_MG_ALGOS}>:src/neighbors/mg_ivf_pq.cpp>
86+
$<$<BOOL:${BUILD_MG_ALGOS}>:src/neighbors/mg_ivf_flat.cpp>
87+
$<$<BOOL:${BUILD_MG_ALGOS}>:src/neighbors/mg_cagra.cpp>
88+
src/neighbors/nn_descent.cpp
89+
src/neighbors/vamana.cpp
90+
src/neighbors/refine.cpp
91+
src/neighbors/tiered_index.cpp
92+
src/neighbors/all_neighbors.cpp
93+
src/preprocessing/quantize/binary.cpp
94+
src/preprocessing/quantize/scalar.cpp
95+
src/distance/pairwise_distance.cpp
96+
)
97+
add_library(cuvs::c_api ALIAS cuvs_c)
98+
set_target_properties(
99+
cuvs_c
100+
PROPERTIES BUILD_RPATH "\$ORIGIN"
101+
INSTALL_RPATH "\$ORIGIN"
102+
CXX_STANDARD 17
103+
CXX_STANDARD_REQUIRED ON
104+
POSITION_INDEPENDENT_CODE ON
105+
INTERFACE_POSITION_INDEPENDENT_CODE ON
106+
EXPORT_NAME c_api
107+
)
108+
109+
target_compile_definitions(cuvs_c PUBLIC $<$<BOOL:${BUILD_CAGRA_HNSWLIB}>:CUVS_BUILD_CAGRA_HNSWLIB>)
110+
111+
target_compile_options(cuvs_c PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${CUVS_CXX_FLAGS}>")
112+
113+
target_include_directories(
114+
cuvs_c
115+
PUBLIC "$<BUILD_INTERFACE:${DLPACK_INCLUDE_DIR}>"
116+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
117+
INTERFACE "$<INSTALL_INTERFACE:include>"
118+
)
119+
120+
target_link_libraries(
121+
cuvs_c
122+
PUBLIC $<IF:$<BOOL:${CUVSC_STATIC_CUVS_LIBRARY}>,cuvs::cuvs_static,cuvs::cuvs>
123+
PRIVATE raft::raft $<TARGET_NAME_IF_EXISTS:hnswlib::hnswlib>
124+
)
125+
126+
# ##################################################################################################
127+
# * install targets-----------------------------------------------------------
128+
if(PROJECT_IS_TOP_LEVEL)
129+
rapids_cmake_install_lib_dir(lib_dir)
130+
include(GNUInstallDirs)
131+
include(CPack)
132+
133+
# Add CUDAToolkit as an export dependency
134+
rapids_export_package(INSTALL CUDAToolkit cuvs-c-exports)
135+
rapids_export_package(BUILD CUDAToolkit cuvs-c-exports)
136+
137+
install(
138+
TARGETS cuvs_c
139+
DESTINATION ${lib_dir}
140+
COMPONENT cuvs_c
141+
EXPORT cuvs-c-exports
142+
)
143+
144+
rapids_export(
145+
INSTALL cuvs_c
146+
VERSION "${RAPIDS_VERSION}"
147+
EXPORT_SET cuvs-c-exports
148+
GLOBAL_TARGETS cuvs_c
149+
NAMESPACE cuvs::
150+
)
151+
rapids_export(
152+
BUILD cuvs_c
153+
VERSION "${RAPIDS_VERSION}"
154+
EXPORT_SET cuvs-c-exports
155+
GLOBAL_TARGETS cuvs_c
156+
NAMESPACE cuvs::
157+
)
158+
install(
159+
DIRECTORY include/cuvs
160+
COMPONENT cuvs_c
161+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
162+
)
163+
endif()
164+
165+
# ##################################################################################################
166+
# * build tests ----------------------------------------------------
167+
if(BUILD_TESTS)
168+
enable_language(CUDA)
169+
find_package(CUDAToolkit REQUIRED)
170+
171+
enable_testing()
172+
add_subdirectory(tests)
173+
endif()

cpp/include/cuvs/cluster/kmeans.h renamed to c/include/cuvs/cluster/kmeans.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ typedef enum {
3434
/**
3535
* Sample the centroids using the kmeans++ strategy
3636
*/
37-
KMeansPlusPlus,
37+
KMeansPlusPlus = 0,
3838

3939
/**
4040
* Sample the centroids uniformly at random
4141
*/
42-
Random,
42+
Random = 1,
4343

4444
/**
4545
* User provides the array of initial centroids
4646
*/
47-
Array
47+
Array = 2
4848
} cuvsKMeansInitMethod;
4949

5050
/**

cpp/include/cuvs/core/c_api.h renamed to c/include/cuvs/core/c_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C" {
3333
* @brief An enum denoting error statuses for function calls
3434
*
3535
*/
36-
typedef enum { CUVS_ERROR, CUVS_SUCCESS } cuvsError_t;
36+
typedef enum { CUVS_ERROR = 0, CUVS_SUCCESS = 1 } cuvsError_t;
3737

3838
/** @brief Returns a string describing the last seen error on this thread, or
3939
* NULL if the last function succeeded.
File renamed without changes.
File renamed without changes.

cpp/include/cuvs/neighbors/cagra.h renamed to c/include/cuvs/neighbors/cagra.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ extern "C" {
3939
*/
4040
enum cuvsCagraGraphBuildAlgo {
4141
/* Select build algorithm automatically */
42-
AUTO_SELECT,
42+
AUTO_SELECT = 0,
4343
/* Use IVF-PQ to build all-neighbors knn graph */
44-
IVF_PQ,
44+
IVF_PQ = 1,
4545
/* Experimental, use NN-Descent to build all-neighbors knn graph */
46-
NN_DESCENT,
46+
NN_DESCENT = 2,
4747
/* Experimental, use iterative cagra search and optimize to build the knn graph */
48-
ITERATIVE_CAGRA_SEARCH
48+
ITERATIVE_CAGRA_SEARCH = 3
4949
};
5050

5151
/** Parameters for VPQ compression. */
@@ -210,18 +210,18 @@ cuvsError_t cuvsCagraExtendParamsDestroy(cuvsCagraExtendParams_t params);
210210
*/
211211
enum cuvsCagraSearchAlgo {
212212
/** For large batch sizes. */
213-
SINGLE_CTA,
213+
SINGLE_CTA = 0,
214214
/** For small batch sizes. */
215-
MULTI_CTA,
216-
MULTI_KERNEL,
217-
AUTO
215+
MULTI_CTA = 1,
216+
MULTI_KERNEL = 2,
217+
AUTO = 3
218218
};
219219

220220
/**
221221
* @brief Enum to denote Hash Mode used while searching CAGRA index
222222
*
223223
*/
224-
enum cuvsCagraHashMode { HASH, SMALL, AUTO_HASH };
224+
enum cuvsCagraHashMode { HASH = 0, SMALL = 1, AUTO_HASH = 2 };
225225

226226
/**
227227
* @brief Supplemental parameters to search CAGRA index

cpp/include/cuvs/neighbors/common.h renamed to c/include/cuvs/neighbors/common.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, NVIDIA CORPORATION.
2+
* Copyright (c) 2024-2025, NVIDIA CORPORATION.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,11 +34,11 @@ extern "C" {
3434
*/
3535
enum cuvsFilterType {
3636
/* No filter */
37-
NO_FILTER,
37+
NO_FILTER = 0,
3838
/* Filter an index with a bitset */
39-
BITSET,
39+
BITSET = 1,
4040
/* Filter an index with a bitmap */
41-
BITMAP
41+
BITMAP = 2
4242
};
4343

4444
/**

cpp/include/cuvs/neighbors/hnsw.h renamed to c/include/cuvs/neighbors/hnsw.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ extern "C" {
4040
*/
4141
enum cuvsHnswHierarchy {
4242
/* Flat hierarchy, search is base-layer only */
43-
NONE,
43+
NONE = 0,
4444
/* Full hierarchy is built using the CPU */
45-
CPU,
45+
CPU = 1,
4646
/* Full hierarchy is built using the GPU */
47-
GPU
47+
GPU = 2
4848
};
4949

5050
struct cuvsHnswIndexParams {

0 commit comments

Comments
 (0)