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

Enable scalar/broadcast operation for LazyPropagation #167

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/TimeModeling/LinearOperators/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ end
size(jA::jAdjoint) = (jA.op.n, jA.op.m)
display(P::jAdjoint) = println("Adjoint($(P.op))")
display(P::judiProjection{D}) where D = println("JUDI projection operator $(repr(P.n)) -> $(repr(P.m))")
display(P::judiWavelet{T}) where T = println("JUDI wavelet")

############################################################################################################################
# Indexing
Expand Down
2 changes: 2 additions & 0 deletions src/TimeModeling/Types/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ vec(x::judiMultiSourceVector) = vcat(vec.(x.data)...)

time_sampling(ms::judiMultiSourceVector) = [1 for i=1:ms.nsrc]

reshape(ms::judiMultiSourceVector, dims::Dims{1}) = ms ### during AD, size(ms::judiVector) = ms.nsrc
reshape(ms::judiMultiSourceVector, dims::Dims{N}) where N = reshape(vec(ms), dims)

############################################################################################################################
# Linear algebra `*`
(msv::judiMultiSourceVector{mT})(x::AbstractVector{T}) where {mT, T<:Number} = x
Expand Down
32 changes: 28 additions & 4 deletions src/rrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ Parameters
* `F`: the JUDI propgator
* `q`: The source to compute F*q
"""
struct LazyPropagation
mutable struct LazyPropagation
post::Function
F::judiPropagator
q
val # store F * q
end

eval_prop(F::LazyPropagation) = F.post(F.F * F.q)
function eval_prop(F::LazyPropagation)
isnothing(F.val) && (F.val = F.F * F.q)
return F.post(F.val)
end
Base.collect(F::LazyPropagation) = eval_prop(F)
LazyPropagation(F::judiPropagator, q) = LazyPropagation(identity, F, q)
LazyPropagation(post::Function, F::judiPropagator, q) = LazyPropagation(post, F, q, nothing)
LazyPropagation(F::judiPropagator, q) = LazyPropagation(identity, F, q, nothing)

# Only a few arithmetic operation are supported

Expand All @@ -45,10 +50,29 @@ for op in [:+, :-, :*, :/]
end
end

for op in [:*, :/]
@eval begin
$(op)(F::LazyPropagation, y::T) where T <: Number = LazyPropagation(F.post, F.F, $(op)(F.q, y), isnothing(F.val) ? nothing : $(op)(F.val, y))
$(op)(y::T, F::LazyPropagation) where T <: Number = LazyPropagation(F.post, F.F, $(op)(y, F.q), isnothing(F.val) ? nothing : $(op)(y, F.val))
broadcasted(::typeof($op), F::LazyPropagation, y::T) where T <: Number = LazyPropagation(F.post, F.F, broadcasted($(op), F.q, y), isnothing(F.val) ? nothing : broadcasted($(op), F.val, y))
broadcasted(::typeof($op), y::T, F::LazyPropagation) where T <: Number = LazyPropagation(F.post, F.F, broadcasted($(op), y, F.q), isnothing(F.val) ? nothing : broadcasted($(op), y, F.val))
end
end

for op in [:+, :-]
@eval begin
$(op)(F::LazyPropagation, y::T) where T <: Number = $(op)(eval_prop(F), y)
$(op)(y::T, F::LazyPropagation) where T <: Number = $(op)(y, eval_prop(F))
broadcasted(::typeof($op), F::LazyPropagation, y::T) where T <: Number = broadcasted($(op), eval_prop(F), y)
broadcasted(::typeof($op), y::T, F::LazyPropagation) where T <: Number = broadcasted($(op), y, eval_prop(F))
end
end

broadcasted(::typeof(^), y::LazyPropagation, p::Real) = eval_prop(y).^(p)
*(F::judiPropagator, q::LazyPropagation) = F*eval_prop(q)

reshape(F::LazyPropagation, dims...) = LazyPropagation(x->reshape(x, dims...), F.F, Q.q)
reshape(F::LazyPropagation, dims...) = LazyPropagation(x->reshape(x, dims...), F.F, F.q, F.val)
vec(F::LazyPropagation) = LazyPropagation(vec, F.F, F.q, F.val)
copyto!(x::AbstractArray, F::LazyPropagation) = copyto!(x, eval_prop(F))
dot(x::AbstractArray, F::LazyPropagation) = dot(x, eval_prop(F))
dot(F::LazyPropagation, x::AbstractArray) = dot(x, F)
Expand Down
2 changes: 1 addition & 1 deletion test/test_rrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ perturb(x::judiVector) = judiVector(x.geometry, [randx(x.data[i]) for i=1:x.nsrc
reverse(x::judiVector) = judiVector(x.geometry, [x.data[i][end:-1:1, :] for i=1:x.nsrc])

misfit_objective_2p(d_obs, q0, m0, F) = .5f0*norm(F(m0, q0) - d_obs)^2
misfit_objective_1p(d_obs, q0, m0, F) = .5f0*norm(F(m0)*q0 - d_obs)^2
misfit_objective_1p(d_obs, q0, m0, F) = .5f0*norm(F(1f0.*m0)*q0 - d_obs)^2

function loss(misfit, d_obs, q0, m0, F)
local ϕ
Expand Down