# Parallel computation to speed up for loops

**URL:** <https://discourse.julialang.org/t/parallel-computation-to-speed-up-for-loops/126006>\
**Category:** General Usage\
**Tags:** distributed, sharedarrays, parallel-computing\
**Created:** [February 17, 2025, 6:19pm UTC](https://discourse.julialang.org/t/parallel-computation-to-speed-up-for-loops/126006 "2025-02-17T18:19:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AP9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ap9/32/212443_2.png) [@AP9](https://discourse.julialang.org/u/AP9)\
**Post date:** [February 17, 2025, 6:19pm UTC](https://discourse.julialang.org/t/parallel-computation-to-speed-up-for-loops/126006/1 "2025-02-17T18:19:11Z")

</div>

I am computing the Lyapunov exponents in 2D for the system below, by doing a for loop over two parameters (ωA, A) and it takes a really long time.

```julia
using Parameters, ChaosTools, DifferentialEquations, Distributed, SharedArrays
function System!(du, u, p, t = 0)
       @unpack ω, ω_A, A = p
        x, y = u
	s = x^2 + y^2
	du[1] = x + ω * y - s * x - A * sin(ω_A * t)
	du[2] = -ω * x + y - s * y
	du
end

@with_kw mutable struct Params
        ω::Real = 2pi
        ω_A::Real = 2pi
        A :: Real = 10
end

```

I am using the `Distributed` and `SharedArrays` packages for parallel computation. However, I am not sure if it is running in parallel since computing one exponent takes only a couple of minutes. How can I speed up the code below?

```julia
tmax = 10000
t_trans = 1000
ω_As = par.ω .* range(0.01, 5, 100)
As = range(0.01, 15, 100)
par = Params()

Λ = SharedArray{Float64, 2}(length(As), length(ω_As))
@sync @distributed for i in eachindex(As)
    par.A = As[i]
    for j in eachindex(ω_As)
        ω_A = ω_As[j]
        par.ω_A = ω_A
        ds = CoupledODEs(System!, [0., 1.], par)
        λs = lyapunovspectrum(ds, tmax; Ttr = t_trans, Δt = 1)
        λs_nonzero = filter(x -> abs.(x) > 1e-8, λs)
        Λ[i,j] = maximum(λs_nonzero)
    end
end

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 17, 2025, 9:00pm UTC](https://discourse.julialang.org/t/parallel-computation-to-speed-up-for-loops/126006/2 "2025-02-17T21:00:58Z")

</div>

> [@AP9](#):
>
> ```julia
> @with_kw mutable struct Params
> ω::Real = 2pi
> ω_A::Real = 2pi
> A :: Real = 10
> end
> 
> ```

A nice performance boost should result from changing this struct to:

```julia
@with_kw mutable struct Params
        ω::Float64 = 2pi
        ω_A::Float64 = 2pi
        A :: Float64= 10
end

```

> [@AP9](#):
>
> However, I am not sure if it is running in parallel

If you don’t add any workers (either via `addprocs` or the commandline argument) then it won’t be parallel.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 18, 2025, 1:15pm UTC](https://discourse.julialang.org/t/parallel-computation-to-speed-up-for-loops/126006/3 "2025-02-18T13:15:51Z")

</div>

> [@abraemer](#):
>
> A nice performance boost should result from changing this struct to:
> 
> ```julia
> @with_kw mutable struct Params
> ω::Float64 = 2pi
> ω_A::Float64 = 2pi
> A :: Float64= 10
> end
> 
> ```

I believe it will be even more efficient if you make it immutable. It doesn’t look like there should be any problems with making them immutable, and in fact it should be much safer in a parallelized environment.

> [@AP9](#):
>
> How can I speed up the code below?

First of all, put the code in a function(!) Secondly, read the performance tips section of the manual: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

Make a fast single-threaded code. After that you can look into parallelization.

Are you running this on multiple nodes? If not, why not use multithreading instead of multiprocessing?

---

<div class="post-metadata">

**Author:** ![AP9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ap9/32/212443_2.png) [@AP9](https://discourse.julialang.org/u/AP9)\
**Post date:** [February 18, 2025, 7:00pm UTC](https://discourse.julialang.org/t/parallel-computation-to-speed-up-for-loops/126006/4 "2025-02-18T19:00:02Z")

</div>

Thank you! It’s much faster now!
