From bbf1a6d183dd2a6d4da2fac88019387df8f36c62 Mon Sep 17 00:00:00 2001 From: Sungho Shin Date: Sun, 30 Jul 2023 17:27:23 -0500 Subject: [PATCH] changed some variable names --- ext/SIMDiffKernelAbstractions.jl | 8 ++-- src/SIMDiff.jl | 4 +- src/dual.jl | 29 ------------- src/function.jl | 4 +- src/gradient.jl | 20 ++++----- src/graph.jl | 70 ++++++++++++++++++++++++++++++- src/hessian.jl | 72 ++++++++++++++++---------------- src/jacobian.jl | 10 ++--- src/register.jl | 36 ++++++++-------- src/triple.jl | 33 --------------- 10 files changed, 144 insertions(+), 142 deletions(-) delete mode 100644 src/dual.jl delete mode 100644 src/triple.jl diff --git a/ext/SIMDiffKernelAbstractions.jl b/ext/SIMDiffKernelAbstractions.jl index 6dbf513e..620c1be0 100644 --- a/ext/SIMDiffKernelAbstractions.jl +++ b/ext/SIMDiffKernelAbstractions.jl @@ -250,22 +250,22 @@ end @kernel function kerh(y1,y2,@Const(f),@Const(itr),@Const(x),@Const(adj1),@Const(adj2)) I = @index(Global) - SIMDiff.hrpass0(f.f(itr[I], SIMDiff.TripleSource(x)), f.comp2, y1, y2, SIMDiff.offset2(f,I), 0, adj1, adj2) + SIMDiff.hrpass0(f.f(itr[I], SIMDiff.SecondAdjointNodeSource(x)), f.comp2, y1, y2, SIMDiff.offset2(f,I), 0, adj1, adj2) end @kernel function kerh2(y1,y2,@Const(f),@Const(itr),@Const(x),@Const(adjs1),@Const(adj2)) I = @index(Global) - SIMDiff.hrpass0(f.f(itr[I], SIMDiff.TripleSource(x)), f.comp2, y1, y2, SIMDiff.offset2(f,I), 0, adjs1[SIMDiff.offset0(f,itr,I)], adj2) + SIMDiff.hrpass0(f.f(itr[I], SIMDiff.SecondAdjointNodeSource(x)), f.comp2, y1, y2, SIMDiff.offset2(f,I), 0, adjs1[SIMDiff.offset0(f,itr,I)], adj2) end @kernel function kerj(y1,y2,@Const(f),@Const(itr),@Const(x),@Const(adj)) I = @index(Global) - SIMDiff.jrpass(f.f(itr[I], SIMDiff.DualSource(x)), f.comp1, SIMDiff.offset0(f,itr,I), y1, y2, SIMDiff.offset1(f,I), 0, adj) + SIMDiff.jrpass(f.f(itr[I], SIMDiff.AdjointNodeSource(x)), f.comp1, SIMDiff.offset0(f,itr,I), y1, y2, SIMDiff.offset1(f,I), 0, adj) end @kernel function kerg(y,@Const(f),@Const(itr),@Const(x),@Const(adj)) I = @index(Global) - SIMDiff.grpass(f.f(itr[I], SIMDiff.DualSource(x)), f.comp1, y, SIMDiff.offset1(f,I), 0, adj) + SIMDiff.grpass(f.f(itr[I], SIMDiff.AdjointNodeSource(x)), f.comp1, y, SIMDiff.offset1(f,I), 0, adj) end @kernel function kerf(y,@Const(f),@Const(itr),@Const(x)) diff --git a/src/SIMDiff.jl b/src/SIMDiff.jl index eb47d332..b84dda14 100644 --- a/src/SIMDiff.jl +++ b/src/SIMDiff.jl @@ -3,8 +3,6 @@ module SIMDiff import NLPModels include("graph.jl") -include("dual.jl") -include("triple.jl") include("register.jl") include("functionlist.jl") include("function.jl") @@ -14,6 +12,6 @@ include("hessian.jl") include("nlp.jl") include("templates.jl") -export data, variable, objective, constraint, constraint!, WrapperModel +export data, variable, objective, constraint, constraint! end # module SIMDiffes diff --git a/src/dual.jl b/src/dual.jl deleted file mode 100644 index 59e39de5..00000000 --- a/src/dual.jl +++ /dev/null @@ -1,29 +0,0 @@ -struct Dual1{F, T, I} <: AbstractDual - x::T - y::T - inner::I -end -struct Dual2{F, T, I1,I2} <: AbstractDual - x::T - y1::T - y2::T - inner1::I1 - inner2::I2 -end -struct DualVar{I,T} <: AbstractDual - i::I - x::T -end -struct DualSource{T, VT <: AbstractVector{T}} - inner::VT -end -struct DualNullSource end - -@inline Dual1(f::F,x::T,y,inner::I) where {F,T,I} = Dual1{F,T,I}(x,y,inner) -@inline Dual2(f::F,x::T,y1,y2,inner1::I1,inner2::I2) where {F,T,I1,I2} = Dual2{F,T,I1,I2}(x,y1,y2,inner1,inner2) - - -DualSource(::Nothing) = DualNullSource() - -@inbounds @inline Base.getindex(x::I,i) where I <: DualNullSource = DualVar(i,NaN16) -@inbounds @inline Base.getindex(x::I,i) where I <: DualSource = DualVar(i, x.inner[i]) diff --git a/src/function.jl b/src/function.jl index 6e6addc8..79db264e 100644 --- a/src/function.jl +++ b/src/function.jl @@ -19,11 +19,11 @@ function Func(gen::Base.Generator, o0=0, o1=0, o2=0) f = gen.f(p) - d = f(Identity(),DualSource(NaNSource{Float16}())) + d = f(Identity(),AdjointNodeSource(NaNSource{Float16}())) y1 = [] SIMDiff.grpass(d,nothing,y1,nothing,0,NaN16) - t = f(Identity(),TripleSource(NaNSource{Float16}())) + t = f(Identity(),SecondAdjointNodeSource(NaNSource{Float16}())) y2 = [] SIMDiff.hrpass0(t,nothing,y2,nothing,nothing,0,NaN16,NaN16) diff --git a/src/gradient.jl b/src/gradient.jl index cdbf82b4..e24afca3 100644 --- a/src/gradient.jl +++ b/src/gradient.jl @@ -1,13 +1,13 @@ -@inbounds @inline function drpass(d::D, y, adj) where D <: Dual1 +@inbounds @inline function drpass(d::D, y, adj) where D <: AdjointNode1 offset = drpass(d.inner, y, adj * d.y) nothing end -@inbounds @inline function drpass(d::D, y, adj) where D <: Dual2 +@inbounds @inline function drpass(d::D, y, adj) where D <: AdjointNode2 offset = drpass(d.inner1, y, adj * d.y1) offset = drpass(d.inner2, y, adj * d.y2) nothing end -@inbounds @inline function drpass(d::D, y, adj) where D <: DualVar +@inbounds @inline function drpass(d::D, y, adj) where D <: AdjointNodeVar y[d.i] += adj nothing end @@ -15,31 +15,31 @@ end end function gradient!(y,f,x, adj) @simd for k in eachindex(f.itr) - drpass(f.f.f(f.itr[k],DualSource(x)), y, adj) + drpass(f.f.f(f.itr[k],AdjointNodeSource(x)), y, adj) end return y end -@inbounds @inline function grpass(d::D, comp, y, o1, cnt, adj) where D <: Dual1 +@inbounds @inline function grpass(d::D, comp, y, o1, cnt, adj) where D <: AdjointNode1 cnt = grpass(d.inner, comp, y, o1, cnt, adj * d.y) return cnt end -@inbounds @inline function grpass(d::D, comp, y, o1, cnt, adj) where D <: Dual2 +@inbounds @inline function grpass(d::D, comp, y, o1, cnt, adj) where D <: AdjointNode2 cnt = grpass(d.inner1, comp, y, o1, cnt, adj * d.y1) cnt = grpass(d.inner2, comp, y, o1, cnt, adj * d.y2) return cnt end -@inbounds @inline function grpass(d::D, comp, y, o1, cnt, adj) where D <: DualVar +@inbounds @inline function grpass(d::D, comp, y, o1, cnt, adj) where D <: AdjointNodeVar y[o1 + comp(cnt += 1)] += adj return cnt end -@inbounds @inline function grpass(d::DualVar, comp::Nothing, y, o1, cnt, adj) # despecialization +@inbounds @inline function grpass(d::AdjointNodeVar, comp::Nothing, y, o1, cnt, adj) # despecialization push!(y,d.i) return (cnt += 1) end -@inbounds @inline function grpass(d::D, comp, y::V, o1, cnt, adj) where {D <: DualVar, V <: AbstractVector{Tuple{Int,Int}}} +@inbounds @inline function grpass(d::D, comp, y::V, o1, cnt, adj) where {D <: AdjointNodeVar, V <: AbstractVector{Tuple{Int,Int}}} ind = o1 + comp(cnt += 1) y[ind] = (d.i,ind) return cnt @@ -47,7 +47,7 @@ end function sgradient!(y,f,x, adj) @simd for k in eachindex(f.itr) - grpass(f.f.f(f.itr[k], DualSource(x)), f.itr.comp1, y, offset1(f,k), 0, adj) + grpass(f.f.f(f.itr[k], AdjointNodeSource(x)), f.itr.comp1, y, offset1(f,k), 0, adj) end return y end diff --git a/src/graph.jl b/src/graph.jl index 6f57b2b6..22c224c4 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -1,8 +1,8 @@ abstract type AbstractIndex end abstract type AbstractNode end abstract type AbstractPar <: AbstractNode end -abstract type AbstractDual end -abstract type AbstractTriple end +abstract type AbstractAdjointNode end +abstract type AbstractSecondAdjointNode end struct Var{I} <: AbstractNode i::I @@ -58,3 +58,69 @@ struct NaNSource{T} <: AbstractVector{T} end @inbounds (v::ParIndexed)(i::Identity,x) = NaN16 # despecialized @inbounds (v::Par)(i::Identity,x) = NaN16 # despecialized @inbounds (v::Var)(i::Identity,x) = x[v.i] # despecialized + + +struct AdjointNode1{F, T, I} <: AbstractAdjointNode + x::T + y::T + inner::I +end +struct AdjointNode2{F, T, I1,I2} <: AbstractAdjointNode + x::T + y1::T + y2::T + inner1::I1 + inner2::I2 +end +struct AdjointNodeVar{I,T} <: AbstractAdjointNode + i::I + x::T +end +struct AdjointNodeSource{T, VT <: AbstractVector{T}} + inner::VT +end +struct AdjointNodeNullSource end + +@inline AdjointNode1(f::F,x::T,y,inner::I) where {F,T,I} = AdjointNode1{F,T,I}(x,y,inner) +@inline AdjointNode2(f::F,x::T,y1,y2,inner1::I1,inner2::I2) where {F,T,I1,I2} = AdjointNode2{F,T,I1,I2}(x,y1,y2,inner1,inner2) + + +AdjointNodeSource(::Nothing) = AdjointNodeNullSource() + +@inbounds @inline Base.getindex(x::I,i) where I <: AdjointNodeNullSource = AdjointNodeVar(i,NaN16) +@inbounds @inline Base.getindex(x::I,i) where I <: AdjointNodeSource = AdjointNodeVar(i, x.inner[i]) + + +struct SecondAdjointNode1{F, T, I} <: AbstractSecondAdjointNode + x::T + y::T + h::T + inner::I +end +struct SecondAdjointNode2{F, T, I1,I2} <: AbstractSecondAdjointNode + x::T + y1::T + y2::T + h11::T + h12::T + h22::T + inner1::I1 + inner2::I2 +end + +struct SecondAdjointNodeVar{I,T} <: AbstractSecondAdjointNode + i::I + x::T +end +struct SecondAdjointNodeSource{T, VT <: AbstractVector{T}} + inner::VT +end + +@inline SecondAdjointNode1(f::F,x::T,y,h,inner::I) where {F,T,I} = SecondAdjointNode1{F,T,I}(x,y,h,inner) +@inline SecondAdjointNode2(f::F,x::T,y1,y2,h11,h12,h22,inner1::I1,inner2::I2) where {F,T,I1,I2} = SecondAdjointNode2{F,T,I1,I2}(x,y1,y2,h11,h12,h22,inner1,inner2) + +struct SecondAdjointNodeNullSource end +SecondAdjointNodeSource(::Nothing) = SecondAdjointNodeNullSource() + +@inbounds @inline Base.getindex(x::I,i) where I <: SecondAdjointNodeNullSource = SecondAdjointNodeVar(i,NaN) +@inbounds @inline Base.getindex(x::I,i) where I <: SecondAdjointNodeSource = SecondAdjointNodeVar(i, x.inner[i]) diff --git a/src/hessian.jl b/src/hessian.jl index d3d7fb11..a756ac6a 100644 --- a/src/hessian.jl +++ b/src/hessian.jl @@ -1,41 +1,41 @@ -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: Triple1, T2 <: Triple1} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNode1, T2 <: SecondAdjointNode1} cnt =hdrpass(t1.inner,t2.inner, comp, y1,y2, o2, cnt, adj * t1.y * t2.y ) cnt end -@inbounds function hdrpass(t1::Triple1, t2::Triple1, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNode1, t2::SecondAdjointNode1, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1.inner,t2.inner, comp, y1,y2, o2, cnt, adj * t1.y * t2.y ) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: TripleVar, T2 <: Triple1} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNodeVar, T2 <: SecondAdjointNode1} cnt =hdrpass(t1,t2.inner, comp, y1,y2, o2, cnt, adj * t2.y ) cnt end -@inbounds function hdrpass(t1::TripleVar, t2::Triple1, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNodeVar, t2::SecondAdjointNode1, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1,t2.inner, comp, y1,y2, o2, cnt, adj * t2.y ) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: Triple1, T2 <: TripleVar} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNode1, T2 <: SecondAdjointNodeVar} cnt =hdrpass(t1.inner,t2, comp, y1,y2, o2, cnt, adj * t1.y ) cnt end -@inbounds function hdrpass(t1::Triple1, t2::TripleVar, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNode1, t2::SecondAdjointNodeVar, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1.inner,t2, comp, y1,y2, o2, cnt, adj * t1.y ) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: Triple2, T2 <: Triple2} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNode2, T2 <: SecondAdjointNode2} cnt =hdrpass(t1.inner1, t2.inner1, comp, y1,y2, o2, cnt, adj * t1.y1 * t2.y1 ) cnt =hdrpass(t1.inner1, t2.inner2, comp, y1,y2, o2, cnt, adj * t1.y1 * t2.y2 ) cnt =hdrpass(t1.inner2, t2.inner1, comp, y1,y2, o2, cnt, adj * t1.y2 * t2.y1 ) cnt =hdrpass(t1.inner2, t2.inner2, comp, y1,y2, o2, cnt, adj * t1.y2 * t2.y2 ) cnt end -@inbounds function hdrpass(t1::Triple2, t2::Triple2, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNode2, t2::SecondAdjointNode2, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1.inner1, t2.inner1, comp, y1,y2, o2, cnt, adj * t1.y1 * t2.y1 ) cnt =hdrpass(t1.inner1, t2.inner2, comp, y1,y2, o2, cnt, adj * t1.y1 * t2.y2 ) cnt =hdrpass(t1.inner2, t2.inner1, comp, y1,y2, o2, cnt, adj * t1.y2 * t2.y1 ) @@ -44,51 +44,51 @@ end end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: Triple1, T2 <: Triple2} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNode1, T2 <: SecondAdjointNode2} cnt =hdrpass(t1.inner,t2.inner1, comp, y1,y2, o2, cnt, adj * t1.y * t2.y1 ) cnt =hdrpass(t1.inner,t2.inner2, comp, y1,y2, o2, cnt, adj * t1.y * t2.y2 ) cnt end -@inbounds function hdrpass(t1::Triple1, t2::Triple2, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNode1, t2::SecondAdjointNode2, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1.inner,t2.inner1, comp, y1,y2, o2, cnt, adj * t1.y * t2.y1 ) cnt =hdrpass(t1.inner,t2.inner2, comp, y1,y2, o2, cnt, adj * t1.y * t2.y2 ) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: Triple2, T2 <: Triple1} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNode2, T2 <: SecondAdjointNode1} cnt =hdrpass(t1.inner1, t2.inner, comp, y1,y2, o2, cnt, adj * t1.y1 * t2.y ) cnt =hdrpass(t1.inner2, t2.inner, comp, y1,y2, o2, cnt, adj * t1.y2 * t2.y ) cnt end -@inbounds function hdrpass(t1::Triple2, t2::Triple1, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNode2, t2::SecondAdjointNode1, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1.inner1, t2.inner, comp, y1,y2, o2, cnt, adj * t1.y1 * t2.y ) cnt =hdrpass(t1.inner2, t2.inner, comp, y1,y2, o2, cnt, adj * t1.y2 * t2.y ) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: TripleVar, T2 <: Triple2} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNodeVar, T2 <: SecondAdjointNode2} cnt =hdrpass(t1,t2.inner1, comp, y1,y2, o2, cnt, adj * t2.y1 ) cnt =hdrpass(t1,t2.inner2, comp, y1,y2, o2, cnt, adj * t2.y2 ) cnt end -@inbounds function hdrpass(t1::TripleVar, t2::Triple2, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNodeVar, t2::SecondAdjointNode2, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1,t2.inner1, comp, y1,y2, o2, cnt, adj * t2.y1 ) cnt =hdrpass(t1,t2.inner2, comp, y1,y2, o2, cnt, adj * t2.y2 ) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: Triple2, T2 <: TripleVar} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNode2, T2 <: SecondAdjointNodeVar} cnt =hdrpass(t1.inner1, t2, comp, y1,y2, o2, cnt, adj * t1.y1) cnt =hdrpass(t1.inner2, t2, comp, y1,y2, o2, cnt, adj * t1.y2) cnt end -@inbounds function hdrpass(t1::Triple2, t2::TripleVar, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized +@inbounds function hdrpass(t1::SecondAdjointNode2, t2::SecondAdjointNodeVar, comp::Nothing, y1,y2, o2, cnt, adj) # despecialized cnt =hdrpass(t1.inner1, t2, comp, y1,y2, o2, cnt, adj * t1.y1) cnt =hdrpass(t1.inner2, t2, comp, y1,y2, o2, cnt, adj * t1.y2) cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: TripleVar, T2 <: TripleVar} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1,y2, o2, cnt, adj) where {T1 <: SecondAdjointNodeVar, T2 <: SecondAdjointNodeVar} i,j = t1.i, t2.i if i == j y1[o2 + comp(cnt += 1)] += 2*adj @@ -98,11 +98,11 @@ end cnt end -@inbounds @inline function hrpass(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: Triple1 +@inbounds @inline function hrpass(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: SecondAdjointNode1 cnt =hrpass(t.inner, comp, y1,y2, o2, cnt, adj * t.y, adj2 * (t.y)^2 + adj * t.h) cnt end -@inbounds @inline function hrpass(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: Triple2 +@inbounds @inline function hrpass(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: SecondAdjointNode2 adj2y1y2 = adj2 * t.y1 * t.y2 adjh12 = adj * t.h12 cnt =hrpass(t.inner1, comp, y1,y2, o2, cnt, adj * t.y1, adj2 * (t.y1)^2 + adj * t.h11) @@ -116,80 +116,80 @@ end @inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where { N <: Union{FirstFixed{typeof(*)},SecondFixed{typeof(*)}}, - D <: Triple1{N} + D <: SecondAdjointNode1{N} } cnt =hrpass0(t.inner, comp, y1,y2, o2, cnt, adj * t.y, adj2 * (t.y)^2) cnt end @inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where { N <: Union{FirstFixed{typeof(+)},SecondFixed{typeof(+)}}, - D <: Triple1{N} + D <: SecondAdjointNode1{N} } cnt =hrpass0(t.inner, comp, y1,y2, o2, cnt, adj, adj2) cnt end @inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where { - D <: Triple1{FirstFixed{typeof(-)}} + D <: SecondAdjointNode1{FirstFixed{typeof(-)}} } cnt =hrpass0(t.inner, comp, y1,y2, o2, cnt,-adj, adj2) cnt end @inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where { - D <: Triple1{SecondFixed{typeof(-)}} + D <: SecondAdjointNode1{SecondFixed{typeof(-)}} } cnt =hrpass0(t.inner, comp, y1,y2, o2, cnt, adj, adj2) cnt end -@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: Triple1{typeof(+)} +@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: SecondAdjointNode1{typeof(+)} cnt =hrpass0(t.inner, comp, y1,y2, o2, cnt, adj, adj2) cnt end -@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: Triple1{typeof(-)} +@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: SecondAdjointNode1{typeof(-)} cnt =hrpass0(t.inner, comp, y1,y2, o2, cnt,-adj, adj2) cnt end -@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: Triple2{typeof(+)} +@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: SecondAdjointNode2{typeof(+)} cnt =hrpass0(t.inner1, comp, y1,y2, o2, cnt, adj, adj2) cnt =hrpass0(t.inner2, comp, y1,y2, o2, cnt, adj, adj2) cnt end -@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: Triple2{typeof(-)} +@inbounds @inline function hrpass0(t::D, comp, y1,y2, o2, cnt, adj, adj2) where D <: SecondAdjointNode2{typeof(-)} cnt =hrpass0(t.inner1, comp, y1,y2, o2, cnt, adj, adj2) cnt =hrpass0(t.inner2, comp, y1,y2, o2, cnt,-adj, adj2) cnt end -@inbounds @inline function hrpass0(t::T, comp, y1,y2, o2, cnt, adj, adj2) where T <: TripleVar +@inbounds @inline function hrpass0(t::T, comp, y1,y2, o2, cnt, adj, adj2) where T <: SecondAdjointNodeVar cnt end -@inbounds @inline function hrpass0(t::T, comp::Nothing, y1,y2, o2, cnt, adj, adj2) where T <: TripleVar +@inbounds @inline function hrpass0(t::T, comp::Nothing, y1,y2, o2, cnt, adj, adj2) where T <: SecondAdjointNodeVar cnt end -@inbounds function hdrpass(t1::TripleVar, t2::TripleVar, comp::Nothing, y1,y2, o2, cnt, adj) +@inbounds function hdrpass(t1::SecondAdjointNodeVar, t2::SecondAdjointNodeVar, comp::Nothing, y1,y2, o2, cnt, adj) cnt += 1 push!(y1, (t1.i, t2.i)) cnt end -@inbounds function hrpass(t::TripleVar, comp::Nothing, y1,y2, o2, cnt, adj, adj2) +@inbounds function hrpass(t::SecondAdjointNodeVar, comp::Nothing, y1,y2, o2, cnt, adj, adj2) cnt += 1 push!(y1,(t.i, t.i)) cnt end -@inbounds @inline function hrpass(t::T, comp, y1,y2, o2, cnt, adj, adj2) where T <: TripleVar +@inbounds @inline function hrpass(t::T, comp, y1,y2, o2, cnt, adj, adj2) where T <: SecondAdjointNodeVar y1[o2 + comp(cnt += 1)] += adj2 cnt end -@inbounds @inline function hrpass(t::T, comp, y1::V, y2::V, o2, cnt, adj, adj2) where {T <: TripleVar, I <: Integer, V <: AbstractVector{I}} +@inbounds @inline function hrpass(t::T, comp, y1::V, y2::V, o2, cnt, adj, adj2) where {T <: SecondAdjointNodeVar, I <: Integer, V <: AbstractVector{I}} ind = o2 + comp(cnt += 1) y1[ind] = t.i y2[ind] = t.i cnt end -@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1::V, y2::V, o2, cnt, adj) where {T1 <: TripleVar, T2 <: TripleVar, I <: Integer, V <: AbstractVector{I}} +@inbounds @inline function hdrpass(t1::T1, t2::T2, comp, y1::V, y2::V, o2, cnt, adj) where {T1 <: SecondAdjointNodeVar, T2 <: SecondAdjointNodeVar, I <: Integer, V <: AbstractVector{I}} i,j = t1.i, t2.i ind = o2 + comp(cnt += 1) if i >= j @@ -204,13 +204,13 @@ end function shessian!(y1,y2, f, x, adj1, adj2) @simd for k in eachindex(f.itr) - hrpass0(f.f.f(f.itr[k],TripleSource(x)), f.f.comp2, y1,y2, offset2(f,k), 0, adj1, adj2) + hrpass0(f.f.f(f.itr[k],SecondAdjointNodeSource(x)), f.f.comp2, y1,y2, offset2(f,k), 0, adj1, adj2) end end function shessian!(y1,y2, f, x, adj1s::V, adj2) where {V <: AbstractVector} @simd for k in eachindex(f.itr) - hrpass0(f.f.f(f.itr[k],TripleSource(x)), f.f.comp2, y1, y2, offset2(f,k), 0, adj1s[offset0(f,k)], adj2) + hrpass0(f.f.f(f.itr[k],SecondAdjointNodeSource(x)), f.f.comp2, y1, y2, offset2(f,k), 0, adj1s[offset0(f,k)], adj2) end end diff --git a/src/jacobian.jl b/src/jacobian.jl index b8e1f786..f310d82d 100644 --- a/src/jacobian.jl +++ b/src/jacobian.jl @@ -1,17 +1,17 @@ -@inbounds @inline function jrpass(d::D, comp, i, y1,y2, o1, cnt, adj) where D <: Dual1 +@inbounds @inline function jrpass(d::D, comp, i, y1,y2, o1, cnt, adj) where D <: AdjointNode1 cnt = jrpass(d.inner, comp, i, y1,y2, o1, cnt, adj * d.y) return cnt end -@inbounds @inline function jrpass(d::D, comp, i, y1,y2, o1, cnt, adj) where D <: Dual2 +@inbounds @inline function jrpass(d::D, comp, i, y1,y2, o1, cnt, adj) where D <: AdjointNode2 cnt = jrpass(d.inner1, comp, i, y1,y2, o1, cnt, adj * d.y1) cnt = jrpass(d.inner2, comp, i, y1,y2, o1, cnt, adj * d.y2) return cnt end -@inbounds @inline function jrpass(d::D, comp, i, y1,y2, o1, cnt, adj) where D <: DualVar +@inbounds @inline function jrpass(d::D, comp, i, y1,y2, o1, cnt, adj) where D <: AdjointNodeVar y1[o1 + comp(cnt += 1)] += adj return cnt end -@inbounds @inline function jrpass(d::D, comp, i, y1::V, y2::V, o1, cnt, adj) where {D <: DualVar, I <: Integer, V <: AbstractVector{I}} +@inbounds @inline function jrpass(d::D, comp, i, y1::V, y2::V, o1, cnt, adj) where {D <: AdjointNodeVar, I <: Integer, V <: AbstractVector{I}} ind = o1 + comp(cnt += 1) y1[ind] = i y2[ind] = d.i @@ -21,6 +21,6 @@ end function sjacobian!(y1,y2,f,x,adj) @simd for i in eachindex(f.itr) - jrpass(f.f.f(f.itr[i],DualSource(x)), f.f.comp1, offset0(f,i), y1, y2, offset1(f,i), 0, adj) + jrpass(f.f.f(f.itr[i],AdjointNodeSource(x)), f.f.comp1, offset0(f,i), y1, y2, offset1(f,i), 0, adj) end end diff --git a/src/register.jl b/src/register.jl index 82fe2f94..99f35b63 100644 --- a/src/register.jl +++ b/src/register.jl @@ -3,10 +3,10 @@ macro register_univariate(f,df,ddf) quote @inline $f(n::N) where {N <: SIMDiff.AbstractNode} = SIMDiff.Node1($f,n) - @inline $f(d::D) where {D <: SIMDiff.AbstractDual} = - SIMDiff.Dual1($f,$f(d.x), $df(d.x), d) - @inline $f(t::T) where {T <: SIMDiff.AbstractTriple} = - SIMDiff.Triple1($f,$f(t.x), $df(t.x), $ddf(t.x), t) + @inline $f(d::D) where {D <: SIMDiff.AbstractAdjointNode} = + SIMDiff.AdjointNode1($f,$f(d.x), $df(d.x), d) + @inline $f(t::T) where {T <: SIMDiff.AbstractSecondAdjointNode} = + SIMDiff.SecondAdjointNode1($f,$f(t.x), $df(t.x), $ddf(t.x), t) @inbounds @inline (n::SIMDiff.Node1{typeof($f),I})(i,x) where I = $f(n.inner(i,x)) end @@ -42,13 +42,13 @@ macro register_bivariate(f,df1,df2,ddf11,ddf12,ddf22) end @inline function $f(d1::D1, d2::D2) where { - D1 <: SIMDiff.AbstractDual, - D2 <: SIMDiff.AbstractDual + D1 <: SIMDiff.AbstractAdjointNode, + D2 <: SIMDiff.AbstractAdjointNode } x1 = d1.x x2 = d2.x - SIMDiff.Dual2( + SIMDiff.AdjointNode2( $f, $f(x1,x2), $df1(x1,x2), @@ -58,14 +58,14 @@ macro register_bivariate(f,df1,df2,ddf11,ddf12,ddf22) ) end @inline function $f(d1::D1, d2::D2) where { - D1 <: SIMDiff.AbstractDual, + D1 <: SIMDiff.AbstractAdjointNode, D2 <: Real } x1 = d1.x x2 = d2 - SIMDiff.Dual1( + SIMDiff.AdjointNode1( $f, $f(x1,x2), $df1(x1,x2), @@ -74,12 +74,12 @@ macro register_bivariate(f,df1,df2,ddf11,ddf12,ddf22) end @inline function $f(d1::D1, d2::D2) where { D1 <: Real, - D2 <: SIMDiff.AbstractDual + D2 <: SIMDiff.AbstractAdjointNode } x1 = d1 x2 = d2.x - SIMDiff.Dual1( + SIMDiff.AdjointNode1( $f, $f(x1,x2), $df2(x1,x2), @@ -88,13 +88,13 @@ macro register_bivariate(f,df1,df2,ddf11,ddf12,ddf22) end @inline function $f(t1::T1, t2::T2) where { - T1 <: SIMDiff.AbstractTriple, - T2 <: SIMDiff.AbstractTriple + T1 <: SIMDiff.AbstractSecondAdjointNode, + T2 <: SIMDiff.AbstractSecondAdjointNode } x1 = t1.x x2 = t2.x - SIMDiff.Triple2( + SIMDiff.SecondAdjointNode2( $f, $f(x1,x2), $df1(x1,x2), @@ -107,13 +107,13 @@ macro register_bivariate(f,df1,df2,ddf11,ddf12,ddf22) ) end @inline function $f(t1::T1, t2::T2) where { - T1 <: SIMDiff.AbstractTriple, + T1 <: SIMDiff.AbstractSecondAdjointNode, T2 <: Real } x1 = t1.x x2 = t2 - SIMDiff.Triple1( + SIMDiff.SecondAdjointNode1( SIMDiff.SecondFixed($f), $f(x1,x2), $df1(x1,x2), @@ -123,12 +123,12 @@ macro register_bivariate(f,df1,df2,ddf11,ddf12,ddf22) end @inline function $f(t1::T1, t2::T2) where { T1 <: Real, - T2 <: SIMDiff.AbstractTriple + T2 <: SIMDiff.AbstractSecondAdjointNode } x1 = t1 x2 = t2.x - SIMDiff.Triple1( + SIMDiff.SecondAdjointNode1( SIMDiff.FirstFixed($f), $f(x1,x2), $df2(x1,x2), diff --git a/src/triple.jl b/src/triple.jl deleted file mode 100644 index 78b67bcc..00000000 --- a/src/triple.jl +++ /dev/null @@ -1,33 +0,0 @@ -struct Triple1{F, T, I} <: AbstractTriple - x::T - y::T - h::T - inner::I -end -struct Triple2{F, T, I1,I2} <: AbstractTriple - x::T - y1::T - y2::T - h11::T - h12::T - h22::T - inner1::I1 - inner2::I2 -end - -struct TripleVar{I,T} <: AbstractTriple - i::I - x::T -end -struct TripleSource{T, VT <: AbstractVector{T}} - inner::VT -end - -@inline Triple1(f::F,x::T,y,h,inner::I) where {F,T,I} = Triple1{F,T,I}(x,y,h,inner) -@inline Triple2(f::F,x::T,y1,y2,h11,h12,h22,inner1::I1,inner2::I2) where {F,T,I1,I2} = Triple2{F,T,I1,I2}(x,y1,y2,h11,h12,h22,inner1,inner2) - -struct TripleNullSource end -TripleSource(::Nothing) = TripleNullSource() - -@inbounds @inline Base.getindex(x::I,i) where I <: TripleNullSource = TripleVar(i,NaN) -@inbounds @inline Base.getindex(x::I,i) where I <: TripleSource = TripleVar(i, x.inner[i])