# Macro for parallel out-of-order execution?

**URL:** <https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706>\
**Category:** General Usage\
**Created:** [May 3, 2020, 11:16pm UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706 "2020-05-03T23:16:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 3, 2020, 11:16pm UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/1 "2020-05-03T23:16:24Z")

</div>

Do we have a macro in some package that turns something like this

```julia
@parallel_out_of_order begin
    a = foo()
    bar()
    b = baz()
end

```

into an equivalent of this

```julia
    future_1 = Threads.@spawn foo()
    future_2 = Threads.@spawn bar()
    future_3 = Threads.@spawn baz()

    a = fetch(future_1)
    wait(future_2)
    b = fetch(future_3)

```

?

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [May 4, 2020, 1:37am UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/2 "2020-05-04T01:37:02Z")

</div>

You can use a `@sync` block to wait:

```julia
@sync begin
  Threads.@spawn global a = foo() 
  Threads.@spawn bar() 
  Threads.@spawn global b = baz() 
end

```

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 4, 2020, 2:51am UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/3 "2020-05-04T02:51:51Z")

</div>

The problem with that is that if you do

```julia
function foo()
    @sync begin
      Threads.@spawn global a = 42
    end
end

foo()
a == 42 # But don't want a defined here!

```

you end up with having `a` in the global scope.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 4, 2020, 2:58am UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/4 "2020-05-04T02:58:37Z")

</div>

@tkf, I think I have something that works (though the implementation is probably not very elegant):

[https://github.com/oschulz/ParallelProcessingTools.jl/pull/6/commits/b550ca9238cc9d5ff6e332031663f3f07ae869fd](https://github.com/oschulz/ParallelProcessingTools.jl/pull/6/commits/b550ca9238cc9d5ff6e332031663f3f07ae869fd)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [May 4, 2020, 4:59am UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/5 "2020-05-04T04:59:43Z")

</div>

Cool. It looks like a useful thing to have 🙂

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 4, 2020, 3:45pm UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/6 "2020-05-04T15:45:34Z")

</div>

@tkf, it’s ready to try now, if you’re interested:

```julia
using Pkg; pkg"add ParallelProcessingTools#master"

using ParallelProcessingTools

b = 0
foo() = global b = 9
bar() = 33

@mt_out_of_order begin
    a = begin
        sleep(1.5); println("step 1")
        42
    end

    begin
        sleep(1.0), println("step 2")
        foo()
    end

    c = begin
        sleep(0.5); println("step 3")
        bar()
    end

    d = :trivial
end

(a, b, c, d) == (42, 9, 33, :trivial)

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [May 5, 2020, 6:43am UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/7 "2020-05-05T06:43:26Z")

</div>

Hi, just skimmed the docs, but sounds like an interesting package. Is this going to add something you discussed in [Propagation of available/assigned worker-IDs in hierarchical computations?](https://discourse.julialang.org/t/propagation-of-available-assigned-worker-ids-in-hierarchical-computations/38670)?

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [May 5, 2020, 10:46am UTC](https://discourse.julialang.org/t/macro-for-parallel-out-of-order-execution/38706/8 "2020-05-05T10:46:05Z")

</div>

> but sounds like an interesting package.

ParallelProcessingTools is a bit of a mix of pre-Julia-v1.3 workarounds and long-term usable functionality. It’s been around for a while, I’ll need to refresh some parts of it sometime. 🙂

> Is this going to add something you discussed in [Propagation of available/assigned worker-IDs in hierarchical computations](https://discourse.julialang.org/t/propagation-of-available-assigned-worker-ids-in-hierarchical-computations/38670)

I was thinking about adding something like that, yes - however, I’m still not sure about how to propagate worker-availability to child tasks in a consistent fashion.
