Commit 91ea698f authored by Julien Jerphanion's avatar Julien Jerphanion

Fix partitionning bug

The bug was due to a pointer offset
parent d03116f0
# distutils: language = c++
# cython: language_level = 3
cimport numpy as np
import numpy as np
np.import_array()
from runtime.runtime cimport BatchMailBox, NullResult, Scheduler
from libc.stdio cimport printf
from libc.stdlib cimport malloc, free
from stdlib.string cimport string
## Types declaration
ctypedef int I_t
ctypedef double D_t
cdef lock Scheduler scheduler
DEF DEBUG = True
cdef extern from *:
"""
#include <algorithm>
......@@ -41,29 +37,31 @@ cdef extern from *:
void partition_node_indices(
const D *data,
I *node_indices,
I start,
I mid,
I end,
const I &split_dim,
const I &split_index,
const I &n_features,
const I &n_points) {
const I &n_features) {
IndexComparator<D, I> index_comparator(data, split_dim, n_features);
std::cout << "D " << data << ", " << data +
split_index << ", " << data + n_points << std::endl
<< "I " << node_indices << ", " << node_indices +
split_index << ", " << node_indices + n_points << std::endl;
std::nth_element(
node_indices,
node_indices + split_index,
node_indices + n_points,
index_comparator);
node_indices + start,
node_indices + mid,
node_indices + end,
index_comparator
);
}
"""
void partition_node_indices[D, I](
D *data,
I *node_indices,
I start,
I mid,
I end,
I split_dim,
I split_index,
I n_features,
I n_points) nogil except +
I n_features
) nogil except +
cdef cypclass Node activable:
"""A KDTree Node"""
......@@ -91,61 +89,27 @@ cdef cypclass Node activable:
):
cdef I_t i
cdef I_t next_dim = (dim + 1) % n_dims
cdef I_t nn = end - start
cdef I_t n_mid = nn // 2
cdef I_t split_index = (start + end) // 2
cdef D_t * rel_points = points + start
cdef I_t * rel_indices = indices + start
cdef I_t mid = (start + end) // 2
self.n_dims = n_dims
if (depth < 0) or (nn <= 1):
if (depth < 0) or (end - start <= 1):
return
partition_node_indices(rel_points,
rel_indices, dim, n_mid, n_dims, nn)
self.point = points + split_index
if DEBUG:
printf("Depth %d on dim %d: [%d, %d) med: %d %d %d %d\t"
"[%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d,"
" %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]\n "
"[%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f]\n\n",
depth, dim, start, end, split_index,
rel_points, rel_points + n_mid, rel_points + nn,
indices[0], indices[1], indices[2],
indices[3], indices[4], indices[5],
indices[6], indices[7], indices[8],
indices[9], indices[10], indices[11],
indices[12], indices[13], indices[14],
indices[15], indices[16], indices[17],
indices[18], indices[19], indices[20],
indices[21], indices[22], indices[23],
points[n_dims * indices[0] + dim],
points[n_dims * indices[1] + dim],
points[n_dims * indices[2] + dim],
points[n_dims * indices[3] + dim],
points[n_dims * indices[4] + dim],
points[n_dims * indices[5] + dim],
points[n_dims * indices[6] + dim],
points[n_dims * indices[7] + dim],
points[n_dims * indices[8] + dim],
points[n_dims * indices[9] + dim],
points[n_dims * indices[10] + dim],
points[n_dims * indices[11] + dim],
)
self.left = activate(consume Node())
self.right = activate(consume Node())
self.left.build_node(NULL, points, indices,
partition_node_indices(points, indices, start, mid, end, dim, n_dims)
self.point = points + mid
self.left = consume Node()
self.right = consume Node()
self.left.build_node(NULL,
points, indices,
depth - 1, n_dims, next_dim,
start, split_index)
self.right.build_node(NULL, points, indices,
start, mid)
self.right.build_node(NULL,
points, indices,
depth - 1, n_dims, next_dim,
split_index, end)
mid, end)
cdef cypclass KDTree:
......@@ -192,8 +156,8 @@ cdef cypclass KDTree:
for i in range(n):
self.indices[i] = i
self.points[i * d] = i # (i / golden_ratio) % 1
self.points[i * d + 1] = i # i / n
self.points[i * d] = (i / golden_ratio) % 1
self.points[i * d + 1] = i / n
# TODO: end
......@@ -209,7 +173,7 @@ cdef cypclass KDTree:
global scheduler
scheduler = Scheduler()
self.root = activate(consume Node())
self.root = consume Node()
if self.root is NULL:
printf("Error consuming node\n")
......@@ -220,16 +184,13 @@ cdef cypclass KDTree:
self.root.build_node(NULL,
self.points,
self.indices,
depth, d, dim=0, start=0, end=n)
depth, n_dims=d, dim=0, start=0, end=n)
scheduler.finish()
del scheduler
for i in range(n):
printf("indices[%d] = %d\n", i, self.indices[i])
for i in range(n):
printf("X[indices[%d]]= %f %f\n", i,
printf("X[indices[%d] = %d]= %f %f\n", i, self.indices[i]
self.points[self.indices[i] * d],
self.points[self.indices[i] * d + 1])
......@@ -238,15 +199,11 @@ cdef cypclass KDTree:
free(self.points)
free(self.indices)
printf("Done deallocating KDTree datastructures\n")
# XXX It sometimes blocks over here and then segfaults
cdef public int main() nogil:
# Entry point for the compiled binary file
printf("main: called\n")
tree = KDTree()
printf("Done\n")
# XXX a segfault is thrown when exiting
# this function, but not others
printf("main: done\n")
return 0
def python_main():
tree = KDTree()
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