Bruteforce#

2026-08-27

4 min read time

Applies to Linux

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 DLManagedTensor which has underlying DLDeviceType equal to kDLCUDA, kDLCUDAHost, kDLCUDAManaged, or kDLCPU. Also, acceptable underlying types are:

  1. kDLDataType.code == kDLFloat and kDLDataType.bits = 64

  2. kDLDataType.code == kDLFloat and kDLDataType.bits = 32

  3. kDLDataType.code == kDLFloat and kDLDataType.bits = 16

Note that metric_arg is a float for every dataset type. It is read only by the Minkowski (LpUnexpanded) metric, and a p that is not exactly representable in float32 is rounded, so a float64 search returns the exact Lp distances for the rounded p &#8212; 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 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, f4 and e2 restore a float64, float32 and float16 index respectively, and index->dtype is 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