Skip to content

Commit 0e43d8b

Browse files
authored
Merge pull request #236 from nigels-com/improved-concurrency
Improved concurrency for multi-threaded indexing
2 parents 418ba18 + 83fe805 commit 0e43d8b

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

include/nanoflann.hpp

+13-24
Original file line numberDiff line numberDiff line change
@@ -1226,28 +1226,13 @@ class KDTreeBaseClass
12261226

12271227
node->node_type.sub.divfeat = cutfeat;
12281228

1229-
std::future<NodePtr> left_future, right_future;
1230-
1231-
BoundingBox left_bbox(bbox);
1232-
left_bbox[cutfeat].high = cutval;
1233-
if (++thread_count < n_thread_build_)
1234-
{
1235-
left_future = std::async(
1236-
std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
1237-
this, std::ref(obj), left, left + idx, std::ref(left_bbox),
1238-
std::ref(thread_count), std::ref(mutex));
1239-
}
1240-
else
1241-
{
1242-
--thread_count;
1243-
node->child1 = this->divideTreeConcurrent(
1244-
obj, left, left + idx, left_bbox, thread_count, mutex);
1245-
}
1229+
std::future<NodePtr> right_future;
12461230

12471231
BoundingBox right_bbox(bbox);
12481232
right_bbox[cutfeat].low = cutval;
12491233
if (++thread_count < n_thread_build_)
12501234
{
1235+
// Concurrent right sub-tree
12511236
right_future = std::async(
12521237
std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
12531238
this, std::ref(obj), left + idx, right,
@@ -1257,20 +1242,24 @@ class KDTreeBaseClass
12571242
else
12581243
{
12591244
--thread_count;
1260-
node->child2 = this->divideTreeConcurrent(
1261-
obj, left + idx, right, right_bbox, thread_count, mutex);
12621245
}
12631246

1264-
if (left_future.valid())
1265-
{
1266-
node->child1 = left_future.get();
1267-
--thread_count;
1268-
}
1247+
BoundingBox left_bbox(bbox);
1248+
left_bbox[cutfeat].high = cutval;
1249+
node->child1 = this->divideTreeConcurrent(
1250+
obj, left, left + idx, left_bbox, thread_count, mutex);
1251+
12691252
if (right_future.valid())
12701253
{
1254+
// Block and wait for concurrent right sub-tree
12711255
node->child2 = right_future.get();
12721256
--thread_count;
12731257
}
1258+
else
1259+
{
1260+
node->child2 = this->divideTreeConcurrent(
1261+
obj, left + idx, right, right_bbox, thread_count, mutex);
1262+
}
12741263

12751264
node->node_type.sub.divlow = left_bbox[cutfeat].high;
12761265
node->node_type.sub.divhigh = right_bbox[cutfeat].low;

0 commit comments

Comments
 (0)