2018-02-26 18:23:17 +08:00
|
|
|
/**
|
2019-06-12 21:46:08 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-02-26 18:23:17 +08:00
|
|
|
*
|
2019-06-12 21:46:08 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2018-02-26 18:23:17 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
// -*- c -*-
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "error_c.h"
|
2020-07-27 23:26:17 +08:00
|
|
|
#include "index_factory_c.h"
|
2018-05-02 19:39:59 +08:00
|
|
|
#include "index_io_c.h"
|
2018-02-26 18:23:17 +08:00
|
|
|
#include "Index_c.h"
|
|
|
|
#include "IndexFlat_c.h"
|
|
|
|
#include "AutoTune_c.h"
|
2019-11-19 18:36:44 +08:00
|
|
|
#include "clone_index_c.h"
|
2018-02-26 18:23:17 +08:00
|
|
|
|
|
|
|
#define FAISS_TRY(C) \
|
|
|
|
{ \
|
|
|
|
if (C) { \
|
|
|
|
fprintf(stderr, "%s", faiss_get_last_error()); \
|
|
|
|
exit(-1); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:58:23 +08:00
|
|
|
double drand() {
|
2018-02-26 18:23:17 +08:00
|
|
|
return (double)rand() / (double)RAND_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
time_t seed = time(NULL);
|
|
|
|
srand(seed);
|
|
|
|
printf("Generating some data...\n");
|
|
|
|
int d = 128; // dimension
|
|
|
|
int nb = 100000; // database size
|
|
|
|
int nq = 10000; // nb of queries
|
|
|
|
float *xb = malloc(d * nb * sizeof(float));
|
|
|
|
float *xq = malloc(d * nq * sizeof(float));
|
|
|
|
|
|
|
|
for(int i = 0; i < nb; i++) {
|
|
|
|
for(int j = 0; j < d; j++) xb[d * i + j] = drand();
|
|
|
|
xb[d * i] += i / 1000.;
|
|
|
|
}
|
|
|
|
for(int i = 0; i < nq; i++) {
|
|
|
|
for(int j = 0; j < d; j++) xq[d * i + j] = drand();
|
|
|
|
xq[d * i] += i / 1000.;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Building an index...\n");
|
|
|
|
|
|
|
|
FaissIndex* index = NULL;
|
|
|
|
FAISS_TRY(faiss_index_factory(&index, d, "Flat", METRIC_L2)); // use factory to create index
|
|
|
|
printf("is_trained = %s\n", faiss_Index_is_trained(index) ? "true" : "false");
|
|
|
|
FAISS_TRY(faiss_Index_add(index, nb, xb)); // add vectors to the index
|
|
|
|
printf("ntotal = %ld\n", faiss_Index_ntotal(index));
|
|
|
|
|
|
|
|
printf("Searching...\n");
|
|
|
|
int k = 5;
|
|
|
|
|
|
|
|
{ // sanity check: search 5 first vectors of xb
|
2019-06-24 19:29:38 +08:00
|
|
|
idx_t *I = malloc(k * 5 * sizeof(idx_t));
|
2018-02-26 18:23:17 +08:00
|
|
|
float *D = malloc(k * 5 * sizeof(float));
|
|
|
|
FAISS_TRY(faiss_Index_search(index, 5, xb, k, D, I));
|
|
|
|
printf("I=\n");
|
|
|
|
for(int i = 0; i < 5; i++) {
|
|
|
|
for(int j = 0; j < k; j++) printf("%5ld (d=%2.3f) ", I[i * k + j], D[i * k + j]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
free(I);
|
|
|
|
free(D);
|
|
|
|
}
|
|
|
|
{ // search xq
|
2019-06-24 19:29:38 +08:00
|
|
|
idx_t *I = malloc(k * nq * sizeof(idx_t));
|
2018-02-26 18:23:17 +08:00
|
|
|
float *D = malloc(k * nq * sizeof(float));
|
|
|
|
FAISS_TRY(faiss_Index_search(index, 5, xb, k, D, I));
|
|
|
|
printf("I=\n");
|
|
|
|
for(int i = 0; i < 5; i++) {
|
|
|
|
for(int j = 0; j < k; j++) printf("%5ld (d=%2.3f) ", I[i * k + j], D[i * k + j]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
free(I);
|
|
|
|
free(D);
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:39:59 +08:00
|
|
|
printf("Saving index to disk...\n");
|
|
|
|
FAISS_TRY(faiss_write_index_fname(index, "example.index"));
|
|
|
|
|
2018-02-26 18:23:17 +08:00
|
|
|
printf("Freeing index...\n");
|
|
|
|
faiss_Index_free(index);
|
|
|
|
printf("Done.\n");
|
|
|
|
|
|
|
|
return 0;
|
2018-06-04 17:58:23 +08:00
|
|
|
}
|