# First attempt at parallel programming failed

**URL:** https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854
**Category:** New to Julia
**Created:** [April 7, 2019, 12:04am UTC](https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854 "2019-04-07T00:04:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [April 7, 2019, 12:04am UTC](https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854/1 "2019-04-07T00:04:52Z")

</div>

This is my attempt at parallel programming but it did not produce the same result as serial programming.

```julia
function mysquare(x::Float64)
    return x*x
end

function main()
    Result = Tuple[]
    for x in 1.0:100.0
        y = mysquare(x)
        push!(Result,(x,y))
    end
    SortedResult = sort(Result,lt=(x,y)->x[2]<y[2],rev=true)
    println(SortedResult[1])
    println(VERSION)
end

main()

```

with output

```julia
(100.0, 10000.0)
1.0.3

```

But my parallel attempt has failed to produce the same output

```julia
using SharedArrays
using Distributed
addprocs(4)

@everywhere function mysquare(x::Float64)
    return x*x
end

function main()
    Result = SharedArray{Float64,2}((100,2))
    @distributed for x in 1.0:100.0
        n = Int64(x)
        y = mysquare(x)
        Result[n,1] = x
        Result[n,2] = y
    end
    TupleResult = [(Result[n,1],Result[n,2]) for n in 1:100 ]
    SortedResult = sort(TupleResult,lt=(x,y)->x[2]<y[2],rev=true)
    println(SortedResult[1])
    println(VERSION)
end

main()

```

Output

```julia
(0.0, 0.0)
1.0.3

```

I am not sure where I went wrong, I double check all my code and it looked correct.

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [April 7, 2019, 1:42am UTC](https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854/2 "2019-04-07T01:42:04Z")

</div>

Try prefixing the loop with `@sync`.

`@distributed` spawns tasks on workers asynchronously and returns immediately.

You need to wait on the `Future` returned by `@distributed` or use `@sync`.

[https://docs.julialang.org/en/v1/manual/parallel-computing/#Parallel-Map-and-Loops-1](https://docs.julialang.org/en/v1/manual/parallel-computing/#Parallel-Map-and-Loops-1)

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [April 7, 2019, 4:18am UTC](https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854/3 "2019-04-07T04:18:16Z")

</div>

Thanks for your advise. This code using @sync seems to work

```julia
using SharedArrays
using Distributed
addprocs(4)

@everywhere function mysquare(x::Float64)
    return x*x
end

function main()
    Result = SharedArray{Float64,1}(100)
    @sync @distributed for x in 1.0:100.0
        Result[Int64(x)] = mysquare(x)
    end
    TupleResult = [(n,Result[n]) for n in 1:100 ]
    SortedResult = sort(TupleResult,lt=(x,y)->x[2]<y[2],rev=true)
    println(SortedResult[1])
    println(VERSION)
end

main()

```

output

```julia
(100, 10000.0)
1.0.3

```

Can someone explain why SharedArray cannot handle an array of tuple, forcing me to recreate the TupleResult to be consistent with the serial version of the program. Can the program work if I do not use the SharedArray? Is there another way of storing the result so that it can later be sorted to find the value I am interested in (aka the largest value) and the value of x that resulted in that value?

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [April 7, 2019, 9:11am UTC](https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854/4 "2019-04-07T09:11:00Z")

</div>

> [@StevenSiew](#):
>
> Can someone explain why SharedArray cannot handle an array of tuple

You can create a `SharedArray` of any bits type. In your example:

```julia-auto
SharedArray{Tuple{Float64,Float64},1}(100)

```

> [@StevenSiew](#):
>
> Can the program work if I do not use the SharedArray? Is there another way of storing the result so that it can later be sorted to find the value I am interested in (aka the largest value) and the value of x that resulted in that value?

If you just want the maximum value, you could use a “reducer” on the `@distributed` loop:

```julia-auto
function main()
    result = @distributed max for x in 1.0 : 100.0
        (mysquare(x), x)
    end
    println(result)
    println(VERSION)
end

```

See this section of the manual: [Parallel Computing · The Julia Language](https://docs.julialang.org/en/v1.1/manual/parallel-computing/#Parallel-Map-and-Loops-1)

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [April 7, 2019, 9:40am UTC](https://discourse.julialang.org/t/first-attempt-at-parallel-programming-failed/22854/5 "2019-04-07T09:40:53Z")

</div>

> [@greg\_plowman](#):
>
> Tuple{Float64,Float64}

Thank you very much. This made my life a lot easier and speed up my computations
