OpenMP Andrew Williams
References Chandra et al, Parallel Programming in OpenMP, Morgan Kaufmann Publishers 1999 OpenMP home: Intel has superb resources, including some podcasts: –Have a look round their site
OpenMP Development MS Visual Studio 2005 Professional has support for OpenMP –Not available in Express or Standard editions –You have to set a flag in your Project Settings It is under Code Gen / C++ / Language The Intel C++ compilers have OpenMP support (version 10.0 onwards) –Free for non-commercial use –Not free for us to install here
OpenMP Development Omni OpenMP compiler –More of a command-line facility –Drives gcc / g++ I’ve tried it on Linux (Ubuntu Dapper) but it wouldn’t install properly –Will try to sort that out when I have time
Parallel Regions Fundamental to understanding OpenMP In a parallel region, every thread executes every instruction See “hello world” example later –For non-trivial problems, this takes some thinking about –It’s actually easier to understand with for- loops
Parallel Regions Regions where the work is shared between many threads #pragma omp parallel #pragma omp barrier Single thread Multiple threads Single thread
OpenMP Syntax OpenMP is implemented as a set of directives which are implemented using #pragma in C or C++ –Also available for Fortran A #pragma is used to give some information to the compiler Used to implement extra features which are not defined as part of a language –Ideal for OpenMP
OpenMP Syntax Examples: #pragma omp parallel Splits thread of execution (see later) #pragma omp parallel for for(i=0; i < 1000; i++) { do_something_or_other(); } This divides the iterations of the loop among the available threads –Note there are severe restrictions on the for() loop
OpenMP Syntax A #pragma is divided into a directive and clauses –There may be no clauses #pragma omp parallel num_threads(6)
OpenMP Syntax A #pragma is divided into a directive and clauses –There may be no clauses #pragma omp parallel num_threads(6) Directive
OpenMP Syntax A #pragma is divided into a directive and clauses –There may be no clauses #pragma omp parallel num_threads(6) Clause
OpenMP Syntax #pragma omp parallel for private(temp)
OpenMP Syntax #pragma omp parallel for private(temp) Directive
OpenMP Syntax #pragma omp parallel for private(temp) Directive Clause
Task or Data Parallelism? Last week we contrasted task parallelism and data parallelism –Which one do we have here? #pragma omp parallel for for(i=0; i < 1000; i++) { do_something_or_other(); }
Parallel Sections We will look at this in more detail later #pragma omp parallel sections { #pragma omp section { do_AI(); } #pragma omp section { do_physics(); }
Shared Data Everything we have discussed ignores the problem of data which is being shared between threads We’ll not bother with that too much this week But you must understand that having two threads write to the same data in an uncontrolled manner must result in disaster