System.Parallel

lib/System/Parallel.gob

Work spread over the workers that run tasks (System.Task): a function of each element of an array, and a graph of steps each run once the steps it depends on have finished.

Each runs its function concurrently with itself when there is more than one worker, so what the calls share must be safe to share: reading data nothing writes meanwhile, and writing only what is the call's own. Every result is the function's, in the order of the input, whatever order the calls ran in.

With one worker each is the sequential loop it stands for, in order, with no tasks.

Work is split into chunks, a few per worker, each one task that runs its elements in turn. A task is a coroutine with a stack of its own, so a task per element would hold as many stacks as there are elements.

fun workers : fun() -> Int

How many workers run tasks, the program's own included.

fun map : [Iterator c, Index c, Length c, Item c ~ a, Key c ~ Int, Value c ~ a] fun(c, fun(a) -> b) -> Array b

f of each of xs, in order.

fun mapWith : [Iterator c, Index c, Length c, Item c ~ a, Key c ~ Int, Value c ~ a] fun(c, fun() -> s, fun(s, a) -> b) -> Array b

map, with state of each chunk's own: perChunk() is made once for the elements one task runs, in turn, and each call is given it. Scratch space worth reusing across elements is the use, and so a call's result must not depend on what an earlier one left in it.

fun each : [Iterator c, Index c, Length c, Item c ~ a, Key c ~ Int, Value c ~ a] fun(c, fun(a) -> Unit) -> Unit

f of each of xs, for what it does.

fun mapInOrder : [Index c, Length c, Key c ~ Int, Value c ~ a] fun(c, fun(a) -> b, fun(Int, b) -> Unit) -> Unit

f of each of xs, each result handed to use with its index, in order, on the caller, as soon as it and every result before it are made. At most a few chunks per worker are made ahead of the one use is waiting for, so the results held at once stay bounded however many elements there are.

fun graph : [Iterator d, Item d ~ Int] fun(Int, fun(Int) -> d, fun(Int, fun(Int) -> b) -> b) -> Array b

The steps of a dependency graph, run once each.

Step i runs, as run(i, finished), once every step dependsOn(i) names has finished; finished(j) is step j's result, for any step i depends on. The results are in step order. A cycle among the steps is an error, found before any step runs.

One task per worker takes ready steps from a queue, rather than a task per step waiting on its dependencies: a waiting task holds its stack.

fun graphWith : [Iterator d, Item d ~ Int] fun(Int, fun(Int) -> d, fun() -> s, fun(s, Int, fun(Int) -> b) -> b) -> Array b

graph, with state of each worker's own: perWorker() is made once for each task taking steps, and every step that task runs is given it. A cache of answers that do not depend on which steps have run is the use: a step may run on any worker, so what it finds there must not change its result.