Unverified Commit 16aed09c authored by Lucas Hosseini's avatar Lucas Hosseini Committed by GitHub

Make tutorial examples use dynamic linkage. (#343)

* Add shared lib target for GPU version.

* Dynamically link tutorial examples.

* Add shared lib to clean target in GPU Makefile.

* Add clean target to tutorial examples Makefile.

* Minor modifications to tutorial GPU examples.
parent 97eba202
......@@ -85,6 +85,10 @@ CUOBJ= impl/BroadcastSum.o \
$(LIBNAME).a: $(CPPOBJ) $(CUOBJ)
ar r $@ $^
$(LIBNAME).$(SHAREDEXT): $(CPPOBJ) $(CUOBJ)
$(CC) -L$(CUDAROOT)/lib64 -lcublas -lcudart $(LDFLAGS) \
$(FAISSSHAREDFLAGS) -o $(LIBNAME).$(SHAREDEXT) $^
test/demo_ivfpq_indexing_gpu: test/demo_ivfpq_indexing_gpu.cpp \
$(LIBNAME).a ../libfaiss.a
......@@ -115,6 +119,7 @@ test/demo_ivfpq_indexing_gpu: test/demo_ivfpq_indexing_gpu.cpp \
clean:
rm -rf *.o impl/*.o utils/*.o test/*.o $(LIBNAME).a \
$(LIBNAME).$(SHAREDEXT) \
../python/*swigfaiss_gpu* ../*swigfaiss_gpu*
......
......@@ -36,8 +36,6 @@ int main() {
xq[d * i] += i / 1000.;
}
int k = 4;
faiss::gpu::StandardGpuResources res;
// Using a flat index
......@@ -48,6 +46,8 @@ int main() {
index_flat.add(nb, xb); // add vectors to the index
printf("ntotal = %ld\n", index_flat.ntotal);
int k = 4;
{ // search xq
long *I = new long[k * nq];
float *D = new float[k * nq];
......
......@@ -38,6 +38,8 @@ int main() {
int ngpus = faiss::gpu::getNumDevices();
printf("Number of GPUs: %d\n", ngpus);
std::vector<faiss::gpu::GpuResources*> res;
std::vector<int> devs;
for(int i = 0; i < ngpus; i++) {
......@@ -55,7 +57,7 @@ int main() {
);
printf("is_trained = %s\n", gpu_index->is_trained ? "true" : "false");
gpu_index->add(nb, xb); // vectors to the index
gpu_index->add(nb, xb); // add vectors to the index
printf("ntotal = %ld\n", gpu_index->ntotal);
int k = 4;
......
......@@ -2,25 +2,38 @@ MAKEFILE_INC=../../makefile.inc
-include $(MAKEFILE_INC)
NVCCLDFLAGS = -Xcompiler \"-Wl,-rpath=../../:../../gpu/\" \
-L../.. -L../../gpu -lfaiss -lgpufaiss
LDFLAGS = -L../.. -Wl,-rpath=../.. -lfaiss
all: cpu gpu
cpu: 1-Flat 2-IVFFlat 3-IVFPQ
gpu: 4-GPU 5-Multiple-GPUs
1-Flat: 1-Flat.cpp ../../libfaiss.a
$(CC) -o $@ $(CFLAGS) $^ -I../../../ $(LDFLAGS) $(BLASLDFLAGS)
1-Flat: 1-Flat.cpp ../../libfaiss.$(SHAREDEXT)
$(CC) -o $@ $(CFLAGS) $< -I../../../ $(LDFLAGS)
2-IVFFlat: 2-IVFFlat.cpp ../../libfaiss.$(SHAREDEXT)
$(CC) -o $@ $(CFLAGS) $< -I../../../ $(LDFLAGS)
3-IVFPQ: 3-IVFPQ.cpp ../../libfaiss.$(SHAREDEXT)
$(CC) -o $@ $(CFLAGS) $< -I../../../ $(LDFLAGS)
4-GPU: 4-GPU.cpp ../../libfaiss.$(SHAREDEXT) ../../gpu/libgpufaiss.$(SHAREDEXT)
$(NVCC) $(NVCCFLAGS) -o $@ $< $(NVCCLDFLAGS) -I../../../
2-IVFFlat: 2-IVFFlat.cpp ../../libfaiss.a
$(CC) -o $@ $(CFLAGS) $^ -I../../../ $(LDFLAGS) $(BLASLDFLAGS)
5-Multiple-GPUs: 5-Multiple-GPUs.cpp ../../libfaiss.$(SHAREDEXT) \
../../gpu/libgpufaiss.$(SHAREDEXT)
$(NVCC) $(NVCCFLAGS) -o $@ $< $(NVCCLDFLAGS) -I../../../
3-IVFPQ: 3-IVFPQ.cpp ../../libfaiss.a
$(CC) -o $@ $(CFLAGS) $^ -I../../../ $(LDFLAGS) $(BLASLDFLAGS)
../../libfaiss.$(SHAREDEXT):
cd ../../ && make libfaiss.$(SHAREDEXT)
4-GPU: 4-GPU.cpp ../../libfaiss.a ../../gpu/libgpufaiss.a
$(NVCC) $(NVCCFLAGS) -o $@ $^ -I../../../ -Xcompiler -fopenmp -lcublas \
$(BLASLDFLAGSNVCC)
../../gpu/libgpufaiss.$(SHAREDEXT):
cd ../../gpu/ && make libgpufaiss.$(SHAREDEXT)
5-Multiple-GPUs: 5-Multiple-GPUs.cpp ../../libfaiss.a ../../gpu/libgpufaiss.a
$(NVCC) $(NVCCFLAGS) -o $@ $^ -I../../../ -Xcompiler -fopenmp -lcublas \
$(BLASLDFLAGSNVCC)
clean:
rm -f 1-Flat 2-IVFFlat 3-IVFPQ 4-GPU 5-Multiple-GPUs
......@@ -17,8 +17,6 @@ xq[:, 0] += np.arange(nq) / 1000.
import faiss # make faiss available
print("number of GPUs:", faiss.get_num_gpus())
res = faiss.StandardGpuResources() # use a single GPU
## Using a flat index
......
......@@ -21,14 +21,16 @@ ngpus = faiss.get_num_gpus()
print("number of GPUs:", ngpus)
index = faiss.index_cpu_to_all_gpus( # build the index
faiss.IndexFlatL2(d)
cpu_index = faiss.IndexFlatL2(d)
gpu_index = faiss.index_cpu_to_all_gpus( # build the index
cpu_index
)
index.add(xb) # add vectors to the index
print(index.ntotal)
gpu_index.add(xb) # add vectors to the index
print(gpu_index.ntotal)
k = 4 # we want to see 4 nearest neighbors
D, I = index.search(xq, k) # actual search
D, I = gpu_index.search(xq, k) # actual search
print(I[:5]) # neighbors of the 5 first queries
print(I[-5:]) # neighbors of the 5 last queries
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment