Previous: Sharing Data Up: Structured Parallel Loops: parfor Next: Pitfalls
The conversion of the loop control variable to a constant value
within each iteration permits unraveling of the loop without
executing the body of any iteration. After the initialization
of the loop control variable and the test of the loop condition,
execution of the body of the first iteration can begin, but so
can the increment of the loop control variable, followed by the test
of the loop condition. The flow of control for a generic
parfor statement is represented in Figure .
Notice that, as in a parallel block, nothing can be said about the order of execution of the individual iterations. There is no guarantee that the first iteration will even begin before any other iteration.
The semantics of loop unraveling are therefore defined in terms of a sequential repetition of condition test evaluation then loop control variable increment. Though the generality of C and C++ permit these operations to be arbitrarily complicated and hence require sequential evaluation, a particular implementation of CC++ may do better in certain instances. For example, it is not difficult to envision a compiler that detects the common loop format
parfor (int i=0; i<N; i++)
and is able to flatten the creation of the N parallel threads of control. Thus, the linear complexity of the semantic definition of parfor does not necessarily imply a linear component in the performance of parfor in all cases.