The Art of R Programming

(WallPaper) #1

16.3.5 Other OpenMP Pragmas........................................


OpenMP includes a wide variety of possible operations—far too many to list
here. This section provides an overview of some OpenMP pragmas that I
consider especially useful.

16.3.5.1 The omp barrier Pragma
The parallel-processing termbarrierrefers to a line of code at which the
threads rendezvous. The syntax for theomp barrierpragma is simple:

#pragma omp barrier

When a thread reaches a barrier, its execution is suspended until all
other threads have reached that line. This is very useful for iterative algo-
rithms; threads wait at a barrier at the end of every iteration.
Note that in addition to this explicit barrier invocation, some other
pragmas place an implicit barrier following their blocks. These include
singleandparallel. There is an implied barrier immediately following
line 31 in the previous listing, for example, which is why the manager
stays dormant until all worker threads finish.

16.3.5.2 The omp critical Pragma
The block that follows this pragma is acritical section, meaning one in which
only one thread is allowed to execute at a time. Theomp criticalpragma
essentially serves the same purpose as theatomicpragma discussed earlier,
except that the latter is limited to a single statement.

NOTE The OpenMP designers defined a special pragma for this single-statement situation in
the hope that the compiler can translate this to an especially fast machine instruction.

Here is theomp criticalsyntax:

1 #pragma omp critical
2 {
3 // place one or more statements here
4 }

16.3.5.3 The omp single Pragma
The block that follows this pragma is to be executed by only one of the
threads. Here is the syntax for theomp singlepragma:

1 #pragma omp single
2 {
3 // place one or more statements here
4 }

344 Chapter 16

Free download pdf