Mercurial > repos > public > sbplib_julia
comparison stencil.jl @ 134:79699dda29be
Merge in cell_based_test
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 21 Feb 2019 16:27:28 +0100 |
parents | 6b6d921e8f05 |
children |
comparison
equal
deleted
inserted
replaced
84:48079bd39969 | 134:79699dda29be |
---|---|
1 struct Stencil{T<:Real,N} | 1 struct Stencil{T<:Real,N} |
2 range::Tuple{Int,Int} | 2 range::Tuple{Int,Int} |
3 weights::NTuple{N,T} | 3 weights::NTuple{N,T} |
4 | |
5 function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T <: Real, N} | |
6 @assert range[2]-range[1]+1 == N | |
7 new{T,N}(range,weights) | |
8 end | |
4 end | 9 end |
5 | 10 |
6 function flip(s::Stencil) | 11 function flip(s::Stencil) |
7 range = (-s.range[2], -s.range[1]) | 12 range = (-s.range[2], -s.range[1]) |
8 return Stencil(range, reverse(s.weights)) | 13 return Stencil(range, reverse(s.weights)) |
9 end | 14 end |
10 | 15 |
11 # Provides index into the Stencil based on offset for the root element | 16 # Provides index into the Stencil based on offset for the root element |
12 function Base.getindex(s::Stencil, i::Int) | 17 @inline function Base.getindex(s::Stencil, i::Int) |
13 # TBD: Rearrange to mark with @boundscheck? | 18 @boundscheck if i < s.range[1] || s.range[2] < i |
14 if s.range[1] <= i <= s.range[2] | 19 return eltype(s.weights)(0) |
15 return s.weights[1 + i - s.range[1]] | |
16 else | |
17 return 0 | |
18 end | 20 end |
21 return s.weights[1 + i - s.range[1]] | |
19 end | 22 end |
20 | 23 |
21 function apply(s::Stencil, v::AbstractVector, i::Int) | 24 Base.@propagate_inbounds @inline function apply(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N} |
22 w = zero(eltype(v)) | 25 w = s.weights[1]*v[i + s.range[1]] |
23 for j ∈ s.range[1]:s.range[2] | 26 @simd for k ∈ 2:N |
24 w += s[j]*v[i+j] # TBD: Make this without boundschecks? | 27 w += s.weights[k]*v[i + s.range[1] + k-1] |
25 end | 28 end |
26 return w | 29 return w |
27 end | 30 end |
31 | |
32 Base.@propagate_inbounds @inline function apply_backwards(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N} | |
33 w = s.weights[N]*v[i - s.range[2]] | |
34 @simd for k ∈ N-1:-1:1 | |
35 w += s.weights[k]*v[i - s.range[1] - k + 1] | |
36 end | |
37 return w | |
38 end |