Skip to content

Commit

Permalink
c_api/
Browse files Browse the repository at this point in the history
  • Loading branch information
lianapatel committed Jun 11, 2024
1 parent b30fdc8 commit 445b1ca
Show file tree
Hide file tree
Showing 62 changed files with 5,717 additions and 0 deletions.
114 changes: 114 additions & 0 deletions c_api/AutoTune_c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-

#include "AutoTune_c.h"
#include <faiss/AutoTune.h>
#include <cstring>
#include "macros_impl.h"

using faiss::Index;
using faiss::ParameterRange;
using faiss::ParameterSpace;

const char* faiss_ParameterRange_name(const FaissParameterRange* range) {
return reinterpret_cast<const ParameterRange*>(range)->name.c_str();
}

void faiss_ParameterRange_values(
FaissParameterRange* range,
double** p_values,
size_t* p_size) {
auto& values = reinterpret_cast<ParameterRange*>(range)->values;
*p_values = values.data();
*p_size = values.size();
}

int faiss_ParameterSpace_new(FaissParameterSpace** space) {
try {
auto new_space = new ParameterSpace();
*space = reinterpret_cast<FaissParameterSpace*>(new_space);
}
CATCH_AND_HANDLE
}

DEFINE_DESTRUCTOR(ParameterSpace)

size_t faiss_ParameterSpace_n_combinations(const FaissParameterSpace* space) {
return reinterpret_cast<const ParameterSpace*>(space)->n_combinations();
}

int faiss_ParameterSpace_combination_name(
const FaissParameterSpace* space,
size_t cno,
char* char_buffer,
size_t size) {
try {
auto rep = reinterpret_cast<const ParameterSpace*>(space)
->combination_name(cno);
strncpy(char_buffer, rep.c_str(), size);
}
CATCH_AND_HANDLE
}

int faiss_ParameterSpace_set_index_parameters(
const FaissParameterSpace* space,
FaissIndex* cindex,
const char* param_string) {
try {
auto index = reinterpret_cast<Index*>(cindex);
reinterpret_cast<const ParameterSpace*>(space)->set_index_parameters(
index, param_string);
}
CATCH_AND_HANDLE
}

/// set a combination of parameters on an index
int faiss_ParameterSpace_set_index_parameters_cno(
const FaissParameterSpace* space,
FaissIndex* cindex,
size_t cno) {
try {
auto index = reinterpret_cast<Index*>(cindex);
reinterpret_cast<const ParameterSpace*>(space)->set_index_parameters(
index, cno);
}
CATCH_AND_HANDLE
}

int faiss_ParameterSpace_set_index_parameter(
const FaissParameterSpace* space,
FaissIndex* cindex,
const char* name,
double value) {
try {
auto index = reinterpret_cast<Index*>(cindex);
reinterpret_cast<const ParameterSpace*>(space)->set_index_parameter(
index, name, value);
}
CATCH_AND_HANDLE
}

void faiss_ParameterSpace_display(const FaissParameterSpace* space) {
reinterpret_cast<const ParameterSpace*>(space)->display();
}

int faiss_ParameterSpace_add_range(
FaissParameterSpace* space,
const char* name,
FaissParameterRange** p_range) {
try {
ParameterRange& range =
reinterpret_cast<ParameterSpace*>(space)->add_range(name);
if (p_range) {
*p_range = reinterpret_cast<FaissParameterRange*>(&range);
}
}
CATCH_AND_HANDLE
}
83 changes: 83 additions & 0 deletions c_api/AutoTune_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c -*-

#ifndef FAISS_AUTO_TUNE_C_H
#define FAISS_AUTO_TUNE_C_H

#include "Index_c.h"
#include "faiss_c.h"

#ifdef __cplusplus
extern "C" {
#endif

/// possible values of a parameter, sorted from least to most expensive/accurate
FAISS_DECLARE_CLASS(ParameterRange)

FAISS_DECLARE_GETTER(ParameterRange, const char*, name)

/// Getter for the values in the range. The output values are invalidated
/// upon any other modification of the range.
void faiss_ParameterRange_values(FaissParameterRange*, double**, size_t*);

/** Uses a-priori knowledge on the Faiss indexes to extract tunable parameters.
*/
FAISS_DECLARE_CLASS(ParameterSpace)

FAISS_DECLARE_DESTRUCTOR(ParameterSpace)

/// Parameter space default constructor
int faiss_ParameterSpace_new(FaissParameterSpace** space);

/// nb of combinations, = product of values sizes
size_t faiss_ParameterSpace_n_combinations(const FaissParameterSpace*);

/// get string representation of the combination
/// by writing it to the given character buffer.
/// A buffer size of 1000 ensures that the full name is collected.
int faiss_ParameterSpace_combination_name(
const FaissParameterSpace*,
size_t,
char*,
size_t);

/// set a combination of parameters described by a string
int faiss_ParameterSpace_set_index_parameters(
const FaissParameterSpace*,
FaissIndex*,
const char*);

/// set a combination of parameters on an index
int faiss_ParameterSpace_set_index_parameters_cno(
const FaissParameterSpace*,
FaissIndex*,
size_t);

/// set one of the parameters
int faiss_ParameterSpace_set_index_parameter(
const FaissParameterSpace*,
FaissIndex*,
const char*,
double);

/// print a description on stdout
void faiss_ParameterSpace_display(const FaissParameterSpace*);

/// add a new parameter (or return it if it exists)
int faiss_ParameterSpace_add_range(
FaissParameterSpace*,
const char*,
FaissParameterRange**);

#ifdef __cplusplus
}
#endif

#endif
59 changes: 59 additions & 0 deletions c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

cmake_minimum_required(VERSION 3.17 FATAL_ERROR)

project(faiss_c_library LANGUAGES C CXX)

set(CMAKE_C_STANDARD 11)

set(FAISS_C_SRC
AutoTune_c.cpp
Clustering_c.cpp
IndexFlat_c.cpp
IndexIVFFlat_c.cpp
IndexIVF_c.cpp
IndexLSH_c.cpp
IndexPreTransform_c.cpp
VectorTransform_c.cpp
IndexShards_c.cpp
IndexReplicas_c.cpp
Index_c.cpp
IndexBinary_c.cpp
IndexScalarQuantizer_c.cpp
MetaIndexes_c.cpp
clone_index_c.cpp
error_impl.cpp
index_factory_c.cpp
index_io_c.cpp
impl/AuxIndexStructures_c.cpp
utils/distances_c.cpp
)
add_library(faiss_c ${FAISS_C_SRC})
target_link_libraries(faiss_c PRIVATE faiss)

function(faiss_install_headers headers p)
foreach(h ${headers})
get_filename_component(f ${h} DIRECTORY)
install(FILES ${h}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/faiss/${p}/${f}
)
endforeach()
endfunction()

file(GLOB FAISS_C_API_HEADERS
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"*.h"
"impl/*.h"
"utils/*.h")

faiss_install_headers("${FAISS_C_API_HEADERS}" c_api)

add_executable(example_c EXCLUDE_FROM_ALL example_c.c)
target_link_libraries(example_c PRIVATE faiss_c)

if(FAISS_ENABLE_GPU)
add_subdirectory(gpu)
endif()
Loading

0 comments on commit 445b1ca

Please sign in to comment.