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