JuliaMath / Interpolations.jl

Fast, continuous interpolation of discrete datasets in Julia
http://juliamath.github.io/Interpolations.jl/
Other
523 stars 110 forks source link

Bounds Checking Dramatically Reduces Performance #548

Open SBuercklin opened 1 year ago

SBuercklin commented 1 year ago

I have a script which does some interpolations over a 5D grid.

When running the script below normally from the command line, it takes ~25 seconds on my machine, mostly compilation time. However, when I run it with julia --bounds-check=yes (or in the test suite, which is how I encountered into this problem), it takes nearly 12 minutes.

For interactive/production usage the package currently works fine, but when using an interpolator in a test, performance drops dramatically. Can anything be done to improve performance when bounds checking is turned on?

Script to demonstrate:

import Interpolations as ITP

function make_interpolator(preds, data)
    itp = ITP.interpolate(data, ITP.BSpline(ITP.Cubic(ITP.Line(ITP.OnGrid()))))
    etpf = ITP.extrapolate(itp, ITP.Flat())

    return ITP.scale(etpf, preds...)
end

data = [
        556349.6008704818 685054.7413928926; 337374.20860475855 917776.3775522095;;; 334561.15326837776 656045.6541346011; 531275.5763673111 956019.3625939675;;;; 503423.6161355341 885549.2837607361; 60388.76774041346 879492.3386633231;;; 603084.6996757543 991251.0585302819; 579549.8986152456 367420.19662000256;;;;;
        869553.4452428828 66685.83346470346; 479207.3430831957 114053.8123128425;;; 34064.47078306818 90426.76885625877; 964992.7885397973 580234.4705046738;;;; 417425.5870441628 106769.75855182836; 218856.14729614765 84848.46219219244;;; 382595.28561278287 91699.85134152214; 585552.6744809537 269352.3289748475
    ]
preds = (0.0:20.0:20.0, 1.3:0.1:1.4, 0.9:0.1:1.0, 1.0:2.0:3.0, 130.0:5.0:135.0)

itp = make_interpolator(preds, data)

itp(0.0, 1.3, 0.9, 1.0, 130.0)
mkitti commented 1 year ago

The interpolator construction does look a bit unusual. Usually, one scales before applying the extrapolation.

The overall design of this package is meant to elide bounds checking to enable SIMD, so forcing bounds checking is going to create a major slowdown. That said I have not seen a slowdown of that magnitude.

SBuercklin commented 1 year ago

Thanks for the tip on the scaling; I will make that change in our codebase.

We can get by with no bounds checking for deployment, so this isn't a showstopping issue for us. The only problem is we can't test a case where we need the 5D interpolation, but I can work on a reduced dimension test for our use case. Nevertheless, it would be nice if there were a way around this, but it sounds like that's a larger problem to solve with bounds-checking that's not specific to Interpolations.jl