Mercurial > repos > public > sbplib_julia
changeset 1553:f1eacb923f45
Merge bugfix/sbp_operators/stencil_return_type
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 13 Apr 2024 23:44:56 +0200 |
parents | 5193e6cd6c6a (current diff) a0296d9a609d (diff) |
children | 05e0bcf7d7ca ec5e7926c37b |
files | |
diffstat | 3 files changed, 93 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/Notes.md Thu Apr 11 23:48:11 2024 +0200 +++ b/Notes.md Sat Apr 13 23:44:56 2024 +0200 @@ -348,3 +348,11 @@ * This would impact how tensor grid works. * To make things homogenous maybe these index collections should be used for the more simple grids too. * The function `to_indices` from Base could be useful to implement for `IndexCollection` + + +## Stencil application pipeline +We should make sure that `@inbounds` and `Base.@propagate_inbounds` are +applied correctly throughout the stack. When testing the performance of +stencil application on the bugfix/sbp_operators/stencil_return_type branch +there seemed to be some strange results where such errors could be the +culprit.
--- a/src/SbpOperators/stencil.jl Thu Apr 11 23:48:11 2024 +0200 +++ b/src/SbpOperators/stencil.jl Sat Apr 13 23:44:56 2024 +0200 @@ -69,22 +69,29 @@ end Base.@propagate_inbounds @inline function apply_stencil(s::Stencil, v::AbstractVector, i::Int) - w = zero(promote_type(eltype(s),eltype(v))) - @simd for k ∈ 1:length(s) - w += s.weights[k]*v[i + s.range[k]] + return sum(enumerate(s.weights)) do (k,w) #TBD: Which optimizations are needed here? + w*v[i + @inbounds s.range[k]] end - - return w end Base.@propagate_inbounds @inline function apply_stencil_backwards(s::Stencil, v::AbstractVector, i::Int) - w = zero(promote_type(eltype(s),eltype(v))) - @simd for k ∈ length(s):-1:1 - w += s.weights[k]*v[i - s.range[k]] + return sum(enumerate(s.weights)) do (k,w) #TBD: Which optimizations are needed here? + w*v[i - @inbounds s.range[k]] end - return w end +# There are many options for the implementation of `apply_stencil` and +# `apply_stencil_backwards`. Some alternatives were tried on the branch +# bugfix/sbp_operators/stencil_return_type and can be found at the following +# revision: +# +# * 237b980ffb91 (baseline) +# * a72bab15228e (mapreduce) +# * ffd735354d54 (multiplication) +# * b5abd5191f2c (promote_op) +# * 8d56846185fc (return_type) +# + function left_pad(s::Stencil, N) weights = LazyTensors.left_pad_tuple(s.weights, zero(eltype(s)), N) range = (first(s.range) - (N - length(s.weights))):last(s.range)
--- a/test/SbpOperators/stencil_test.jl Thu Apr 11 23:48:11 2024 +0200 +++ b/test/SbpOperators/stencil_test.jl Sat Apr 13 23:44:56 2024 +0200 @@ -1,8 +1,10 @@ using Test using Sbplib.SbpOperators +using StaticArrays import Sbplib.SbpOperators.Stencil import Sbplib.SbpOperators.NestedStencil import Sbplib.SbpOperators.scale +import Sbplib.SbpOperators: apply_stencil, apply_stencil_backwards @testset "Stencil" begin s = Stencil(-2:2, (1.,2.,2.,3.,4.)) @@ -44,6 +46,52 @@ @test promote(Stencil(1,1;center=1), Stencil(2.,2.;center=2)) == (Stencil(1.,1.;center=1), Stencil(2.,2.;center=2)) end + @testset "apply_stencil" begin + v = [1, 2, 4, 8, 16, 32, 64, 128] + s = Stencil(1,2,3,4, center = 2) + @test apply_stencil(s,v, 2) == v[1] + 2*v[2] + 3*v[3] + 4*v[4] + @test apply_stencil(s,v, 4) == v[3] + 2*v[4] + 3*v[5] + 4*v[6] + @test apply_stencil_backwards(s,v, 3) == 4*v[1] + 3*v[2] + 2*v[3] + 1*v[4] + @test apply_stencil_backwards(s,v, 7) == 4*v[5] + 3*v[6] + 2*v[7] + 1*v[8] + @test apply_stencil(s,v, 2) isa Int + @test apply_stencil_backwards(s,v, 7) isa Int + + v = [1, 2, 4, 8, 16, 32, 64, 128] + s = Stencil(1.,2.,3.,4., center = 2) + @test apply_stencil(s,v, 4) == v[3] + 2. *v[4] + 3. *v[5] + 4. *v[6] + @test apply_stencil_backwards(s,v, 7) == 4. *v[5] + 3. *v[6] + 2. *v[7] + v[8] + @test apply_stencil(s,v, 2) isa Float64 + @test apply_stencil_backwards(s,v, 7) isa Float64 + + v = [1., 2., 4., 8., 16., 32., 64., 128.] + s = Stencil(1,2,3,4, center = 2) + @test apply_stencil(s,v, 2) == v[1] + 2*v[2] + 3*v[3] + 4*v[4] + @test apply_stencil_backwards(s,v, 3) == 4*v[1] + 3*v[2] + 2*v[3] + 1*v[4] + @test apply_stencil(s,v, 2) isa Float64 + @test apply_stencil_backwards(s,v, 3) isa Float64 + + v = [@SVector[1, 2], @SVector[3, 4], @SVector[5, 6], @SVector[7, 8]] + s = Stencil(1,2, center = 1) + @test apply_stencil(s,v,1) == @SVector[7, 10] + @test apply_stencil_backwards(s,v,3) == @SVector[11, 14] + @test apply_stencil(s,v,1) isa SVector{2, Int} + @test apply_stencil_backwards(s,v,3) isa SVector{2, Int} + + v = [@SVector[1., 2.], @SVector[3., 4.], @SVector[5., 6.], @SVector[7., 8.]] + s = Stencil(1,2, center = 1) + @test apply_stencil(s,v,1) == @SVector[7., 10.] + @test apply_stencil_backwards(s,v,3) == @SVector[11., 14.] + @test apply_stencil(s,v,1) isa SVector{2, Float64} + @test apply_stencil_backwards(s,v,3) isa SVector{2, Float64} + + v = [@SVector[1, 2], @SVector[3, 4], @SVector[5, 6], @SVector[7, 8]] + s = Stencil(1.,2., center = 1) + @test apply_stencil(s,v,1) == @SVector[7., 10.] + @test apply_stencil_backwards(s,v,3) == @SVector[11., 14.] + @test apply_stencil(s,v,1) isa SVector{2, Float64} + @test apply_stencil_backwards(s,v,3) isa SVector{2, Float64} + end + @testset "type stability" begin s_int = CenteredStencil(1,2,3) s_float = CenteredStencil(1.,2.,3.) @@ -155,6 +203,27 @@ @test SbpOperators.apply_stencil(ns, c, v, 4) == 5*7 + 11*11 + 6*13 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -3*3 - 7*5 - 4*7 + + # Different types in vector and stencil + ns = NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-2.,2.), center=2) + @test SbpOperators.apply_inner_stencils(ns, c, 4) isa Stencil{Float64, 3} + @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(4.,9.,10.; center=2) + @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) isa Stencil{Float64, 3} + @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-5.,-9.,-8.; center=2) + + @test SbpOperators.apply_stencil(ns, c, v, 4) isa Float64 + @test SbpOperators.apply_stencil(ns, c, v, 4) == 193. + @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) isa Float64 + @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -158. + + # Arrays of vectors + ns = NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-2.,2.), center=2) + c = [ 1, 3, 6, 10] + v = [@SVector[1, 2], @SVector[3, 4], @SVector[5, 6], @SVector[7, 8]] + @test SbpOperators.apply_stencil(ns, c, v, 2) isa SVector{2,Float64} + @test SbpOperators.apply_stencil(ns, c, v, 2) == 2v[1] + 5v[2] + 6v[3] + @test SbpOperators.apply_stencil_backwards(ns, c, v, 2) isa SVector{2,Float64} + @test SbpOperators.apply_stencil_backwards(ns, c, v, 2) == -4v[1] - 5v[2] - 3v[3] end @testset "type stability" begin