Parallel programming loops

Hi
I am new to parallel programming concept and I do not know how to make this piece of my code runs parallel


scatter([Person[i]["x"] for i in 1:Npopulations], [Person[i]["y"] for i in1:Npopulations],legend=false, color = [Person[i]["color"] for i in 1:Npopulations]) 
 

Could you post a simplified example that illustrates your problem? It is easiest to help if it is possible for one of us to paste the code into our Julia REPL and see the problem without having to edit anything (test it in your own REPL to make sure that you have no typos in it). You could also try to post this in the “General Usage” forum as it sounds like quite an advanced question.

just edited it.

Okay, it still does not run in a newly started Julia, what I meant was something like the code shown below. Is this a good example of what you want to parallelize?:

using Plots, Random

x=collect(1:1000)
anim = @animate for ist=1:100
	y=rand(1000)
	scatter(x, y)
end
gif(anim,"rand.gif", fps=3)

I can’t answer this for you, but there was a post about it in 2020 Animation with threads in Plots.jl? . I would recommend that you post in “General Usage” and see if anyone there can help. Use a title like “can I parallelize animated gifs?”.

Try creating arrays to store the results first and then use Threads.@threads on the for loop:

x = Vector{Any}(undef, Npopulations)
y = similar(x)
colors = Vector{Any}(undef, Npopulations)
Threads.@threads for i in 1:Npopulations
x[i] = Person[i]["x"]
y[i] = Person[i]["y"]
colors[i] = Person[i]["color"]
end
scatter(x, y,legend=false, color = colors)

Be sure to change the type Any to the appropriate type, this is for speed so each element isn’t boxed. You should make sure that all elements of x and y are the same type. Each element of colours should have matching types too.

From the looks of this, it doesn’t seem like doing this in parallel will speed it up by much, but it will create the intermediate arrays in parallel, but since there doesn’t appear to be any significant processing (other than a dictionary lookup which is pretty fast), you may actually experience a slowdown, and the code is now harder to read and less maintainable.

P.s. check Threads.nthreads() to see if you have threads available for parallelism.

1 Like

Before trying to parallelize, please first try to optimize your code to figure out where the bottlenecks are. There are many different parallelization strategies (multiprocessing, multithreading, GPUs, clusters), and using any of them requires understanding and eliminating those bottlenecks. What’s the runtime of your code at the moment? What functions dominate the runtime? Have you read through and applied any relevant performance tips?

Separately, see this PSA on how to construct a minimal reproducible example for others to run: Please read: make it easier to help you

1 Like