Parallel julia code

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

Where did you find @parallel?

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

If you look for built-in solution, only @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). If you want a multithreading solution, you would have to write it yourself or you could use, e.g., OhMyThreads.jl

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
3 Likes

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.

1 Like

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

any recommendation?

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

1 Like

@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’

1.8.1
Thanks

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

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 - 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
1 Like

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

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

https://www.youtube.com/results?search_query=doggo.jl

3 Likes

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


please help me running this example

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

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)‘’’

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.

1 Like

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

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

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.

1 Like

To be clear, the solution stated above is as follows.

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)
1 Like