# Parallel run in for loop

**URL:** https://discourse.julialang.org/t/parallel-run-in-for-loop/58858
**Category:** General Usage
**Tags:** parallel
**Created:** [April 8, 2021, 5:04pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858 "2021-04-08T17:04:30Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Rasoul\_Jafarzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasoul_jafarzadeh/32/9967_2.png) [@Rasoul\_Jafarzadeh](https://discourse.julialang.org/u/Rasoul_Jafarzadeh)
#### Post date: [April 8, 2021, 5:04pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/1 "2021-04-08T17:04:31Z")

</div>

in my code I have 32 parameter, so I used 32 for loop which have 2 steps in each loop. 2^32 computation should be done and it takes long long time. how can I run them parallel to run fast?  
my code is this form  
c=[0,1];  
d=[0.1,0.5];  
for x1=c  
for x2=c  
…  
for x16=c  
for y1=d  
for y2=d  
for y3=d  
…  
for y16=d  
H= function of 32 parameter ;  
tout, ρₜ = steadystate.master(H, J);  
e2= expect(e2e2, ρₜ[end]);  
if e2\>0.3  
println(e2);  
end #(in this part I have some calculations to find the best values of 32 parameter )  
end  
…  
end

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 8, 2021, 5:13pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/2 "2021-04-08T17:13:11Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [April 8, 2021, 5:15pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/3 "2021-04-08T17:15:44Z")

</div>

Do you have 32 nested loops or just 32 separate loops? Could you give a bit more details about what you want to do so that we can try and help you? Sharing a MWE (Minimal Working Example) or just the code you are trying to run can help us a lot.

---

<div class="post-metadata">

### Author: ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)
#### Post date: [April 8, 2021, 5:35pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/4 "2021-04-08T17:35:23Z")

</div>

If it is something like independent samplings, then `Threads.@threads` is a good choice in front of the for loop.

---

<div class="post-metadata">

### Author: ![Rasoul\_Jafarzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasoul_jafarzadeh/32/9967_2.png) [@Rasoul\_Jafarzadeh](https://discourse.julialang.org/u/Rasoul_Jafarzadeh)
#### Post date: [April 8, 2021, 6:50pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/5 "2021-04-08T18:50:56Z")

</div>

Yes. It is nested loop. I used @distributed and @thread but there is not any useful result.

---

<div class="post-metadata">

### Author: ![Rasoul\_Jafarzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasoul_jafarzadeh/32/9967_2.png) [@Rasoul\_Jafarzadeh](https://discourse.julialang.org/u/Rasoul_Jafarzadeh)
#### Post date: [April 8, 2021, 6:54pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/6 "2021-04-08T18:54:27Z")

</div>

I’ve used it. But did not help me to do fast

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [April 8, 2021, 10:37pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/7 "2021-04-08T22:37:50Z")

</div>

You could try using FLoops.jl, or depending if your code supports it @avxt from LoopVectorization.jl

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 8, 2021, 11:00pm UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/8 "2021-04-08T23:00:17Z")

</div>

Maybe you can try using `Iterators.product` to convert the 32 loops into one loop over the combinations of parameters. Them, make this single loop parallel. But really with only that information is not easy to help.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [April 9, 2021, 1:22am UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/9 "2021-04-09T01:22:15Z")

</div>

FYI, [FLoops.jl](https://github.com/JuliaFolds/FLoops.jl) supports nested loops (as in `@floop ThreadedEx() for i in 1:32, j in 1:32`) and also `Iterators.product`.

---

<div class="post-metadata">

### Author: ![Rasoul\_Jafarzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasoul_jafarzadeh/32/9967_2.png) [@Rasoul\_Jafarzadeh](https://discourse.julialang.org/u/Rasoul_Jafarzadeh)
#### Post date: [April 9, 2021, 5:49am UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/10 "2021-04-09T05:49:03Z")

</div>

thanks.  
I edited my question to show form of my code.

---

<div class="post-metadata">

### Author: ![Rasoul\_Jafarzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rasoul_jafarzadeh/32/9967_2.png) [@Rasoul\_Jafarzadeh](https://discourse.julialang.org/u/Rasoul_Jafarzadeh)
#### Post date: [April 9, 2021, 6:12am UTC](https://discourse.julialang.org/t/parallel-run-in-for-loop/58858/11 "2021-04-09T06:12:29Z")

</div>

I edited my question to give more details. sorry, I am a beginner. thank you if you give me more explanation.
