Docsity

OpenMP: Parallel Programming with Shared Address Space using OpenMP Directives, Slides of Parallel Computing and Programming

Partial preview of the text

Download OpenMP: Parallel Programming with Shared Address Space using OpenMP Directives and more Slides Parallel Computing and Programming in PDF only on Docsity!

Programming Shared Address Space

Platforms using Threads

Processes: Creation & Completion

Adding an Array: 2-proc Shared Memory Computer

Procedure invoked by main program

Procedure invoked by main program

SPMD program for Addition of Array onp-processors SM computer

(N > p) & (N not-div by p)

OpenMP: a Standard for Compiler Directives

Based Parallel Programming

-^

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.

Parallel Directive in OpenMP

•^

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.

Parallel Directive Example in OpenMP

*#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).

Example: reduction Clause in OpenMP

note: The Serial For Loop usage

*/ ******************************************************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 ++;

} }

Specifying Concurrent Tasks in OpenMP

•^

The parallel directive can be used in conjunction withother directives to specify concurrency across iterations and tasks

•^

OpenMP provides two directives -

for and sections

  • to

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.

Assigning Iterations to Threads

•^

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.

Assigning Iterations to Threads: Example

*/ 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); } } }