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

Conversation

glemaitre
Copy link
Member

@glemaitre glemaitre commented Jul 10, 2017

Reference Issue

partly addressing #9272
continues #9133
continues #9296

What does this implement/fix? Explain your changes.

Due to the inheritance, dist needs to be declared with nogil.
Therefore a _dist declared with gil is used and called in dist.

Any other comments?

@@ -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.

@glemaitre
Copy link
Member Author

glemaitre commented Jul 10, 2017

@amueller @lesteve

The test were passing locally in both cython 0.25 and master.
I put a comment explaining the reason for this warper but I would appreciate if you could revise it (even remove it if we don't need it)

@glemaitre
Copy link
Member Author

I forgot to ping @jnothman @ogrisel

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

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

I don't understand. I'll try read the back-story when I have time.

# in cython < 0.26, GIL was required to be acquired during definition of
# 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 inherate `dist` from the base class
Copy link
Member

Choose a reason for hiding this comment

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

*inherit

@@ -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

Choose a reason for hiding this comment

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

It's helpful

@@ -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

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.

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:
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 get why this should be no gil.

@glemaitre
Copy link
Member Author

glemaitre commented Jul 10, 2017 via email

@TomDLT
Copy link
Member

TomDLT commented Jul 10, 2017

Not a cython expert, but it LGTM

@jmschrei
Copy link
Member

This looks good to me. I think the issue may have been that defining a function as nogil doesn't release the GIL, it simply allows the function to be called when the GIL is released, so the author may have thought that with gil worked the same way.

@scoder
Copy link

scoder commented Jul 10, 2017

The explanation, IIUC, is that Cython requires the with gil signature declaration in order to allow local Python object variables, but previous Cython versions ignored it when it came to actually acquiring the GIL and instead looked at the nogil declaration of the base type method and did nothing at runtime. Thus, a second with gil block inside of the function body was required in order to acquire the GIL. This has been fixed and the duplicate GIL acquiring was turned into an error, but the change in this pull request is needed in order to support both the old buggy Cython versions and the new corrected ones.

@amueller
Copy link
Member

LGTM.

Does this run on cython master? The beta segfaults, right?

@amueller amueller changed the title [MRG] FIX gil acquisition in dist_metric [MRG + 1] FIX gil acquisition in dist_metric Jul 10, 2017
@glemaitre
Copy link
Member Author

glemaitre commented Jul 10, 2017 via email

@TomDLT
Copy link
Member

TomDLT commented Jul 18, 2017

The problem seems well understood, let's merge this fix

@TomDLT TomDLT merged commit 8cee70d into scikit-learn:master Jul 18, 2017
@TomDLT
Copy link
Member

TomDLT commented Jul 18, 2017

Thanks @glemaitre !

@amueller
Copy link
Member

Thanks!

jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Aug 6, 2017
dmohns pushed a commit to dmohns/scikit-learn that referenced this pull request Aug 7, 2017
dmohns pushed a commit to dmohns/scikit-learn that referenced this pull request Aug 7, 2017
paulha pushed a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017
AishwaryaRK pushed a commit to AishwaryaRK/scikit-learn that referenced this pull request Aug 29, 2017
maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
jwjohnson314 pushed a commit to jwjohnson314/scikit-learn that referenced this pull request Dec 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants