Skip to content

Commit e512e13

Browse files
committed
Fixing error for empty results
1 parent a3f8397 commit e512e13

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

src/cpp/flann/algorithms/lsh_index.h

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -261,25 +261,33 @@ class LshIndex : public NNIndex<Distance>
261261

262262
int count = 0;
263263
if (params.use_heap==FLANN_True) {
264-
KNNUniqueResultSet<DistanceType> resultSet(knn);
265-
for (size_t i = 0; i < queries.rows; i++) {
266-
resultSet.clear();
267-
findNeighbors(resultSet, queries[i], params);
268-
size_t n = std::min(resultSet.size(), knn);
269-
resultSet.copy(indices[i], dists[i], n, params.sorted);
270-
indices_to_ids(indices[i], indices[i], n);
271-
count += n;
264+
#pragma omp parallel num_threads(params.cores)
265+
{
266+
KNNUniqueResultSet<DistanceType> resultSet(knn);
267+
#pragma omp for schedule(static) reduction(+:count)
268+
for (size_t i = 0; i < queries.rows; i++) {
269+
resultSet.clear();
270+
findNeighbors(resultSet, queries[i], params);
271+
size_t n = std::min(resultSet.size(), knn);
272+
resultSet.copy(indices[i], dists[i], n, params.sorted);
273+
indices_to_ids(indices[i], indices[i], n);
274+
count += n;
275+
}
272276
}
273277
}
274278
else {
275-
KNNResultSet<DistanceType> resultSet(knn);
276-
for (size_t i = 0; i < queries.rows; i++) {
277-
resultSet.clear();
278-
findNeighbors(resultSet, queries[i], params);
279-
size_t n = std::min(resultSet.size(), knn);
280-
resultSet.copy(indices[i], dists[i], n, params.sorted);
281-
indices_to_ids(indices[i], indices[i], n);
282-
count += n;
279+
#pragma omp parallel num_threads(params.cores)
280+
{
281+
KNNResultSet<DistanceType> resultSet(knn);
282+
#pragma omp for schedule(static) reduction(+:count)
283+
for (size_t i = 0; i < queries.rows; i++) {
284+
resultSet.clear();
285+
findNeighbors(resultSet, queries[i], params);
286+
size_t n = std::min(resultSet.size(), knn);
287+
resultSet.copy(indices[i], dists[i], n, params.sorted);
288+
indices_to_ids(indices[i], indices[i], n);
289+
count += n;
290+
}
283291
}
284292
}
285293

@@ -306,33 +314,41 @@ class LshIndex : public NNIndex<Distance>
306314

307315
int count = 0;
308316
if (params.use_heap==FLANN_True) {
309-
KNNUniqueResultSet<DistanceType> resultSet(knn);
310-
for (size_t i = 0; i < queries.rows; i++) {
311-
resultSet.clear();
312-
findNeighbors(resultSet, queries[i], params);
313-
size_t n = std::min(resultSet.size(), knn);
314-
indices[i].resize(n);
315-
dists[i].resize(n);
316-
if (n > 0) {
317-
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
317+
#pragma omp parallel num_threads(params.cores)
318+
{
319+
KNNUniqueResultSet<DistanceType> resultSet(knn);
320+
#pragma omp for schedule(static) reduction(+:count)
321+
for (size_t i = 0; i < queries.rows; i++) {
322+
resultSet.clear();
323+
findNeighbors(resultSet, queries[i], params);
324+
size_t n = std::min(resultSet.size(), knn);
325+
indices[i].resize(n);
326+
dists[i].resize(n);
327+
if (n > 0) {
328+
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
329+
indices_to_ids(&indices[i][0], &indices[i][0], n);
330+
}
331+
count += n;
318332
}
319-
indices_to_ids(&indices[i][0], &indices[i][0], n);
320-
count += n;
321333
}
322334
}
323335
else {
324-
KNNResultSet<DistanceType> resultSet(knn);
325-
for (size_t i = 0; i < queries.rows; i++) {
326-
resultSet.clear();
327-
findNeighbors(resultSet, queries[i], params);
328-
size_t n = std::min(resultSet.size(), knn);
329-
indices[i].resize(n);
330-
dists[i].resize(n);
331-
if (n > 0) {
332-
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
336+
#pragma omp parallel num_threads(params.cores)
337+
{
338+
KNNResultSet<DistanceType> resultSet(knn);
339+
#pragma omp for schedule(static) reduction(+:count)
340+
for (size_t i = 0; i < queries.rows; i++) {
341+
resultSet.clear();
342+
findNeighbors(resultSet, queries[i], params);
343+
size_t n = std::min(resultSet.size(), knn);
344+
indices[i].resize(n);
345+
dists[i].resize(n);
346+
if (n > 0) {
347+
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
348+
indices_to_ids(&indices[i][0], &indices[i][0], n);
349+
}
350+
count += n;
333351
}
334-
indices_to_ids(&indices[i][0], &indices[i][0], n);
335-
count += n;
336352
}
337353
}
338354

src/cpp/flann/algorithms/nn_index.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class NNIndex : public IndexBase
341341
}
342342
}
343343
return count;
344-
}
344+
}
345345

346346
/**
347347
*
@@ -410,8 +410,8 @@ class NNIndex : public IndexBase
410410
dists[i].resize(n);
411411
if (n>0) {
412412
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
413+
indices_to_ids(&indices[i][0], &indices[i][0], n);
413414
}
414-
indices_to_ids(&indices[i][0], &indices[i][0], n);
415415
count += n;
416416
}
417417
}
@@ -429,8 +429,8 @@ class NNIndex : public IndexBase
429429
dists[i].resize(n);
430430
if (n>0) {
431431
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
432+
indices_to_ids(&indices[i][0], &indices[i][0], n);
432433
}
433-
indices_to_ids(&indices[i][0], &indices[i][0], n);
434434
count += n;
435435
}
436436
}
@@ -623,8 +623,8 @@ class NNIndex : public IndexBase
623623
dists[i].resize(n);
624624
if (n > 0) {
625625
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
626+
indices_to_ids(&indices[i][0], &indices[i][0], n);
626627
}
627-
indices_to_ids(&indices[i][0], &indices[i][0], n);
628628
}
629629
}
630630
}
@@ -644,8 +644,8 @@ class NNIndex : public IndexBase
644644
dists[i].resize(n);
645645
if (n > 0) {
646646
resultSet.copy(&indices[i][0], &dists[i][0], n, params.sorted);
647+
indices_to_ids(&indices[i][0], &indices[i][0], n);
647648
}
648-
indices_to_ids(&indices[i][0], &indices[i][0], n);
649649
}
650650
}
651651
}

0 commit comments

Comments
 (0)