|
| 1 | +using BenchmarkTools, Base.Cartesian |
| 2 | +using Interpolations |
| 3 | + |
| 4 | +const suite = BenchmarkGroup() |
| 5 | + |
| 6 | +suite["bsplines"] = BenchmarkGroup() |
| 7 | +for bspline in ["constant", "linear", "quadratic", "cubic"] |
| 8 | + suite["bsplines"][bspline] = BenchmarkGroup() |
| 9 | +end |
| 10 | + |
| 11 | +# To evaluate at fractional positions without any "unnecessary" |
| 12 | +# overhead, safer to use Base.Cartesian |
| 13 | +@generated function sumvalues{T,N}(itp::AbstractInterpolation{T,N}, inds) |
| 14 | + quote |
| 15 | + @nexprs $N d->inds_d = inds[d] |
| 16 | + s = zero(eltype(itp)) |
| 17 | + @inbounds @nloops $N i d->inds_d begin |
| 18 | + s += @nref($N, itp, i) |
| 19 | + end |
| 20 | + s |
| 21 | + end |
| 22 | +end |
| 23 | + |
| 24 | +function sumvalues_indices(itp) |
| 25 | + inds = indices(itp) |
| 26 | + n = Int(round(10^(3/ndims(itp)))) |
| 27 | + ntuple(d->collect(linspace(first(inds[d])+0.001, last(inds[d])-0.001, n)), ndims(itp)) |
| 28 | +end |
| 29 | + |
| 30 | +strip_prefix(str) = replace(str, "Interpolations.", "") |
| 31 | +benchstr{T<:Interpolations.GridType}(::Type{T}) = strip_prefix(string(T)) |
| 32 | + |
| 33 | +benchstr(::Type{Constant}) = "Constant()" |
| 34 | +benchstr(::Type{Linear}) = "Linear()" |
| 35 | +benchstr{BC<:Interpolations.Flag}(::Type{Quadratic{BC}}) = |
| 36 | + string("Quadratic(", strip_prefix(string(BC)), "())") |
| 37 | +benchstr{BC<:Interpolations.Flag}(::Type{Cubic{BC}}) = |
| 38 | + string("Quadratic(", strip_prefix(string(BC)), "())") |
| 39 | + |
| 40 | +groupstr(::Type{Constant}) = "constant" |
| 41 | +groupstr(::Type{Linear}) = "linear" |
| 42 | +groupstr(::Type{Quadratic}) = "quadratic" |
| 43 | +groupstr(::Type{Cubic}) = "cubic" |
| 44 | + |
| 45 | + |
| 46 | +for A in (collect(Float64, 1:3), |
| 47 | + reshape(collect(Float64, 1:9), 3, 3), |
| 48 | + reshape(collect(Float64, 1:27), 3, 3, 3)) |
| 49 | + # Constant & Linear |
| 50 | + for D in (Constant, Linear) |
| 51 | + gstr = groupstr(D) |
| 52 | + for GT in (OnGrid, OnCell) |
| 53 | + Ac = copy(A) |
| 54 | + idstr = string(ndims(A), "d_", benchstr(D), '_', benchstr(GT)) |
| 55 | + suite["bsplines"][gstr][string(idstr, "_construct")] = |
| 56 | + @benchmarkable interpolate($Ac, BSpline($D()), $GT()) |
| 57 | + itp = interpolate(copy(A), BSpline(D()), GT()) |
| 58 | + inds = sumvalues_indices(itp) |
| 59 | + suite["bsplines"][gstr][string(idstr, "_use")] = |
| 60 | + @benchmarkable sumvalues($itp, $inds) |
| 61 | + end |
| 62 | + end |
| 63 | + # Quadratic |
| 64 | + gstr = groupstr(Quadratic) |
| 65 | + for BC in (Flat,Line,Free,Periodic,Reflect,Natural), GT in (OnGrid, OnCell) |
| 66 | + Ac = copy(A) |
| 67 | + idstr = string(ndims(A), "d_", benchstr(Quadratic{BC}), '_', benchstr(GT)) |
| 68 | + suite["bsplines"][gstr][string(idstr, "_construct")] = |
| 69 | + @benchmarkable interpolate($Ac, BSpline(Quadratic($BC())), $GT()) |
| 70 | + itp = interpolate(copy(A), BSpline(Quadratic(BC())), GT()) |
| 71 | + inds = sumvalues_indices(itp) |
| 72 | + suite["bsplines"][gstr][string(idstr, "_use")] = |
| 73 | + @benchmarkable sumvalues($itp, $inds) |
| 74 | + end |
| 75 | + for BC in (InPlace,InPlaceQ) |
| 76 | + Ac = copy(A) |
| 77 | + idstr = string(ndims(A), "d_", benchstr(Quadratic{BC}), '_', benchstr(OnCell)) |
| 78 | + suite["bsplines"][gstr][string(idstr, "_construct")] = |
| 79 | + @benchmarkable interpolate!($Ac, BSpline(Quadratic($BC())), OnCell()) |
| 80 | + itp = interpolate!(copy(A), BSpline(Quadratic(BC())), OnCell()) |
| 81 | + inds = sumvalues_indices(itp) |
| 82 | + suite["bsplines"][gstr][string(idstr, "_use")] = |
| 83 | + @benchmarkable sumvalues($itp, $inds) |
| 84 | + end |
| 85 | + # Cubic |
| 86 | + gstr = groupstr(Cubic) |
| 87 | + for BC in (Flat,Line,Free,Periodic), GT in (OnGrid, OnCell) |
| 88 | + Ac = copy(A) |
| 89 | + idstr = string(ndims(A), "d_", benchstr(Cubic{BC}), '_', benchstr(GT)) |
| 90 | + suite["bsplines"][gstr][string(idstr, "_construct")] = |
| 91 | + @benchmarkable interpolate($Ac, BSpline(Cubic($BC())), $GT()) |
| 92 | + itp = interpolate(copy(A), BSpline(Cubic(BC())), GT()) |
| 93 | + inds = sumvalues_indices(itp) |
| 94 | + suite["bsplines"][gstr][string(idstr, "_use")] = |
| 95 | + @benchmarkable sumvalues($itp, $inds) |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +paramspath = joinpath(dirname(@__FILE__), "params.jld") |
| 100 | + |
| 101 | +if isfile(paramspath) |
| 102 | + loadparams!(suite, BenchmarkTools.load(paramspath, "suite"), :evals); |
| 103 | +else |
| 104 | + info("Tuning suite (this may take a while)") |
| 105 | + tune!(suite) |
| 106 | + BenchmarkTools.save(paramspath, "suite", params(suite)); |
| 107 | +end |
0 commit comments