MongoCluster::ShardMap MongoCluster::getShardList() { ShardMap shardMap(shards().begin(), shards().end()); return std::move(shardMap); }
Index *clone_Index(const Index *index) override { long n = sub_cloners.size(); if (n == 1) return sub_cloners[0].clone_Index(index); if(dynamic_cast<const IndexFlat *>(index) || dynamic_cast<const faiss::IndexIVFFlat *>(index) || dynamic_cast<const faiss::IndexIVFPQ *>(index)) { if(!shard) { IndexProxy * res = new IndexProxy(); for(auto & sub_cloner: sub_cloners) { res->addIndex(sub_cloner.clone_Index(index)); } res->own_fields = true; return res; } else { auto index_ivfpq = dynamic_cast<const faiss::IndexIVFPQ *>(index); auto index_ivfflat = dynamic_cast<const faiss::IndexIVFFlat *>(index); FAISS_ASSERT (index_ivfpq || index_ivfflat || !"IndexShards implemented only for " "IndexIVFFlat or IndexIVFPQ"); std::vector<faiss::Index*> shards(n); for(long i = 0; i < n; i++) { // make a shallow copy long i0 = i * index->ntotal / n; long i1 = (i + 1) * index->ntotal / n; if(verbose) printf("IndexShards shard %ld indices %ld:%ld\n", i, i0, i1); if(reserveVecs) sub_cloners[i].reserveVecs = (reserveVecs + n - 1) / n; if (index_ivfpq) { faiss::IndexIVFPQ idx2( index_ivfpq->quantizer, index_ivfpq->d, index_ivfpq->nlist, index_ivfpq->code_size, index_ivfpq->pq.nbits); idx2.pq = index_ivfpq->pq; idx2.use_precomputed_table = 0; idx2.is_trained = index->is_trained; index_ivfpq->copy_subset_to(idx2, 0, i0, i1); shards[i] = sub_cloners[i].clone_Index(&idx2); } else if (index_ivfflat) { faiss::IndexIVFFlat idx2( index_ivfflat->quantizer, index->d, index_ivfflat->nlist, index_ivfflat->metric_type); index_ivfflat->copy_subset_to(idx2, 0, i0, i1); shards[i] = sub_cloners[i].clone_Index(&idx2); } } faiss::IndexShards *res = new faiss::IndexShards(index->d, true, false); for (int i = 0; i < n; i++) { res->add_shard(shards[i]); } res->own_fields = true; assert(index->ntotal == res->ntotal); return res; } } else if(auto miq = dynamic_cast<const MultiIndexQuantizer *>(index)) { if (verbose) { printf("cloning MultiIndexQuantizer: " "will be valid only for search k=1\n"); } const ProductQuantizer & pq = miq->pq; IndexSplitVectors *splitv = new IndexSplitVectors(pq.d, true); splitv->own_fields = true; for (int m = 0; m < pq.M; m++) { // which GPU(s) will be assigned to this sub-quantizer long i0 = m * n / pq.M; long i1 = pq.M <= n ? (m + 1) * n / pq.M : i0 + 1; std::vector<ToGpuCloner> sub_cloners_2; sub_cloners_2.insert( sub_cloners_2.begin(), sub_cloners.begin() + i0, sub_cloners.begin() + i1); ToGpuClonerMultiple cm(sub_cloners_2, *this); IndexFlatL2 idxc (pq.dsub); idxc.add (pq.ksub, pq.centroids.data() + m * pq.d * pq.ksub); Index *idx2 = cm.clone_Index(&idxc); splitv->add_sub_index(idx2); } return splitv; } else { return Cloner::clone_Index(index); } }