Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython_plus_experiments
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
0
Merge Requests
0
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
Julien Jerphanion
cython_plus_experiments
Commits
2c760866
Commit
2c760866
authored
Jun 08, 2021
by
Julien Jerphanion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Makes attributes private
parent
c286077c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
60 deletions
+64
-60
kdtree/kdtree.pyx
kdtree/kdtree.pyx
+64
-60
No files found.
kdtree/kdtree.pyx
View file @
2c760866
...
...
@@ -14,6 +14,7 @@ ctypedef int I_t
ctypedef
double
D_t
cdef
lock
Scheduler
scheduler
cdef
D_t
INF
=
1e38
cdef
extern
from
*
:
"""
...
...
@@ -66,24 +67,24 @@ cdef extern from *:
cdef
cypclass
Node
activable
:
"""A KDTree Node"""
D_t
*
data_ptr
I_t
*
indices_ptr
D_t
*
_
data_ptr
I_t
*
_
indices_ptr
D_t
*
point
I_t
n_dims
I_t
dim
I_t
start
I_t
end
bint
is_leaf
active
Node
left
active
Node
right
D_t
*
_
point
I_t
_
n_dims
I_t
_
dim
I_t
_
start
I_t
_
end
bint
_
is_leaf
active
Node
_
left
active
Node
_
right
__init__
(
self
):
self
.
_active_result_class
=
WaitResult
.
construct
self
.
_active_queue_class
=
consume
BatchMailBox
(
scheduler
)
self
.
left
=
NULL
self
.
right
=
NULL
self
.
is_leaf
=
False
self
.
_
left
=
NULL
self
.
_
right
=
NULL
self
.
_
is_leaf
=
False
void
build_node
(
self
,
...
...
@@ -100,33 +101,33 @@ cdef cypclass Node activable:
cdef
I_t
next_dim
=
(
dim
+
1
)
%
n_dims
cdef
I_t
mid
=
(
start
+
end
)
//
2
self
.
data_ptr
=
data_ptr
self
.
indices_ptr
=
indices_ptr
self
.
_
data_ptr
=
data_ptr
self
.
_
indices_ptr
=
indices_ptr
self
.
dim
=
dim
self
.
n_dims
=
n_dims
self
.
start
=
start
self
.
end
=
end
self
.
_
dim
=
dim
self
.
_
n_dims
=
n_dims
self
.
_
start
=
start
self
.
_
end
=
end
if
(
end
-
start
<=
leaf_size
):
self
.
is_leaf
=
True
self
.
_
is_leaf
=
True
counter
.
add
(
NULL
,
end
-
start
)
return
partition_node_indices
(
data_ptr
,
indices_ptr
,
start
,
mid
,
end
,
dim
,
n_dims
)
self
.
point
=
data_ptr
+
mid
self
.
_
point
=
data_ptr
+
mid
self
.
left
=
consume
Node
()
self
.
right
=
consume
Node
()
self
.
_
left
=
consume
Node
()
self
.
_
right
=
consume
Node
()
self
.
left
.
build_node
(
NULL
,
data_ptr
,
indices_ptr
,
leaf_size
,
n_dims
,
next_dim
,
start
,
mid
,
counter
)
self
.
right
.
build_node
(
NULL
,
self
.
_left
.
build_node
(
NULL
,
data_ptr
,
indices_ptr
,
leaf_size
,
n_dims
,
next_dim
,
mid
,
end
,
counter
)
start
,
mid
,
counter
)
self
.
_right
.
build_node
(
NULL
,
data_ptr
,
indices_ptr
,
leaf_size
,
n_dims
,
next_dim
,
mid
,
end
,
counter
)
void
get_closest
(
self
,
D_t
*
query_points
,
...
...
@@ -135,17 +136,17 @@ cdef cypclass Node activable:
):
cdef
:
I_t
j
,
k
,
closest
=
-
1
D_t
dist
=
1e38
D_t
dist
=
INF
D_t
tmp
D_t
min_distance
=
1e38
D_t
min_distance
=
INF
if
self
.
is_leaf
:
for
j
in
range
(
s
tart
,
end
):
if
self
.
_
is_leaf
:
for
j
in
range
(
s
elf
.
_start
,
self
.
_
end
):
dist
=
0
for
k
in
range
(
self
.
n_dims
):
for
k
in
range
(
self
.
_
n_dims
):
tmp
=
(
query_points
[
i
*
self
.
n_dims
+
k
]
-
self
.
data_ptr
[
self
.
indices_ptr
[
j
]
*
self
.
n_dims
+
k
]
query_points
[
i
*
self
.
_
n_dims
+
k
]
-
self
.
_data_ptr
[
self
.
_indices_ptr
[
j
]
*
self
.
_
n_dims
+
k
]
)
dist
+=
tmp
*
tmp
...
...
@@ -156,10 +157,10 @@ cdef cypclass Node activable:
container
.
update
(
NULL
,
i
,
closest
)
return
if
query_points
[
i
*
self
.
n_dims
+
self
.
dim
]
<
self
.
point
[
self
.
dim
]:
self
.
left
.
get_closest
(
NULL
,
query_points
,
i
,
container
)
if
query_points
[
i
*
self
.
_n_dims
+
self
.
_dim
]
<
self
.
_point
[
self
.
_
dim
]:
self
.
_
left
.
get_closest
(
NULL
,
query_points
,
i
,
container
)
else
:
self
.
right
.
get_closest
(
NULL
,
query_points
,
i
,
container
)
self
.
_
right
.
get_closest
(
NULL
,
query_points
,
i
,
container
)
cdef
cypclass
Counter
activable
:
...
...
@@ -168,21 +169,21 @@ cdef cypclass Counter activable:
Useful for synchronisation, as it can be used as a barrier.
"""
I_t
n
I_t
_
n
__init__
(
self
):
self
.
n
=
0
self
.
_
n
=
0
self
.
_active_result_class
=
WaitResult
.
construct
self
.
_active_queue_class
=
consume
BatchMailBox
(
scheduler
)
void
add
(
self
,
I_t
value
):
self
.
n
+=
value
self
.
_
n
+=
value
void
reset
(
self
):
self
.
n
=
0
self
.
_
n
=
0
I_t
value
(
self
):
return
self
.
n
return
self
.
_
n
cdef
cypclass
Container
activable
:
...
...
@@ -223,14 +224,16 @@ cdef cypclass KDTree:
This relies on a Cython+ runtime using actors.
"""
I_t
n
# number of data
I_t
d
# number of dimensions / features
I_t
leaf_size
# maximum number of vectors at leaf
I_t
_n
# number of examples
I_t
_d
# number of dimensions / features
I_t
_leaf_size
# maximum number of vectors at leaf
I_t
_n_leafs
active
Node
_root
D_t
*
data_ptr
I_t
*
indices_ptr
D_t
*
_
data_ptr
I_t
*
_
indices_ptr
__init__
(
self
,
np
.
ndarray
X
,
...
...
@@ -240,14 +243,14 @@ cdef cypclass KDTree:
cdef
I_t
n
=
X
.
shape
[
0
]
cdef
I_t
d
=
X
.
shape
[
1
]
self
.
n
=
n
self
.
d
=
d
self
.
leaf_size
=
leaf_size
self
.
_
n
=
n
self
.
_
d
=
d
self
.
_
leaf_size
=
leaf_size
self
.
data_ptr
=
<
D_t
*>
X
.
data
self
.
indices_ptr
=
<
I_t
*>
malloc
(
n
*
sizeof
(
I_t
))
self
.
_
data_ptr
=
<
D_t
*>
X
.
data
self
.
_
indices_ptr
=
<
I_t
*>
malloc
(
n
*
sizeof
(
I_t
))
for
i
in
range
(
n
):
self
.
indices_ptr
[
i
]
=
i
self
.
_
indices_ptr
[
i
]
=
i
global
scheduler
scheduler
=
Scheduler
()
...
...
@@ -267,14 +270,15 @@ cdef cypclass KDTree:
# a new argument is prepredend for a predicate,
# which we aren't using using here, hence the extra NULL.
self
.
_root
.
build_node
(
NULL
,
self
.
data_ptr
,
self
.
indices_ptr
,
leaf_size
,
n_dims
=
d
,
dim
=
0
,
start
=
0
,
end
=
n
,
self
.
_data_ptr
,
self
.
_indices_ptr
,
self
.
_leaf_size
,
n_dims
=
self
.
_d
,
dim
=
0
,
start
=
0
,
end
=
self
.
_n
,
counter
=
counter
)
initialised
=
counter
.
value
(
NULL
).
getIntResult
()
while
(
initialised
<
self
.
n
):
while
(
initialised
<
self
.
_
n
):
initialised
=
counter
.
value
(
NULL
).
getIntResult
()
counter
.
reset
(
NULL
)
...
...
@@ -282,7 +286,7 @@ cdef cypclass KDTree:
void
__dealloc__
(
self
):
scheduler
.
finish
()
free
(
self
.
indices_ptr
)
free
(
self
.
_
indices_ptr
)
void
get_closest
(
self
,
...
...
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