This article is the first of a series that will explore all the lesser known gems of Ateji PX.
Non-local exits are all the statements that take the flow of control "away" from the current local scope. In Java, they are
- return,
- throw,
- break,
- continue
Other than Ateji PX, I do not know of any parallel programming framework that properly handles the combination of parallelism and non-local exits.
Which is a pity, because this combination proves very useful. For one, it makes it possible to parallelize existing code without having to rewrite all the control flow, a long and error-prone operation making code difficult to read.
It also makes it possible to design interesting algorithms specifically making use of this combination.
A good example is speculative parallelism. "speculative" in this context means starting work before being totally sure that it is needed. This is a way to make the best use of idle cores.
You can use speculative parallelism to put different algorithms in competition, and take the result from the first one that terminates. Here is a try at speculatively sorting an array in Ateji PX:
[
|| return bubbleSortAlgorithm(array);
|| return insertionSortAlgorithm(array);
]
This code runs the two algorithms in parallel, take the result from the first algorithm that terminates and return it as the global result, stopping all remaining branches.
Here the interesting work is done by the return statements enclosed within a parallel block. As expected, a return statement returns from the enclosing method, stopping all existing branches as necessary. Without the return statements, the program would wait until both branches have terminated.
Properly stopping other threads/tasks/processes is one of the trickiest part of parallel programming. If you've ever tried it, you know what I'm talking about. With Ateji PX, you simply add a return (or break, or continue) statement inside a branch.
No comments:
Post a Comment