I have a module that is supposed to perform some numerical tasks, including random sampling. These tasks, in particular the sampling, depend on using functions for which there is no closed-form expression, so I brute-forced a calculation for a big range of parameters with decent granularity to obtain a table that I then use to approximate these functions by linear interpolation. The resulting values have been stored in an HDF5 file that is read by the module.
I want the module to read the HDF5 file on runtime and to define the corresponding interpolation function that is based on that file’s tabular data. I currently have it defined in a function, but I can’t figure out how to avoid multiple instances of reading the HDF5 file and creating the interpolating function, which happens for each random number generation (without defining a global variable that is particular instance of this function). Keep in mind I defined a
rand(rng::AbstractRNG, d::MyContinuousUnivariateDistributionType) method in the style of Distributions.jl, which then allows me to use the inplace and the array version of this method, and I wish to keep that functionality.
I’ve tried RuntimeGeneratedFunctions.jl, but it didn’t work, which I suspect is because this is akin to a closure. I have yet to try GeneralizedGenerated.jl.
Here’s a sketch of what is going on (doesn’t run, I will have to write a script to produce an artificial file if necessary):
module MyModule import HDF5 import Interpolations function read_truncations_fromfile() truncfile = HDF5.h5open(dirname(@__DIR__) * "/data/rng_truncations.h5") # println("reading from truncations table") # used this to detect multiple reads grids = truncfile["grids"] nodes = (HDF5.read(grids["alpha"]), HDF5.read(grids["c"]), HDF5.read(grids["theta"]) ) itp = Interpolations.interpolate(nodes, HDF5.read(truncfile["truncations"]), Interpolations.Gridded(Interpolations.Linear()) ) HDF5.close(truncfile) return itp end end
If I define
function truncation(α::Real, c::Real, θ::Real) itp = read_truncations_fromfile() return itp(α, c, θ) end
It will read the file and construct the interpolating function every time
truncation is called, so this seems useless.
If I simply declare a variable that is an instance of the function in the middle of the module
truncation = read_truncations_fromfile()
It seems to work, but I am wary of doing this.