diff stencil.jl @ 129:1aaeb46ba5f4 cell_based_test

Improve efficiency of apply by the following: - Remove divisions in interior loop by storing and multiplying by the reciprocal of grid spacing instead. - Add @inline to apply(::Laplace - Remove initialization of w = 0 in apply(::Stencil) by manually unrolling first iteration of the loop.
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 14 Feb 2019 16:25:22 +0100
parents 7c0b9bb7ab4d
children 8569c637d923
line wrap: on
line diff
--- a/stencil.jl	Thu Feb 14 12:46:58 2019 +0100
+++ b/stencil.jl	Thu Feb 14 16:25:22 2019 +0100
@@ -17,13 +17,14 @@
 end
 
 Base.@propagate_inbounds @inline function apply(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}
-    w = zero(eltype(v))
-    @simd for k ∈ 1:N
+    w = s.weights[1]*v[i+ s.range[1]]
+    @simd for k ∈ 2:N
         w += s.weights[k]*v[i+ s.range[1] + k-1]
     end
     return w
 end
 
+# TODO: Fix loop unrolling here as well. Then we can also remove Base.getindex(::Stencil)
 Base.@propagate_inbounds @inline function apply_backwards(s::Stencil, v::AbstractVector, i::Int)
     w = zero(eltype(v))
     for j ∈ s.range[2]:-1:s.range[1]