I need linear interpolation on a grid to be fast and work with ForwardDiff. The Interpolations.jl package appears to be extremely fast (especially for a large number of interpolations), however it is not compatible with ForwardDiff duals. Since my case is extremely simple, I was hoping to write a similar performance simple grid interpolation function that would accept duals, however, I am not able to come anywhere close to matching the performance of Interpolations.jl. Does anyone see what I’m doing wrong that leads me to perform so poorly by comparison?
For a trivial example, the respective performances for my own code and Interpolations.jl are:
26.290 ms (200002 allocations: 3.81 MiB)
2.776 ms (4 allocations: 781.36 KiB)
Code below:
using Interpolations
function make_my_interp(ms,lb,ub,ys)
xs=lb:ms:ub
function fxn(x)
l_ind = 1+floor(Int,(x-lb)/ms)
return ys[l_ind] + (ys[l_ind+1] - ys[l_ind])*(x-xs[l_ind])/ms
end
return x -> fxn(x)
end
lb=-10.0
ub=10.0
ms = .1
yvals = [xx^2 for xx in lb:ms:ub]
my_interp = make_my_interp(ms,lb,ub,yvals)
function vector_interp(N)
x=zeros(N)
@inbounds for ii=1:N
x[ii] = my_interp(3.05)
end
return x
end
pkg_interp = LinearInterpolation(lb:ms:ub,yvals)
N=100_000
v_input=3.05 .* ones(N)
@btime vector_interp(N)
@btime pkg_interp(v_input)
Any advice on how I can approach Interpolations.jl performance and/or use ForwardDiff duals with Interpolations.jl would be greatly appreciated. Thank you!