comparison 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
comparison
equal deleted inserted replaced
128:7c0b9bb7ab4d 129:1aaeb46ba5f4
15 end 15 end
16 return s.weights[1 + i - s.range[1]] 16 return s.weights[1 + i - s.range[1]]
17 end 17 end
18 18
19 Base.@propagate_inbounds @inline function apply(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N} 19 Base.@propagate_inbounds @inline function apply(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}
20 w = zero(eltype(v)) 20 w = s.weights[1]*v[i+ s.range[1]]
21 @simd for k ∈ 1:N 21 @simd for k ∈ 2:N
22 w += s.weights[k]*v[i+ s.range[1] + k-1] 22 w += s.weights[k]*v[i+ s.range[1] + k-1]
23 end 23 end
24 return w 24 return w
25 end 25 end
26 26
27 # TODO: Fix loop unrolling here as well. Then we can also remove Base.getindex(::Stencil)
27 Base.@propagate_inbounds @inline function apply_backwards(s::Stencil, v::AbstractVector, i::Int) 28 Base.@propagate_inbounds @inline function apply_backwards(s::Stencil, v::AbstractVector, i::Int)
28 w = zero(eltype(v)) 29 w = zero(eltype(v))
29 for j ∈ s.range[2]:-1:s.range[1] 30 for j ∈ s.range[2]:-1:s.range[1]
30 @inbounds weight = s[j] 31 @inbounds weight = s[j]
31 w += weight*v[i-j] 32 w += weight*v[i-j]