Skip to content

Commit a79e807

Browse files
wip
1 parent 78044e7 commit a79e807

File tree

2 files changed

+72
-24
lines changed

2 files changed

+72
-24
lines changed

demo.jl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ function test(mats)
2727
r_noncached = CPU1(Algorithm.FFT())
2828
for i in axes(mat, 3)
2929
frame = @view mat[:, :, i]
30+
@info "imfilter! noncached"
3031
imfilter!(r_noncached, f2, frame, kernel)
32+
@info "imfilter! cached"
3133
imfilter!(r_cached, f1, frame, kernel)
32-
@show f1[1:2, 1:2] f2[1:2, 1:2]
33-
all(f1 . f2) || error("f1 !≈ f2")
34+
@show f1[1:4] f2[1:4]
35+
f1 f2 || error("f1 !≈ f2")
3436
end
3537
return
3638
end
@@ -58,4 +60,20 @@ function profile()
5860
# GC.gc(true)
5961
end
6062

61-
profile()
63+
profile()
64+
65+
using ImageFiltering
66+
using ImageFiltering.RFFT
67+
68+
function mwe()
69+
a = rand(Float64, 10, 10)
70+
out1 = rfft(a)
71+
72+
buf = RFFT.RCpair{Float64}(undef, size(a))
73+
rfft_plan = RFFT.plan_rfft!(buf)
74+
copy!(buf, a)
75+
out2 = complex(rfft_plan(buf))
76+
77+
return out1 out2
78+
end
79+
mwe()

src/imfilter.jl

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -829,65 +829,95 @@ function _imfilter_fft!(r::AbstractCPU{FFT},
829829
Af = filtfft(A, krn, r.settings.plan1, r.settings.plan2, r.settings.plan3)
830830
if map(first, axes(out)) == map(first, axes(Af))
831831
R = CartesianIndices(axes(out))
832+
# @info "here" axes(out) axes(Af)
832833
copyto!(out, R, Af, R)
833834
else
834835
# Exploit the periodic boundary conditions of FFTView
835836
dest = FFTView(out)
836837
# src = OffsetArray(view(FFTView(Af), axes(dest)...), axes(dest))
837838
src = view(FFTView(Af), axes(dest)...)
839+
@info "there"
838840
copyto!(dest, src)
839841
end
840-
out
842+
return out
841843
end
842844

843845
function buffered_planned_rfft(a::AbstractArray{T}) where {T<:AbstractFloat}
844846
buf = RFFT.RCpair{T}(undef, size(a))
845-
plan = RFFT.plan_rfft!(buf)
846-
return function (a::AbstractArray)
847-
copy!(buf, OffsetArrays.no_offset_view(a))
847+
plan = RFFT.plan_rfft!(buf; flags=FFTW.MEASURE)
848+
return function (arr::AbstractArray{T}) where {T<:AbstractFloat}
849+
copy!(buf, FFTW.AbstractFFTs.to1(arr))
848850
return plan(buf)
849851
end
850852
end
851-
function buffered_planned_irfft(a::AbstractArray{T}) where {T<:AbstractFloat}
853+
function buffered_planned_irfft(a::AbstractArray{T}) where {T}
854+
# @show size(a)
852855
buf = RFFT.RCpair{T}(undef, size(a))
853-
plan = RFFT.plan_irfft!(buf)
854-
return function (a::AbstractArray)
855-
copy!(buf, a)
856+
plan = RFFT.plan_irfft!(buf; flags=FFTW.MEASURE)
857+
return function (arr::AbstractArray{T}) where {T<:Complex}
858+
# @info "inner" size(buf.R) size(buf.C) size(a)
859+
copy!(buf, FFTW.AbstractFFTs.to1(arr))
856860
return plan(buf)
857861
end
858862
end
859863

860864
function planned_fft(A::AbstractArray{T,N},
861-
kernel::Tuple{AbstractArray,Vararg{AbstractArray}},
862-
border::BorderSpecAny=Pad(:replicate)) where {T,N}
865+
kernel::Tuple{AbstractArray,Vararg{AbstractArray}},
866+
border::BorderSpecAny=Pad(:replicate)) where {T,N}
863867
bord = border(kernel, A, Algorithm.FFT())
864868
_A = padarray(T, A, bord)
869+
# @info "bfp1" size(_A)
865870
bfp1 = buffered_planned_rfft(_A)
866-
871+
# B = complex(bfp1(_A))
867872
kern = samedims(_A, kernelconv(kernel...))
868873
krn = FFTView(zeros(eltype(kern), map(length, axes(_A))))
874+
for I in CartesianIndices(axes(kern))
875+
krn[I] = kern[I]
876+
end
877+
# @info "bfp2" size(B)
878+
@show krn[1:3] sum(krn)
879+
@show size(_A) size(krn)
869880
bfp2 = buffered_planned_rfft(krn)
881+
# B .*= conj!(complex(bfp2(real(krn))))
882+
# @info "bfp3" size(B) size(krn) size(A) size(_A) size(conj!(_A))
870883
bfp3 = buffered_planned_irfft(_A)
884+
# @info "return"
871885
return Algorithm.FFT(bfp1, bfp2, bfp3)
872886
end
873887

874888
function filtfft(A, krn, planned_rfft1::Function, planned_rfft2::Function, planned_irfft::Function)
875-
B = complex(planned_rfft1(A)) * FFTW.AbstractFFTs.to1(A)
876-
_B = rfft(A)
877-
@show B[1:4] _B[1:4]
889+
B = complex(planned_rfft1(A))
890+
# @info "filtfft" size(A) size(FFTW.AbstractFFTs.to1(A)) size(B)
891+
# @info "filtfft planned" FFTW.AbstractFFTs.to1(A)[1:2] B[1:2] typeof(krn) axes(krn)
892+
@show krn[1:3] sum(krn)
893+
@info "bad one 1" complex(planned_rfft2(krn))[1:3]
878894
B .*= conj!(complex(planned_rfft2(krn)))
879-
out = irfft(B, length(axes(A, 1)))
880-
@show complex(B)[1:2,1:2] axes(complex(B)) out[1:2,1:2]
881-
return out
882-
# return real(planned_irfft(complex(B)))
895+
Af = real(planned_irfft(complex(B)))
896+
897+
#
898+
buf = RFFT.RCpair{Float64}(undef, size(krn))
899+
_rfft_plan = RFFT.plan_rfft!(buf)
900+
copy!(buf, FFTW.AbstractFFTs.to1(krn))
901+
_k = complex(_rfft_plan(buf))
902+
@info "bad one 2" _k[1:3]
903+
#
904+
905+
@info "filtfft planned" B[1:2] Af[1:2]
906+
# TODO: convert Af back to a padded view so that the center of the image is copied in _imfilter_fft!
907+
# @info "filtfft planned return" typeof(A) size(A) size(B) typeof(Af) size(Af)
908+
return Af
883909
end
884910
filtfft(A, krn, ::Nothing, ::Nothing, ::Nothing) = filtfft(A, krn)
885911
function filtfft(A, krn)
886912
B = rfft(A)
913+
# @info "filtfft unplanned" FFTW.AbstractFFTs.to1(A)[1:2] B[1:2] typeof(krn) axes(krn)
914+
# @show krn[1:3] sum(krn)
915+
@info "good one" rfft(krn)[1:3]
887916
B .*= conj!(rfft(krn))
888-
out = irfft(B, length(axes(A, 1)))
889-
@show B[1:2,1:2] axes(B) out[1:2,1:2]
890-
return out
917+
Af = irfft(B, length(axes(A, 1)))
918+
@info "filtfft unplanned" B[1:2] Af[1:2]
919+
# @info "filtfft unplanned return" typeof(A) size(A) size(B) typeof(Af) size(Af)
920+
return Af
891921
end
892922
function filtfft(A::AbstractArray{C}, krn) where {C<:Colorant}
893923
Av, dims = channelview_dims(A)

0 commit comments

Comments
 (0)