Title: OpenMP????
1 2OpenMP????
- OpenMP??
- OpenMP????
- ??????????
- OpenMP????
3OpenMP??
- OpenMP??????API??????????????????
- ??????(Compiler Directive)??????(Runtime
Library)?????(Environment Variables) - ???????(Incremental Parallelization)
4???OpenMP
- ???OpenMP
- ??????API(Application Programming Interface )
- ?????API??(??????????????)??
- ?C/C ?Fortan????????
- ????????????????????
- OpenMP??????
- ??????????????
- ??????????????
- ????????????????????
5OpenMP???
- 1994?,???ANSI X3H5????,???
- 1997?,OpenMP????????????ANSI X3H5,?????
- 1997?10?????Fortran???????????? FORTRAN version
1.0 - 1998?11?9??????C?C?????C/C version 1.0
- 2000?11???FORTRAN version 2.0
- 2002?3???C/C version 2.0
- 2005?5?OpenMP2.5????Fortran?C/C ???????
- ???????http//www.openmp.org/drupal/node/view/8???
6OpenMP???
7OpenMP??????
- ???????????(Programming Model)
- OpenMP??Fork-Join??????
8OpenMP????
- ??Fortran???OpenMP?????
- PROGRAM HELLOINTEGER VAR1, VAR2, VAR3
!Serial code !Beginning of parallel
section. Fork a team of threads. !Specify
variable scoping !OMP PARALLEL PRIVATE(VAR1,
VAR2) SHARED(VAR3) !Parallel section
executed by all threads !All threads
join master thread and disband!OMP END
PARALLEL !Resume serial code END
9OpenMP????
- ??c/c???OpenMP?????
- include ltomp.hgt
- main ()
- int var1, var2, var3
- /Serial code/
-
- /Beginning of parallel section. Fork a team
ofthreads/ - /Specify variable scoping /
- pragma omp parallel private(var1, var2)
shared(var3) -
- /Parallel section executed by all threads/
-
- /All threads join master thread and disband/
-
- /Resume serial code /
-
-
10?????OpenMP????
- ??C/C???OpenMP???????????
- include "omp.h//eg1
- int main(int argc, char argv)
-
- int nthreads, tid
- int nprocs
- char buf32
- / Fork a team of threads /
- pragma omp parallel private(nthreads, tid)
-
- / Obtain and print thread id /
- tid omp_get_thread_num()
- printf("Hello World from OMP thread d\n",
tid) - / Only master thread does this /
- if (tid0)
- nthreads omp_get_num_threads()
- printf("Number of threads d\n",
nthreads) -
-
11????
- OpenMP?pragma??????
- pragma omp directive_name
pragma omp directive-name clause, ... newline
???????? ???OpenMP? ???????? ?? OpenMP??? ???????? ???????? ?????? OpenMP??? ?? ???????? ?????,?? ????,??? ?????? ?? ???????? ???????? ????????
12????
- ???
- ????
- ???????????????,??????????
- ????
- ??OpenMP????????????????
- ????
- ???????????
13???
???? ????
???? for?????????????? ???? critical?sections?????????????
pragma omp parallel pragma omp for for() sub1() sub2() void sub1() pragma omp critical void sub2() pragma omp sections
14?????
- ???????????????
- ????
- pragma omp parallel clause,clausenewline
- clause
- if(scalar-expression)
- private(list)
- firstprivate(list)
- default(shared none)
- shared(list)
- copyin(list)
- reduction(operator list)
- num_threads(integer-expression)
15??????
- ???????????????????????????
- ??for??
- ??sections
- ????
16for??????
- for??????????????????????
- ????
- pragma omp for clause,clause newline
- clause
- Schedule(type ,chunk)
- ordered
- private (list)
- firstprivate (list)
- lastprivate (list)
- shared (list)
- reduction (operator list)
- nowait
17for??????
- schedule??????????????????????
- ??????chunk??,????????????????
- type?static,???????? chunk??,???????
- type?dynamic,???????????chunk??,???????
18Sections??????
- sections?????????????????????????
- ???section????????
- Section????
- pragma omp sections clause,clause
newline -
- pragma omp section newline
-
- pragma omp section newline
-
-
19Sections??????
- clause
- private (list)
- firstprivate (list)
- lastprivate (list)
- reduction (operator list)
- nowait
- ?sections?????????????,???nowait????
20Sections??????
- include ltomp.hgt//eg2
- define N 1000
- int main ()
- int i
- float aN, bN, cN
- / Some initializations /
- for (i0 i lt N i)
- ai bi i 1.0
- pragma omp parallel shared(a,b,c) private(i)
-
- pragma omp sections nowait
-
- pragma omp section
- for (i0 i lt N/2 i)
- ci ai bi
- pragma omp section
- for (iN/2 i lt N i)
- ci ai bi
- / end of sections /
21single??????
- single??????????????????????????
- ????????single????????????????,??nowait????
- ????
- pragma omp single clause,clause newline
- clause
- private(list)
- firstprivate(list)
- nowait
22???????????
- parallel for??????
- parallel sections??????
23parallel for??????
- Parallel for????????????????????for??
- ????
- pragma omp parallel for clause newline
- clause
- if (scalar_logical_expression)
- default (shared none)
- schedule (type ,chunk)
- shared (list)
- private (list)
- firstprivate (list)
- lastprivate (list)
- reduction (operator list)
- copyin (list)
24parallel for??????
- include ltomp.hgt //ex2
- define N 1000
- define CHUNKSIZE 100
- int main ()
- int i, chunk
- float aN, bN, cN
- / Some initializations /
- for (i0 i lt N i)
- ai bi i 1.0
- chunk CHUNKSIZE
- pragma omp parallel for \
- shared(a,b,c,chunk) private(i) \
- schedule(static,chunk)
- for (i0 i lt n i)
- ci ai bi
-
25parallel sections??????
- parallel sections????????????????????sections??
- ????
- pragma omp parallel sections clause newline
- clause
- default (shared none)
- shared (list)
- private (list)
- firstprivate (list)
- lastprivate (list)
- reduction (operator list)
- copyin (list)
- ordered
26????
- master ????
- critical????
- barrier????
- atomic????
- flush????
- ordered????
27master ????
- master????????????????
- ????
- pragma omp master newline
28critical????
- critical?????????????????????
- ???????????
- ????
- pragma omp critical name newline
29critical????
- include ltomp.hgt
- main()
-
- int x
- x 0
- pragma omp parallel shared(x)
-
- pragma omp critical
- x x 1
- / end of parallel section /
-
30barrier????
- barrier???????????????????
- ??????????,??????
- barrier????????????????
- ????
- pragma omp barrier newline
31barrier????
?? ??
if (x 0) pragma omp barrier if (x 0) pragma omp barrier
32atomic????
- atomic???????????????????
- ????
- pragma omp atomic newline
- atomic?????
33flush????
- flush?????????????,???????????????????
- ????
- pragma omp flush (list) newline
- flush?????????????,nowait????
barrier critical??????? ordered??????? parallel
???? for???? sections???? single????
34ordered????
- ordered???????????????
- ??????????????ordered?????
- ?????for??parallel for????????
- ????
- pragma omp ordered newline
35threadprivate??????
- threadprivate????????????????????????????
- ????????????????
- ????
- pragma omp threadprivate (list) newline
36threadprivate??????
- int alpha10, beta10, i//eg3
- pragma omp threadprivate(alpha)
- int main ()
-
- / First parallel region /
- pragma omp parallel private(i,beta)
- for (i0 i lt 10 i)
- alphai betai i
- / Second parallel region /
- pragma omp parallel
- printf("alpha3 d and beta3d\n",alpha3,
beta3) -
37???????
- ???????
- ???????
- private??
- shared??
- default??
- firstprivate??
- lastprivate??
- copyin??
- reduction??
38private??
- private???????????????????? ?
- ????
- private(list)
- private?threadprivate??
PRIVATE THREADPRIVATE
???? ?? ??
?? ???????????? ????????????????
??? ? ?
??? ?????- ????????????? ???
??? ?? FIRSTPRIVATE ?? COPYIN
39shared??
- shared???????????????????????
- ??????????????
- ????
- shared (list)
40default??
- default??????????????????????????????????
- ????
- default (shared none)
41firstprivate??
- firstprivate???private?????
- ?????????
- ????
- firstprivate (list)
42lastprivate??
- lastprivate???private?????
- ?????????????????????
- ????
- lastprivate (list)
43copyin??
- copyin??????????????threadprivate???????
- ?????????????
- ????
- copyin(list)
44reduction??
- reduction???????????????????????
- ???,?????????????
- ?????????????????????????,??????????
- ????
- reduction (operator list)
45reduction??
- include ltomp.hgt//eg4
- int main ()
-
- int i, n, chunk
- float a100, b100, result
- / Some initializations /
- n 100
- chunk 10
- result 0.0
- for (i0 i lt n i)
-
- ai i 1.0
- bi i 2.0
-
- pragma omp parallel for default(shared)
private(i)\ - schedule(static,chunk) reduction(result)
- for (i0 i lt n i)
- result result (ai bi)
- printf("Final result f\n",result)
46reduction??
xx op expr x expr op x (except subtraction) x
binop expr x x x-- --x
x????? expr??????x????????,????? binop?,,-,/,,
,??,????? op?,,-,/,,,,,or??,?????
47??/????????
?? ???? ???? ???? ???? ???? ????
?? PARALLEL DO/for SECTIONS SINGLE PARALLEL DO/for PARALLEL SECTIONS
IF v v v
PRIVATE v v v v v v
SHARED v v v v
DEFAULT v v v
FIRSTPRIVATE v v v v v v
LASTPRIVATE v v v v
REDUCTION v v v v v
COPYIN v v v
SCHEDULE v v
ORDERED v v
NOWAIT v v v
48?????????
- ????
- ??DO/for?SECTIONS?SINGLE?MASTER?BARRIER????????PAR
ALLEL?,?????????,???????? - ??ORDERED???????DO/for???
- ??ATOMIC??ATOMIC?????????????,??????????
- ??CRITICAL???????CRITICAL???????,??????????
- ?PARALLEL???,????????????????
49?????????
- ????
- PARALALL ??????????????,?????????????,????????????
????,????????? - DO/for?SECTION?SINGLE????????PARALLEL
?,???????????? - DO/for?SECTION?SINGLE???????????CRITICAL?ORDERED?M
ASTER?? - CRITICAL?????????
- BARRIER???????????DO/for?ORDERED?SECTIONS?SINGLE?M
ASTER?CRITICAL?? - MASTER???????????DO/for?SECTIONS?SINGLE???
- ORDERED???????????CRITICAL??
- ????????PARALLEL ?????,???????????????????????????
,????????????
50??????????
- ?????
- OpenMP???????????????????????
- ??C/C,???????????omp.h
- ????
- OMP_SCHEDULE????for,parallel for????????????????
- OMP_NUM_THREADS???????????
- OMP_DYNAMIC???????TRUE?FALSE,??????????????????
- OMP_NESTED??????????
51OpenMP????
52OpenMP????
- C????????
- / Seriel Code /
- static long num_steps 100000
- double step
- void main ()
- int i
- double x, pi, sum 0.0
- step 1.0/(double) num_steps
- for (i0ilt num_steps i)
- x (i0.5)step
- sum sum 4.0/(1.0xx)
-
- pi step sum
53OpenMP????
- ???????????//1
- include ltomp.hgt
- static long num_steps 100000
- double step
- define NUM_THREADS 2
- void main ()
- int i
- double x, pi, sumNUM_THREADS
- step 1.0/(double) num_steps
- omp_set_num_threads(NUM_THREADS) //
- pragma omp parallel
-
- double x
- int id
- id omp_get_thread_num()
- for (iid, sumid0.0ilt num_steps
iiNUM_THREADS)// - x (i0.5)step
- sumid 4.0/(1.0xx)
54OpenMP????
- ?????????????? //2
- include ltomp.hgt
- static long num_steps 100000
- double step
- define NUM_THREADS 2
- void main ()
-
- int i
- double x, pi, sumNUM_THREADS
- step 1.0/(double) num_steps
- omp_set_num_threads(NUM_THREADS) //
- pragma omp parallel //
-
- double x
- int id
- id omp_get_thread_num()
- sumid 0 //
- pragma omp for//
- for (i0ilt num_steps i)
55OpenMP????
- ??private???critical????????
- include ltomp.hgt
- static long num_steps 100000
- double step
- define NUM_THREADS 2
- void main ()
-
- int i
- double x, sum, pi0.0
- step 1.0/(double) num_steps
- omp_set_num_threads(NUM_THREADS)
- pragma omp parallel private (x, sum)
-
- id omp_get_thread_num()
- for (iid,sum0.0ilt num_stepsiiNUM_THREADS
) - x (i0.5)step
- sum 4.0/(1.0xx)
-
- pragma omp critical
56OpenMP????
- ?????????????
- include ltomp.hgt
- static long num_steps 100000
- double step
- define NUM_THREADS 2
- void main ()
- int i
- double x, pi, sum 0.0
- step 1.0/(double) num_steps
- omp_set_num_threads(NUM_THREADS)
- pragma omp parallel for reduction(sum)
private(x) - for (i0iltnum_steps i)
- x (i0.5)step
- sum sum 4.0/(1.0xx)
-
- pi step sum
-