# Scalar indexing is disallowed - ODE solve using GPU

**URL:** <https://discourse.julialang.org/t/scalar-indexing-is-disallowed-ode-solve-using-gpu/121645>\
**Category:** New to Julia\
**Tags:** gpu, ode, differentialequation\
**Created:** [October 23, 2024, 2:08pm UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed-ode-solve-using-gpu/121645 "2024-10-23T14:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![agme2014](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/agme2014/32/212791_2.png) [@agme2014](https://discourse.julialang.org/u/agme2014)\
**Post date:** [October 23, 2024, 2:08pm UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed-ode-solve-using-gpu/121645/1 "2024-10-23T14:08:13Z")

</div>

Hi, I want to use NVIDIA GPU in my laptop to speed up Neural ODE computation. I started with testing out GPU on ODEs but I am getting the below error during the “solve” step :  
_ERROR: Scalar indexing is disallowed._  
_Invocation of getindex resulted in scalar indexing of a GPU array._  
_This is typically caused by calling an iterating implementation of a method._  
_Such implementations do not execute on the GPU, but very slowly on the CPU,_  
_and therefore should be avoided._

_If you want to allow scalar iteration, use `allowscalar` or `@allowscalar`_  
_to enable scalar iteration globally or for the operations in question._

The code is here :

```using
using DiffEqFlux
using Lux
using Optimization
using OptimizationOptimJL
using OptimizationOptimisers
using OrdinaryDiffEq
using Plots
using Random
using CUDA
using LinearAlgebra
println("Use NN to solve SIR ODE model")

const cdev = cpu_device()
const gdev = gpu_device()

# u = [s(t), I(t), R(t)]
function trueSirModel!(du, u, p, t)
    beta, gamma, N = p
    du[1] = -(beta * u[1] * u[2]) / N
    du[2] = ((beta * u[1] * u[2]) / N) - (gamma * u[2])
    du[3] = gamma * u[2]
end

# Boundary conditions

N = 1000
i0 = 1
r0 = 0
s0 = (N - i0 - r0)
u0 = cu(Float32[s0, i0, r0])

# constants
beta = 0.3
gamma = 0.1
p = cu(Float32[beta, gamma, N])
# time duration
tspan = (0.0, 160.0)
datasize = 160
tsteps = cu(range(tspan[1], tspan[2]; length=datasize))

# Solving the ODE solution

trueOdeProblem = ODEProblem(trueSirModel!, u0, tspan, p)
sol = solve(trueOdeProblem, Tsit5(), saveat=tsteps) 

```

I have been reading the online help / tutorials but still could not find a solution. Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![agme2014](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/agme2014/32/212791_2.png) [@agme2014](https://discourse.julialang.org/u/agme2014)\
**Post date:** [October 23, 2024, 2:37pm UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed-ode-solve-using-gpu/121645/2 "2024-10-23T14:37:06Z")

</div>

I found a work around using CUDA.allowscalar(true)  
Deleted these :  
const cdev = cpu\_device()  
const gdev = gpu\_device()

---

<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:** [October 23, 2024, 3:11pm UTC](https://discourse.julialang.org/t/scalar-indexing-is-disallowed-ode-solve-using-gpu/121645/3 "2024-10-23T15:11:27Z")

</div>

But note that this case will be slow on GPUs. This is not an example that would use GPU parallelism very well, which is why the warning/error exists. You can disable the error, but that doesn’t change the problem.
