# Dubious code: @spawn psort!(v, lo, mid)

**URL:** <https://discourse.julialang.org/t/dubious-code-spawn-psort-v-lo-mid/31742>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 2, 2019, 11:40am UTC](https://discourse.julialang.org/t/dubious-code-spawn-psort-v-lo-mid/31742 "2019-12-02T11:40:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 2, 2019, 11:40am UTC](https://discourse.julialang.org/t/dubious-code-spawn-psort-v-lo-mid/31742/1 "2019-12-02T11:40:29Z")

</div>

Here is the code snippet taken from [Announcing composable multi-threaded parallelism in Julia](https://julialang.org/blog/2019/07/multithreading)

```julia

import Base.Threads.@spawn

# sort the elements of `v` in place, from indices `lo` to `hi` inclusive
function psort!(v, lo::Int=1, hi::Int=length(v))

   # omitted above
    mid = (lo+hi)>>>1 # find the midpoint

    half = @spawn psort!(v, lo, mid) # task to sort the lower half; will run
    psort!(v, mid+1, hi) # in parallel with the current call sorting
                                      # the upper half
    wait(half) # wait for the lower half to finish
    temp = v[lo:mid]

```

What’s dubious is this:  
` half = @spawn psort!(v, lo, mid)`

When psort!(v) is spawned, the entire array that v points to must be copied to the remote process. so it is necessary to fetch(half) and copy the result to the local v[lo:mid].

Actually the above program never ends and keep spinning. More strangely, even if I put `v[lo:mid] .= fetch(half)`, it never ends.

I think the following simple example confirms my understanding; data must be fetched from @spawn.

```julia
using Base.Threads, Distributed

arr = zeros(10)
@everywhere function update!(arr::AbstractArray)
    for i = 1:length(arr)
        arr[i] = i
    end
end

r = @spawn update!(arr)
wait(r)
println(arr)

```

the result of which is all-zero array without fetch.

What mistake am I making? or the example is really flawed?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 2, 2019, 11:56am UTC](https://discourse.julialang.org/t/dubious-code-spawn-psort-v-lo-mid/31742/2 "2019-12-02T11:56:34Z")

</div>

> [@Sijun](#):
>
> When psort!(v) is spawned, the entire array that v points to must be copied to the remote process. so it is necessary to fetch(half) and copy the result to the local v[lo:mid].

`Threads.@spawn` doesn’t use multiple processes (distributed memory), it uses shared memory and there is no need to copy any data between tasks spawned in the same process.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 2, 2019, 12:03pm UTC](https://discourse.julialang.org/t/dubious-code-spawn-psort-v-lo-mid/31742/3 "2019-12-02T12:03:25Z")

</div>

Ah, I see. Indeed there are two @spawn: Threads.@spawn and Distributed.@spawn. The latter is deprecated. Thank you for the clarification 🙂 (It’s till strange why the program never terminated on my PC)
