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

**URL:** <https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418>\
**Category:** Performance\
**Tags:** question\
**Created:** [July 18, 2022, 5:38pm UTC](https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418 "2022-07-18T17:38:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [July 18, 2022, 5:38pm UTC](https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418/1 "2022-07-18T17:38:35Z")

</div>

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](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658). 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).

```julia
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.

---

<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:** [July 18, 2022, 6:57pm UTC](https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418/2 "2022-07-18T18:57:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)\
**Post date:** [July 18, 2022, 7:02pm UTC](https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418/3 "2022-07-18T19:02:09Z")

</div>

`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

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

```

in an imperative way.

---

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [July 19, 2022, 3:24am UTC](https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418/4 "2022-07-19T03:24:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [July 19, 2022, 5:10am UTC](https://discourse.julialang.org/t/how-to-use-all-of-the-cpu-cores-in-ijulia-using-broadcasting-function/84418/5 "2022-07-19T05:10:55Z")

</div>

Not sure what you mean but you should take a look at the [multi-threading documentation.](https://docs.julialang.org/en/v1/manual/multi-threading/). 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.
