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