Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/src/literate/howtos/gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ LinearOperators.storage_type(fop) == typeof(storage)
# Which backends are supported depends on the operator and the target. The following operators implement Adapt:
# * `FFTOp`, `NFFTOp` (including its Toeplitz normal operator) and `RadonOp`
# * `GradientOp`, `SamplingOp` and `WeightingOp`
# * `WaveletOp`. Its transform runs on the CPU; on another backend, input and output are copied through host arrays.
# * `DiagOp`, `ProdOp`, `RowBlockOp` and `NormalOp`, and the normal operators of `DiagOp`, `ProdOp` and `RowBlockOp`. These are built from other operators and adapt them, so the target must also be supported by each of those operators.
# Other operators, such as `WaveletOp`, `DCTOp` and `DSTOp`, do not implement Adapt.
# Other operators, such as `DCTOp` and `DSTOp`, do not implement Adapt.

# The target is the backing array type without element-type parameters, such as `Array`, `CuArray` or `ROCArray`.
# LinearOperatorCollection does not support element-typed targets such as `Array{ComplexF64}` or `CuArray{Float32}`, even when the element type matches the operator.
Expand Down
1 change: 1 addition & 0 deletions ext/LinearOperatorWaveletExt/LinearOperatorWaveletExt.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module LinearOperatorWaveletExt

using LinearOperatorCollection, Wavelets
using Adapt

include("WaveletOp.jl")

Expand Down
46 changes: 42 additions & 4 deletions ext/LinearOperatorWaveletExt/WaveletOp.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@

"""
WaveletOp(shape, wt=wavelet(WT.db2))
WaveletOp(T::Type; shape::Tuple, wt=wavelet(WT.db2), S = Vector{T})

returns a `ẀaveletOp <: AbstractLinearOperator`, which performs a Wavelet transform on
returns a `WaveletOp <: AbstractLinearOperator`, which performs a Wavelet transform on
a given input array.

`Adapt.adapt(target, op)` and `copy(op)` rebuild the operator, with new temporary arrays,
from its shape and wavelet. The transform always runs on the CPU: with another storage type
than `Vector`, input and output are copied through host arrays. See
[Moving an Operator to Another Backend](@ref) for the supported targets.

# Arguments

* `shape` - size of the Array to transform
* (`wt=wavelet(WT.db2)`) - Wavelet to apply
* (`S = Vector{T}`) - storage type of the operator
"""
function LinearOperatorCollection.WaveletOp(::Type{T}; shape::Tuple, wt=wavelet(WT.db2), S = Vector{T}) where T <: Number
shape = filter(x-> x != 1, shape) # Drop dimension with 1
shape = map(Int, filter(x-> x != 1, shape)) # Drop dimension with 1
tmp = Array{T}(undef, shape...)
tmpRes = Array{T}(undef, shape...)
return LinearOperator(T, prod(shape), prod(shape), false, false
op = LinearOperator(T, prod(shape), prod(shape), false, false
, (res,x)->prodwt!(res, x, wt, tmp, tmpRes)
, nothing
, (res,x)->ctprodwt!(res, x, wt, tmp, tmpRes), S = S)
return WaveletOpImpl(op, shape, wt, tmp, tmpRes)
end

struct WaveletOpImpl{T, vecT <: AbstractVector, O <: AbstractLinearOperator{T}, N, W} <: WaveletOp{T}
op::O
# shape without the dimensions of size 1
shape::NTuple{N, Int}
wt::W
# host arrays the transform reads from and writes to
tmp::Array{T, N}
tmpRes::Array{T, N}
function WaveletOpImpl(op::O, shape::NTuple{N, Int}, wt::W, tmp::Array{T, N}, tmpRes::Array{T, N}) where {T, O <: AbstractLinearOperator{T}, N, W}
return new{T, LinearOperators.storage_type(op), O, N, W}(op, shape, wt, tmp, tmpRes)
end
end

function Base.getproperty(op::WaveletOpImpl, field::Symbol)
if in(field, (:op, :shape, :wt, :tmp, :tmpRes))
return getfield(op, field)
else
return getproperty(getfield(op, :op), field)
end
end
Base.setproperty!(op::WaveletOpImpl, field::Symbol, value) = setproperty!(op.op, field, value)

LinearOperators.storage_type(::WaveletOpImpl{T, vecT}) where {T, vecT} = vecT

Base.copy(op::WaveletOpImpl) = WaveletOp(eltype(op); shape = op.shape, wt = op.wt, S = LinearOperators.storage_type(op))

function Adapt.adapt_structure(to, op::WaveletOpImpl)
target_storage = LinearOperatorCollection._adapt_target_storage(to, op.Mv, "WaveletOp")
return WaveletOp(eltype(op); shape = op.shape, wt = op.wt, S = typeof(target_storage))
end

prodwt!(res::Vector{T}, x::Vector{T}, wt, tmp::Array{T, D}, tmpRes::Array{T, D}) where {T, D} = dwt!(reshape(res,size(tmpRes)), reshape(x,size(tmpRes)), wt)
Expand Down
7 changes: 5 additions & 2 deletions test/gpu/metal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ using NFFT
using RadonKA

# Metal has no Float64, so runtests.jl cannot run here. This runs the Float32 and ComplexF32
# nested operators, including SamplingOp ∘ FFTOp, moved to MtlArray and compared with the CPU.
# nested operators, including SamplingOp ∘ FFTOp, and a Float32 WaveletOp moved to MtlArray
# and compared with the CPU.

# Load the test helpers without the testsets of the file
# Load the test helpers without the testsets of the files
include(ex -> Meta.isexpr(ex, :macrocall) ? nothing : ex, joinpath(@__DIR__(), "..", "testAdapt.jl"))
include(ex -> Meta.isexpr(ex, :macrocall) ? nothing : ex, joinpath(@__DIR__(), "..", "testOperators.jl"))

@testset "LinearOperatorCollection on Metal" begin
N = 16
@testset "ProdOp, NormalOpImpl, and ProdNormalOp recurse" check_nested_prod_adapt(MtlArray, N)
@testset "SamplingOp ∘ FFTOp" check_sampling_fft_adapt(MtlArray, N)
@testset "WaveletOp" testWaveletAdapt(64, 60, Float32; arrayType = MtlArray)
end
27 changes: 27 additions & 0 deletions test/testOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,33 @@ function testWavelet(M=64,N=60;arrayType = Array)
x_reco = reshape( adjoint(WOp)*x_wavelet, M, N)

@test norm(x_reco - x) / norm(x) ≈ 0 atol=0.001

testWaveletAdapt(M, N, Float64; arrayType)
end

# Adapt and copy rebuild the shape without singleton dimensions, the wavelet and the host arrays
function testWaveletAdapt(M, N, T; arrayType = Array)
x = rand(T, M*N)
source = WaveletOp(T, shape=(M,1,N), wt=wavelet(WT.haar))
@test createLinearOperator(WaveletOp{T}; shape=(M,N)) isa WaveletOp{T}
y = source*x
z = adjoint(source)*y
tmp = copy(rand!(source.tmp))
target = Adapt.adapt(arrayType, source)
copied = copy(target)
back = Adapt.adapt(Array, copied)
@test source.tmp == tmp
for (op, S) in ((target, arrayType), (copied, arrayType), (source, Array), (back, Array), (target, arrayType))
check_storage(op, S)
@test op isa WaveletOp{T}
@test size(op) == (M*N, M*N) && op.shape == (M,N) && op.wt === source.wt
@test Array(op*S(x)) ≈ y
@test Array(adjoint(op)*S(y)) ≈ z
end
ops = (source, target, copied, back)
for field in (:op, :tmp, :tmpRes)
@test allunique(map(op -> objectid(getproperty(op, field)), ops))
end
end

# test FourierOperators
Expand Down
Loading