annotate stencil.jl @ 81:7f72e7e14659 patch_based_test

Add benchmarktest and mark all apply functions with @inline and @inbounds
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 24 Jan 2019 14:58:22 +0100
parents 700a74c41b26
children 38733e84ef1a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)
69
8cd8d83b92e7 Add some notes to stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 43
diff changeset
20 # TBD: Rearrange to mark with @boundscheck?
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
21 if s.range[1] <= i <= s.range[2]
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
22 return s.weights[1 + i - s.range[1]]
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
23 else
80
700a74c41b26 Improve type stability
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 69
diff changeset
24 return eltype(s.weights)(0)
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
25 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
26 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
27
81
7f72e7e14659 Add benchmarktest and mark all apply functions with @inline and @inbounds
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 80
diff changeset
28 @inline function apply(s::Stencil, v::AbstractVector, i::Int)
32
64530c07165b Allow stride in apply stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 8
diff changeset
29 w = zero(eltype(v))
43
ef060ab3b035 remove stride and remove some bugs
Ylva Rydin <ylva.rydin@telia.com>
parents: 32
diff changeset
30 for j ∈ s.range[1]:s.range[2]
81
7f72e7e14659 Add benchmarktest and mark all apply functions with @inline and @inbounds
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 80
diff changeset
31 @inbounds w += s[j]*v[i+j] # TBD: Make this without boundschecks?
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
32 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
33 return w
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
34 end