We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
UNIT 3
OpenMP Execution Model – Memory Model –
OpenMP Directives – Work-sharing Constructs - Library functions – Handling Data and Functional Parallelism – Handling Loops - PerformanceConsiderations • OpenMP is an Application Program Interface (API) that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran. • API components: Compiler Directives, Runtime Library Routines, Environment Variables. • OpenMP is a directive-based method to invoke parallel computations on share-memory multiprocessor OpenMP vs Pthreads • OpenMP and Pthreads are both they have many fundamental differences. APIs for shared-memory programming, • Pthreads requires that the programmer explicitly specify the behavior of each thread. • OpenMP allows the programmer to simply state that a block of code should be executed in parallel, and the precise determination of the tasks and which thread should execute them is left to the compiler and the run-time system. • Pthreads is a library of functions that can be linked to a C program, so any Pthreads program can be used with any C compiler, provided the system has a Pthreads library. • OpenMP requires compiler support for some operations, and hence it’s entirely possible that you may run across a C compiler that can’t compile OpenMP programs into parallel programs. • OpenMP uses the fork-join model of parallel execution. – All OpenMP programs begin with a single master thread. – The master thread executes sequentially until a parallel region is encountered, when it creates a team of parallel threads (FORK). – When the team threads complete the parallel region, they synchronize and terminate, leaving only the master thread that executes sequentially (JOIN). Simple Open MP hello world program /* OpenMP program to print Hello World using C language OpenMP header */ #include <omp.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { // Beginning of parallel region #pragma omp parallel { printf("Hello World... from thread = %d\ n", omp_get_thread_num()); } // Ending of parallel region Compiling and running OpenMP programs Open MP Memory Model : Shared-Memory Model : • The shared-memory model is an abstraction of the generic centralized multiprocessor. • The hardware is assumed to be a collection of processors, each with access to the same shared memory. • Because they have access 10 the same memory locations, processors can interact and synchronize with each other through shared variables. OpenMP directives • OpenMP directives exploit shared memory parallelism by defining various types of parallel regions. • Pragmas that let you define parallel regions in which work is done by threads in parallel (#pragma omp parallel). OpenMP directives either statically or dynamically bind to an enclosing parallel region. • Pragmas that let you define how work is distributed or shared across the threads in a parallel region (#pragma omp section, #pragma omp for, #pragma omp single, #pragma omp task). • Pragmas that let you control synchronization among threads #pragma omp atomic, #pragma omp master, #pragma omp barrier, #pragma omp critical, #pragma omp flush, #pragma omp ordered. • Pragmas that let you define the scope of data visibility across threads (#pragma omp threadprivate). • Pragmas for task synchronization (#pragma omp taskwait, #pragma omp barrier) Four important OpenMP functions
– omp_get_num_procs, which returns the number of CPUs in
the multiprocessor on which this thread is executing – omp_get_num_threads, which returns the number of threads active in the current parallel region
– omp_get _thread_num, which returns the thread
identification number – omp_set_num_threads, which allows you to fiX the number of threads executing the parallel sections of code Directive Format • Syntax of OpenMP directive #pragma omp directive-name [clause[ [,] clause]...] new- line – Each directive starts with #pragma omp, to reduce the potential for conflict with other pragma directives with the same names. – Preprocessing tokens following the #pragma omp are subject to macro replacement. – Directives are case-sensitive. The order in which clauses appear in directives is not significant. Clauses on directives may be repeated as needed, subject to the restrictions listed in the description of each clause. – If variable-list appears in a clause, it must specify only variables. Only one directive-name can be specified per directive. SPMD vs. worksharing –A parallel construct by itself creates an SPMD or “Single Program Multiple Data” program i.e., each thread redundantly executes the same code. –To split up pathways through the code between threads within a team.This is called worksharing. #pragma omp c •Thread creation construct PARALLEL / parallel –Original process (master thread) forked additional threads to run code enclosed in the parallel construct. –Thread ID for master thread is 0. Work-sharing constructs •DO / for split up loop iterations among the threads in a parallel region SECTIONS / sections •divide consecutive but independent section(s) of code block amongst the threads barrier implied at the end unless the NOWAIT/nowait clause if used. –code block to be executed by 1 thread –barrier implied at the end unless the NOWAIT/nowait clause if used –MASTER / master : code block to be executed by master thread only (no barrier on other threads implied)