Skip to content
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

Type stability #16

Open
milankl opened this issue Sep 2, 2022 · 4 comments
Open

Type stability #16

milankl opened this issue Sep 2, 2022 · 4 comments

Comments

@milankl
Copy link
Collaborator

milankl commented Sep 2, 2022

Counting the float operations, we currently aren't as type-stable as I was expecting

julia> using GenericFFT, GFlops
julia> rfft_plan = plan_rfft(zeros(Float16,1024))
GenericFFT.DummyrFFTPlan{ComplexF16, false, UnitRange{Int64}}(1024, 1:1, #undef)

julia> @count_ops rfft_plan*randn(Float16,1024)
Flop Counter: 61382 flop
┌────────┬─────────┬─────────┬─────────┐
│        │ Float16 │ Float32 │ Float64 │
├────────┼─────────┼─────────┼─────────┤
│    fma │       004 │
│ muladd │       00153 │
│    add │   18449083 │
│    sub │   16447039 │
│    mul │   2462201250 │
│    div │      40232 │
│    abs │      401917 │
│    neg │      1503 │
│   sqrt │       0190 │
└────────┴─────────┴─────────┴─────────┘

The Float32 operations might be miscounted similar to triscale-innov/GFlops.jl#40 but I doubt the Float64 operations are. Just raising this as we may want to ensure type stability and explicit conversions rather than relying on promotions (which can easily cascade into Float64s where we don't actually want to use them). The output may still be of eltype T but users of this package probably want a Fourier transform fully in T when they provide an input vector of eltype T?

@daanhb
Copy link
Member

daanhb commented Sep 2, 2022

Heh, I did not know about @count_ops. I'd say you're right that it is fair to expect a transform in T if T is provided (up to the Float16 vs Float32 issue).

@milankl
Copy link
Collaborator Author

milankl commented Sep 2, 2022

The culprit might be the sinpi function. Mimicking generic_fft_pow2!

julia> function sinpi_test(::Type{T},n::Integer) where T
           big2 = 2one(T)
           logn = 2
           while logn < n
               θ = -big2/logn
               wtemp = sinpi/2)
               wpi = sinpi(θ)
               logn <<= 1
           end
       end
sinpi_test (generic function with 1 method)

julia> @count_ops sinpi_test(Float16,1024)
Flop Counter: 431 flop
┌────────┬─────────┬─────────┬─────────┐
│        │ Float16 │ Float32 │ Float64 │
├────────┼─────────┼─────────┼─────────┤
│ muladd │       0028 │
│    add │      18032 │
│    sub │      5800 │
│    mul │      36090 │
│    div │      36210 │
│    abs │      36170 │
│    neg │      1400 │
│   sqrt │       0170 │
└────────┴─────────┴─────────┴─────────┘

So maybe it's okay because the call of sinpi does not depend on the input vector x only on its length? Are these sinpi factors usually something that would be contained in a plan?

@milankl
Copy link
Collaborator Author

milankl commented Sep 2, 2022

As far as I understand this, for an $2^n$-length vector only $n$ calls to sinpi are actually needed. generic_fft_pow2! currently performs $2n$.

@daanhb
Copy link
Member

daanhb commented Sep 3, 2022

Are the sinpi calls expensive? It seems like reducing the number of calls requires some analytical thinking here about the algorithm. Overall, anything could be put into a plan, as long as the plan itself does not become too large. FFTW precomputes lots of things. To get an idea, see their Documentation, for example The design and implementation of FFTW3 (link to pdf).

On topic, if the issue arises from the sinpi calls, then the current code of GenericFFT actually is type-stable?

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

No branches or pull requests

2 participants