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
| import numpy as np import os os.environ["CUDA_VISIBLE_DEVICES"] = "4,5,6" d = 64 nb = 100000 nq = 10000 np.random.seed(1234) xb = np.random.random((nb, d)).astype('float32') xb[:, 0] += np.arange(nb) / 1000. xq = np.random.random((nq, d)).astype('float32') xq[:, 0] += np.arange(nq) / 1000. ids = np.random.randint(0, 5, 100000) ids = ids.astype('int') import faiss index2 = faiss.IndexFlatL2(d)
gpu_index_flat = faiss.index_cpu_to_all_gpus(index2)
gpu_index_flat.add(xb)
print(gpu_index_flat.ntotal) import time while True: start_time = time.time() k = 4 D, I = gpu_index_flat.search(xb[:5], k) end_time = time.time() print(f"costTime: {end_time - start_time}")
|