






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























(N > p) & (N not-div by p)
-^
OpenMP is a directive-based API that can be usedwith FORTRAN, C, and C++ for programmingshared address space machines.
-^
OpenMP directives provide support forconcurrency, synchronization, and data handlingwhile obviating the need for explicitly setting upmutexes, condition variables, data scope, andinitialization
-^
Explicit threads like Pthreads API require theprogrammer to work at lower level of details &hence also provide more control. It is more suitedto system programmer vs OpenMP which is betterfor application programmer.
OpenMP directives in C and C++ are based on the#pragma compiler directives.
-^
A directive consists of a directive name followedby clauses.#pragma omp directive [clause list]
-^
OpenMP programs execute serially until theyencounter the parallel directive, which creates agroup of threads.^ #pragma omp parallel [clause list]^ /* structured block */
-^
The main thread that encounters the paralleldirective becomes the
master
of this group of
threads and is assigned the thread id 0 within thegroup.
OpenMP vs Pthread program Structure
A sample OpenMP program along with its Pthreads translation that might be performed by
an OpenMP compiler.
*#pragma omp parallel if (is_parallel== 1) num_threads(8) *
private (a) shared (b) firstprivate(c)
{
/* structured block */}
-^
If the value of the variable is_parallel equals one, eightthreads are created.
-^
Each of these threads gets private copies of variables a & c
, and shares a single value of variable
b
The value of each copy of
c
is initialized to the value of
c^
before the parallel directive.
-^
The default state of a variable is specified by theclause default (shared) or default (none).
*/ ******************************************************An OpenMP version of a threaded program to compute PI.****************************************************** /#pragma omp parallel default(private) shared (npoints) *
reduction(+: sum) num_threads(8) {
num_threads = omp_get_num_threads();sample_points_per_thread = npoints / num_threads;sum = 0;for (i = 0; i < sample_points_per_thread; i++) {
rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1);rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1);if (((rand_no_x - 0.5) * (rand_no_x - 0.5) +
(rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25)sum ++;
} }
The parallel directive can be used in conjunction withother directives to specify concurrency across iterations and tasks
OpenMP provides two directives -
for and sections
specify concurrent iterations and tasks.
-^
The for directive is used to split parallel iteration spacesacross threads. The general form of a for directive is asfollows:#pragma omp for [clause list]
/* for loop */
The clauses that can be used in this context are:private, firstprivate, lastprivate, reduction, schedule,nowait, and ordered.
The schedule clause of the for directive dealswith the assignment of iterations to threads.
-^
The general form of the schedule directive isschedule(scheduling_class[, parameter]).
-^
OpenMP supports four scheduling classes:static, dynamic, guided, and runtime.
*/ static scheduling of matrix multiplication loops /#pragma omp parallel default(private) shared (a, b, c, dim) *
num_threads(4)#pragma omp for schedule(static)for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
c(i,j) = 0;for (k = 0; k < dim; k++) {
c(i,j) += a(i, k) * b(k, j); } } }