Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved concurrency for multi-threaded indexing #236

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions include/nanoflann.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,28 +1226,13 @@ class KDTreeBaseClass

node->node_type.sub.divfeat = cutfeat;

std::future<NodePtr> left_future, right_future;

BoundingBox left_bbox(bbox);
left_bbox[cutfeat].high = cutval;
if (++thread_count < n_thread_build_)
{
left_future = std::async(
std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
this, std::ref(obj), left, left + idx, std::ref(left_bbox),
std::ref(thread_count), std::ref(mutex));
}
else
{
--thread_count;
node->child1 = this->divideTreeConcurrent(
obj, left, left + idx, left_bbox, thread_count, mutex);
}
std::future<NodePtr> right_future;

BoundingBox right_bbox(bbox);
right_bbox[cutfeat].low = cutval;
if (++thread_count < n_thread_build_)
{
// Concurrent right sub-tree
right_future = std::async(
std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
this, std::ref(obj), left + idx, right,
Expand All @@ -1257,20 +1242,24 @@ class KDTreeBaseClass
else
{
--thread_count;
node->child2 = this->divideTreeConcurrent(
obj, left + idx, right, right_bbox, thread_count, mutex);
}

if (left_future.valid())
{
node->child1 = left_future.get();
--thread_count;
}
BoundingBox left_bbox(bbox);
left_bbox[cutfeat].high = cutval;
node->child1 = this->divideTreeConcurrent(
obj, left, left + idx, left_bbox, thread_count, mutex);

if (right_future.valid())
{
// Block and wait for concurrent right sub-tree
node->child2 = right_future.get();
--thread_count;
}
else
{
node->child2 = this->divideTreeConcurrent(
obj, left + idx, right, right_bbox, thread_count, mutex);
}

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