Previous: Sharing Data Up: Structured Parallel Blocks: par Next: Pitfalls
Parallel blocks can contain simple statements, sequential blocks, or even other parallel blocks. The behavior of such nesting is precisely what one would expect. The statements within the sequential block are executed sequentially with respect to each other, but are composed in parallel with the other threads of control of the parallel block. For example:
par { { result1 = trial(params1); stats1 = generate_stats(result1); } { result2 = trial(params2); stats1 = generate_stats(result2); } }
Here the function trial() is performed on the argument params1 and the assignment to result1 is completed before the function generate_stats() begins. Between the two threads of control, however, there is no ordering of actions. The statistics could be generated for the first trial before the second trial even begins, or vice versa, or some interleaving of the two could occur. But within each thread, the order of execution is strictly sequential.
Similarly, one of the statements of a parallel block could be another parallel block. Consider the following general example, where si represents a generic statement.
par { par { s1; s2; s3; } { s4; par { s5; s6; } s7; } s8; }
The flow of control for this program is represented in
Figure .