Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
K
kdtree
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cython-plus
kdtree
Commits
5951d2e3
Commit
5951d2e3
authored
Nov 22, 2021
by
Julien Jerphanion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DEBUG] Query
parent
fefabcc0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
69 deletions
+75
-69
kdtree.pyx
kdtree.pyx
+75
-69
No files found.
kdtree.pyx
View file @
5951d2e3
...
...
@@ -520,7 +520,7 @@ cdef cypclass QueryActor:
__init__
(
self
,
D_t
*
query_points
_ptr
,
D_t
*
query_points
,
KDTree
tree
,
NeighborsHeaps
heaps
,
I_t
idx_worker
,
...
...
@@ -528,6 +528,68 @@ cdef cypclass QueryActor:
I_t
idx_end
,
):
printf
(
"QueryActor: (%d, %d, %d)
\
n
"
,
idx_worker
,
idx_start
,
idx_end
)
cdef
I_t
idx_pt
cdef
D_t
*
query_point
cdef
D_t
reduced_dist_LB
for
idx_pt
in
range
(
idx_start
,
idx_end
):
printf
(
"query %d
\
n
"
,
idx_pt
)
query_point
=
query_points
+
idx_pt
*
tree
.
_n_features
reduced_dist_LB
=
tree
.
min_rdist
(
0
,
query_point
)
self
.
_query_single_depthfirst
(
tree
,
0
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB
)
int
_query_single_depthfirst
(
self
,
KDTree
tree
,
I_t
idx_node
,
D_t
*
query_points
,
I_t
idx_pt
,
NeighborsHeaps
heaps
,
D_t
reduced_dist_LB
,
)
except
-
1
:
"""Recursive Single-tree k-neighbors query, depth-first approach"""
cdef
NodeData_t
node_info
=
tree
.
_node_data_ptr
[
idx_node
]
cdef
D_t
sq_dist
,
reduced_dist_LB_1
,
reduced_dist_LB_2
cdef
I_t
i
,
idx_left_node
,
idx_right_node
cdef
D_t
*
query_point
=
query_points
+
idx_pt
*
tree
.
_n_features
#------------------------------------------------------------
# Case 1: query point is outside node radius:
# trim it from the query
cdef
D_t
largest
=
heaps
.
largest
(
idx_pt
)
if
reduced_dist_LB
>
largest
:
pass
#------------------------------------------------------------
# Case 2: this is a leaf node. Update set of nearby points
elif
node_info
.
is_leaf
:
for
i
in
range
(
node_info
.
idx_start
,
node_info
.
idx_end
):
sq_dist
=
sqeuclidean_dist
(
x1
=
query_point
,
x2
=
tree
.
_data_ptr
+
tree
.
_indices_ptr
[
i
]
*
tree
.
_n_features
,
k
=
tree
.
_n_features
,
)
heaps
.
push
(
idx_pt
,
sq_dist
,
tree
.
_indices_ptr
[
i
])
#------------------------------------------------------------
# Case 3: Node is not a leaf. Recursively query subnodes
# starting with the closest
else
:
idx_left_node
=
2
*
idx_node
+
1
idx_right_node
=
idx_left_node
+
1
reduced_dist_LB_1
=
tree
.
min_rdist
(
idx_left_node
,
query_point
)
reduced_dist_LB_2
=
tree
.
min_rdist
(
idx_right_node
,
query_point
)
# recursively query subnodes
if
reduced_dist_LB_1
<=
reduced_dist_LB_2
:
self
.
_query_single_depthfirst
(
tree
,
idx_left_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_1
)
self
.
_query_single_depthfirst
(
tree
,
idx_right_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_2
)
else
:
self
.
_query_single_depthfirst
(
tree
,
idx_right_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_2
)
self
.
_query_single_depthfirst
(
tree
,
idx_left_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_1
)
return
0
cdef
cypclass
KDTree
:
...
...
@@ -638,59 +700,6 @@ cdef cypclass KDTree:
free
(
self
.
_node_data_ptr
)
free
(
self
.
_node_bounds_ptr
)
int
_query_single_depthfirst
(
self
,
I_t
idx_node
,
D_t
*
query_points
,
I_t
idx_pt
,
NeighborsHeaps
heaps
,
D_t
reduced_dist_LB
,
)
except
-
1
:
"""Recursive Single-tree k-neighbors query, depth-first approach"""
cdef
NodeData_t
node_info
=
self
.
_node_data_ptr
[
idx_node
]
cdef
D_t
sq_dist
,
reduced_dist_LB_1
,
reduced_dist_LB_2
cdef
I_t
i
,
idx_left_node
,
idx_right_node
cdef
D_t
*
this_query_point
=
query_points
+
idx_pt
*
self
.
_n_features
#------------------------------------------------------------
# Case 1: query point is outside node radius:
# trim it from the query
cdef
D_t
largest
=
heaps
.
largest
(
idx_pt
)
if
reduced_dist_LB
>
largest
:
pass
#------------------------------------------------------------
# Case 2: this is a leaf node. Update set of nearby points
elif
node_info
.
is_leaf
:
for
i
in
range
(
node_info
.
idx_start
,
node_info
.
idx_end
):
sq_dist
=
sqeuclidean_dist
(
x1
=
this_query_point
,
x2
=
self
.
_data_ptr
+
self
.
_indices_ptr
[
i
]
*
self
.
_n_features
,
k
=
self
.
_n_features
,
)
heaps
.
push
(
idx_pt
,
sq_dist
,
self
.
_indices_ptr
[
i
])
#------------------------------------------------------------
# Case 3: Node is not a leaf. Recursively query subnodes
# starting with the closest
else
:
idx_left_node
=
2
*
idx_node
+
1
idx_right_node
=
idx_left_node
+
1
reduced_dist_LB_1
=
self
.
min_rdist
(
idx_left_node
,
this_query_point
)
reduced_dist_LB_2
=
self
.
min_rdist
(
idx_right_node
,
this_query_point
)
# recursively query subnodes
if
reduced_dist_LB_1
<=
reduced_dist_LB_2
:
self
.
_query_single_depthfirst
(
idx_left_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_1
)
self
.
_query_single_depthfirst
(
idx_right_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_2
)
else
:
self
.
_query_single_depthfirst
(
idx_right_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_2
)
self
.
_query_single_depthfirst
(
idx_left_node
,
query_points
,
idx_pt
,
heaps
,
reduced_dist_LB_1
)
return
0
void
query
(
self
,
np
.
ndarray
query_points
,
# IN
np
.
ndarray
knn_indices
,
# IN/OUT
...
...
@@ -702,7 +711,7 @@ cdef cypclass KDTree:
I_t
n_query
=
query_points
.
shape
[
0
]
I_t
n_features
=
query_points
.
shape
[
1
]
I_t
n_neighbors
=
knn_indices
.
shape
[
1
]
D_t
*
_query_points
_ptr
=
<
D_t
*>
query_points
.
data
D_t
*
_query_points
=
<
D_t
*>
query_points
.
data
D_t
rdist_lower_bound
I_t
n_workers
=
omp_get_max_threads
()
I_t
n_points_worker
=
<
I_t
>
ceil
(
n_query
/
n_workers
)
...
...
@@ -716,20 +725,17 @@ cdef cypclass KDTree:
# This Counter is used as a way to implement a barrier for
# the asynchronous construction of the tree.
active
Counter
counter
=
consume
Counter
()
idx_worker
=
0
idx_start
=
n_points_worker
*
idx_worker
idx_end
=
min
(
n_points_worker
*
(
idx_worker
+
1
),
n_query
)
cdef
QueryActor
query_actor
=
QueryActor
(
query_points
=
_query_points_ptr
,
tree
=
tree
,
heaps
=
heaps
,
idx_worker
=
idx_worker
,
idx_start
=
n_points_worker
*
idx_worker
,
idx_end
=
min
(
n_points_worker
*
(
idx_worker
+
1
),
n_query
),
)
QueryActor
query_actor
for
idx_worker
in
range
(
n_workers
):
query_actor
=
QueryActor
(
_query_points
,
self
,
heaps
,
idx_worker
,
n_points_worker
*
idx_worker
,
min
(
n_points_worker
*
(
idx_worker
+
1
),
n_query
),
)
heaps
.
sort
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment