







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Jingke Li
Portland State University
Jingke Li (Portland State University) CS 415/515 OpenMP 1 / 25
An API for a set of compiler directives, library routines, and environment variables that can be used to specify shared-memory parallelism.
Characteristics:
Release History:
More info can be found at the official website: www.openmp.org
Jingke Li (Portland State University) CS 415/515 OpenMP 3 / 25
program hello integer tid !$omp parallel num_threads(4) private(tid) tid = omp_get_thread_num() print *, "hello world from thread = ", tid !$omp end parallel end
Jingke Li (Portland State University) CS 415/515 OpenMP 7 / 25
Jingke Li (Portland State University) CS 415/515 OpenMP 9 / 25
!$omp directive-name [clauses]
Directive Names:
parallel — defining parallel regions do — executing do loops in parallel
parallel do — combined form of parallel and do directives
sections — executing different code blocks with different threads single — restricting a block to a single thread of execution master — restricting a block to execution by the master thread critical — restricting a block to execution by one thread at a time barrier — synchronizing all threads flush — forcing a consistent view of memory for a thread atomic — avoiding conflicts when writing to memory ordered — specifying a block for serial execution
OpenMP provides a relaxed memory consistency model, similar to weak ordering. It specifically allows the reordering of accesses within a thread to different variables unless they are separated by a flush (see below) that includes the variables.
Jingke Li (Portland State University) CS 415/515 OpenMP 13 / 25
The flush operation is the only way in an OpenMP program, to guarantee that a value will move between two threads. In order to move a value from one thread to a second thread, OpenMP requires these four actions in exactly the following order:
A = 1; ... flush(A);
flush(A); B = A;
The above code does not work as written, since we need to guarantee that the flush(A) op in P2 happens after the same op in P1.
A flush of all visible variables is implied
Pre-Version 2.5 Headaches:
opm_set_lock(lck); local_idx = global_idx; global_idx++; opm_unset_lock(lck);
opm_set_lock(lck); #pragma omp flush(global_idx) local_idx = global_idx; global_idx++; #pragma omp flush(global_idx) opm_unset_lock(lck); Wrong! Still wrong!
Jingke Li (Portland State University) CS 415/515 OpenMP 15 / 25
The correct version:
opm_set_lock(lck); #pragma omp flush(global_idx, lck) local_idx = global_idx; global_idx++; #pragma omp flush(global_idx, lck) opm_unset_lock(lck);
The lock variable lck must be included in the flush set, to both prevent reordering with respect to the lock calls, and to keep the global value of global idx up to date.
Jingke Li (Portland State University) CS 415/515 OpenMP 19 / 25
OpenMP provides four environment variables for controlling the execution of parallel code.
All environment variable names are uppercase. The values assigned to them are not case sensitive.
OMP SCHEDULE — Applies only to do, parallel do (Fortran) and for, parallel for (C/C++) directives which have their schedule clause set to runtime. The value of this variable determines how iterations of the loop are scheduled on processors. For example: setenv OMP SCHEDULE "guided, 4" setenv OMP SCHEDULE "dynamic"
OMP NUM THREADS — Sets the maximum number of threads to use during execution. For example: setenv OMP NUM THREADS 8
OMP DYNAMIC — Enables or disables dynamic adjustment of the number of threads available for execution of parallel regions. Valid values are TRUE or FALSE.
OMP NESTED — Enables or disables nested parallelism. Valid values are TRUE or FALSE.
Jingke Li (Portland State University) CS 415/515 OpenMP 21 / 25
OpenMP 2.5:
!$omp parallel do do i = 1, n !$omp parallel do do j = 1, m do k = 1, L work(i,j,k) enddo enddo enddo
OpenMP 3.0:
!$omp parallel do collapse(2) do i = 1, n do j = 1, m do k = 1, L work(i,j,k) enddo enddo enddo