Mercurial > repos > public > sbplib_julia
view stencil.jl @ 86:34fd86e9d0b9 patch_based_test
Change benchmarkTest from function to script since @benchmark does not work within functions
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Fri, 25 Jan 2019 10:10:30 +0100 |
parents | 7f72e7e14659 |
children | 38733e84ef1a |
line wrap: on
line source
struct Stencil{T<:Real} range::NTuple{2,Int} weights::Vector{T} # TBD: Should this be a tuple? function Stencil(range, weights) width = range[2]-range[1]+1 if width != length(weights) error("The width and the number of weights must be the same") end new{eltype(weights)}(range, weights) end end function flip(s::Stencil) range = (-s.range[2], -s.range[1]) s = Stencil(range, s.weights[end:-1:1]) end # Provides index into the Stencil based on offset for the root element function Base.getindex(s::Stencil, i::Int) # TBD: Rearrange to mark with @boundscheck? if s.range[1] <= i <= s.range[2] return s.weights[1 + i - s.range[1]] else return eltype(s.weights)(0) end end @inline function apply(s::Stencil, v::AbstractVector, i::Int) w = zero(eltype(v)) for j ∈ s.range[1]:s.range[2] @inbounds w += s[j]*v[i+j] # TBD: Make this without boundschecks? end return w end