Skip to content

About

Type-stable opaque parameter containers for SciML solvers: keep typeof(p) uniform across underlying payload types, recover the concrete type inside f without allocation.

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

53 Commits

Folders and files

Repository files navigation

RespecializeParams.jl

Join the chat at https://julialang.zulipchat.com #sciml-bridged Global Docs

codecov CI

ColPrac: Contributor's Guide on Collaborative Practices for Community Packages SciML Code Style

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:

  • OpaqueParams for isbits payloads (backed by Vector{UInt8}; unpack is a single unsafe_load).
  • OpaqueRef for arbitrary payloads (backed by Ref{Any}; unpack is a pointer load + ::T assertion).

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.

Installation

using Pkg
Pkg.add("RespecializeParams")

Quick example

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.

About

Type-stable opaque parameter containers for SciML solvers: keep typeof(p) uniform across underlying payload types, recover the concrete type inside f without allocation.

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages