release the GIL in denoise_tv_bregman and denoise_bilateral #5400
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I tried using
apply_parallel
with the variousdenoise_*
functions inskimage.restoration
. Most showed a substantial speedup, butdenoise_tv_bregman
anddenoise_bilateral
showed worse performance when calling withapply_parallel
than without. This turns out to be because the GIL is not being released by these two functions.As in #5399, this PR just adds a
with nogil
context block into each of these functions so the GIL will be released, allowing multithreading. I confirmed locally that this leads to the expected improvement.The somewhat large diff is misleading as nearly all line differences are just the change in indentation.
Timing
For example for
denoise_tv_bregman
on an input of shape (2048, 4096) it takes:normal call (single thread): 2.33 s
call with apply_parallel (prior to this PR, GIL not released): 3.2 s
call with apply_parallel (after this PR): 0.53 s
The result for
denoise_bilateral
is very similar.