Skip to content

Large Memory allocation for fft applied on real valued array #194

@roflmaostc

Description

@roflmaostc

Hey,

I was wondering why the memory usage of fft is high for a real array.
I suspect that x needs to be converted to a complex array which gives extra 2MB. For the output we need another 2MB, so totally 4MB.
However, couldn't we technically use an in-place fft! for the temporary complex version of x because we can't access it afterwards?

julia> using FFTW, BenchmarkTools

julia> x = randn((256, 512));

julia> varinfo(r"x")
  name      size summary                
  –––– ––––––––– –––––––––––––––––––––––
  x    1.000 MiB 256×512 Matrix{Float64}

julia> x_c = Complex.(x);

julia> @btime fft($x);
  2.275 ms (35 allocations: 4.00 MiB)

julia> @btime fft($x_c);
  2.126 ms (33 allocations: 2.00 MiB)

So why we don't fix that similar to that one:

julia> my_fft(x::AbstractArray{<:Real, N} where N) = fft!(Complex.(x));

julia> @btime my_fft($x);
  2.111 ms (33 allocations: 2.00 MiB)

julia> my_fft(x) == fft(x)
true

I was trying to find some information on that but wasn't successful, so I'm sorry in case I missed something!

Thanks,

Felix

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions