Skip to content

Commit

Permalink
Add: Metadata extraction in C
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Apr 8, 2024
1 parent 540fc75 commit 2c698cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
29 changes: 24 additions & 5 deletions c/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ search_result_t search_(index_dense_t* index, void const* vector, scalar_kind_t

extern "C" {

USEARCH_EXPORT char const* usearch_version(void) {
int major = USEARCH_VERSION_MAJOR;
int minor = USEARCH_VERSION_MINOR;
int patch = USEARCH_VERSION_PATCH;
static char version[32];
sprintf(version, "%d.%d.%d", major, minor, patch);
return version;
}

USEARCH_EXPORT usearch_index_t usearch_init(usearch_init_options_t* options, usearch_error_t* error) {

USEARCH_ASSERT(options && error && "Missing arguments");
Expand Down Expand Up @@ -275,6 +284,16 @@ USEARCH_EXPORT size_t usearch_expansion_search(usearch_index_t index, usearch_er
return reinterpret_cast<index_dense_t*>(index)->expansion_search();
}

USEARCH_EXPORT size_t usearch_memory_usage(usearch_index_t index, usearch_error_t* error) {
USEARCH_ASSERT(index && error && "Missing arguments");
return reinterpret_cast<index_dense_t*>(index)->memory_usage();
}

USEARCH_EXPORT char const* usearch_hardware_acceleration(usearch_index_t index, usearch_error_t* error) {
USEARCH_ASSERT(index && error && "Missing arguments");
return reinterpret_cast<index_dense_t*>(index)->metric().isa_name();
}

USEARCH_EXPORT void usearch_change_expansion_add(usearch_index_t index, size_t expansion, usearch_error_t* error) {
USEARCH_ASSERT(index && error && "Missing arguments");
reinterpret_cast<index_dense_t*>(index)->change_expansion_add(expansion);
Expand Down Expand Up @@ -348,11 +367,11 @@ USEARCH_EXPORT size_t usearch_search(
return result.dump_to(found_keys, found_distances);
}

USEARCH_EXPORT size_t usearch_filtered_search( //
usearch_index_t index, //
void const* query, usearch_scalar_kind_t query_kind, //
int (*filter)(usearch_key_t key, void* filter_state), void* filter_state, //
size_t results_limit, usearch_key_t* found_keys, usearch_distance_t* found_distances, usearch_error_t* error) {
USEARCH_EXPORT size_t usearch_filtered_search( //
usearch_index_t index, //
void const* query, usearch_scalar_kind_t query_kind, size_t results_limit, //
int (*filter)(usearch_key_t key, void* filter_state), void* filter_state, //
usearch_key_t* found_keys, usearch_distance_t* found_distances, usearch_error_t* error) {

USEARCH_ASSERT(index && query && filter && error && "Missing arguments");
search_result_t result =
Expand Down
4 changes: 4 additions & 0 deletions c/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ void test_init(size_t const collection_size, size_t const dimensions) {
ASSERT(usearch_capacity(index, &error) == collection_size, error);
ASSERT(usearch_dimensions(index, &error) == dimensions, error);
ASSERT(usearch_connectivity(index, &error) == opts.connectivity, error);
ASSERT(usearch_hardware_acceleration(index, &error), error);
ASSERT(usearch_memory_usage(index, &error), error);

usearch_free(index, &error);
ASSERT(!error, error);
Expand Down Expand Up @@ -361,6 +363,8 @@ void test_view(size_t const collection_size, size_t const dimensions) {
}

int main(int argc, char const* argv[]) {
printf("Running tests...\n");
printf("USearch version: %s\n", usearch_version());

size_t collection_sizes[] = {11, 512};
size_t dimensions[] = {83, 2}; // Not all distance functions make sense for 1 dimensional data
Expand Down
36 changes: 30 additions & 6 deletions c/usearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ USEARCH_EXPORT typedef struct usearch_init_options_t {
bool multi;
} usearch_init_options_t;

/**
* @brief Retrieves the version of the library.
* @return The version of the library.
*/
USEARCH_EXPORT char const* usearch_version(void);

/**
* @brief Initializes a new instance of the index.
* @param options Pointer to the `usearch_init_options_t` structure containing initialization options.
Expand All @@ -112,6 +118,22 @@ USEARCH_EXPORT usearch_index_t usearch_init(usearch_init_options_t* options, use
*/
USEARCH_EXPORT void usearch_free(usearch_index_t index, usearch_error_t* error);

/**
* @brief Reports the memory usage of the index.
* @param[in] index The handle to the USearch index to be queried.
* @param[out] error Pointer to a string where the error message will be stored, if an error occurs.
* @return Number of bytes used by the index.
*/
USEARCH_EXPORT size_t usearch_memory_usage(usearch_index_t index, usearch_error_t* error);

/**
* @brief Reports the SIMD capabilities used by the index on the current CPU.
* @param[in] index The handle to the USearch index to be queried.
* @param[out] error Pointer to a string where the error message will be stored, if an error occurs.
* @return The codename of the SIMD instruction set used by the index.
*/
USEARCH_EXPORT char const* usearch_hardware_acceleration(usearch_index_t index, usearch_error_t* error);

/**
* @brief Reports expected file size after serialization.
* @param[in] index The handle to the USearch index to be serialized.
Expand Down Expand Up @@ -320,10 +342,10 @@ USEARCH_EXPORT size_t usearch_count(usearch_index_t index, usearch_key_t, usearc
* @param[out] error Pointer to a string where the error message will be stored, if an error occurs.
* @return Number of found matches.
*/
USEARCH_EXPORT size_t usearch_search( //
usearch_index_t index, //
void const* query_vector, usearch_scalar_kind_t query_kind, //
size_t count, usearch_key_t* keys, usearch_distance_t* distances, usearch_error_t* error);
USEARCH_EXPORT size_t usearch_search( //
usearch_index_t index, //
void const* query_vector, usearch_scalar_kind_t query_kind, size_t count, //
usearch_key_t* keys, usearch_distance_t* distances, usearch_error_t* error);

/**
* @brief Performs k-Approximate Nearest Neighbors (kANN) Search for closest vectors to query,
Expand All @@ -333,16 +355,18 @@ USEARCH_EXPORT size_t usearch_search( //
* @param[in] query_vector Pointer to the query vector data.
* @param[in] query_kind The scalar type used in the query vector data.
* @param[in] count Upper bound on the number of neighbors to search, the "k" in "kANN".
* @param[in] filter The custom filter function that returns `true` for vectors to be included.
* @param[in] filter_state The @b optional state pointer to be passed to the custom filter function.
* @param[out] keys Output buffer for up to `count` nearest neighbors keys.
* @param[out] distances Output buffer for up to `count` distances to nearest neighbors.
* @param[out] error Pointer to a string where the error message will be stored, if an error occurs.
* @return Number of found matches.
*/
USEARCH_EXPORT size_t usearch_filtered_search( //
usearch_index_t index, //
void const* query_vector, usearch_scalar_kind_t query_kind, //
void const* query_vector, usearch_scalar_kind_t query_kind, size_t count, //
int (*filter)(usearch_key_t key, void* filter_state), void* filter_state, //
size_t count, usearch_key_t* keys, usearch_distance_t* distances, usearch_error_t* error);
usearch_key_t* keys, usearch_distance_t* distances, usearch_error_t* error);

/**
* @brief Retrieves the vector associated with the given key from the index.
Expand Down

0 comments on commit 2c698cd

Please sign in to comment.