# Parallel julia code

**URL:** <https://discourse.julialang.org/t/parallel-julia-code/111579>\
**Category:** New to Julia\
**Created:** [March 13, 2024, 2:14pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579 "2024-03-13T14:14:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 13, 2024, 2:14pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/1 "2024-03-13T14:14:06Z")

</div>

hello everyone,  
I have some algorithms that I wanna run them in parallel. I started by learning how to make simple parallel examples.  
I have the following code but it is not running. can U help me on this  
function pflip\_coin(times)  
count = 0  
@parallel (+) for i in 1:times  
Int(rand(Bool))  
end  
end

@time pflip\_coin(50000000000)

is it @parallel depreacted now?  
Thanks in advanced

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 13, 2024, 2:27pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/2 "2024-03-13T14:27:30Z")

</div>

Where did you find `@parallel`?

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 13, 2024, 3:22pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/3 "2024-03-13T15:22:40Z")

</div>

I found it in Seven more language in seven weeks. I know this book is a bit old. But I am asking how to find some alternatives to test my first code

---

<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:** [March 13, 2024, 3:30pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/4 "2024-03-13T15:30:07Z")

</div>

> [@AbdelazimHussien](#):
>
> function pflip\_coin(times)  
> count = 0  
> @parallel (+) for i in 1:times  
> Int(rand(Bool))  
> end  
> end

If you look for built-in solution, only [`@distributed`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.@distributed) has support for parallel reductions (multiprocessing). If my memory doesn’t fail me, it was named `@parallel` a (very) long time ago ([it doesn’t fail me](https://docs.julialang.org/en/v0.5/stdlib/parallel/?highlight=%40parallel#Base.@parallel)). If you want a multithreading solution, you would have to write it yourself or you could use, e.g., OhMyThreads.jl

```julia
using OhMyThreads

function pflip_coin(times)
    count = @tasks for i in 1:times
        @set reducer=+
        Int(rand(Bool))
    end
end

# or, equivalently

function pflip_coin(times)
    count = treduce(+, 1:times) do i
        Int(rand(Bool))
    end
end

```

---

<div class="post-metadata">

**Author:** ![mkoculak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkoculak/32/28310_2.png) [@mkoculak](https://discourse.julialang.org/u/mkoculak)\
**Post date:** [March 13, 2024, 4:36pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/5 "2024-03-13T16:36:35Z")

</div>

> [@AbdelazimHussien](#):
>
> I found it in Seven more language in seven weeks. I know this book is a bit old.

Out of curiosity, I checked and it was published in 2014 (so more or less around Julia 0.3). This is rather extremally old in terms of Julia development (right now we are at version 1.10).

To minimize chances of running into code that is not working any more, look for materials that were published after 2018 and use Julia 1.0 or later. Of course, the newer - the better.

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 13, 2024, 9:37pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/6 "2024-03-13T21:37:39Z")

</div>

I try to run your code. However, it is not work

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 13, 2024, 9:38pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/7 "2024-03-13T21:38:17Z")

</div>

any recommendation?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 13, 2024, 11:24pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/8 "2024-03-13T23:24:15Z")

</div>

Sorry. It is difficult for us to help you if you do not describe the error messages you see when it does not work.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [March 14, 2024, 8:09am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/9 "2024-03-14T08:09:32Z")

</div>

@AbdelazimHussien please give us the version of Julia which you are using  
Use this function then cut and paste the output

julia\> versioninfo()

If you are following documentation which is old you may be using an old version of Julia.  
Please tell us how you installed Julia. I can highly reccomend that you use ‘juliaup’

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 14, 2024, 8:28am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/10 "2024-03-14T08:28:23Z")

</div>

1.8.1  
Thanks

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 14, 2024, 8:33am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/11 "2024-03-14T08:33:45Z")

</div>

previously UndefVarError: #parallels not defined  
now if i run the following code

using Distributed  
addprocs(4)  
function count\_heads(trials)  
flips = rand(Bool, trials)  
num\_heads = sum(flips)  
return num\_heads  
end  
trial\_counts = [1000, 2000, 3000, 4000]  
head\_counts = pmap(count\_heads, trial\_counts)  
println(“Trial Counts:”, trial\_counts)  
println(“Head Counts:”, head\_counts)  
it gives me UndefVarError: #count\_heads not defined

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 14, 2024, 8:40am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/12 "2024-03-14T08:40:10Z")

</div>

Please:

- Format your code by enclosing it in triple backticks `````
- Use Julia 1.10.2 - this won’t solve any of your issues but it’s always a good idea to use the current stable release
- Read the [documentation on distributed computing in Julia](https://docs.julialang.org/en/v1/manual/distributed-computing/) - if you want to refer to a function on a worker process you have to define that function as `@everywhere function head_counts(trials) ... end`

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [March 14, 2024, 9:38am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/13 "2024-03-14T09:38:26Z")

</div>

@AbdelazimHussien I think you are going too fast. As @nilshg says look at some tutorials.

[![](https://global.discourse-cdn.com/julialang/original/3X/a/0/a0fe97ba500f0fa10ca47766054880e9e7639dc2.jpeg "Parallel Computing in Julia") ](https://www.youtube.com/watch?v=Kld1Vzo64v8)

doggo is also very, very good and has easily followed tutorials

[https://www.youtube.com/results?search\_query=doggo.jl](https://www.youtube.com/results?search_query=doggo.jl)

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 14, 2024, 9:54am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/14 "2024-03-14T09:54:48Z")

</div>

I have already done the 1st and 2nd steps and I will look on the 3rd one. but could u

 ![Screenshot 2024-03-14 at 10.54.39](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f9e40423d59db4e652dfb6a44decf199fffc8da.png)  
please help me running this example

---

<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:** [March 14, 2024, 11:44am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/15 "2024-03-14T11:44:20Z")

</div>

> [@AbdelazimHussien](#):
>
> I have already done the 1st […]

You have not? Your code blocks in this thread are still either plain text or screenshots.

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 14, 2024, 11:48am UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/17 "2024-03-14T11:48:25Z")

</div>

> Blockquote ‘’’ using Distributed

addprocs(4)

function count\_heads(trials)  
flips = rand(Bool, trials)  
num\_heads = sum(flips)  
return num\_heads  
end

trial\_counts = [1000, 2000, 3000, 4000]

head\_counts = pmap(count\_heads, trial\_counts)

println(“Trial Counts:”, trial\_counts)  
println(“Head Counts:”, head\_counts)‘’’

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 14, 2024, 12:05pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/18 "2024-03-14T12:05:49Z")

</div>

That still doesn’t work. But irrespective of formatting, I think you should focus on my suggestion number 3 and read the docs. I already gave you the answer to your main issue (which is how to define a function on all worker processes) above, but it’s useful to understand what you’re doing rather than just copy/pasting something you were told on a forum.

---

<div class="post-metadata">

**Author:** ![AbdelazimHussien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdelazimhussien/32/207723_2.png) [@AbdelazimHussien](https://discourse.julialang.org/u/AbdelazimHussien)\
**Post date:** [March 14, 2024, 12:24pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/19 "2024-03-14T12:24:22Z")

</div>

what i understand is puting triple ‘’’ will convert my text to code so u can test it. Am I right? I already done this

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 14, 2024, 12:49pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/20 "2024-03-14T12:49:32Z")

</div>

You have not done this, if you had done it your code would look like this:

```julia
using Distributed

addprocs(4)

function count_heads(trials)
flips = rand(Bool, trials)
num_heads = sum(flips)
return num_heads
end

trial_counts = [1000, 2000, 3000, 4000]

head_counts = pmap(count_heads, trial_counts)

println(“Trial Counts:”, trial_counts)
println(“Head Counts:”, head_counts)

```

In any event I don’t need to test your code, I can already see what’s wrong and have told you what it is & how to fix it. But again, I think your time is better spent reading the documentation to understand how distributed processing works in Julia rather than formatting questions on this site.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 14, 2024, 12:57pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/21 "2024-03-14T12:57:26Z")

</div>

To be clear, the [solution stated above](https://discourse.julialang.org/t/parallel-julia-code/111579/12) is as follows.

```julia
using Distributed

addprocs(4)

@everywhere function count_heads(trials)
    flips = rand(Bool, trials)
    num_heads = sum(flips)
    return num_heads
end

trial_counts = [1000, 2000, 3000, 4000]

head_counts = pmap(count_heads, trial_counts)

println("Trial Counts:", trial_counts)
println("Head Counts:", head_counts)

```

[Next page](https://discourse.julialang.org/t/parallel-julia-code/111579.md?page=2)
