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
1754bf81
Commit
1754bf81
authored
Mar 29, 2022
by
Julien Jerphanion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Boils down to a simple example
The problem looks likely related to the error handling with prange.
parent
a8f04bcc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
118 deletions
+17
-118
debug.py
debug.py
+2
-21
kdtree.pyx
kdtree.pyx
+15
-97
No files found.
debug.py
View file @
1754bf81
import
kdtree
import
numpy
as
np
from
sklearn
import
neighbors
if
__name__
==
"__main__"
:
X
=
np
.
load
(
"X.npy"
)
tree
=
kdtree
.
KDTree
()
n_query
=
10
k
=
3
rng
=
np
.
random
.
RandomState
(
0
)
Y
=
rng
.
rand
(
n_query
,
X
.
shape
[
1
])
knn_indices
=
np
.
zeros
((
n_query
,
k
),
dtype
=
np
.
int32
)
knn_distances
=
np
.
zeros
((
n_query
,
k
),
dtype
=
np
.
float64
)
tree
.
query
(
Y
,
knn_indices
,
knn_distances
)
# print(knn_distances)
sktree
=
neighbors
.
KDTree
(
X
,
leaf_size
=
256
)
knn_distances
,
knn_indices
=
sktree
.
query
(
Y
,
k
=
k
,
return_distance
=
True
)
# print(knn_distances)
tree
=
kdtree
.
Clazz
()
tree
.
outer_method
()
kdtree.pyx
View file @
1754bf81
# distutils: language = c++
# cython: language_level = 3
cimport
numpy
as
np
import
numpy
as
np
np
.
import_array
()
from
runtime.runtime
cimport
BatchMailBox
,
Scheduler
from
libc.math
cimport
log2
,
fmax
,
fmin
,
fabs
from
libc.stdio
cimport
printf
from
libc.stdlib
cimport
malloc
,
free
,
exit
from
openmp
cimport
omp_get_max_threads
from
cython.operator
cimport
dereference
as
deref
from
cython.parallel
import
prange
## Types declaration
ctypedef
int
I_t
ctypedef
double
D_t
cdef
extern
from
"<sys/syscall.h>"
nogil
:
int
SYS_gettid
long
syscall
(
long
)
# TODO (jjerphan): integrate this where needed once the
# compilation problem with Cython has been resolved
# TODO(jjerphan): debug without using any cypclass instances.
cpdef
void
prange_workaround
(
KDTree
tree
,
I_t
n_query
,
I_t
num_threads
,
I_t
n_features
,
D_t
*
_query_points_ptr
,
)
nogil
:
cdef
void
prange_workaround
(
Clazz
instance
)
nogil
:
cdef
:
D_t
rdist_lower_bound
=
0
I_t
i
printf
(
"prange_workaround: start
\
n
"
)
for
i
in
prange
(
10
,
schedule
=
'static'
,
num_threads
=
10
):
printf
(
"Inner method: %d
\
n
"
,
i
)
instance
.
inner_method
()
printf
(
"prange_workaround: end
\
n
"
)
printf
(
"pre-prange tid: %d
\
n
"
,
syscall
(
SYS_gettid
))
for
i
in
prange
(
n_query
,
schedule
=
'static'
,
num_threads
=
num_threads
):
printf
(
"i, tid: %d
\
n
"
,
syscall
(
SYS_gettid
))
printf
(
"%d / %d: start
\
n
"
,
i
,
n_query
)
rdist_lower_bound
=
tree
.
min_rdist
(
0
,
_query_points_ptr
+
i
*
n_features
)
# tree._query_single_depthfirst(0, _query_points_ptr, i, rdist_lower_bound)
printf
(
"%d / %d: end
\
n
"
,
i
,
n_query
)
printf
(
"end prange
\
n
"
)
exit
(
0
)
cdef
cypclass
KDTree
:
__init__
(
self
,
):
# Recursively building the tree here
# printf("Scheduler: starting\n")
# cdef lock Scheduler scheduler = Scheduler(omp_get_max_threads())
# printf("Scheduler: started\n")
# scheduler.finish()
printf
(
"Scheduler: None
\
n
"
)
void
query
(
self
,
np
.
ndarray
query_points
,
# IN
np
.
ndarray
knn_indices
,
# IN/OUT
np
.
ndarray
knn_distances
,
# IN/OUT
):
cdef
:
I_t
completed_queries
=
0
I_t
i
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
rdist_lower_bound
I_t
num_threads
=
omp_get_max_threads
()
printf
(
"Query: starting
\
n
"
)
prange_workaround
(
self
,
n_query
,
num_threads
,
n_features
,
_query_points_ptr
)
printf
(
"Query: finished
\
n
"
)
D_t
min_rdist
(
KDTree
self
,
I_t
idx_node
,
D_t
*
pt
,
)
except
-
1
:
"""Compute the minimum reduced-distance between a point and a node"""
cdef
:
D_t
d
,
d_lo
,
d_hi
,
node_min_j
,
node_max_j
,
rdist
=
0.0
I_t
j
for
j
in
range
(
10
):
node_min_j
=
0
node_max_j
=
10
d_lo
=
node_min_j
-
pt
[
j
]
d_hi
=
pt
[
j
]
-
node_max_j
# We use the following identity:
#
# 0.5 * (x + abs(x)) = 0.5 * max(x, 0)
#
# twice.
d
=
0.5
*
((
d_lo
+
fabs
(
d_lo
))
+
(
d_hi
+
fabs
(
d_hi
)))
rdist
+=
d
**
2
return
rdist
cdef
cypclass
Clazz
:
void
outer_method
(
Clazz
self
):
printf
(
"Outer method: start
\
n
"
)
prange_workaround
(
self
)
printf
(
"Outer method: finish
\
n
"
)
cdef
public
int
main
()
nogil
:
#
Entry point for the compiled binary file
printf
(
"empty public int main() nogil:"
)
return
0
# Exception handling with prange makes the
#
execution block for some reasons.
double
inner_method
(
Clazz
self
)
except
-
1
:
return
0.
0
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