Parallel Numerical Integration with OpenMP

We recalled the basics of OpenMP programming with a second hello world program, with two new features: the master and the single directive. A run of this program on copper produces the following output:
Hello from the master thread 0!
Thread 6 says hello.
Thread 7 says hello.
Thread 1 says hello.
Thread 2 says hello.
Only one thread 6 says more...
Thread 4 says hello.
Thread 3 says hello.
Thread 5 says hello.
Thread 0 says hello.
The program uses 8 threads set from inside the program. The parallel directive corresponds to the language construct forall we discussed in lecture 28 .

We covered numerical integration as one of the scientific computing examples of an embarrassingly parallel computation. We "translate" the MPI program of lecture 13 into OpenMP, in three stages, respectively in the programs comptrap1.c, comptrap2.c, and comptrap3.c, along the way introducing new directives.

The first program comptrap1.c uses a shared memory array to hold the results of all threads. This array holds all results which were distributed in the MPI version of the program. In particular, thread i writes to the i-th entry of the array. The MPI_Reduce of the MPI program is spelled out explicitly by means of a loop at the end of the program.

Because keeping one array for the results of all processes is not necessary and thus wasteful, we want to use one variable to store the numerical approximation. This is possible, except that when this shared variable is updated, it must happen inside a critical section, illustrated in comptrap2.c. Note that it is inefficient to put the function call inside the critical section. Compared to the program of lecture 27, observe how much shorter the implementation of a monitor in OpenMP is.

This second version is still not satisfactory enough, because it is more natural to think of a for loop. We introduced the parallel for in at the end of lecture 29. Using the parallel for directive yields our final version comptrap3.c. The if statements have been removed because of the limitations on the branching in a parallel for. The main point of this lecture is to illustrate how much compacter programs with OpenMP can be, compared to MPI. Ignoring the pragmas in the program leads to a relatively normal sequential program.

Bibliography