28 13
Summary of
OpenMP 3.0
C/C++ Syntax
Download the full OpenMP API Specication at www.openmp.org.
Directives
An OpenMP executable directive applies to the succeeding
structured block or an OpenMP Construct. A “structured block”
is a single statement or a compound statement with a single entry
at the top and a single exit at the bottom.
The
parallel
construct forms a team of threads and starts parallel
execution.
#pragma omp parallel
[clause[ [
,
]clause] ...] new-line
structured-block
clause
:
if(
scalar-expression
)
num_threads
(integer-expression
)
de
fault(shared
|
none)
private(
list
)
rstprivate(
list
)
shared(
list
)
copyin(
list
)
reduction(
operator
:
list
)
The loop construct species that the iterations of loops will be distributed
among and executed by the encountering team of threads.
#pragma omp for
[clause[[
,
] clause] ... ] new-line
for-loops
clause
:
private(
list
)
rstprivate(
list
)
lastprivate(
list
)
reduction(
operator
:
list
)
schedule(
kind[, chunk_size]
)
collapse(
n
)
ordered
nowait
The
sections
construct contains a set of structured blocks that are to
be distributed among and executed by the encountering team of threads.
#pragma omp sections
[clause[[
,
] clause] ...] new-line
{
[
#pragma omp section
new-line]
structured-block
[
#pragma omp section
new-line
structured-block ]
...
}
Copyright © 1997-2008 OpenMP Architecture Review Board. Permission to
copy without fee all or part of this material is granted, provided the OpenMP
Architecture Review Board copyright notice and the title of this document
appear. Notice is given that copying is by permission of the OpenMP
Architecture Review Board. Products or publications based on one or more of
the OpenMP specications must acknowledge the copyright by displaying the
following statement: “OpenMP is a trademark of the OpenMP Architecture
Review Board. Portions of this product/publication may have been derived from
the OpenMP Language Application Program Interface Specication.”
Rev 1108-001
Directives (continued)
clause:
private(
list
)
rstprivate(
list
)
lastprivate(
list
)
reduction(
operator
:
list
)
nowait
The
single
construct species that the associated structured block
is executed by only one of the threads in the team (not necessarily
the master thread), in the context of its implicit task.
#pragma omp single
[clause[[
,
] clause] ...] new-line
structured-block
clause
:
private(
list
)
rstprivate(
list
)
copyprivate(
list
)
nowait
The combined parallel worksharing constructs are a shortcut for
specifying a parallel construct containing one worksharing construct
and no other statements. Permitted clauses are the union of the clauses
allowed for the
parallel
and worksharing contructs.
#pragma omp parallel for
[clause[[
,
] clause] ...] new-line
for-loop
#pragma omp parallel sections
[clause[ [
,
]clause] ...]
new-line
{
[
#pragma omp section
new-line]
structured-block
[
#pragma omp section
new-line
structured-block ]
...
}
The
task
construct denes an explicit task. The data environment
of the task is created according to the data-sharing attribute clauses
on the task construct and any defaults that apply.
#pragma omp task
[clause[ [
,
]clause] ...] new-line
structured-block
clause
:
if(
scalar-expression
)
untied
default(shared | none)
private(
list
)
rstprivate(
list
)
shared(
list
)
The
master
construct species a structured block that is executed
by the master thread
of the team. There is no implied barrier either
on entry to, or exit from, the master construct.
#pragma omp master
new-line
structured-block
Directives (continued)
The
critical
construct restricts execution of the associated structured
block to a single thread at a time.
#pragma omp critical
[
(
name
)
] new-line
structured-block
The
barrier
construct species an explicit barrier at the point at which
the construct appears.
#pragma omp barrier
new-line
The
taskwait
construct species a wait on the completion of child tasks
generated since the beginning of the current task.
#pragma omp taskwait
newline
The
atomic
construct ensures that a specic storage location is updated
atomically, rather than exposing it to the possibility of multiple,
simultaneous writing threads.
#pragma omp atomic
new-line
expression-stmt
expression-stmt: one of the following forms:
x binop
=
expr
x
++
++
x
x
--
--
x
The
ush
construct executes the OpenMP ush operation, which makes a
thread’s temporary view of memory consistent with memory, and enforces
an order on the memory operations of the variables.
#pragma omp ush
[
(
list
)
] new-line
The
ordered
construct species a structured block in a loop region that
will be executed in the order of the loop iterations. This sequentializes and
orders the code within an ordered region while allowing code outside the
region to run in parallel.
#pragma omp ordered
new-line
structured-block
The
threadprivate
directive species that variables are replicated, with
each thread having its own copy.
#pragma omp threadprivate
(list) new-line
Details
Operators legally allowed in a reduction
Operator Initialization value
+ 0
* 1
- 0
& ~0
| 0
^ 0
&& 1
|| 0
Schedule types for the loop construct
static Iterations are divided into chunks of size chunk_size, and
the chunks are assigned to the threads in the team in a
round-robin fashion in the order of the thread number.
dynamic Each thread executes a chunk of iterations, then requests
another chunk, until no chunks remain to be distributed.
guided Each thread executes a chunk of iterations, then requests
another chunk, until no chunks remain to be assigned.
The chunk sizes start large and shrink to the indicated
chunk_size as chunks are scheduled.
auto The decision regarding scheduling is delegated to the
compiler and/or runtime system.
runtime The schedule and chunk size are taken from the
run-sched-var ICV.
(See applicable clauses on next page.)
The most common form
of the for loop is shown
below.
for(var = lb;
var relational-op b;
var += incr)