Parallelizing interpolation? (BoundsError)

Hi, I am new to Julia and I have been trying to use Interpolations.jl to obtain interpolated values for grids. At some point, the number of points became quite large and I felt I should try to parallelize the code but have been unsuccessful :frowning:

I was wondering if it is possible send interpolated objects to multiple processes, and obtain interpolated values? I have attached a small attempt here, which resulted in BoundsError.

@everywhere using Interpolations

using Distributed

using SharedArrays

# two variables, not necessarily evenly spaced

x = [1,2,3,4,5]

y = [2,4,6,8,10]

nodes = (x,y)

# some known values for (x,y)

V = randn(5,5)

itp = interpolate(nodes, V, Gridded(Linear()));

# inputs, interior of (x,y)

x2 = collect(range(1.1,4.9,length = 1000));

y2 = collect(range(2.1,9.9,length = 1000));

z = SharedArray{Float64}(1000)

@distributed for i = 1:1000

    z[i] = itp(x2[i], y2[i]);

end

#= Error Message

Task (failed) @0x00000000157370f0

BoundsError: attempt to access 5×5 interpolate((::Array{Int64,1},::Array{Int64,1}), ::Array{Float64,2}, Gridded(Linear())) with element type Float64 at index [5, 1]

throw_boundserror(::Interpolations.GriddedInterpolation{Float64,2,Float64,Gridded{Linear},Tuple{Array{Int64,1},Array{Int64,1}}}, ::Tuple{Int64,Int64}) at .\abstractarray.jl:541

=#

I would really appreciate any advice!! Thank you in advance. :slight_smile:

After some research, I realized that GridInterpolations.jl can offer good performance in the context of the above example.

using Distributed
using SharedArrays
@everywhere using GridInterpolations

# two variables, not necessarily evenly spaced
x = [1,2,3,4,5]
y = [2,4,6,8,10]

V = randn(5,5)
V = V[:]

grid = RectangleGrid(x,y)

NN = 10000000
# inputs, interior of (x,y)
x2 = collect(range(1.1,4.9,length = NN));
y2 = collect(range(2.1,9.9,length = NN));

XX = hcat(x2,y2);

zz = SharedArray{Float64}(NN)

@sync @distributed for i = 1:NN
    zz[i] = interpolate(grid,V,XX[i,:])
end

1 Like

nice sleuthing

1 Like