# Using @distributed for loop

**URL:** https://discourse.julialang.org/t/using-distributed-for-loop/117831
**Category:** New to Julia
**Tags:** question, loops, parallel-computing
**Created:** [August 5, 2024, 8:08am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831 "2024-08-05T08:08:51Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [August 5, 2024, 8:08am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/1 "2024-08-05T08:08:51Z")

</div>

Hello everyone,

Can someone point out why following setup is not working ? Issue is that I want to copy function result in xm variable that is defined by @everywhere to not have issue with workers. But when I run loop it prints results perfectly but its copying locally beacuse everytime I try to execute `xm` on RFPL for its values. Its always all zeros.

```julia
@everywhere xm = zeros(10)

@sync @distributed for i in 1:10

    p = [0.18, 4.0, 2.0]
    xm .= copy(sim_func(p))              
    println(xm)
    println(i)

end

```

---

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [August 5, 2024, 8:17am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/2 "2024-08-05T08:17:14Z")

</div>

I guess answer is `SharedArray`.

---

<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: [August 5, 2024, 8:20am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/3 "2024-08-05T08:20:15Z")

</div>

Hi, your problem is very similar to another recent thread where I explained the issue:

> [@Probable data race condition causing problems when trying to parallelize a loop used to populate an array](https://discourse.julialang.org/t/probable-data-race-condition-causing-problems-when-trying-to-parallelize-a-loop-used-to-populate-an-array/117733/2):
>
> The issue is not a race condition. I didn’t see one during a quick read. You have misunderstood the fundamental difference between using processes (via Distributed.jl) and threads. You cannot just slap @distributed on the code and expect it to just work. You need to think about moving data between workers. Let me try to explain: Workers have totally different memory spaces on your machine. In fact for all practical purposes they could live on physically different machines. So when you preallo…

So yes a `SharedArray` is a valid answer, if all processes live on the same physical machine. However if that is the case, do you have a reason to use processes instead of threads? Threads have generally less overhead and are easier to work with (e.g. your code example would’ve just worked).

---

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [August 5, 2024, 8:24am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/4 "2024-08-05T08:24:03Z")

</div>

Yea, its on same machine. I’m new to parallel processing. I didn’t know that, I will give it a shot. 🙂 Thank you

---

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [August 5, 2024, 8:37am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/5 "2024-08-05T08:37:18Z")

</div>

I made this small example for same but it doesn’t seems to be working . Here I would expect to get m = 1.0 in end but it gives m = 0.0 which is originally assigned value. Though it prints 1.0 inside loop.

```julia
function test(x)
    x += 1
    return x
end

m = 0.0

Threads.@threads for i in 1:1029092090

    m = test(0.0)
    println(m)

end

```

This one works…

```julia
function test(x)
    x += 1
    return x
end

Threads.@threads for i in 1:1029092090

    global m = test(0.0)

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: [August 5, 2024, 8:57am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/6 "2024-08-05T08:57:52Z")

</div>

> [@Sahil\_Khan](#):
>
> I made this small example for same but it doesn’t seems to be working . Here I would expect to get m = 1.0 in end but it gives m = 0.0 which is originally assigned value. Though it prints 1.0 inside loop.

This is purely an issue because you used global scope. This also works:

```julia
julia> function foo()
       m = 0
       Threads.@threads for i in 1:1029092090
           m = test(0.0)
           println(m)
       end
       return m
       end

```

---

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [August 5, 2024, 9:02am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/7 "2024-08-05T09:02:17Z")

</div>

Outside of question by any chance due you know any source from where I can get intuitive idea of threading and memory allocation in threading process? I read Julia’s documentation but still I feel gap.

Thank you 🙂

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [August 5, 2024, 9:09am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/8 "2024-08-05T09:09:17Z")

</div>

Maybe [Thread-Safe Storage · OhMyThreads.jl](https://juliafolds2.github.io/OhMyThreads.jl/stable/literate/tls/tls/) could be of interest to you.

---

<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: [August 5, 2024, 9:11am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/9 "2024-08-05T09:11:22Z")

</div>

> [@Sahil\_Khan](#):
>
> where I can get intuitive idea of threading and memory allocation in threading process?

Sorry I don’t really understand what you are asking for.

Threads share the same memory space and all that memory is managed by the same garbage collector (GC). This can lead to bottlenecks if you have a lot of threads that allocate a lot of memory. So to get good scaling it is important to try and reduce allocations to a minimum (this is generally a good idea if you want to get maximal performance). In recent Julia version the GC gained the ability to work in parallel as well which helps.

Process on the other hand have totally separate memory spaces. As they are completely separate Julia processes each has their own GC which can help with allocation pressure. On the other hand it means that each process needs to load all required resource for themselves (like libraries), compile code for themselves and communication between them is not as easy.

---

<div class="post-metadata">

### Author: ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)
#### Post date: [August 5, 2024, 9:21am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/10 "2024-08-05T09:21:57Z")

</div>

Thank you:) Another quick question - So, if Im using a function which has some multithreaded loop inside (like one in example here) in my code and that code need to run this function multiple times which leads to consumption of alot of memory. Is there a way that memory can be cleaned up after each round function was used so we go back to original memory and process repeats?

---

<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: [August 5, 2024, 10:26am UTC](https://discourse.julialang.org/t/using-distributed-for-loop/117831/11 "2024-08-05T10:26:41Z")

</div>

This question is too general to be meaningful. Memory use per se is no problem: Julia has a garbage collector and will clean up/free unused memory on its own. However it can be that the way you wrote your code introduces additional, unnecessary allocations (e.g. due to the [known issues with captured variables](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured)).  
If you have a piece of code that you think has inefficiencies, then I’d encourage you to post a self-contained, executable snippet to #Performance 🙂
