Commit 5951d2e3 authored by Julien Jerphanion's avatar Julien Jerphanion

[DEBUG] Query

parent fefabcc0
...@@ -520,7 +520,7 @@ cdef cypclass QueryActor: ...@@ -520,7 +520,7 @@ cdef cypclass QueryActor:
__init__( __init__(
self, self,
D_t * query_points_ptr, D_t * query_points,
KDTree tree, KDTree tree,
NeighborsHeaps heaps, NeighborsHeaps heaps,
I_t idx_worker, I_t idx_worker,
...@@ -528,6 +528,68 @@ cdef cypclass QueryActor: ...@@ -528,6 +528,68 @@ cdef cypclass QueryActor:
I_t idx_end, I_t idx_end,
): ):
printf("QueryActor: (%d, %d, %d)\n", idx_worker, idx_start, 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: cdef cypclass KDTree:
...@@ -638,59 +700,6 @@ cdef cypclass KDTree: ...@@ -638,59 +700,6 @@ cdef cypclass KDTree:
free(self._node_data_ptr) free(self._node_data_ptr)
free(self._node_bounds_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, void query(self,
np.ndarray query_points, # IN np.ndarray query_points, # IN
np.ndarray knn_indices, # IN/OUT np.ndarray knn_indices, # IN/OUT
...@@ -702,7 +711,7 @@ cdef cypclass KDTree: ...@@ -702,7 +711,7 @@ cdef cypclass KDTree:
I_t n_query = query_points.shape[0] I_t n_query = query_points.shape[0]
I_t n_features = query_points.shape[1] I_t n_features = query_points.shape[1]
I_t n_neighbors = knn_indices.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 D_t rdist_lower_bound
I_t n_workers = omp_get_max_threads() I_t n_workers = omp_get_max_threads()
I_t n_points_worker = <I_t> ceil(n_query / n_workers) I_t n_points_worker = <I_t> ceil(n_query / n_workers)
...@@ -716,20 +725,17 @@ cdef cypclass KDTree: ...@@ -716,20 +725,17 @@ cdef cypclass KDTree:
# This Counter is used as a way to implement a barrier for # This Counter is used as a way to implement a barrier for
# the asynchronous construction of the tree. # the asynchronous construction of the tree.
active Counter counter = consume Counter() active Counter counter = consume Counter()
QueryActor query_actor
idx_worker = 0
idx_start = n_points_worker * idx_worker for idx_worker in range(n_workers):
idx_end = min(n_points_worker * (idx_worker + 1), n_query) query_actor = QueryActor(
_query_points,
self,
cdef QueryActor query_actor = QueryActor( heaps,
query_points=_query_points_ptr, idx_worker,
tree=tree, n_points_worker * idx_worker,
heaps=heaps, min(n_points_worker * (idx_worker + 1), n_query),
idx_worker=idx_worker, )
idx_start=n_points_worker * idx_worker,
idx_end=min(n_points_worker * (idx_worker + 1), n_query),
)
heaps.sort() heaps.sort()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment