code: def compute_populated_index(preproc):
      """Add elements to a sharded index. Return the index and if available
      a sharded gpu_index that contains the same data."""
  
      indexall = prepare_trained_index(preproc)
  
      co = faiss.GpuMultipleClonerOptions()
      co.useFloat16 = use_float16
      co.useFloat16CoarseQuantizer = False
      co.usePrecomputed = use_precomputed_tables
      co.indicesOptions = faiss.INDICES_CPU
      co.verbose = True
      co.reserveVecs = max_add if max_add > 0 else xb.shape[0]
      co.shard = True
      assert co.shard_type in (0, 1, 2)
      vres, vdev = make_vres_vdev()
      gpu_index = faiss.index_cpu_to_gpu_multiple(
          vres, vdev, indexall, co
      )
  
      print("add...")
      t0 = time.time()
      nb = xb.shape[0]
      for i0, xs in dataset_iterator(xb, preproc, add_batch_size):
          i1 = i0 + xs.shape[0]
          gpu_index.add_with_ids(xs, np.arange(i0, i1))
          if max_add > 0 and gpu_index.ntotal > max_add:
              print("Flush indexes to CPU")
              for i in range(ngpu):
                  index_src_gpu = faiss.downcast_index(gpu_index.at(i))
                  index_src = faiss.index_gpu_to_cpu(index_src_gpu)
                  print("  index %d size %d" % (i, index_src.ntotal))
                  index_src.copy_subset_to(indexall, 0, 0, nb)
                  index_src_gpu.reset()
                  index_src_gpu.reserveMemory(max_add)
              gpu_index.sync_with_shard_indexes()
  
          print(
              "\r%d/%d (%.3f s)  " % (i0, nb, time.time() - t0), end=" "
          )
          sys.stdout.flush()
      print("Add time: %.3f s" % (time.time() - t0))
  
      print("Aggregate indexes to CPU")
      t0 = time.time()
  
      if hasattr(gpu_index, "at"):
          # it is a sharded index
          for i in range(ngpu):
              index_src = faiss.index_gpu_to_cpu(gpu_index.at(i))
              print("  index %d size %d" % (i, index_src.ntotal))
              index_src.copy_subset_to(indexall, 0, 0, nb)
      else:
          # simple index
          index_src = faiss.index_gpu_to_cpu(gpu_index)
          index_src.copy_subset_to(indexall, 0, 0, nb)
  
      print("  done in %.3f s" % (time.time() - t0))
  
      if max_add > 0:
          # it does not contain all the vectors
          gpu_index = None
  
      return gpu_index, indexall
  
imports: from __future__ import print_function
  import numpy
  import time
  import os
  import sys
  import faiss
  import re
  from multiprocessing.pool import ThreadPool
  from datasets import ivecs_read
label_desc: Use image node_id 15 for calling a global function with example usage: compute_populated_index(preproc) and returns: gpu_index,  indexall