-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Init working GPU_Plan based on package extension
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module NFFTGPUArraysExt | ||
|
||
using NFFT, NFFT.AbstractNFFTs | ||
using NFFT.SparseArrays, NFFT.LinearAlgebra, NFFT.FFTW | ||
using GPUArrays, Adapt | ||
|
||
include("implementation.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
mutable struct GPU_NFFTPlan{T,D, arrTc <: AbstractGPUArray{Complex{T}, D}, vecI <: AbstractGPUVector{Int32}, FP, BP, SM} <: AbstractNFFTPlan{T,D,1} | ||
N::NTuple{D,Int64} | ||
NOut::NTuple{1,Int64} | ||
J::Int64 | ||
k::Matrix{T} | ||
Ñ::NTuple{D,Int64} | ||
dims::UnitRange{Int64} | ||
params::NFFTParams{T} | ||
forwardFFT::FP | ||
backwardFFT::BP | ||
tmpVec::arrTc | ||
tmpVecHat::arrTc | ||
deconvolveIdx::vecI | ||
windowLinInterp::Vector{T} | ||
windowHatInvLUT::arrTc | ||
B::SM | ||
end | ||
|
||
function AbstractNFFTs.plan_nfft(arr::Type{<:AbstractGPUArray}, k::Matrix{T}, N::NTuple{D,Int}, rest...; | ||
timing::Union{Nothing,TimingStats} = nothing, kargs...) where {T,D} | ||
t = @elapsed begin | ||
p = GPU_NFFTPlan(arr, k, N, rest...; kargs...) | ||
end | ||
if timing != nothing | ||
timing.pre = t | ||
end | ||
return p | ||
end | ||
|
||
function GPU_NFFTPlan(arr, k::Matrix{T}, N::NTuple{D,Int}; dims::Union{Integer,UnitRange{Int64}}=1:D, | ||
fftflags=nothing, kwargs...) where {T,D} | ||
|
||
if dims != 1:D | ||
error("GPU NFFT does not work along directions right now!") | ||
end | ||
|
||
params, N, NOut, J, Ñ, dims_ = NFFT.initParams(k, N, dims; kwargs...) | ||
params.storeDeconvolutionIdx = true # GPU_NFFT only works this way | ||
params.precompute = NFFT.FULL # GPU_NFFT only works this way | ||
|
||
tmpVec = adapt(arr, zeros(Complex{T}, Ñ)) | ||
|
||
FP = plan_fft!(tmpVec, dims_) | ||
BP = plan_bfft!(tmpVec, dims_) | ||
|
||
windowLinInterp, windowPolyInterp, windowHatInvLUT, deconvolveIdx, B = NFFT.precomputation(k, N[dims_], Ñ[dims_], params) | ||
|
||
U = params.storeDeconvolutionIdx ? N : ntuple(d->0,D) | ||
tmpVecHat = adapt(arr, zeros(Complex{T}, U)) | ||
|
||
deconvIdx = adapt(arr, Int32.(deconvolveIdx)) | ||
winHatInvLUT = adapt(arr, windowHatInvLUT[1]) | ||
B_ = adapt(arr, Complex{T}.(B)) # Bit hacky | ||
|
||
GPU_NFFTPlan{T,D, typeof(tmpVec), typeof(deconvIdx), typeof(FP), typeof(BP), typeof(B_)}(N, NOut, J, k, Ñ, dims_, params, FP, BP, tmpVec, tmpVecHat, | ||
deconvIdx, windowLinInterp, winHatInvLUT, B_) | ||
end | ||
|
||
AbstractNFFTs.size_in(p::GPU_NFFTPlan) = p.N | ||
AbstractNFFTs.size_out(p::GPU_NFFTPlan) = p.NOut | ||
|
||
|
||
function AbstractNFFTs.convolve!(p::GPU_NFFTPlan{T,D, arrTc}, g::arrTc, fHat::arr) where {D,T,arr<: AbstractGPUArray, arrTc <: arr} | ||
mul!(fHat, transpose(p.B), vec(g)) | ||
return | ||
end | ||
|
||
function AbstractNFFTs.convolve_transpose!(p::GPU_NFFTPlan{T,D, arrTc}, fHat::arr, g::arrTc) where {D,T,arr<: AbstractGPUArray, arrTc <: arr} | ||
mul!(vec(g), p.B, fHat) | ||
return | ||
end | ||
|
||
function AbstractNFFTs.deconvolve!(p::GPU_NFFTPlan{T,D, arrTc}, f::arr, g::arrTc) where {D,T,arr<: AbstractGPUArray, arrTc <: arr} | ||
p.tmpVecHat[:] .= vec(f) .* p.windowHatInvLUT | ||
g[p.deconvolveIdx] = p.tmpVecHat | ||
return | ||
end | ||
|
||
function AbstractNFFTs.deconvolve_transpose!(p::GPU_NFFTPlan{T,D, arrTc}, g::arrTc, f::arr) where {D,T,arr<: AbstractGPUArray, arrTc <: arr} | ||
p.tmpVecHat[:] = g[p.deconvolveIdx] | ||
f[:] .= vec(p.tmpVecHat) .* p.windowHatInvLUT | ||
return | ||
end | ||
|
||
""" in-place NFFT on the GPU""" | ||
function LinearAlgebra.mul!(fHat::arr, p::GPU_NFFTPlan{T,D, arrT}, f::arr; | ||
verbose=false, timing::Union{Nothing,TimingStats} = nothing) where {T,D,arr<: AbstractGPUArray, arrT <: arr} | ||
NFFT.consistencyCheck(p, f, fHat) | ||
|
||
fill!(p.tmpVec, zero(Complex{T})) | ||
t1 = @elapsed @inbounds deconvolve!(p, f, p.tmpVec) | ||
t2 = @elapsed p.forwardFFT * p.tmpVec | ||
t3 = @elapsed @inbounds convolve!(p, p.tmpVec, fHat) | ||
if verbose | ||
@info "Timing: deconv=$t1 fft=$t2 conv=$t3" | ||
end | ||
if timing != nothing | ||
timing.conv = t3 | ||
timing.fft = t2 | ||
timing.deconv = t1 | ||
end | ||
|
||
return fHat | ||
end | ||
|
||
""" in-place adjoint NFFT on the GPU""" | ||
function LinearAlgebra.mul!(f::arr, pl::Adjoint{Complex{T},<:GPU_NFFTPlan{T,D, arrT}}, fHat::arr; | ||
verbose=false, timing::Union{Nothing,TimingStats} = nothing) where {T,D,arr<: AbstractGPUArray, arrT <: arr} | ||
p = pl.parent | ||
NFFT.consistencyCheck(p, f, fHat) | ||
|
||
t1 = @elapsed @inbounds convolve_transpose!(p, fHat, p.tmpVec) | ||
t2 = @elapsed p.backwardFFT * p.tmpVec | ||
t3 = @elapsed @inbounds deconvolve_transpose!(p, p.tmpVec, f) | ||
if verbose | ||
@info "Timing: conv=$t1 fft=$t2 deconv=$t3" | ||
end | ||
if timing != nothing | ||
timing.conv_adjoint = t1 | ||
timing.fft_adjoint = t2 | ||
timing.deconv_adjoint = t3 | ||
end | ||
|
||
return f | ||
end | ||
|