How to use all of the cpu cores in IJulia using broadcasting function?

I am relatively new to Julia and I have been exploring how to parallel process functions in IJulia. I have created a Jupyter notebook for 128 cores using the directions given here. I can confirm that it has imported all of the requested cores using the .nthreads. Now I have a function that calls a bunch of functions inside ( My actual function is very long, so for instance for an explanation as shown).

function calculate(variables)  #variables is a vector
   x1,x2,x3,x4,x5,x6 = variables
   value1 = funct1(x1,x2,x3) # this function returns a real value
   value2 = funct2(x3,x4,x3)  # this function returns a real value
   value3 = funct3(x3,x5,x6)  # this function returns a real value
   return funct4(value1, value2, value3)  # this function returns a vector.
end

numbers = [[rand() for i=1:6] for j=1:100000000]
answer =  calculate.(numbers) # I get the expected answer but CPU is not utilised well. 

As I execute these codes and when I check the “system monitor”, I see that only one core is being used at a time. Please help how I can use all of the cores. I hope I make some sense. I have also tried FLOOP and multithreading packages but I see that all cores to 100% and never give me an answer. Thanks for your help.

P.S. I use Ubuntu 22.

1 Like

Where are you actually multithreading your code? Why do you think this is an IJulia issue, do you see more cores being used when running the code in the REPL?

1 Like

calculate.(numbers)

does not utilize parallelism and its functional version is

map(calculate, numbers)

and ThreadsX package includes the multi-thread version of this function as

ThreadsX.map(calculate, numbers).

Another way is to use Threads package and @threads but in this case you implement a vector of answers like

answers = ...
Threads.@threads for i in 1:n
    answers[i] = ...
end

in an imperative way.

3 Likes

Sorry, looks like my code is not multithreading. I am a bit confused I guess. The confusion between explicit for loop and broadcasting or map function. I am still not able to identify the merits/demerits of the latter. Thanks.

Not sure what you mean but you should take a look at the multi-threading documentation.. The idea is that you have to 1) launch Julia with the number of threads you want, and 2) decorate your code with either Julia’s low level @threads or use a library like ThreadsX.

You don’t get parallelism for free just because you use broadcasting.

2 Likes