# Can ModelingToolkit be used with GPU arrays?

**URL:** https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352
**Category:** Modelling & Simulations
**Created:** [December 4, 2025, 10:00am UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352 "2025-12-04T10:00:09Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [December 4, 2025, 10:00am UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/1 "2025-12-04T10:00:09Z")

</div>

Hello, everyone. I’m trying to solve a complex system using ModelingToolkit, which involves an array variable. One of the equations is quite heavy computationally and I was trying to pass this array as an argument, then build a GPU array (CUDA in this case, I don’t have more GPUs now), do the calculation and return the value back in the original format. The problem is that symbolic variables are of type `Num` and they are not `isbitstype`.

I then decided to try a small example and build a simple model with a CuArray with the idea of knowing if I can build everything directly:

```julia-auto
using CUDA, ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

x0 = ones(2)
x0CU = CuArray(x0)
A = Float64[2 1; 1 2]
ACU = CuArray(A)

@mtkmodel Example begin
    @variables begin
        x(t)[1:2] = x0
    end
    @equations begin
        D(x) ~ A * x
    end
end

@mtkcompile example = Example()

@mtkmodel ExampleCU begin
    @variables begin
        x(t)[1:2] = x0CU
    end
    @equations begin
        D(x) ~ derivative(x, ACU)
    end
end

@mtkcompile exampleCU = ExampleCU()

```

The `example` model is compiled correctly on the CPU, but the `exampleCU` one for the GPU does not compile since it throws the scalar indexing error

```julia-auto
ERROR: Scalar indexing is disallowed.

```

Note that something like

```julia-auto
ACU * x0CU

```

outside of `@mtkmodel` works and returns a CuArray as a result.

Is there a way in ModelingToolkit for me to take a symbolic array, convert it to the desired numerical format for me to parallelize it and then return it back to MTK with the original type so the program can continue the evaluation? Or am I restricted to, at most, CPU parallelization?

---

<div class="post-metadata">

### Author: ![hersle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hersle/32/211689_2.png) [@hersle](https://discourse.julialang.org/u/hersle)
#### Post date: [December 4, 2025, 11:02am UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/2 "2025-12-04T11:02:23Z")

</div>

I would try to not set `x(t)[1:2] = x0CU` in the `@mtkmodel`, but just declare `@variables x(t)[1:2]` without setting the default value. Instead set `x` when you create the `ODEProblem`.

I think the `@mtkmodel` step should be the same for the CPU and GPU. Just create one system and use that in both cases. The `Num` should not get transferred to the CPU; this is just a symbolic type that is used for the symbolic-to-numeric code generation. The CPU/GPU specialization happens first when you create and solve an `ODEProblem`.

I have not tried to run MTK code on the GPU myself, but you could have a look at [this test](https://github.com/SciML/ModelingToolkit.jl/blob/6a34ef8a2b48b6c1a02b2eb1274cd0057e768036/test/mtkparameters.jl#L433) and build from there. Maybe there also exists a fully documented and working example somewhere that I am not aware of?

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [December 4, 2025, 3:10pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/3 "2025-12-04T15:10:57Z")

</div>

I can start working from there and define the initial values like they do, thanks!

I guess I also have to rethink how I implement what I want. Because that expensive function makes use of StaticArrays (I’m using a vector of 3x3 SMatrix) and when I implement it into MTK it fails since it doesn’t understand how to multiply a `Num` with an `SMatrix` and then sum another `SMatrix`. I’ve tried using `@register_array_symbolic` without success.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 4, 2025, 3:23pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/4 "2025-12-04T15:23:06Z")

</div>

Not yet, we’re working on a GPU codegen and DiffEqGPU support

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [December 4, 2025, 3:59pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/5 "2025-12-04T15:59:27Z")

</div>

I see, thanks! Do you have any estimated timeline about that? I’ll continue with CPU for now.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 5, 2025, 12:11pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/6 "2025-12-05T12:11:03Z")

</div>

We’ll have a big post coming up soon with lots of things about ModelingToolkit v11 changes and how that effects much of the coming development, including Symbolics code generation to GPUs.

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [December 5, 2025, 1:51pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/7 "2025-12-05T13:51:55Z")

</div>

Amazing, thanks Chris!

May I ask an offtopic question? Are StaticArrays supported by ModelingToolkit, i.e., a symbolic variable `x(t)` represents for example a 3x3 `SMatrix` and likewise a symbolic array `x(t)[1:10]` represents a `Vector{SMatrix}`?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 5, 2025, 2:10pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/8 "2025-12-05T14:10:21Z")

</div>

> [@pmc4](#):
>
> May I ask an offtopic question? Are StaticArrays supported by ModelingToolkit, i.e., a symbolic variable `x(t)` represents for example a 3x3 `SMatrix` and likewise a symbolic array `x(t)[1:10]` represents a `Vector{SMatrix}`?

The new codegen knows how to use them in the generated code when appropriate as an optimization, so the user doesn’t really need to care.

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [December 5, 2025, 2:37pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/9 "2025-12-05T14:37:32Z")

</div>

> [@ChrisRackauckas](#):
>
> The new codegen knows how to use them in the generated code when appropriate as an optimization, so the user doesn’t really need to care.

Then I’ve messed up something quite well. I’ll review it, thanks!

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 5, 2025, 9:13pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/10 "2025-12-05T21:13:37Z")

</div>

Oh no worries, the new codegen requires MTK v11 so no one is using it yet 😅 I’m just saying it’s in there already.

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [December 6, 2025, 7:42pm UTC](https://discourse.julialang.org/t/can-modelingtoolkit-be-used-with-gpu-arrays/134352/11 "2025-12-06T19:42:39Z")

</div>

Perfect then, I’ll eagerly wait for it. Thanks!
