Macro for parallel out-of-order execution?

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

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

into an equivalent of this

    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)

?

1 Like

You can use a @sync block to wait:

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

The problem with that is that if you do

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.

@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

1 Like

Cool. It looks like a useful thing to have :slight_smile:

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

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)
1 Like

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??

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. :slight_smile:

Is this going to add something you discussed in Propagation of available/assigned worker-IDs in hierarchical computations

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.

1 Like