# Parallel julia code

**URL:** <https://discourse.julialang.org/t/parallel-julia-code/111579>\
**Category:** New to Julia\
**Created:** [March 13, 2024, 2:14pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579 "2024-03-13T14:14:06Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 14, 2024, 1:09pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/22 "2024-03-14T13:09:19Z")

</div>

In modern Julia, I would probably prefer to use threads for this.

Start julia with `julia --threads auto`.

```julia
function count_heads(trials)
    flips = rand(Bool, trials)
    num_heads = sum(flips)
    return num_heads
end

trial_counts = [1000, 2000, 3000, 4000]
head_counts = zeros(Int, length(trial_counts))
Threads.@threads for i in eachindex(trial_counts)
    head_counts[i] = count_heads(trial_counts[i])
end

println("Trial Counts:", trial_counts)
println("Head Counts:", head_counts)

```

Executing the code in the Julia REPL outputs the following.

```julia-repl
julia> Threads.nthreads()
6

julia> function count_heads(trials)
           flips = rand(Bool, trials)
           num_heads = sum(flips)
           return num_heads
       end
count_heads (generic function with 1 method)

julia> trial_counts = [1000, 2000, 3000, 4000]
4-element Vector{Int64}:
 1000
 2000
 3000
 4000

julia> head_counts = zeros(Int, length(trial_counts))
4-element Vector{Int64}:
 0
 0
 0
 0

julia> Threads.@threads for i in eachindex(trial_counts)
           head_counts[i] = count_heads(trial_counts[i])
       end

julia> println("Trial Counts:", trial_counts)
Trial Counts:[1000, 2000, 3000, 4000]

julia> println("Head Counts:", head_counts)
Head Counts:[486, 1016, 1489, 2006]

```

To format code, do the following in this forum.

````julia
```julia
function foo()
    println("Hello world")
end
```

```julia-repl
julia> foo()
Hello world
```

````

Note to others, you can use more backticks to escape triple backticks:

`````julia
````
```julia
function foo()
    println("Hello world")
end
```
````

`````

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [March 14, 2024, 1:50pm UTC](https://discourse.julialang.org/t/parallel-julia-code/111579/23 "2024-03-14T13:50:11Z")

</div>

I agree with @mkitti It is easy to get frustrated and angry here. Take a step back - everyone here is very friendly. Maybe you could use four pieces of paper and pretend these are threads of computation of distributed processes. Maybe you think better by using a whiteboard.  
Or talk to your cat. Cats are very wise and are excellent programmers.

[Previous page](https://discourse.julialang.org/t/parallel-julia-code/111579.md?page=1)
