Skip to content

Kernels for SimpleTauLeaping#490

Merged
ChrisRackauckas merged 19 commits into
SciML:masterfrom
sivasathyaseeelan:kernel-simpletauleaping
Jul 11, 2025
Merged

Kernels for SimpleTauLeaping#490
ChrisRackauckas merged 19 commits into
SciML:masterfrom
sivasathyaseeelan:kernel-simpletauleaping

Conversation

@sivasathyaseeelan
Copy link
Copy Markdown
Contributor

@sivasathyaseeelan sivasathyaseeelan commented Jun 20, 2025

using JumpProcesses, DiffEqBase, Plots
using Test, LinearAlgebra
using KernelAbstractions, Adapt, CUDA
using StableRNGs
rng = StableRNG(12345)

# SEIR model with exposed compartment
β = 0.3 / 1000.0
σ = 0.2
ν = 0.01
p = (β, σ, ν)

regular_rate = (out, u, p, t) -> begin
    out[1] = p[1] * u[1] * u[3]  # β*S*I (infection)
    out[2] = p[2] * u[2]         # σ*E (progression)
    out[3] = p[3] * u[3]         # ν*I (recovery)
end

regular_c = (dc, u, p, t, counts, mark) -> begin
    dc .= 0.0
    dc[1] = -counts[1]           # S: -infection
    dc[2] = counts[1] - counts[2] # E: +infection - progression
    dc[3] = counts[2] - counts[3] # I: +progression - recovery
    dc[4] = counts[3]            # R: +recovery
end

 # Initial state
u0 = [999.0, 0.0, 10.0, 0.0]  # S, E, I, R
tspan = (0.0, 250.0)

# Create JumpProblem
prob_disc = DiscreteProblem(u0, tspan, p)
rj = RegularJump(regular_rate, regular_c, 3)
jump_prob = JumpProblem(prob_disc, Direct(), rj; rng=StableRNG(12345))
sol = solve(EnsembleProblem(jump_prob), SimpleTauLeaping(), EnsembleGPUKernel(CUDABackend()); trajectories = 10, dt = 1.0)
plot(sol)

Screenshot from 2025-06-26 00-08-35

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

Comment thread Project.toml Outdated
@isaacsas
Copy link
Copy Markdown
Member

Can Github CI test GPU code for free?

@ChrisRackauckas
Copy link
Copy Markdown
Member

No we have to setup the Buildkite

@ChrisRackauckas
Copy link
Copy Markdown
Member

@ChrisRackauckas
Copy link
Copy Markdown
Member

Rebase onto latest master and accept the new runtests.yml and buildkite changes. With that, then add gpu tests into the gpu folder

Comment thread ext/JumpProcessesExt.jl Outdated
Comment thread ext/JumpProcessesExt.jl Outdated
Comment thread ext/simple_regular_solve.jl Outdated
Comment thread ext/simple_regular_solve.jl Outdated
Comment thread ext/simple_regular_solve.jl Outdated
Comment thread test/regular_jumps.jl Outdated
sivasathyaseeelan and others added 4 commits June 25, 2025 18:21
Co-authored-by: Christopher Rackauckas <accounts@chrisrackauckas.com>
Comment thread ext/JumpProcessesExt.jl Outdated
Comment thread Project.toml Outdated
Comment thread ext/JumpProcessesExt.jl
Comment thread ext/JumpProcessesKernelAbstractionsExt.jl Outdated

# Poisson sampling
@inbounds for k in 1:num_jumps
counts[k] = pois_rand(rng, rate_cache[k])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.nvidia.com/cuda/curand/device-api-overview.html

CUDA has a builtin Poisson RNG, wouldn't we expect that to be more appropriate for GPU use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but in that case we can't use kernels for other GPUs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AMD offers it too in ROCRAND: differentiable programming for odes review. Can we not dispatch between NVDIA vs. AMD, and otherwise then fall back to pois_rand if in neither of those? I'd imagine there might be substantial performance benefits to using the vendor provided versions.

Comment thread test/gpu/regular_jumps.jl
Copy link
Copy Markdown
Member

@isaacsas isaacsas Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs some actual correctness tests for statistics of the obtained solutions (compare to CPU tau-leaping or Gillespie). All this is testing right now is that the code doesn't crash.

Copy link
Copy Markdown
Contributor Author

@sivasathyaseeelan sivasathyaseeelan Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that needs updating too.

local_dc_buf = allocate(backend, Float64, (state_dim, n_trajectories))

# Initialize current_u_buf with u0 values
@kernel function init_buffers_kernel(@Const(probs_data), current_u_buf)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should probably just be on CPU

Copy link
Copy Markdown
Contributor Author

@sivasathyaseeelan sivasathyaseeelan Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be done in CPU as we are allocating GPU memory (for current_u_buf) which can only be accessed inside kernels

@isaacsas
Copy link
Copy Markdown
Member

isaacsas commented Jul 2, 2025

Sorry, I don’t really have much gpu experience so I’ll have to leave this for @ChrisRackauckas to review.

end

# GPU-compatible Poisson sampling
@inline function poisson_rand(lambda::T) where T
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you make the upstream issue on PoissonRandom.jl?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I thought @oscardssmith will take that. I will create an issue now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread test/gpu/regular_jumps.jl Outdated
@ChrisRackauckas
Copy link
Copy Markdown
Member

Buildkite doesn't run on forks, so what we can do here is merge as the code seems finished now, but I'll open a master test to double check the test passes.

@ChrisRackauckas ChrisRackauckas merged commit d1d2544 into SciML:master Jul 11, 2025
8 checks passed
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

Successfully merging this pull request may close these issues.

4 participants