RespecializeParams.jl provides type-stable opaque parameter containers for SciML
solvers. The goal is to keep typeof(p) uniform across underlying payload
types — so a precompiled solver path is shared — while still recovering the
concrete payload type inside f for fully specialized inner kernels.
Two containers are exported:
OpaqueParamsforisbitspayloads (backed byVector{UInt8};unpackis a singleunsafe_load).OpaqueReffor arbitrary payloads (backed byRef{Any};unpackis a pointer load +::Tassertion).
Both wrappers have a fixed Julia type regardless of the payload, so a solver
dispatching on typeof(p) hits the same precompiled code path across
problems whose underlying parameter structs differ. Inside f, unpack is
type-stable and allocation-free.
using Pkg
Pkg.add("RespecializeParams")using RespecializeParams, OrdinaryDiffEqTsit5
struct LorenzP
σ::Float64
ρ::Float64
β::Float64
end
struct OpaqueRHS{T, F} <: Function
f::F
end
OpaqueRHS(::Type{T}, f::F) where {T, F} = OpaqueRHS{T, F}(f)
@inline function (r::OpaqueRHS{T})(du, u, op::OpaqueParams, t) where {T}
p = unpack(op, T)
r.f(du, u, p, t)
return nothing
end
function lorenz_kernel!(du, u, p::LorenzP, t)
du[1] = p.σ * (u[2] - u[1])
du[2] = u[1] * (p.ρ - u[3]) - u[2]
du[3] = u[1] * u[2] - p.β * u[3]
return nothing
end
prob = ODEProblem(
OpaqueRHS(LorenzP, lorenz_kernel!),
[1.0, 0.0, 0.0],
(0.0, 5.0),
pack(LorenzP(10.0, 28.0, 8 / 3)),
)
solve(prob, Tsit5())See the documentation for
the full API, the non-isbits OpaqueRef story, and design notes.