Bruteforce#
2026-08-27
4 min read time
The bruteforce method is running the KNN algorithm. It performs an extensive search, and in contrast to ANN methods produces an exact result.
#include <cuvs/neighbors/brute_force.h>
Index#
-
typedef cuvsBruteForceIndex *cuvsBruteForceIndex_t#
- CUVS_EXPORT cuvsError_t cuvsBruteForceIndexCreate (cuvsBruteForceIndex_t *index)
Allocate BRUTEFORCE index.
- Parameters:
index – [in] cuvsBruteForceIndex_t to allocate
- Returns:
cuvsError_t
- CUVS_EXPORT cuvsError_t cuvsBruteForceIndexDestroy (cuvsBruteForceIndex_t index)
De-allocate BRUTEFORCE index.
- Parameters:
index – [in] cuvsBruteForceIndex_t to de-allocate
-
struct cuvsBruteForceIndex#
- #include <brute_force.h>
Struct to hold address of cuvs::neighbors::brute_force::index and its active trained dtype.
Index build#
- CUVS_EXPORT cuvsError_t cuvsBruteForceBuild (cuvsResources_t res, DLManagedTensor *dataset, cuvsDistanceType metric, float metric_arg, cuvsBruteForceIndex_t index)
Build a BRUTEFORCE index with a
DLManagedTensorwhich has underlyingDLDeviceTypeequal tokDLCUDA,kDLCUDAHost,kDLCUDAManaged, orkDLCPU. Also, acceptable underlying types are:kDLDataType.code == kDLFloatandkDLDataType.bits = 64kDLDataType.code == kDLFloatandkDLDataType.bits = 32kDLDataType.code == kDLFloatandkDLDataType.bits = 16
Note that
metric_argis afloatfor every dataset type. It is read only by the Minkowski (LpUnexpanded) metric, and apthat is not exactly representable in float32 is rounded, so a float64 search returns the exact Lp distances for the roundedp— about 1e-7 relative away from the requested ones. Neighbor order is unaffected unless two candidates are that close together.#include <cuvs/core/c_api.h> #include <cuvs/neighbors/brute_force.h> // Create cuvsResources_t cuvsResources_t res; cuvsError_t res_create_status = cuvsResourcesCreate(&res); // Assume a populated `DLManagedTensor` type here DLManagedTensor dataset; // Create BRUTEFORCE index cuvsBruteForceIndex_t index; cuvsError_t index_create_status = cuvsBruteForceIndexCreate(&index); // Build the BRUTEFORCE Index cuvsError_t build_status = cuvsBruteForceBuild(res, &dataset_tensor, L2Expanded, 0.f, index); // de-allocate `index` and `res` cuvsError_t index_destroy_status = cuvsBruteForceIndexDestroy(index); cuvsError_t res_destroy_status = cuvsResourcesDestroy(res);
- Parameters:
res – [in] cuvsResources_t opaque C handle
dataset – [in] DLManagedTensor* training dataset
metric – [in] metric
metric_arg – [in] metric_arg
index – [out] cuvsBruteForceIndex_t Newly built BRUTEFORCE index
- Returns:
cuvsError_t
Index search#
- CUVS_EXPORT cuvsError_t cuvsBruteForceSearch (cuvsResources_t res, cuvsBruteForceIndex_t index, DLManagedTensor *queries, DLManagedTensor *neighbors, DLManagedTensor *distances, cuvsFilter prefilter)
Search a BRUTEFORCE index with a
DLManagedTensorwhich has underlyingDLDeviceTypeequal tokDLCUDA,kDLCUDAHost,kDLCUDAManaged. It is also important to note that the BRUTEFORCE index must have been built with the same type ofqueries, such that thecode,bitsandlanesofqueries.dl_tensor.dtypeall matchindex.dtype. Types for input are:queries:kDLDataType.code == kDLFloatandkDLDataType.bits = 64,32or16neighbors:kDLDataType.code == kDLIntandkDLDataType.bits = 64distances:kDLDataType.code == kDLFloatandkDLDataType.bits = 64for a float64 index,kDLDataType.bits = 32for a float32 or float16 index
#include <cuvs/core/c_api.h> #include <cuvs/neighbors/brute_force.h> // Create cuvsResources_t cuvsResources_t res; cuvsError_t res_create_status = cuvsResourcesCreate(&res); // Assume a populated `DLManagedTensor` type here DLManagedTensor dataset; DLManagedTensor queries; DLManagedTensor neighbors; DLManagedTensor bitmap; cuvsFilter prefilter{(uintptr_t)&bitmap, BITMAP}; // Search the `index` built using `cuvsBruteForceBuild` cuvsError_t search_status = cuvsBruteForceSearch(res, index, &queries, &neighbors, &distances, prefilter); // de-allocate `res` cuvsError_t res_destroy_status = cuvsResourcesDestroy(res);
- Parameters:
res – [in] cuvsResources_t opaque C handle
index – [in] cuvsBruteForceIndex which has been returned by
cuvsBruteForceBuildqueries – [in] DLManagedTensor* queries dataset to search
neighbors – [out] DLManagedTensor* output
kneighbors for queriesdistances – [out] DLManagedTensor* output
kdistances for queriesprefilter – [in] cuvsFilter input prefilter that can be used to filter queries and neighbors based on the given bitmap.
Index serialize#
- CUVS_EXPORT cuvsError_t cuvsBruteForceSerialize (cuvsResources_t res, const char *filename, cuvsBruteForceIndex_t index)
Save the index to file. The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.
#include <cuvs/neighbors/brute_force.h> // Create cuvsResources_t cuvsResources_t res; cuvsError_t res_create_status = cuvsResourcesCreate(&res); // create an index with `cuvsBruteforceBuild` cuvsBruteForceSerialize(res, "/path/to/index", index);
- Parameters:
res – [in] cuvsResources_t opaque C handle
filename – [in] the file name for saving the index
index – [in] BRUTEFORCE index
- CUVS_EXPORT cuvsError_t cuvsBruteForceDeserialize (cuvsResources_t res, const char *filename, cuvsBruteForceIndex_t index)
Load index from file. The serialization format can be subject to changes, therefore loading an index saved with a previous version of cuvs is not guaranteed to work.
The dtype of the loaded index is read from the file: numpy descriptors
f8,f4ande2restore a float64, float32 and float16 index respectively, andindex->dtypeis set accordingly.#include <cuvs/neighbors/brute_force.h> // Create cuvsResources_t cuvsResources_t res; cuvsError_t res_create_status = cuvsResourcesCreate(&res); // Deserialize an index previously built with `cuvsBruteforceBuild` cuvsBruteForceIndex_t index; cuvsBruteForceIndexCreate(&index); cuvsBruteForceDeserialize(res, "/path/to/index", index);
- Parameters:
res – [in] cuvsResources_t opaque C handle
filename – [in] the name of the file that stores the index
index – [out] BRUTEFORCE index loaded disk