Faiss provides a pure C interface, which can subsequently be used either in pure C programs or to produce bindings for programming languages with Foreign Function Interface (FFI) support. Although this is not required for the Python interface, some other programming languages (e.g. Rust and Julia) do not have SWIG support.
The full contents of the pure C API are in the "c_api" folder. Please be sure to follow the instructions on building the main C++ library first. Then, enter the c_api directory and run
make
This builds the dynamic library "faiss_c", containing the full implementation of Faiss and the necessary wrappers for the C interface. It does not depend on libfaiss.a or the C++ standard library. It will also build an example program bin/example_c
.
The C API is composed of:
«name»_c.h
, where «name»
is the respective name from the C++ API. For example, the file Index_c.h file corresponds to the base Index
API. Functions are declared with the faiss_
prefix (e.g. faiss_IndexFlat_new
), whereas new types have the Faiss
prefix (e.g. FaissIndex
, FaissMetricType
, ...).The index factory is available via the faiss_index_factory
function in AutoTune_c.h
:
FaissIndex* index = NULL;
int c = faiss_index_factory(&index, 64, "Flat", METRIC_L2);
if (c) {
// operation failed
}
Most operations that you would find as member functions are available with the format faiss_«classname»_«member»
.
idx_t ntotal = faiss_Index_ntotal(index);
Since this is C, the index needs to be freed manually in the end:
faiss_Index_free(index);
Error handling is done by examining the error code returned by operations with recoverable errors.
The code identifies the type of exception that rose from the implementation. Fetching the
corresponding error message can be done by calling the function faiss_get_last_error()
from
error_c.h
. Getter functions and free
functions do not return an error code.
int c = faiss_Index_add(index, nb, xb);
if (c) {
printf("%s", faiss_get_last_error());
exit(-1);
}
An example is included, which is built automatically for the target all
. It can also be built separately:
make bin/example_c
For GPU support, a separate dynamic library in the "c_api/gpu" directory needs to be built.
make
The "gpufaiss_c" dynamic library contains the GPU and CPU implementations of Faiss, which means that it can be used in place of "faiss_c". The same library will dynamically link with the CUDA runtime and cuBLAS.
A standard GPU resurces object can be obtained by the name FaissStandardGpuResources
:
FaissStandardGpuResources* gpu_res = NULL;
int c = faiss_StandardGpuResources_new(&gpu_res);
if (c) {
printf("%s", faiss_get_last_error());
exit(-1);
}
Similarly to the C++ API, a CPU index can be converted to a GPU index:
FaissIndex* cpu_index = NULL;
int c = faiss_index_factory(&cpu_index, d, "Flat", METRIC_L2);
if (c) { /* ... */ }
FaissGpuIndex* gpu_index = NULL;
c = faiss_index_cpu_to_gpu(gpu_res, 0, cpu_index, &gpu_index);
if (c) { /* ... */ }
A more complete example is available by the name bin/example_gpu_c
.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )