Skip to content

Conversation

IanButterworth
Copy link
Member

@IanButterworth IanButterworth commented Jan 13, 2024

Requires

TODO:


Because they are not threadsafe FFTW FFT plans are behind a lock. They should be reusable for same-sized images and kernels, so this is WIP towards exposing a way to provide plans, so that imfilter! can be run concurrently on julia threads without lock conflict.

Note that this is using JuliaLang/julia#52883 to count conflicts.

There's a demo temporarily in this PR.

Before it hits 34255 lock conflicts

% FFTW_NUM_THREADS=1 BLAS_NUM_THREADS=3 ../julia/julia --project -t6,1 -q
julia> include("demo.jl")
warm run of benchmark(mats): 5.483911 seconds (2.14 M allocations: 10.671 GiB, 16.62% gc time, 34255 lock conflicts)

This PR removes all but the lock conflicts from the singlular plans, which could be moved out of the for loop

% FFTW_NUM_THREADS=1 BLAS_NUM_THREADS=3 ../julia/julia --project -t6,1 -q
julia> include("demo.jl")
warm run of benchmark(mats): 2.440659 seconds (478.12 k allocations: 7.888 GiB, 21.53% gc time, 8 lock conflicts)

@IanButterworth IanButterworth marked this pull request as draft January 13, 2024 05:34
@IanButterworth

This comment was marked as outdated.

@IanButterworth

This comment was marked as outdated.

@IanButterworth
Copy link
Member Author

Ok, I think this seems reasonable now. @timholy would you mind reviewing for general design and I can add tests & docs if you agree

@IanButterworth IanButterworth changed the title WIP: allow providing fft plans WIP: enable reusing fft plans Jan 13, 2024
@timholy
Copy link
Member

timholy commented Jan 13, 2024

I confess I haven't thought about these plans in ages (the code I linked earlier was probably written in the Julia 0.1 days), but nothing bad jumps out about this. Thanks for doing it!!

@IanButterworth

This comment was marked as resolved.

@IanButterworth
Copy link
Member Author

Comparing buffered vs standard I get different values here

B = complex(planned_rfft1(A)) * FFTW.AbstractFFTs.to1(A)
_B = rfft(A)
@show B[1:4] _B[1:4]

B[1:4] =  ComplexF32[3065.7727f0 + 113.279686f0im, -254.48924f0 - 85.513504f0im, -369.77383f0 - 146.0826f0im, -267.98865f0 - 201.43063f0im]
_B[1:4] = ComplexF32[4595.6045f0 + 0.0f0im,         3.6047888f0 + 15.441383f0im,  14.378552f0 - 19.552792f0im, -18.684448f0 - 19.284311f0im]

My trial and error phase of debugging isn't being very fruitful, so I think I'll wait for review. @timholy if you happen to have time

@mkitti
Copy link
Member

mkitti commented Jan 31, 2024

Commeting to remind myself to take a look

Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Nothing jumps out as a serious problem just from a read through the code, but it's probably been a decade since I put the serious work into writing RFFT.jl (it used to be part of a more monolithic repository so the git history may not reflect that). Do the disagreements only arise when you use multiple threads?

@IanButterworth

This comment was marked as outdated.

Copy link

codecov bot commented Mar 11, 2024

Codecov Report

Attention: Patch coverage is 96.11650% with 4 lines in your changes missing coverage. Please review.

Project coverage is 92.36%. Comparing base (01e99cd) to head (037ac37).

Files with missing lines Patch % Lines
src/ImageFiltering.jl 80.00% 2 Missing ⚠️
src/imfilter.jl 98.91% 1 Missing ⚠️
src/utils.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #271      +/-   ##
==========================================
+ Coverage   91.86%   92.36%   +0.50%     
==========================================
  Files          12       12              
  Lines        1672     1769      +97     
==========================================
+ Hits         1536     1634      +98     
+ Misses        136      135       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@IanButterworth
Copy link
Member Author

IanButterworth commented Mar 11, 2024

Ok. Tests are passing here.

Note that planned_fft doesn't support non-float eltypes currently because RFFT.RCpair only supports floats (and other reasons, I guess).

However, demo.jl still fails

f1[1:4] = [0.11624783622854669, 0.15113825707056688, 0.16887924282352731, 0.20249257393260642]
f2[1:4] = [0.17744583830964283, -0.0655415564571991, -0.10802786527654895, 0.11185075216343188]
ERROR: LoadError: f1 !≈ f2

@IanButterworth IanButterworth force-pushed the ib/threading branch 2 times, most recently from e32b32f to a948fae Compare March 11, 2024 20:13
Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Thanks for this! Not only is the new functionality appreciated, but the various cleanups are too.

I haven't played with this myself, but does demo pass now? If so, it seems like you're almost over the hump. I don't see anything objectionable here (assuming you'll get to a point of full test coverage for the new functionality), so feel free to run with this.

@IanButterworth IanButterworth changed the title WIP: enable reusing fft plans Enable reusing fft plans Jun 10, 2025
@IanButterworth IanButterworth marked this pull request as ready for review June 10, 2025 18:09
@IanButterworth
Copy link
Member Author

IanButterworth commented Jun 10, 2025

@timholy I got unstuck here with the help of AI.. (claude 4 agent mode in vscode copilot, see dev/debug_planned.jl for the debugging investigation I got it to put together along the way)

Tests pass and coverage looks good to me, but there might be more correct ways of doing the various transforms.

@timholy timholy self-assigned this Jun 11, 2025
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.

4 participants