Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial utilising MTK to highlight the utility of DiffEqGPU.jl #330

Open
H-Sax opened this issue Jul 12, 2024 · 2 comments
Open

Tutorial utilising MTK to highlight the utility of DiffEqGPU.jl #330

H-Sax opened this issue Jul 12, 2024 · 2 comments

Comments

@H-Sax
Copy link

H-Sax commented Jul 12, 2024

Is your feature request related to a problem? Please describe.

It would be nice to see a tutorial where MTK examples show how to create a GPU compatible form to utilise the DiffEqGPU solvers

@ChrisRackauckas
Copy link
Member

The simple thing:

using OrdinaryDiffEq, ModelingToolkit
using SymbolicIndexingInterface
using ModelingToolkit: t_nounits as t, D_nounits as D

@parameters σ ρ β
@variables x(t) y(t) z(t)

eqs = [D(D(x)) ~ σ * (y - x),
    D(y) ~ x *- z) - y,
    D(z) ~ x * y - β * z]

@mtkbuild sys = ODESystem(eqs, t)

u0 = [D(x) => 2f0,
    x => 1f0,
    y => 0f0,
    z => 0f0]

p ==> 28f0,
    ρ => 10f0,
    β => 8f0 / 3f0]

tspan = (0f0, 100f0)
prob = ODEProblem(sys, u0, tspan, p, jac = true)

prob_func = function (prob, i, repeat)
	prob = deepcopy(prob)
	prob.p.tunable .= rand(Float32, 3) .* prob.p.tunable
	prob
end

monteprob = EnsembleProblem(prob, prob_func = prob_func, safetycopy = false)
sol = solve(monteprob, Tsit5(), EnsembleGPUArray(CUDA.CUDABackend()), trajectories = 10_000,
    saveat = 1.0f0);

Should just work. But you probably don't want to remake using such low level stuff and instead use the SymbolicIndexingInterface. For that you probably want the following:

probchanger = SymbolicIndexingInterface.setp_oop(prob, [σ, ρ, β]);
prob_func = (prob, i, repeat) -> (probchanger(prob, rand(Float32, 3) .* [28f0, 10f0, 8f0/3f0]); prob)
monteprob = EnsembleProblem(prob, prob_func = prob_func, safetycopy = false)
sol = solve(monteprob, Tsit5(), EnsembleThreads(), trajectories = 10_000, saveat = 1.0f0);

using DiffEqGPU, CUDA
sol = solve(monteprob, Tsit5(), EnsembleGPUArray(CUDA.CUDABackend()), trajectories = 10_000,
    saveat = 1.0f0);

@AayushSabharwal is going to improve setsym_oop which allows for mixing initial conditions and parameters in what's set to make it even easier. I still need to test this works in a GPU kernel too, you might need to flat eval of the RGF so it's cache-free.

@utkarsh530
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants