Опубликован: 12.07.2012 | Доступ: свободный | Студентов: 334 / 21 | Оценка: 4.00 / 4.20 | Длительность: 11:07:00
Специальности: Программист
Лекция 7:

OpenMP fundamentials

< Лекция 6 || Лекция 7: 12 || Лекция 8 >
Аннотация: In this lecture describes OpenMP basics. OpenMP workflow and its limitation. Different variables and sections of the code, critical and concurrent sections, loop parallelization directives, loop iteration scheduling.

OpenMP is

The presentation can be downloaded here.

  • An application programming interface (API) that supports shared-memory programming for C/C++ and Fortran
  • Pros:
    • Simple
    • Cross-platform
    • Small overhead
    • Data parallelism support


Compiler directives:


#pragma omp directive [clause, …]


!$OMP directive [clause, …]
C$OMP directive [clause, …]
*$OMP directive [clause, …]

Parallel execution

Parallel Regions

Main OpenMP directive

#pragma omp parallel
#pragma omp parallel
  printf( "hello world from thread %d of %d\n",
    omp_get_num_threads() );

Параллельное исполнение

  • Most of the OpenMP instructions are preprocessor directives
  • Main construction is "omp parallel [smth]"

Рис. 7.1.

OpenMP parallel model

  • Memory is shared
  • Task is divided into the threads
    • Variables can be
      • shared by the threads
      • private, available only for one thread
  • Uncareful or wrong variable usage can lead to wrong execution results.

Fork-join model

  • Program execution starts from the master thread
  • With OpenMP directive master thread creates the additional threads
  • After the parallel region is finished all threads are synchronized
  • Main thread continues to execute the sequential part

Рис. 7.2.

Основные конструкции OpenMP

  • #pragma omp for Each thread gets its own amount of data – data parallelism
  • #pragma omp section Each section will be executed in a separate thread – functional parallelism
  • #pragma omp single Sequential execution. Only one thread will execute this code

OpenMP sections

#pragma omp sections [ clause [ clause ] ... ] new-line
[#pragma omp section new-line ]
[#pragma omp section new-line
structured-block2 ]

Functional Parallelism

#pragma omp parallel
#pragma omp sections nowait 
#pragma omp section
#pragma omp section
#pragma omp section
< Лекция 6 || Лекция 7: 12 || Лекция 8 >
Мария Зайцева
Мария Зайцева
Россия, Воронеж, ВГУ