Commit 1754bf81 authored by Julien Jerphanion's avatar Julien Jerphanion

Boils down to a simple example

The problem looks likely related to the
error handling with prange.
parent a8f04bcc
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()
# 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
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