chaos icon indicating copy to clipboard operation
chaos copied to clipboard

[RFC] Parallelism

Open mertyildiran opened this issue 3 years ago • 2 comments

Implement parallelism using async keyword on function calls:

void def iterate_and_print(num list a)
    foreach a as i
        print i
    end
end

async iterate_and_print([1, 2, 3, 4, 5])
print "Print me in the mean time"

The output should look like:

1
2
Print me in the mean time
3
4
5

The async should also be assignable so that using the wait keyword the execution can wait until the function called by async finishes:

num def iterate_print_and_return_last(num list a)
    foreach a as i
        print i
    end
    num r = a[-1]
    return r
end

ptr call = async iterate_print_and_return_last([1, 2, 3, 4, 5])
print "Print me in the mean time"
wait [num a = call]

echo "Last element: "
print a

wait keyword should be able to accept a num list:

num def iterate_print_and_return_last(num list a)
    foreach a as i
        print i
    end
    num r = a[-1]
    return r
end

ptr call1 = async iterate_print_and_return_last([1, 2, 3, 4, 5])
ptr call2 = async iterate_print_and_return_last([6, 7, 8, 9, 10])
print "Print me in the mean time"
wait [num a = call1, num b = call2]

echo "Last elements: "
echo a
echo ", "
print b

such that the output will look like:

1
2
6
3
Print me in the mean time
8
4
9
5
10
Last elements: 5, 10

mertyildiran avatar Dec 16 '20 03:12 mertyildiran

Interesting - basically you're turning the "traditional async" approach on its head into a futures semantics. Shall be quite performant and actually avoid many (most?) of the pitfalls of the "traditional" async (most notably your approach seems to solve the stdlib of double size and mutually incompatible "function colors" split).

Though admittedly I'm curious how you want to implement this under the hood. The only implementation semantically resembling yours is the one in Dao which though uses plain os-level threads. But you seem to indicate that you want to have it single-threaded which would mean either you need to distinguish between return and yield (which I can't find anywhere in your description) or simply insert yield points automatically in compile time (which I also can't find anywhere in your description :wink:). Or I'm missing some other option... IDK

Dare to elaborate :wink:?

dumblob avatar Oct 22 '21 17:10 dumblob

@dumblob it's just a syntax proposal at this point. I've no idea how are we going to implement it. Also the current state of master far from making parallelism possible. Right now, I'm focused on making the language JIT compiled. So this RFC is kind of on hold.

mertyildiran avatar Oct 24 '21 11:10 mertyildiran