-
-
Notifications
You must be signed in to change notification settings - Fork 35
Add DAE support for GPU kernels with mass matrices and initialization #361
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
Open
ChrisRackauckas
wants to merge
7
commits into
master
Choose a base branch
from
add-dae-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+272
−14
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed55709
Add DAE support for GPU kernels with mass matrices and initialization
claude 3bedccc
Simplify initialization to use SimpleNonlinearSolve directly
claude 1532ce1
Add ModelingToolkit cartesian pendulum DAE test
claude afc5727
Integrate ModelingToolkit DAE test into test suite
claude 2a4db5c
Add OverrideInitData adapt_structure for GPU compatibility
claude c2654eb
Update src/DiffEqGPU.jl
ChrisRackauckas f7ab9d5
try fixing Ws
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Override SciMLBase adapt functions to allow DAEs for GPU kernels | ||
import SciMLBase: adapt_structure | ||
import Adapt | ||
|
||
# Allow DAE adaptation for GPU kernels | ||
function adapt_structure(to, f::SciMLBase.ODEFunction{iip}) where {iip} | ||
# For GPU kernels, we now support DAEs with mass matrices and initialization | ||
SciMLBase.ODEFunction{iip, SciMLBase.FullSpecialize}( | ||
f.f, | ||
jac = f.jac, | ||
mass_matrix = f.mass_matrix, | ||
initialization_data = f.initialization_data | ||
) | ||
end | ||
|
||
# Adapt OverrideInitData for GPU compatibility | ||
function adapt_structure(to, f::SciMLBase.OverrideInitData) | ||
SciMLBase.OverrideInitData( | ||
adapt(to, f.initializeprob), # Also adapt initializeprob | ||
f.update_initializeprob!, | ||
f.initializeprobmap, | ||
f.initializeprobpmap, | ||
nothing, # Set metadata to nothing for GPU compatibility | ||
f.is_update_oop | ||
) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
@inline function gpu_initialization_solve(prob, nlsolve_alg, abstol, reltol) | ||
f = prob.f | ||
u0 = prob.u0 | ||
p = prob.p | ||
|
||
# Check if initialization is actually needed | ||
if !SciMLBase.has_initialization_data(f) || f.initialization_data === nothing | ||
return u0, p, true | ||
end | ||
|
||
initdata = f.initialization_data | ||
if initdata.initializeprob === nothing | ||
return u0, p, true | ||
end | ||
|
||
# Use SimpleNonlinearSolve directly - it's GPU compatible | ||
try | ||
# Default to SimpleTrustRegion if no algorithm specified | ||
alg = nlsolve_alg === nothing ? SimpleTrustRegion() : nlsolve_alg | ||
|
||
# Create initialization problem | ||
initprob = initdata.initializeprob | ||
|
||
# Update the problem if needed | ||
if initdata.update_initializeprob! !== nothing | ||
if initdata.is_update_oop === Val(true) | ||
initprob = initdata.update_initializeprob!(initprob, (u=u0, p=p)) | ||
else | ||
initdata.update_initializeprob!(initprob, (u=u0, p=p)) | ||
end | ||
end | ||
|
||
# Solve initialization problem using SimpleNonlinearSolve | ||
sol = solve(initprob, alg; abstol, reltol) | ||
|
||
# Extract results | ||
if SciMLBase.successful_retcode(sol) | ||
# Apply result mappings if they exist | ||
u_init = if initdata.initializeprobmap !== nothing | ||
initdata.initializeprobmap(sol) | ||
else | ||
u0 | ||
end | ||
|
||
p_init = if initdata.initializeprobpmap !== nothing | ||
initdata.initializeprobpmap((u=u0, p=p), sol) | ||
else | ||
p | ||
end | ||
|
||
return u_init, p_init, true | ||
else | ||
# If initialization fails, use original values | ||
return u0, p, false | ||
end | ||
catch | ||
# If anything goes wrong, fall back to original values | ||
return u0, p, false | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/gpu_kernel_de/stiff_ode/gpu_ode_modelingtoolkit_dae.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using ModelingToolkit, DiffEqGPU, OrdinaryDiffEq, LinearAlgebra, Test | ||
using ModelingToolkit: t_nounits as t, D_nounits as D | ||
using KernelAbstractions: CPU | ||
|
||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# ModelingToolkit problems are too complex for GPU array adaptation, | ||
# so we use CPU backend for DAE testing | ||
const backend = CPU() | ||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Define the cartesian pendulum DAE system | ||
@parameters g = 9.81 L = 1.0 | ||
@variables x(t) y(t) [state_priority = 10] λ(t) | ||
|
||
# The cartesian pendulum DAE system: | ||
# m*ddot(x) = (x/L)*λ (simplified with m=1) | ||
# m*ddot(y) = (y/L)*λ - mg (simplified with m=1) | ||
# x^2 + y^2 = L^2 (constraint equation) | ||
eqs = [D(D(x)) ~ λ * x / L | ||
D(D(y)) ~ λ * y / L - g | ||
x^2 + y^2 ~ L^2] | ||
|
||
@named pendulum = ODESystem(eqs, t, [x, y, λ], [g, L]) | ||
|
||
# Perform structural simplification with index reduction | ||
pendulum_sys = structural_simplify(dae_index_lowering(pendulum)) | ||
|
||
# Initial conditions: pendulum starts at bottom right position | ||
u0 = [x => 1.0, y => 0.0, λ => -g] # λ initial guess for tension | ||
|
||
# Time span | ||
tspan = (0.0f0, 1.0f0) | ||
|
||
# Create the ODE problem | ||
prob = ODEProblem(pendulum_sys, u0, tspan, Float32[]) | ||
|
||
# Verify DAE properties | ||
@test SciMLBase.has_initialization_data(prob.f) == true | ||
@test prob.f.mass_matrix !== nothing | ||
|
||
# Create ensemble problem for GPU testing | ||
monteprob = EnsembleProblem(prob, safetycopy = false) | ||
|
||
# Test with GPURosenbrock23 | ||
sol = solve(monteprob, GPURosenbrock23(), EnsembleGPUKernel(backend), | ||
trajectories = 2, | ||
dt = 0.01f0, | ||
adaptive = false) | ||
|
||
@test length(sol.u) == 2 | ||
|
||
# Check constraint satisfaction: x^2 + y^2 ≈ L^2 | ||
final_state = sol.u[1][end] | ||
x_final, y_final = final_state[1], final_state[2] | ||
constraint_error = abs(x_final^2 + y_final^2 - 1.0f0) | ||
@test constraint_error < 0.1f0 # Reasonable tolerance for fixed timestep | ||
|
||
# Test with GPURodas4 | ||
sol2 = solve(monteprob, GPURodas4(), EnsembleGPUKernel(backend), | ||
trajectories = 2, | ||
dt = 0.01f0, | ||
adaptive = false) | ||
|
||
@test length(sol2.u) == 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.