Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG + 1] FIX gil acquisition in dist_metric #9311

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

Merged
merged 2 commits into from
Jul 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions sklearn/neighbors/dist_metrics.pyx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1093,22 +1093,29 @@ cdef class PyFuncDistance(DistanceMetric):
self.func = func
self.kwargs = kwargs

# in cython < 0.26, GIL was required to be acquired during definition of
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this comments or shall I remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's helpful

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we needed to acquire it twice previously. Seems strange. But I've not been following the discussions.

Copy link
Contributor

@robertwb robertwb Jul 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has been some cleanup regarding inconsistencies between nogil and withgil on declaration (including inheritance) vs. definition in previous versions of Cython. This seems like a fine workaround to support both versions.

# the function and inside the body of the function. This behaviour is not
# allowed in cython >= 0.26 since it is a redundant GIL acquisition. The
# only way to be back compatible is to inherit `dist` from the base class
# without GIL and called an inline `_dist` which acquire GIL.
cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2,
ITYPE_t size) except -1 with gil:
ITYPE_t size) nogil except -1:
return self._dist(x1, x2, size)

cdef inline DTYPE_t _dist(self, DTYPE_t* x1, DTYPE_t* x2,
ITYPE_t size) except -1 with gil:
cdef np.ndarray x1arr
cdef np.ndarray x2arr
with gil:
x1arr = _buffer_to_ndarray(x1, size)
x2arr = _buffer_to_ndarray(x2, size)
d = self.func(x1arr, x2arr, **self.kwargs)
try:
# Cython generates code here that results in a TypeError
# if d is the wrong type.
return d
except TypeError:
raise TypeError("Custom distance function must accept two "
"vectors and return a float.")

x1arr = _buffer_to_ndarray(x1, size)
x2arr = _buffer_to_ndarray(x2, size)
d = self.func(x1arr, x2arr, **self.kwargs)
try:
# Cython generates code here that results in a TypeError
# if d is the wrong type.
return d
except TypeError:
raise TypeError("Custom distance function must accept two "
"vectors and return a float.")


cdef inline double fmax(double a, double b) nogil:
Expand Down