1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#include "Clustering_c.h"
#include "Clustering.h"
#include "Index.h"
#include <vector>
#include "macros_impl.h"
extern "C" {
using faiss::Clustering;
using faiss::ClusteringParameters;
using faiss::Index;
DEFINE_GETTER(Clustering, int, niter)
DEFINE_GETTER(Clustering, int, nredo)
DEFINE_GETTER(Clustering, int, verbose)
DEFINE_GETTER(Clustering, int, spherical)
DEFINE_GETTER(Clustering, int, update_index)
DEFINE_GETTER(Clustering, int, frozen_centroids)
DEFINE_GETTER(Clustering, int, min_points_per_centroid)
DEFINE_GETTER(Clustering, int, max_points_per_centroid)
DEFINE_GETTER(Clustering, int, seed)
/// getter for d
DEFINE_GETTER(Clustering, size_t, d)
/// getter for k
DEFINE_GETTER(Clustering, size_t, k)
void faiss_ClusteringParameters_init(FaissClusteringParameters* params) {
ClusteringParameters d;
params->frozen_centroids = d.frozen_centroids;
params->max_points_per_centroid = d.max_points_per_centroid;
params->min_points_per_centroid = d.min_points_per_centroid;
params->niter = d.niter;
params->nredo = d.nredo;
params->seed = d.seed;
params->spherical = d.spherical;
params->update_index = d.update_index;
params->verbose = d.verbose;
}
// This conversion is required because the two types are not memory-compatible
inline ClusteringParameters from_faiss_c(const FaissClusteringParameters* params) {
ClusteringParameters o;
o.frozen_centroids = params->frozen_centroids;
o.max_points_per_centroid = params->max_points_per_centroid;
o.min_points_per_centroid = params->min_points_per_centroid;
o.niter = params->niter;
o.nredo = params->nredo;
o.seed = params->seed;
o.spherical = params->spherical;
o.update_index = params->update_index;
o.verbose = params->verbose;
return o;
}
/// getter for centroids (size = k * d)
void faiss_Clustering_centroids(
FaissClustering* clustering, float** centroids, size_t* size) {
std::vector<float>& v = reinterpret_cast<Clustering*>(clustering)->centroids;
if (centroids) {
*centroids = v.data();
}
if (size) {
*size = v.size();
}
}
/// getter for objective values (sum of distances reported by index)
/// over iterations
void faiss_Clustering_obj(
FaissClustering* clustering, float** obj, size_t* size) {
std::vector<float>& v = reinterpret_cast<Clustering*>(clustering)->obj;
if (obj) {
*obj = v.data();
}
if (size) {
*size = v.size();
}
}
/// the only mandatory parameters are k and d
int faiss_Clustering_new(FaissClustering** p_clustering, int d, int k) {
try {
Clustering* c = new Clustering(d, k);
*p_clustering = reinterpret_cast<FaissClustering*>(c);
return 0;
} CATCH_AND_HANDLE
}
int faiss_Clustering_new_with_params(
FaissClustering** p_clustering, int d, int k, const FaissClusteringParameters* cp) {
try {
Clustering* c = new Clustering(d, k, from_faiss_c(cp));
*p_clustering = reinterpret_cast<FaissClustering*>(c);
return 0;
} CATCH_AND_HANDLE
}
/// Index is used during the assignment stage
int faiss_Clustering_train(
FaissClustering* clustering, idx_t n, const float* x, FaissIndex* index) {
try {
reinterpret_cast<Clustering*>(clustering)->train(
n, x, *reinterpret_cast<Index*>(index));
return 0;
} CATCH_AND_HANDLE
}
void faiss_Clustering_free(FaissClustering* clustering) {
delete reinterpret_cast<Clustering*>(clustering);
}
int faiss_kmeans_clustering (size_t d, size_t n, size_t k,
const float *x,
float *centroids,
float *q_error) {
try {
float out = faiss::kmeans_clustering(d, n, k, x, centroids);
if (q_error) {
*q_error = out;
}
return 0;
} CATCH_AND_HANDLE
}
}