# Parallel programming loops

**URL:** <https://discourse.julialang.org/t/parallel-programming-loops/82284>\
**Category:** New to Julia\
**Created:** [June 5, 2022, 8:54pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284 "2022-06-05T20:54:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 5, 2022, 8:54pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284/1 "2022-06-05T20:54:25Z")

</div>

Hi  
I am new to parallel programming concept and I do not know how to make this piece of my code runs parallel

```julia

scatter([Person[i]["x"] for i in 1:Npopulations], [Person[i]["y"] for i in1:Npopulations],legend=false, color = [Person[i]["color"] for i in 1:Npopulations]) 
 

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [June 6, 2022, 7:38pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284/2 "2022-06-06T19:38:01Z")

</div>

Could you post a simplified example that illustrates your problem? It is easiest to help if it is possible for one of us to paste the code into our Julia REPL and see the problem without having to edit anything (test it in your own REPL to make sure that you have no typos in it). You could also try to post this in the “General Usage” forum as it sounds like quite an advanced question.

---

<div class="post-metadata">

**Author:** ![Mahmoud](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mahmoud](https://discourse.julialang.org/u/Mahmoud)\
**Post date:** [June 6, 2022, 7:58pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284/3 "2022-06-06T19:58:23Z")

</div>

just edited it.

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [June 6, 2022, 9:18pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284/4 "2022-06-06T21:18:48Z")

</div>

Okay, it still does not run in a newly started Julia, what I meant was something like the code shown below. Is this a good example of what you want to parallelize?:

```julia
using Plots, Random

x=collect(1:1000)
anim = @animate for ist=1:100
	y=rand(1000)
	scatter(x, y)
end
gif(anim,"rand.gif", fps=3)

```

I can’t answer this for you, but there was a post about it in 2020 [Animation with threads in Plots.jl?](https://discourse.julialang.org/t/animation-with-threads-in-plots-jl/33958) . I would recommend that you post in “General Usage” and see if anyone there can help. Use a title like “can I parallelize animated gifs?”.

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [June 6, 2022, 10:43pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284/5 "2022-06-06T22:43:17Z")

</div>

Try creating arrays to store the results first and then use Threads.@threads on the for loop:

```julia
x = Vector{Any}(undef, Npopulations)
y = similar(x)
colors = Vector{Any}(undef, Npopulations)
Threads.@threads for i in 1:Npopulations
x[i] = Person[i]["x"]
y[i] = Person[i]["y"]
colors[i] = Person[i]["color"]
end
scatter(x, y,legend=false, color = colors)

```

Be sure to change the type Any to the appropriate type, this is for speed so each element isn’t boxed. You should make sure that all elements of x and y are the same type. Each element of colours should have matching types too.

From the looks of this, it doesn’t seem like doing this in parallel will speed it up by much, but it will create the intermediate arrays in parallel, but since there doesn’t appear to be any significant processing (other than a dictionary lookup which is pretty fast), you may actually experience a slowdown, and the code is now harder to read and less maintainable.

P.s. check Threads.nthreads() to see if you have threads available for parallelism.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [June 6, 2022, 10:57pm UTC](https://discourse.julialang.org/t/parallel-programming-loops/82284/6 "2022-06-06T22:57:52Z")

</div>

Before trying to parallelize, please first try to optimize your code to figure out where the bottlenecks are. There are many different parallelization strategies (multiprocessing, multithreading, GPUs, clusters), and using any of them requires understanding and eliminating those bottlenecks. What’s the runtime of your code at the moment? What functions dominate the runtime? Have you read through and applied any relevant [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/)?

Separately, see this PSA on how to construct a minimal reproducible example for others to run: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)
