Pyramid example

This is the Pyramid example in the book "Evaluate Derivatives", Sec. 3.5.

using NiLang, NiLang.AD

@i function pyramid!(y!, v!, x::AbstractVector{T}) where T
    @safe @assert size(v!,2) == size(v!,1) == length(x)
    @invcheckoff @inbounds for j=1:length(x)
        v![1,j] += x[j]
    end
    @invcheckoff @inbounds for i=1:size(v!,1)-1
        for j=1:size(v!,2)-i
            @routine begin
                @zeros T c s
                c += cos(v![i,j+1])
                s += sin(v![i,j])
            end
            v![i+1,j] += c * s
            ~@routine
        end
    end
    y! += v![end,1]
end

x = randn(20)
pyramid!(0.0, zeros(20, 20), x)
(-0.2796035498892192, [-0.695265184571149 0.23953143340106084 … 0.0054225624676272725 -0.13960889837326657; -0.6222997925011231 0.22131301470493137 … 0.005369777308764798 0.0; … ; -0.28694947263648657 0.15571085451621725 … 0.0 0.0; -0.2796035498892192 0.0 … 0.0 0.0], [-0.695265184571149, 0.23953143340106084, -0.3685903600513449, 0.5018692736856223, 0.09562520644493691, 0.13179774785349804, -0.4447352808506539, -0.7183392973053995, 0.016605019521405202, 0.537013071481501, -2.2668338443602503, -0.6389014205684265, 0.19369306503478936, -0.9716665965565073, 0.3211967404080062, 0.9554476850197204, -1.3858790788286592, -1.2352964998388456, 0.0054225624676272725, -0.13960889837326657])

Let' compare our implementation with those in the book

using BenchmarkTools
@benchmark gradient(Val(1), pyramid!, (0.0, zeros(20, 20), $x))
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  16.602 μs …  6.205 ms  ┊ GC (min … max): 0.00% … 99.33%
 Time  (median):     23.252 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   24.423 μs ± 62.689 μs  ┊ GC (mean ± σ):  2.52% ±  0.99%

   ▂▂    ▄▅▅▃▁ ▁▇█▇▆▅▇█▆▅▃▁ ▁                                 ▂
  ▃███▇▅▇█████▇████████████████▇▇▅▆▄▅▃▅▂▃▅▅▅▅▆▇▇▇█▇▇▆▆▆▅▆▅▃▆▆ █
  16.6 μs      Histogram: log(frequency) by time      39.5 μs <

 Memory estimate: 13.48 KiB, allocs estimate: 5.

This page was generated using Literate.jl.