I am observing a significant memory overhead when using FastInterpolations.jl for high-order (quadratic and cubic) interpolation of 3D scalar fields compared to linear interpolation. While linear interpolation has nearly zero overhead (~1.0x), both quadratic and cubic methods increase the memory footprint by a factor of approximately 8x.
In 3D, it seems that these methods precompute and store 8 coefficients (values and partial derivatives) per grid node to ensure $O(1)$ lookup performance. For large numerical fields (e.g., $512^3$ or larger), this 8x multiplier can make high-order interpolation unfeasible on standard hardware.
using FastInterpolations
# Setup a 3D scalar field
nx, ny, nz = 8, 8, 8
x = range(0.0f0, 1.0f0, length=nx)
y = range(0.0f0, 1.0f0, length=ny)
z = range(0.0f0, 1.0f0, length=nz)
A = rand(Float32, nx, ny, nz)
# Construct interpolators
itp1 = linear_interp((x, y, z), A)
itp2 = quadratic_interp((x, y, z), A)
itp3 = cubic_interp((x, y, z), A)
# Report memory usage
size_A = Base.summarysize(A)
size_itp1 = Base.summarysize(itp1)
size_itp2 = Base.summarysize(itp2)
size_itp3 = Base.summarysize(itp3)
println("--- Memory Usage Demo ---")
println("Data Size: ", size_A, " bytes")
println("Order 1 (Linear): ", size_itp1, " bytes (Ratio: ", round(size_itp1/size_A, digits=2), "x)")
println("Order 2 (Quadratic): ", size_itp2, " bytes (Ratio: ", round(size_itp2/size_A, digits=2), "x)")
println("Order 3 (Cubic): ", size_itp3, " bytes (Ratio: ", round(size_itp3/size_A, digits=2), "x)")
--- Memory Usage Demo ---
Data Size: 2104 bytes
Order 1 (Linear): 2232 bytes (Ratio: 1.06x)
Order 2 (Quadratic): 16576 bytes (Ratio: 7.88x)
Order 3 (Cubic): 16576 bytes (Ratio: 7.88x)
In my experiment with a magnetic field data of 45 GB, constructing a linear interpolator took 134.790 GiB, while constructing a quadratic interpolator took 447.054 GiB.
Questions / Optimization Requests:
- Is there a way to perform higher-order interpolation without such a large precomputed coefficient array (e.g., computing coefficients on-the-fly at the cost of some performance)?
- Does the package support in-place precomputation where the original data array is reused or transformed to save memory?
Any advice on optimizing the memory footprint for large 3D grids would be greatly appreciated!
I am observing a significant memory overhead when using FastInterpolations.jl for high-order (quadratic and cubic) interpolation of 3D scalar fields compared to linear interpolation. While linear interpolation has nearly zero overhead (~1.0x), both quadratic and cubic methods increase the memory footprint by a factor of approximately 8x.
In 3D, it seems that these methods precompute and store 8 coefficients (values and partial derivatives) per grid node to ensure$O(1)$ lookup performance. For large numerical fields (e.g., $512^3$ or larger), this 8x multiplier can make high-order interpolation unfeasible on standard hardware.
In my experiment with a magnetic field data of 45 GB, constructing a linear interpolator took 134.790 GiB, while constructing a quadratic interpolator took 447.054 GiB.
Questions / Optimization Requests:
Any advice on optimizing the memory footprint for large 3D grids would be greatly appreciated!