Parallel Programming in OpenMP

Chapter 2 showed how to use directives for incremental parallelization of some simple examples and described the behavior of the examples in terms of the OpenMP runtime execution model. We briefly review the key features of the execution model before we study the parallel do directive in depth in this chapter. You may find it useful to refer back to Figures 2.2 and 2.3 now to get a concrete picture of the following execution steps of a parallel loop.
Outside of parallel loops a single master thread executes the program serially. Upon encountering a parallel loop, the master thread creates a team of parallel threads consisting of the master along with zero or more additional slave threads. This team of threads executes the parallel loop together. The iterations of the loop are divided among the team of threads; each iteration is executed only once as in the original program, although a thread may execute more than one iteration of the loop. During execution of the parallel loop, each program variable is either shared among all threads or private to each thread. If a thread writes a value to a shared variable, all other threads can read the value, whereas if it writes a value to a private variable, no other thread can access that value. After a thread completes all of its iterations from the loop, it waits at an implicit barrier for the other threads to complete their iterations. When...