Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: cKDTree GIL handling is incorrect #10514

Closed
sturlamolden opened this issue Jul 25, 2019 · 9 comments · Fixed by #10516
Closed

BUG: cKDTree GIL handling is incorrect #10514

sturlamolden opened this issue Jul 25, 2019 · 9 comments · Fixed by #10516
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.spatial
Milestone

Comments

@sturlamolden
Copy link
Contributor

I believe #9915 has introduced errors in cKDTree GIL handling.

Declaring a C++ function nogil in Cython does not ensure that the GIL is automatically released and reaquired. It only declares that it is allowed to call the function in a nogil context.

Releasing the GIL in Cython is accomplished with a with nogil: statement. There is no such statement in ckdtree.pyx. Therefore, cKDTree does not release the GIL and all multithreading code is now defective.

We must either revert #9915 to keep GIL release and exception translation in C++ or fix ckdtree.pyx to actually release the gil.

@sturlamolden
Copy link
Contributor Author

ping @rainwoodman @rgommers

@sturlamolden
Copy link
Contributor Author

Fortunately, fixing ckdtree.pyx is easy. I will take care of it.

@sturlamolden
Copy link
Contributor Author

This is now fixed in #10516

@rainwoodman
Copy link
Contributor

rainwoodman commented Jul 25, 2019 via email

@sturlamolden
Copy link
Contributor Author

I am not sure. We do no use @boundscheck(False) so those indexing can raise exceptions. Maybe Cython will translate them? I do not get errors when those new pointers are not used, but I do get warnings about performence when memoryviews are indexed in the nogil context. Those warnings are silencenced by the new pointer variables, and that is good too, perhaps.

@sturlamolden
Copy link
Contributor Author

I also took liberty to put the STL std::sort calls into nogil. But here I am wondering about performance and #10216. We use quicksort. Is there any chance that we can get O(Nˆ2) here? Maybe we should use heapsort instead, i.e. std::make_heap and std::sort_heap??

And following this line of thought, do we really a custom C++ heap in query.cxx? Maybe we can just use STL?

@sturlamolden
Copy link
Contributor Author

But that is for another PR :)

@peterbell10
Copy link
Member

We use quicksort. Is there any chance that we can get O(Nˆ2) here? Maybe we should use heapsort instead, i.e. std::make_heap and std::sort_heap??

You shouldn't be worried, std::sort is mandated by the standard to have O(N logN) complexity. Implementations will use some hybrid algorithm such as introsort that handles the degenerate cases gracefully and never falls back to O(N^2).

@sturlamolden
Copy link
Contributor Author

Good to know, one thing less to worry about :)

@tylerjereddy tylerjereddy changed the title cKDTree GIL handling is incorrect BUG: cKDTree GIL handling is incorrect Aug 1, 2019
@tylerjereddy tylerjereddy added this to the 1.4.0 milestone Aug 1, 2019
@tylerjereddy tylerjereddy added defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.spatial labels Aug 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.spatial
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants