OpenMP???? - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

OpenMP????

Description:

Title: Author: AN Hong Last modified by: lynn Created Date: 4/4/2004 1:42:01 PM Document presentation format: – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 57
Provided by: ANH82
Category:

less

Transcript and Presenter's Notes

Title: OpenMP????


1
  • OpenMP????

2
OpenMP????
  • OpenMP??
  • OpenMP????
  • ??????????
  • OpenMP????

3
OpenMP??
  • OpenMP??????API??????????????????
  • ??????(Compiler Directive)??????(Runtime
    Library)?????(Environment Variables)
  • ???????(Incremental Parallelization)

4
???OpenMP
  • ???OpenMP
  • ??????API(Application Programming Interface )
  • ?????API??(??????????????)??
  • ?C/C ?Fortan????????
  • ????????????????????
  • OpenMP??????
  • ??????????????
  • ??????????????
  • ????????????????????

5
OpenMP???
  • 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???

6
OpenMP???
  • ???
  • ????
  • ????
  • ????

7
OpenMP??????
  • ???????????(Programming Model)
  • OpenMP??Fork-Join??????

8
OpenMP????
  • ??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

9
OpenMP????
  • ??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
  • ????

16
for??????
  • for??????????????????????
  • ????
  • pragma omp for clause,clause newline
  • clause
  • Schedule(type ,chunk)
  • ordered
  • private (list)
  • firstprivate (list)
  • lastprivate (list)
  • shared (list)
  • reduction (operator list)
  • nowait

17
for??????
  • schedule??????????????????????
  • ??????chunk??,????????????????
  • type?static,???????? chunk??,???????
  • type?dynamic,???????????chunk??,???????

18
Sections??????
  • sections?????????????????????????
  • ???section????????
  • Section????
  • pragma omp sections clause,clause
    newline
  • pragma omp section newline
  • pragma omp section newline

19
Sections??????
  • clause
  • private (list)
  • firstprivate (list)
  • lastprivate (list)
  • reduction (operator list)
  • nowait
  • ?sections?????????????,???nowait????

20
Sections??????
  • 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 /

21
single??????
  • single??????????????????????????
  • ????????single????????????????,??nowait????
  • ????
  • pragma omp single clause,clause newline
  • clause
  • private(list)
  • firstprivate(list)
  • nowait

22
???????????
  • parallel for??????
  • parallel sections??????

23
parallel 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)

24
parallel 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

25
parallel 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????

27
master ????
  • master????????????????
  • ????
  • pragma omp master newline

28
critical????
  • critical?????????????????????
  • ???????????
  • ????
  • pragma omp critical name newline

29
critical????
  • include ltomp.hgt
  • main()
  • int x
  • x 0
  • pragma omp parallel shared(x)
  • pragma omp critical
  • x x 1
  • / end of parallel section /

30
barrier????
  • barrier???????????????????
  • ??????????,??????
  • barrier????????????????
  • ????
  • pragma omp barrier newline

31
barrier????
  • barrier?????????

?? ??
if (x 0) pragma omp barrier if (x 0) pragma omp barrier
32
atomic????
  • atomic???????????????????
  • ????
  • pragma omp atomic newline
  • atomic?????

33
flush????
  • flush?????????????,???????????????????
  • ????
  • pragma omp flush (list) newline
  • flush?????????????,nowait????

barrier critical??????? ordered??????? parallel
???? for???? sections???? single????
34
ordered????
  • ordered???????????????
  • ??????????????ordered?????
  • ?????for??parallel for????????
  • ????
  • pragma omp ordered newline

35
threadprivate??????
  • threadprivate????????????????????????????
  • ????????????????
  • ????
  • pragma omp threadprivate (list) newline

36
threadprivate??????
  • 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??

38
private??
  • private???????????????????? ?
  • ????
  • private(list)
  • private?threadprivate??

PRIVATE THREADPRIVATE
???? ?? ??
?? ???????????? ????????????????
??? ? ?
??? ?????- ????????????? ???
??? ?? FIRSTPRIVATE ?? COPYIN
39
shared??
  • shared???????????????????????
  • ??????????????
  • ????
  • shared (list)

40
default??
  • default??????????????????????????????????
  • ????
  • default (shared none)

41
firstprivate??
  • firstprivate???private?????
  • ?????????
  • ????
  • firstprivate (list)

42
lastprivate??
  • lastprivate???private?????
  • ?????????????????????
  • ????
  • lastprivate (list)

43
copyin??
  • copyin??????????????threadprivate???????
  • ?????????????
  • ????
  • copyin(list)

44
reduction??
  • reduction???????????????????????
  • ???,?????????????
  • ?????????????????????????,??????????
  • ????
  • reduction (operator list)

45
reduction??
  • 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)

46
reduction??
  • Reduction?????

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??????????

51
OpenMP????
  • ?????????????Pi??

52
OpenMP????
  • 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

53
OpenMP????
  • ???????????//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)

54
OpenMP????
  • ?????????????? //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)

55
OpenMP????
  • ??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

56
OpenMP????
  • ?????????????
  • 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
Write a Comment
User Comments (0)
About PowerShow.com