[关闭]
@CrazyHenry 2018-05-07T20:11:53.000000Z 字数 1259 阅读 1177

Running on GPUs

hhhhfaiss

接口

  1. faiss::gpu::StandardGpuResources res; // use a single GPU,开启GPU资源
  2. faiss::gpu::GpuIndexFlatL2 gpu_index_flat(&res, d);//GPU index对象
  3. gpu_index_flat.add(nb, xb); // add vectors to the index
  4. gpu_index_flat.search(nq, xq, k, D, I);

多重GPU

  1. int ngpus = faiss::gpu::getNumDevices();
  2. printf("Number of GPUs: %d\n", ngpus);
  3. std::vector<faiss::gpu::GpuResources*> res;
  4. std::vector<int> devs;
  5. for(int i = 0; i < ngpus; i++) {
  6. res.push_back(new faiss::gpu::StandardGpuResources);
  7. devs.push_back(i);
  8. }
  9. faiss::IndexFlatL2 cpu_index(d);
  10. faiss::Index *gpu_index =
  11. faiss::gpu::index_cpu_to_gpu_multiple(
  12. res,
  13. devs,
  14. &cpu_index
  15. );
  16. printf("is_trained = %s\n", gpu_index->is_trained ? "true" : "false");
  17. gpu_index->add(nb, xb); // vectors to the index
  18. printf("ntotal = %ld\n", gpu_index->ntotal);
  19. int k = 4;
  20. { // search xq
  21. long *I = new long[k * nq];
  22. float *D = new float[k * nq];
  23. gpu_index->search(nq, xq, k, D, I);
  24. // print results
  25. printf("I (5 first results)=\n");
  26. for(int i = 0; i < 5; i++) {
  27. for(int j = 0; j < k; j++)
  28. printf("%5ld ", I[i * k + j]);
  29. printf("\n");
  30. }
  31. printf("I (5 last results)=\n");
  32. for(int i = nq - 5; i < nq; i++) {
  33. for(int j = 0; j < k; j++)
  34. printf("%5ld ", I[i * k + j]);
  35. printf("\n");
  36. }
  37. delete [] I;
  38. delete [] D;
  39. }
  40. delete gpu_index;
  41. for(int i = 0; i < ngpus; i++) {
  42. delete res[i];
  43. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注