Mercurial > repos > public > sbplib_julia
annotate stencil.jl @ 87:38733e84ef1a patch_based_test
Clean up bounds checking in stencil. change to using uview in DiffOp
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Fri, 25 Jan 2019 13:40:15 +0100 |
parents | 7f72e7e14659 |
children | 170e5447bc19 |
rev | line source |
---|---|
80
700a74c41b26
Improve type stability
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
69
diff
changeset
|
1 struct Stencil{T<:Real} |
8
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
2 range::NTuple{2,Int} |
80
700a74c41b26
Improve type stability
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
69
diff
changeset
|
3 weights::Vector{T} # TBD: Should this be a tuple? |
8
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
4 function Stencil(range, weights) |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
5 width = range[2]-range[1]+1 |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
6 if width != length(weights) |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
7 error("The width and the number of weights must be the same") |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
8 end |
80
700a74c41b26
Improve type stability
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
69
diff
changeset
|
9 new{eltype(weights)}(range, weights) |
8
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
10 end |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
11 end |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
12 |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
13 function flip(s::Stencil) |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
14 range = (-s.range[2], -s.range[1]) |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
15 s = Stencil(range, s.weights[end:-1:1]) |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
16 end |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
17 |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
18 # Provides index into the Stencil based on offset for the root element |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
19 function Base.getindex(s::Stencil, i::Int) |
87
38733e84ef1a
Clean up bounds checking in stencil. change to using uview in DiffOp
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
81
diff
changeset
|
20 @boundscheck if i < s.range[1] || s.range[2] < i |
80
700a74c41b26
Improve type stability
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
69
diff
changeset
|
21 return eltype(s.weights)(0) |
8
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
22 end |
87
38733e84ef1a
Clean up bounds checking in stencil. change to using uview in DiffOp
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
81
diff
changeset
|
23 return s.weights[1 + i - s.range[1]] |
8
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
24 end |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
25 |
87
38733e84ef1a
Clean up bounds checking in stencil. change to using uview in DiffOp
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
81
diff
changeset
|
26 Base.@propagate_inbounds function apply(s::Stencil, v::AbstractVector, i::Int) |
32
64530c07165b
Allow stride in apply stencil
Jonatan Werpers <jonatan@werpers.com>
parents:
8
diff
changeset
|
27 w = zero(eltype(v)) |
43
ef060ab3b035
remove stride and remove some bugs
Ylva Rydin <ylva.rydin@telia.com>
parents:
32
diff
changeset
|
28 for j ∈ s.range[1]:s.range[2] |
87
38733e84ef1a
Clean up bounds checking in stencil. change to using uview in DiffOp
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
81
diff
changeset
|
29 @inbounds weight = s[j] |
38733e84ef1a
Clean up bounds checking in stencil. change to using uview in DiffOp
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
81
diff
changeset
|
30 w += weight*v[i+j] |
8
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
31 end |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
32 return w |
433008d3b7d3
Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff
changeset
|
33 end |