# How do I parallelize a while loop?

**URL:** <https://discourse.julialang.org/t/how-do-i-parallelize-a-while-loop/99053>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [May 18, 2023, 11:40am UTC](https://discourse.julialang.org/t/how-do-i-parallelize-a-while-loop/99053 "2023-05-18T11:40:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_231578](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user_231578/32/24170_2.png) [@user\_231578](https://discourse.julialang.org/u/user_231578)\
**Post date:** [May 18, 2023, 11:40am UTC](https://discourse.julialang.org/t/how-do-i-parallelize-a-while-loop/99053/1 "2023-05-18T11:40:33Z")

</div>

I am trying to parallelize a while loop in Julia but I am not sure where to start.

For example, suppose I have the following loop:

```julia
A = []
i = 0
while i < 5000000
	j = rand(1:5000000) 
	if j == i 
		push!(A,i)
		i = 0
	else
		i += 1
	end
end

```

And suppose that I want to run this while loop 1000 times, each time updating the same vector A. How can I parallelize this task?

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 18, 2023, 12:14pm UTC](https://discourse.julialang.org/t/how-do-i-parallelize-a-while-loop/99053/2 "2023-05-18T12:14:07Z")

</div>

I’m not familiar with how, but SO had [a question similar to this](https://stackoverflow.com/questions/51459459/parallel-computing-in-julia-running-a-simple-for-loop-on-multiple-cores). There is an illuminating comment at the bottom by @ChrisRackauckas as well.

Also [here on this board](https://discourse.julialang.org/t/fastest-way-to-run-a-for-loop-in-parallel/52477/3).  
And a recent presentation at [UIC](http://homepages.math.uic.edu/~jan/mcs507/paralleljulia.pdf).

---

<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:** [May 18, 2023, 12:18pm UTC](https://discourse.julialang.org/t/how-do-i-parallelize-a-while-loop/99053/3 "2023-05-18T12:18:17Z")

</div>

Where to start: [Multi-Threading · The Julia Language](https://docs.julialang.org/en/v1/base/multi-threading/)

(and then, it is better if you provide a more realistic example of what you want to do)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 18, 2023, 12:37pm UTC](https://discourse.julialang.org/t/how-do-i-parallelize-a-while-loop/99053/4 "2023-05-18T12:37:49Z")

</div>

> [@user\_231578](#):
>
> And suppose that I want to run this while loop 1000 times, each time updating the same vector A. How can I parallelize this task?

If you want multiple threads to push to the same array `A` in parallel, be aware that you’ll need to [use some kind of lock](https://docs.julialang.org/en/v1/manual/multi-threading/#Data-race-freedom). Alternatively, each thread could push to a different array, and then you could merge them at the end.

Of course, if your code really looks similar to the above, you can probably save a lot of time by careful use of statistics. It might be instructive to look at the [implementation of `randsubseq`](https://github.com/JuliaLang/julia/blob/ce3909cc8ce6dd7ee3d11ef98ce7ff075a5604d6/stdlib/Random/src/misc.jl#L96-L134) to see how it gains efficiency by not iterating over every element, but instead by sampling from the probability distribution on the number of iterations between one `push!` and the next. You can probably do something similar here.

PS. `A = []` allocates an array of `Any`, which is an [abstractly typed container](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container). You probably want `A = Int[]` so that Julia knows that the elements are `Int`.
