Опубликован: 12.07.2012 | Доступ: свободный | Студентов: 355 / 24 | Оценка: 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

Usage

Compiler directives:

C/C++

#pragma omp directive [clause, …]

Fortran

!$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_thread_num(),
    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 ]
structured-block1
[#pragma omp section new-line
structured-block2 ]
}

Functional Parallelism

#pragma omp parallel
#pragma omp sections nowait 
{
  thread1_work();
#pragma omp section
  thread2_work();
#pragma omp section
  thread3_work();
#pragma omp section
  thread4_work(); 
} 
< Лекция 6 || Лекция 7: 12 || Лекция 8 >