@@ -68,6 +68,25 @@ void IndexService::allocIndex(faiss::Index * index, size_t dim, size_t numVector
68
68
}
69
69
}
70
70
71
+ jlong IndexService::initAndAllocateIndex (std::unique_ptr<faiss::Index> &index, size_t threadCount, size_t dim, size_t numVectors) {
72
+ // Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
73
+ if (threadCount != 0 ) {
74
+ omp_set_num_threads (threadCount);
75
+ }
76
+
77
+ std::unique_ptr<faiss::IndexIDMap> idMap (faissMethods->indexIdMap (index .get ()));
78
+ // Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
79
+ idMap->own_fields = true ;
80
+
81
+ // TODO: allocIndex for IVF
82
+ allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
83
+
84
+ // Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
85
+ // in insert and write operations
86
+ index .release ();
87
+ return reinterpret_cast <jlong>(idMap.release ());
88
+ }
89
+
71
90
jlong IndexService::initIndex (
72
91
knn_jni::JNIUtilInterface * jniUtil,
73
92
JNIEnv * env,
@@ -81,11 +100,6 @@ jlong IndexService::initIndex(
81
100
// Create index using Faiss factory method
82
101
std::unique_ptr<faiss::Index> index (faissMethods->indexFactory (dim, indexDescription.c_str (), metric));
83
102
84
- // Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
85
- if (threadCount != 0 ) {
86
- omp_set_num_threads (threadCount);
87
- }
88
-
89
103
// Add extra parameters that cant be configured with the index factory
90
104
SetExtraParameters<faiss::Index, faiss::IndexIVF, faiss::IndexHNSW>(jniUtil, env, parameters, index .get ());
91
105
@@ -94,16 +108,7 @@ jlong IndexService::initIndex(
94
108
throw std::runtime_error (" Index is not trained" );
95
109
}
96
110
97
- std::unique_ptr<faiss::IndexIDMap> idMap (faissMethods->indexIdMap (index .get ()));
98
- // Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
99
- idMap->own_fields = true ;
100
-
101
- allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
102
-
103
- // Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
104
- // in insert and write operations
105
- index .release ();
106
- return reinterpret_cast <jlong>(idMap.release ());
111
+ return initAndAllocateIndex (index , threadCount, dim, numVectors);
107
112
}
108
113
109
114
void IndexService::insertToIndex (
@@ -178,16 +183,31 @@ jlong IndexService::initIndexFromTemplate(
178
183
std::unique_ptr<faiss::Index> index ;
179
184
index .reset (faiss::read_index (&vectorIoReader, 0 ));
180
185
186
+ return initAndAllocateIndex (index , threadCount, dim, numVectors);
187
+ }
188
+
189
+ BinaryIndexService::BinaryIndexService (std::unique_ptr<FaissMethods> _faissMethods)
190
+ : IndexService(std::move(_faissMethods)) {
191
+ }
192
+
193
+ void BinaryIndexService::allocIndex (faiss::Index * index, size_t dim, size_t numVectors) {
194
+ if (auto * indexBinaryHNSW = dynamic_cast <faiss::IndexBinaryHNSW *>(index )) {
195
+ auto * indexBinaryFlat = dynamic_cast <faiss::IndexBinaryFlat *>(indexBinaryHNSW->storage );
196
+ indexBinaryFlat->xb .reserve (dim * numVectors / 8 );
197
+ }
198
+ }
199
+
200
+ jlong BinaryIndexService::initAndAllocateIndex (std::unique_ptr<faiss::IndexBinary> &index, size_t threadCount, size_t dim, size_t numVectors) {
181
201
// Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
182
202
if (threadCount != 0 ) {
183
203
omp_set_num_threads (threadCount);
184
204
}
185
205
186
- std::unique_ptr<faiss::IndexIDMap > idMap (faissMethods->indexIdMap (index .get ()));
206
+ std::unique_ptr<faiss::IndexBinaryIDMap > idMap (faissMethods->indexBinaryIdMap (index .get ()));
187
207
// Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
188
208
idMap->own_fields = true ;
189
209
190
- // TODO: allocIndex
210
+ // TODO: allocIndex for IVF
191
211
allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
192
212
193
213
// Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
@@ -196,17 +216,6 @@ jlong IndexService::initIndexFromTemplate(
196
216
return reinterpret_cast <jlong>(idMap.release ());
197
217
}
198
218
199
- BinaryIndexService::BinaryIndexService (std::unique_ptr<FaissMethods> _faissMethods)
200
- : IndexService(std::move(_faissMethods)) {
201
- }
202
-
203
- void BinaryIndexService::allocIndex (faiss::Index * index, size_t dim, size_t numVectors) {
204
- if (auto * indexBinaryHNSW = dynamic_cast <faiss::IndexBinaryHNSW *>(index )) {
205
- auto * indexBinaryFlat = dynamic_cast <faiss::IndexBinaryFlat *>(indexBinaryHNSW->storage );
206
- indexBinaryFlat->xb .reserve (dim * numVectors / 8 );
207
- }
208
- }
209
-
210
219
jlong BinaryIndexService::initIndex (
211
220
knn_jni::JNIUtilInterface * jniUtil,
212
221
JNIEnv * env,
@@ -219,10 +228,6 @@ jlong BinaryIndexService::initIndex(
219
228
) {
220
229
// Create index using Faiss factory method
221
230
std::unique_ptr<faiss::IndexBinary> index (faissMethods->indexBinaryFactory (dim, indexDescription.c_str ()));
222
- // Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
223
- if (threadCount != 0 ) {
224
- omp_set_num_threads (threadCount);
225
- }
226
231
227
232
// Add extra parameters that cant be configured with the index factory
228
233
SetExtraParameters<faiss::IndexBinary, faiss::IndexBinaryIVF, faiss::IndexBinaryHNSW>(jniUtil, env, parameters, index .get ());
@@ -232,16 +237,7 @@ jlong BinaryIndexService::initIndex(
232
237
throw std::runtime_error (" Index is not trained" );
233
238
}
234
239
235
- std::unique_ptr<faiss::IndexBinaryIDMap> idMap (faissMethods->indexBinaryIdMap (index .get ()));
236
- // Makes sure the index is deleted when the destructor is called
237
- idMap->own_fields = true ;
238
-
239
- allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
240
-
241
- // Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
242
- // in insert and write operations
243
- index .release ();
244
- return reinterpret_cast <jlong>(idMap.release ());
240
+ return initAndAllocateIndex (index , threadCount, dim, numVectors);
245
241
}
246
242
247
243
void BinaryIndexService::insertToIndex (
@@ -319,16 +315,32 @@ jlong BinaryIndexService::initIndexFromTemplate(
319
315
std::unique_ptr<faiss::IndexBinary> index ;
320
316
index .reset (faiss::read_index_binary (&vectorIoReader, 0 ));
321
317
318
+ return initAndAllocateIndex (index , threadCount, dim, numVectors);
319
+ }
320
+
321
+ ByteIndexService::ByteIndexService (std::unique_ptr<FaissMethods> _faissMethods)
322
+ : IndexService(std::move(_faissMethods)) {
323
+ }
324
+
325
+ void ByteIndexService::allocIndex (faiss::Index * index, size_t dim, size_t numVectors) {
326
+ if (auto * indexHNSWSQ = dynamic_cast <faiss::IndexHNSWSQ *>(index )) {
327
+ if (auto * indexScalarQuantizer = dynamic_cast <faiss::IndexScalarQuantizer *>(indexHNSWSQ->storage )) {
328
+ indexScalarQuantizer->codes .reserve (indexScalarQuantizer->code_size * numVectors);
329
+ }
330
+ }
331
+ }
332
+
333
+ jlong ByteIndexService::initAndAllocateIndex (std::unique_ptr<faiss::Index> &index, size_t threadCount, size_t dim, size_t numVectors) {
322
334
// Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
323
335
if (threadCount != 0 ) {
324
336
omp_set_num_threads (threadCount);
325
337
}
326
338
327
- std::unique_ptr<faiss::IndexBinaryIDMap > idMap (faissMethods->indexBinaryIdMap (index .get ()));
339
+ std::unique_ptr<faiss::IndexIDMap > idMap (faissMethods->indexIdMap (index .get ()));
328
340
// Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
329
341
idMap->own_fields = true ;
330
342
331
- // TODO: allocIndex
343
+ // TODO: allocIndex for IVF
332
344
allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
333
345
334
346
// Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
@@ -337,18 +349,6 @@ jlong BinaryIndexService::initIndexFromTemplate(
337
349
return reinterpret_cast <jlong>(idMap.release ());
338
350
}
339
351
340
- ByteIndexService::ByteIndexService (std::unique_ptr<FaissMethods> _faissMethods)
341
- : IndexService(std::move(_faissMethods)) {
342
- }
343
-
344
- void ByteIndexService::allocIndex (faiss::Index * index, size_t dim, size_t numVectors) {
345
- if (auto * indexHNSWSQ = dynamic_cast <faiss::IndexHNSWSQ *>(index )) {
346
- if (auto * indexScalarQuantizer = dynamic_cast <faiss::IndexScalarQuantizer *>(indexHNSWSQ->storage )) {
347
- indexScalarQuantizer->codes .reserve (indexScalarQuantizer->code_size * numVectors);
348
- }
349
- }
350
- }
351
-
352
352
jlong ByteIndexService::initIndex (
353
353
knn_jni::JNIUtilInterface * jniUtil,
354
354
JNIEnv * env,
@@ -362,11 +362,6 @@ jlong ByteIndexService::initIndex(
362
362
// Create index using Faiss factory method
363
363
std::unique_ptr<faiss::Index> index (faissMethods->indexFactory (dim, indexDescription.c_str (), metric));
364
364
365
- // Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
366
- if (threadCount != 0 ) {
367
- omp_set_num_threads (threadCount);
368
- }
369
-
370
365
// Add extra parameters that cant be configured with the index factory
371
366
SetExtraParameters<faiss::Index, faiss::IndexIVF, faiss::IndexHNSW>(jniUtil, env, parameters, index .get ());
372
367
@@ -375,16 +370,7 @@ jlong ByteIndexService::initIndex(
375
370
throw std::runtime_error (" Index is not trained" );
376
371
}
377
372
378
- std::unique_ptr<faiss::IndexIDMap> idMap (faissMethods->indexIdMap (index .get ()));
379
- // Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
380
- idMap->own_fields = true ;
381
-
382
- allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
383
-
384
- // Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
385
- // in insert and write operations
386
- index .release ();
387
- return reinterpret_cast <jlong>(idMap.release ());
373
+ return initAndAllocateIndex (index , threadCount, dim, numVectors);
388
374
}
389
375
390
376
void ByteIndexService::insertToIndex (
@@ -477,22 +463,7 @@ jlong ByteIndexService::initIndexFromTemplate(
477
463
std::unique_ptr<faiss::Index> index ;
478
464
index .reset (faiss::read_index (&vectorIoReader, 0 ));
479
465
480
- // Set thread count if it is passed in as a parameter. Setting this variable will only impact the current thread
481
- if (threadCount != 0 ) {
482
- omp_set_num_threads (threadCount);
483
- }
484
-
485
- std::unique_ptr<faiss::IndexIDMap> idMap (faissMethods->indexIdMap (index .get ()));
486
- // Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
487
- idMap->own_fields = true ;
488
-
489
- // TODO: allocIndex
490
- allocIndex (dynamic_cast <faiss::Index *>(idMap->index ), dim, numVectors);
491
-
492
- // Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
493
- // in insert and write operations
494
- index .release ();
495
- return reinterpret_cast <jlong>(idMap.release ());
466
+ return initAndAllocateIndex (index , threadCount, dim, numVectors);
496
467
}
497
468
} // namespace faiss_wrapper
498
469
} // namesapce knn_jni
0 commit comments