Skip to content

Replace fastrlock with C++ recursive_mutex #9414

Merged
kmaehashi merged 7 commits into
cupy:mainfrom
seberg:remove-fastrlock
Oct 16, 2025
Merged

Replace fastrlock with C++ recursive_mutex #9414
kmaehashi merged 7 commits into
cupy:mainfrom
seberg:remove-fastrlock

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Oct 7, 2025

It seems that fastrlock tends to be a problem with new Python versions
and also freethreading.
This proposes to replace it with a C++ mutex, making it recursive/re-entrant
just because I am not 100% sure about things.

I am not sure if there are very subtle issues (subinterpreters?)
if using a C++ mutex, but I don't think those are of a big concern.
FWIW, NumPy also uses a lot of C++ mutexes for free-threading support at least.

Since Python 3.10 has direct GC C-API functions in the stable API just added
those, I didn't time if it is relevant, but it seemed just as well/nice.


Tested the performance with the test from: #9149 (comment) and saw no difference (beyond just random fluctuations).
I can see that fastrlock may have some advantages, but just avoiding the dependency is maybe nice neough.

seberg added 4 commits October 7, 2025 06:56
It seems that fastrlock tends to be a problem with new Python versions
and also freethreading.
This proposes to replace it with a C++ mutex, making it recursive/re-entrant
just because I am not 100% sure about things.

I am not sure if there are very subtle issues (subinterpreters?)
if using a C++ mutex, but I don't think those are of a big concern.
FWIW, NumPy also uses a lot of C++ mutexes for free-threading support at least.

Since Python 3.10 has direct GC C-API functions in the stable API just added
those, I didn't time if it is relevant, but it seemed just as well/nice.
@seberg seberg requested a review from a team as a code owner October 7, 2025 15:04
@kmaehashi
Copy link
Copy Markdown
Member

Wow this is great, thanks @seberg!

Let me trigger CI, especially I'm curious if there's any roadblocks on Windows

/test mini

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Oct 8, 2025

Hmmm, seems Cython 3.0.x didn't include the recursive_mutex defines (and I am not quite sure if non-recursive is enough).
Do you have a clear preference: Include the defines (with a comment) or be a bit aggressive and require Cython 3.1 (and try to remove the upper limit soon)?

@kmaehashi
Copy link
Copy Markdown
Member

It sounds good to me to require Cython 3.1 as it is build-time-only requirement. We deferred that decision because there were bug fix release planned at the time we added support for it (#9128 (comment)), but I think 3.1.x series today are enough stable.

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Oct 9, 2025

Think it should be good now and also just to try triggering CI. (I do suspect we don't need a recursive mutex but seemed like a good step to do like-for-like first.)

/test mini

@pfn-ci-bot

This comment was marked as outdated.

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Oct 9, 2025

And once more:

/test mini

Copy link
Copy Markdown
Contributor

@bdice bdice left a comment

Choose a reason for hiding this comment

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

The locking code looks valid to me. Thank you, this will help a lot!

Comment thread cupy/cuda/memory.pyx Outdated
@kmaehashi kmaehashi self-assigned this Oct 15, 2025
@kmaehashi kmaehashi added cat:enhancement Improvements to existing features prio:high labels Oct 15, 2025
@asi1024 asi1024 mentioned this pull request Oct 15, 2025
Comment thread cupy/cuda/memory.pyx
Co-authored-by: Bradley Dice <[email protected]>
@kmaehashi
Copy link
Copy Markdown
Member

/test mini

@kmaehashi kmaehashi enabled auto-merge October 16, 2025 04:21
@kmaehashi kmaehashi added this to the v14.0.0rc1 milestone Oct 16, 2025
@kmaehashi kmaehashi merged commit adf0595 into cupy:main Oct 16, 2025
48 checks passed
@seberg seberg deleted the remove-fastrlock branch October 16, 2025 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:enhancement Improvements to existing features prio:high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants