Title: OpenMP
1OpenMP
- E. Bruce Pitman
- October, 2002
2Outline
- What is OpenMP
- Multi-threading
- How to use OpenMP
- Limitations
- OpenMP MPI
- References
3What is OpenMP?
- A portable standard for shared memory
multi-processing - A library of compiler directives
- Fine-grained (loop-level)
- To varying degrees, Sun, SGI, IBM, HP, Intel
4Multi-threading
5Multi-threading
6How to use OpenMP
7How to use OpenMP
8How to use OpenMP
9How to use OpenMP
10How to use OpenMP
11How to use OpenMP
12How to use OpenMP
- schedule(static ,chunk)
- Deal-out blocks of iterations of size chunk to
each thread. - schedule(dynamic,chunk)
- Each thread grabs chunk iterations off a queue
until all iterations have been handled. - uschedule(guided,chunk)
- Threads dynamically grab blocks of iterations.
The size of the block starts large and shrinks
down to size chunk as the calculation proceeds.
13How to use OpenMP
- schedule(runtime)
- Schedule and chunk size taken from the
OMP_SCHEDULE environment variable.
14How to use OpenMP
- Heres an example of PRIVATE and FIRSTPRIVATE
- variables A,B, and C 1COMP PARALLEL
PRIVATE(B) COMP FIRSTPRIVATE(C) - Inside this parallel region A is shared by all
threads and equals 1 - B and C are local to each thread - Bs
initial value is undefined - Cs initial value
equals 1 - Outside this parallel region the values of B
and C are undefined.
15How to use OpenMP
16How to use OpenMP
17How to use OpenMP
18How to use OpenMP
19How to use OpenMP
20How to use OpenMP
21How to use OpenMP
22How to use OpenMP
- !/bin/csh -f
- PBS -l ncpus8
- PBS -V
- PBS -q medium_p
- PBS -M pitman_at_math.buffalo.edu
23How to use OpenMP
- cp /CCRSGI/home/pitman/fortran/sph/sph
PBSTMPDIR - cd PBSTMPDIR
- setenv OMP_NUM_THREADS 8
- f90 -O2 -LNO -IPA -n32 -mips4 -r12000 -mp -o sph
sphomp.f - time ./sph gt outfile
- cp outfile /CCRSGI/home/pitman/fortran/sph/
- remove scratch directory
- cd /FCScratch
- \rm -r PBSTMPDIR
24How to use OpenMP
k 1 do while (k .le. maxit .and. error .gt.
tol) error 0.0 !omp parallel !omp do
do j1,m do i1,n uold(i,j) u(i,j)
enddo enddo
25How to use OpenMP
!omp do private(resid) reduction(error) do j
2,m-1 do i 2,n-1 resid
(ax(uold(i-1,j) uold(i1,j))
ay(uold(i,j-1) uold(i,j1)) b
uold(i,j) - f(i,j))/b u(i,j) uold(i,j) -
omega error error residresid end do
enddo
26How to use OpenMP
!omp enddo nowait !omp end parallel omp end
parallel k k 1 error sqrt(error)/dble(nm
) enddo
27Limitations
- Easy to port serial code to OpenMP
- OpenMP code can run in serial mode
- HOWEVER
- Shared memory machines only
- Limited scalability -- after 8 processors, not
much speed-up - Overhead of parallel do, parallel regions
28Limitations
- OpenMP currently does not specify or provide
constructs for controlling the binding of threads
to processors. - Processors can migrate, causing overhead.
- This behavior is system-dependent.
- System-dependent solutions may be available.
29OpenMP and MPI
30References
- www.openmp.org
- http//www.ccr.buffalo.edu/documents/CCR_openmp_pb
s.PDF - http//www.epcc.ed.ac.uk/research/openmpbench/
- http//www.llnl.gov/computing/tutorials/workshops/
workshop/openMP/MAIN.html - http//scv.bu.edu/SCV/Tutorials/OpenMP/