Strumenti Utente

Strumenti Sito


grid:ompprogramming

openMP

Shared memory model

OpenMP provides a portable, scalable model for developers of shared memory parallel applications with Threads.

The API supports C/C++ and Fortran.

openMP Execution Model

  • Begin execution as a single process (master thread)
  • Start of a parallel construct (using special directives): Master thread creates team of threads
  • Fork-join model of parallel execution

Structure example in C
#include <omp.h>

main ()  {

int var1, var2, var3;

//Serial code executed by master thread

#pragma omp parallel private(var1, var2) shared(var3)  //openMP directive
  {
  //  Parallel section executed by all threads 
  }  

// Resume serial code executed by master thread

}

Library routines

OpenMP defines library routines useful to get and set the openMP envioronment. Examples:

 omp_set_num_threads, omp_get_num_threads,… omp_get_thread_num,  omp_get_nested, omp_set_nested

openMP Directives

Main directives are:

Fork: PARALLEL FOR SECTION SINGLE TASK MASTER

Syntax:

  #pragma omp <directive-name> [cluase, ..]
  {
  // parallelized region
  }  //implicit synchronization

These clauses are used to specify additional information with the directive.

For example, private(i) is a clause to the parallel directive stating that each thread will have its own “i” variable.

int i;
#pragma omp parallel private(i)
( i=rand(); .. }
Join: BARRIER

Syntax

#pragma omp barrier //explicit synchronization
PARALLEL

A parallel region is a block of code that will be executed by multiple threads. This is the fundamental OpenMP parallel construct.

parallel1.c

parallel2.c

PARALLEL FOR

Parallelizes a loop, dividing the iterations among the threads

parallel_for.c

SECTIONS

The SECTIONS directive is a non-iterative work-sharing construct. It specifies that the enclosed section(s) of code are to be divided among the threads in the team.

parallel_sections.c

SINGLE
/var/www/html/dokuwiki/data/pages/grid/ompprogramming.txt · Ultima modifica: Y/m/d H:i da