OpenMP Pragmas and Directives
OpenMP directives, or pragmas, are compiler instructions used in shared-memory
programming (C/C++ and Fortran) to execute code in parallel using multiple threads.
They are the mechanism for de ning parallel regions and sharing work among
threads.
Core Directives
Directive-Purpose-Key Feature
#pragma omp **parallel** -Forks a team of threads to execute the following
structured code block concurrently. - Creates the parallel region.
#pragma omp **for** - Distributes the iterations of a loop among the threads in the
team. -Must be inside a parallel region (or combined as parallel for).
#pragma omp **sections** - Divides a block of non-iterative tasks into independent
sections to be executed concurrently. - Used for function-level parallelism.
#pragma omp **single** - Speci es that the following code block is executed by only
one thread (not necessarily the master). - Often used for I/O or initialization within
the parallel region.
Synchronization Directives
Directive - Purpose
#pragma omp **barrier** Pauses all threads until every thread in the team has
reached that point.
#pragma omp **critical** - Restricts the execution of the following block to only one
thread at a time (mutual exclusion).
#pragma omp **atomic** - Guarantees that a simple memory update (e.g.,
assignment, addition) is performed without interruption.
#pragma omp **master** - Executes the following block only by the master thread
(thread ID 0).
Data Sharing Clauses (Used with Directives)
Clauses are modi ers that de ne how variables are handled within the parallel
region.
Clause - Variable Scope - Effect
shared(list) - Shared - Variables are accessible and writable by all threads.
private(list) - Local (Private) - Each thread gets its own uninitialized copy of the
variable.
reduction(op:list) - Local (Private) - Each thread calculates its local value, and the
results are combined using the speci ed operator (+, *, max, etc.) at the end.
TRAPEZOIDAL RULE
The Trapezoidal Rule approximates the area under the curve of a function $f(x)$
over an interval $[a, b]$ by dividing the interval into $n$ smaller subintervals of equal
width $h$
Formula and ConceptInterval
Division: The interval [a, b] is divided into n subintervals, each forming the base of a
trapezoid.
Width Calculation: The width of each trapezoid (or the step size) is: h = {b - a}/{n}
Area Approximation: The area of each subinterval is approximated by the area of a
trapezoid, which is the width multiplied by the average of the two heights (function
values) at the endpoints.
fi
fi
fi
fi
fi
Area Approximation: The area of each subinterval is approximated by the area of a
trapezoid, which is the width multiplied by the average of the two heights (function
values) at the endpoints.