mirror of
https://github.com/OpenMP/Examples.git
synced 2025-04-10 16:32:11 +01:00
73 lines
2.4 KiB
TeX
73 lines
2.4 KiB
TeX
\pagebreak
|
|
\chapter{Task Dependences}
|
|
\label{chap:task_dep}
|
|
|
|
\section{Flow Dependence}
|
|
|
|
In this example we show a simple flow dependence expressed using the \code{depend}
|
|
clause on the \code{task} construct.
|
|
|
|
\cexample{task_dep}{1c}
|
|
|
|
\fexample{task_dep}{1f}
|
|
|
|
The program will always print \texttt{"}x = 2\texttt{"}, because the \code{depend}
|
|
clauses enforce the ordering of the tasks. If the \code{depend} clauses had been
|
|
omitted, then the tasks could execute in any order and the program and the program
|
|
would have a race condition.
|
|
|
|
\section{Anti-dependence}
|
|
|
|
In this example we show an anti-dependence expressed using the \code{depend}
|
|
clause on the \code{task} construct.
|
|
|
|
\cexample{task_dep}{2c}
|
|
|
|
\fexample{task_dep}{2f}
|
|
|
|
The program will always print \texttt{"}x = 1\texttt{"}, because the \code{depend}
|
|
clauses enforce the ordering of the tasks. If the \code{depend} clauses had been
|
|
omitted, then the tasks could execute in any order and the program would have a
|
|
race condition.
|
|
|
|
\section{Output Dependence}
|
|
|
|
In this example we show an output dependence expressed using the \code{depend}
|
|
clause on the \code{task} construct.
|
|
|
|
\cexample{task_dep}{3c}
|
|
|
|
\fexample{task_dep}{3f}
|
|
|
|
The program will always print \texttt{"}x = 2\texttt{"}, because the \code{depend}
|
|
clauses enforce the ordering of the tasks. If the \code{depend} clauses had been
|
|
omitted, then the tasks could execute in any order and the program would have a
|
|
race condition.
|
|
|
|
\section{Concurrent Execution with Dependences}
|
|
|
|
In this example we show potentially concurrent execution of tasks using multiple
|
|
flow dependences expressed using the \code{depend} clause on the \code{task}
|
|
construct.
|
|
|
|
\cexample{task_dep}{4c}
|
|
|
|
\fexample{task_dep}{4f}
|
|
|
|
The last two tasks are dependent on the first task. However there is no dependence
|
|
between the last two tasks, which may execute in any order (or concurrently if
|
|
more than one thread is available). Thus, the possible outputs are \texttt{"}x
|
|
+ 1 = 3. x + 2 = 4. \texttt{"} and \texttt{"}x + 2 = 4. x + 1 = 3. \texttt{"}.
|
|
If the \code{depend} clauses had been omitted, then all of the tasks could execute
|
|
in any order and the program would have a race condition.
|
|
|
|
\section{Matrix multiplication}
|
|
|
|
This example shows a task-based blocked matrix multiplication. Matrices are of
|
|
NxN elements, and the multiplication is implemented using blocks of BSxBS elements.
|
|
|
|
\cexample{task_dep}{5c}
|
|
|
|
\fexample{task_dep}{5f}
|
|
|