Skip to content

Commit

Permalink
Merge pull request #36 from tataratat/ft-update-nanoflann
Browse files Browse the repository at this point in the history
update nanoflann
  • Loading branch information
j042 authored May 17, 2024
2 parents 21af110 + d163242 commit c46edf1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 46 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ jobs:
with:
submodules: recursive
- name: build wheels
uses: pypa/cibuildwheel@v2.16.2
uses: pypa/cibuildwheel@v2.18
env:
CIBW_ARCHS: ${{ matrix.arch }}
CIBW_BUILD: ${{ matrix.cw_build }}
CIBW_TEST_SKIP: "*-macosx_arm64"
CIBW_TEST_SKIP: "*-macosx_x86_64"

- uses: actions/upload-artifact@v3
with:
Expand All @@ -45,7 +45,7 @@ jobs:
with:
submodules: recursive
- name: build wheels
uses: pypa/cibuildwheel@v2.16.2
uses: pypa/cibuildwheel@v2.18
env:
CIBW_ARCHS: ${{ matrix.arch }}
CIBW_BUILD: ${{ matrix.cw_build }}
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
platforms: arm64, ppc64le

- name: build wheels
uses: pypa/cibuildwheel@v2.16.2
uses: pypa/cibuildwheel@v2.18
env:
CIBW_ARCHS: ${{ matrix.arch }}
CIBW_BUILD: ${{ matrix.cw_build }}
Expand All @@ -104,7 +104,7 @@ jobs:
with:
submodules: recursive
- name: build wheels
uses: pypa/cibuildwheel@v2.16.2
uses: pypa/cibuildwheel@v2.18
env:
CIBW_ARCHS: ${{ matrix.arch }}

Expand Down
2 changes: 1 addition & 1 deletion napf/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.0.7"
version = "0.0.8"
2 changes: 1 addition & 1 deletion napf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def query(self, queries, nthread=None):
Returns
--------
ids_and_distances: tuple
distances_and_ids: tuple
((m, 1) np.ndarray - double dists,)
(m, 1) np.ndarray - uint ids)
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test-command = "python {project}/tests/test_init_and_query.py"

[tool.cibuildwheel.macos]
archs = ["x86_64", "arm64"]
test-skip = ["*arm64"]
test-skip = ["*x86_64"]

[tool.cibuildwheel.windows]
skip = "pp*"
68 changes: 31 additions & 37 deletions third_party/nanoflann.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright 2008-2009 Marius Muja ([email protected]). All rights reserved.
* Copyright 2008-2009 David G. Lowe ([email protected]). All rights reserved.
* Copyright 2011-2023 Jose Luis Blanco ([email protected]).
* Copyright 2011-2024 Jose Luis Blanco ([email protected]).
* All rights reserved.
*
* THE BSD LICENSE
Expand Down Expand Up @@ -49,8 +49,8 @@
#include <atomic>
#include <cassert>
#include <cmath> // for abs()
#include <cstdlib> // for abs()
#include <cstdint>
#include <cstdlib> // for abs()
#include <functional> // std::reference_wrapper
#include <future>
#include <istream>
Expand All @@ -61,7 +61,7 @@
#include <vector>

/** Library version: 0xMmP (M=Major,m=minor,P=patch) */
#define NANOFLANN_VERSION 0x151
#define NANOFLANN_VERSION 0x155

// Avoid conflicting declaration of min/max macros in Windows headers
#if !defined(NOMINMAX) && \
Expand Down Expand Up @@ -273,8 +273,7 @@ class RKNNResultSet
indices = indices_;
dists = dists_;
count = 0;
if (capacity)
dists[capacity - 1] = maximumSearchDistanceSquared;
if (capacity) dists[capacity - 1] = maximumSearchDistanceSquared;
}

CountType size() const { return count; }
Expand Down Expand Up @@ -832,7 +831,7 @@ struct SearchParameters
*/
class PooledAllocator
{
static constexpr size_t WORDSIZE = 16; //WORDSIZE must >= 8
static constexpr size_t WORDSIZE = 16; // WORDSIZE must >= 8
static constexpr size_t BLOCKSIZE = 8192;

/* We maintain memory alignment to word boundaries by requiring that all
Expand Down Expand Up @@ -903,9 +902,7 @@ class PooledAllocator

/* Allocate new storage. */
const Size blocksize =
size > BLOCKSIZE
? size + WORDSIZE
: BLOCKSIZE + WORDSIZE;
size > BLOCKSIZE ? size + WORDSIZE : BLOCKSIZE + WORDSIZE;

// use the standard C malloc to allocate memory
void* m = ::malloc(blocksize);
Expand All @@ -920,7 +917,7 @@ class PooledAllocator
base_ = m;

remaining_ = blocksize - WORDSIZE;
loc_ = static_cast<char*>(m) + WORDSIZE;
loc_ = static_cast<char*>(m) + WORDSIZE;
}
void* rloc = loc_;
loc_ = static_cast<char*>(loc_) + size;
Expand Down Expand Up @@ -1229,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 @@ -1260,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 Expand Up @@ -1302,7 +1288,7 @@ class KDTreeBaseClass
}
ElementType max_spread = -1;
cutfeat = 0;
ElementType min_elem = 0, max_elem = 0;
ElementType min_elem = 0, max_elem = 0;
for (Dimension i = 0; i < dims; ++i)
{
ElementType span = bbox[i].high - bbox[i].low;
Expand All @@ -1315,8 +1301,8 @@ class KDTreeBaseClass
{
cutfeat = i;
max_spread = spread;
min_elem = min_elem_;
max_elem = max_elem_;
min_elem = min_elem_;
max_elem = max_elem_;
}
}
}
Expand Down Expand Up @@ -1641,10 +1627,14 @@ class KDTreeSingleIndexAdaptor
}
else
{
#ifndef NANOFLANN_NO_THREADS
std::atomic<unsigned int> thread_count(0u);
std::mutex mutex;
Base::root_node_ = this->divideTreeConcurrent(
*this, 0, Base::size_, Base::root_bbox_, thread_count, mutex);
#else /* NANOFLANN_NO_THREADS */
throw std::runtime_error("Multithreading is disabled");
#endif /* NANOFLANN_NO_THREADS */
}
}

Expand Down Expand Up @@ -2104,10 +2094,14 @@ class KDTreeSingleIndexDynamicAdaptor_
}
else
{
#ifndef NANOFLANN_NO_THREADS
std::atomic<unsigned int> thread_count(0u);
std::mutex mutex;
Base::root_node_ = this->divideTreeConcurrent(
*this, 0, Base::size_, Base::root_bbox_, thread_count, mutex);
#else /* NANOFLANN_NO_THREADS */
throw std::runtime_error("Multithreading is disabled");
#endif /* NANOFLANN_NO_THREADS */
}
}

Expand Down

0 comments on commit c46edf1

Please sign in to comment.