?????? ??????? ???? Parallel Programming: Fundamentals and Implementation - PowerPoint PPT Presentation

Loading...

PPT – ?????? ??????? ???? Parallel Programming: Fundamentals and Implementation PowerPoint presentation | free to download - id: 815391-ZmUwO



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

?????? ??????? ???? Parallel Programming: Fundamentals and Implementation

Description:

Parallel Programming(Techniques and Applications using Networked Workstations and Parallel Computers). Prentice Hall, 1999. , . – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 109
Provided by: czn7
Learn more at: http://pmo.cas.cn
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: ?????? ??????? ???? Parallel Programming: Fundamentals and Implementation


1
  • ?????????????????Parallel Programming
    Fundamentals and Implementation
  • ? ?
  • dair_at_dawning.com.cn
  • ??????????
  • 2006.4

2
????
  • ??,????,?????. ?????????,?????. ?????????,
    P.3356,P.227237, 2000.
  • ????.????????????. ?????????,1999.
  • Barry Wilkinson and Michael Allen. Parallel
    Programming(Techniques and Applications using
    Networked Workstations and Parallel Computers).
    Prentice Hall, 1999.
  • ???,?????. ?????????????. ?????????,2000.
  • ???,?????. ???????????. ?????????,1999.
  • ????. ???????????MPI??????. ?????????, 2001.

3
????
  • MPI http//ww.mpi-forum.org,
  • http//www.mcs.anl.gov/mpi
  • Pthreads http//www.oreilly.com
  • PVM http//www.epm.ornl.gov/pvm/
  • OpemMP http//www.openmp.org
  • ????www.google.com

4
??????Programming with Shared Memory
5
?????????
  • ??????
  • ??????????????????????,??????????????????.
  • ????????????????.
  • ??????????????????,????????????????.
  • ??????????????????.
  • ?????,?????????????????,???????????????,??????????
    ??????????.

6
???????????
  • ?????????
  • Pthreads(????)
  • X3H5(????)
  • OpenMP(??????????????,????????.)
  • ?????????
  • ????????.(Pthreads).
  • ??????,OpenMP?.
  • ??
  • C,Fortran77,Fortran90/95,C

7
??????
  • ?????(Thread Library)
  • Win32 API.
  • POSIX threads????.
  • X3H5???????
  • ????(Compiler Directives)
  • OpenMP - portable shared memory parallelism.

8
???????????
  • ???????????????.
  • ?????,?????
  • ????????????????????1/30???Sun4/75?????,??????????
    ?52??,?fork()??????1700???
  • ???????????????1/3.
  • ???RPC???,???????????
  • ???????????
  • ????????,???????.
  • ??????????????????,?????????
  • ?????Web?????????????????????,??????????cache????
    ??????
  • ?????
  • ????????????????,??????????????????1995??POSIX????
    ????,????????????????????Pthreads,?Linux?SUN?IBM
    AIX??

9
Pthreads????
  • POSIX1003.4a???????????. ??????,??????????????POSI
    X??.????????????????.
  • IEEE Portable Operating System Interface, POSIX,
    1003.1-1995??POSIX????pthreads.

10
????(Pthread??)
  • ??pthread_create
  • ??pthread_exit
  • ??pthread_join
  • ??pthread_detach
  • ???????pthread_attr_init
  • ????pthread_once

11
????
  • ?????????????,??????????,????????????????
  • pthread?????????? ???????.

12
?????
  • ?? ??
  • Mutex_init() ????????
  • Mutext_lock() ???????
  • Mutex_trylock() ????????
  • Mutex_unlock() ??
  • Mutex_destroy() ??????

13
???????
  • ?? ??
  • pthread_cond_init() ???????
  • pthread_cond_wait() ????????
  • pthread_cond_signal() ??????,????????????
  • pthread_cond_timedwait() ???????????timeout
  • pthread_cond_broadcast() ??????????????
  • pthread_cond _destroy() ??????

14
Hello World(1)
  • include ltpthread.hgt
  • include "stdio.h"
  • void worker()
  • main()
  • pthread_t thread
  • pthread_create(thread,NULL,worker,NULL)
  • pthread_join(thread,NULL)
  • void worker()
  • printf("Hello World!\n")

???? gcc hello.c lpthread ???? Hello World!
15
  • pthread_t ??????
  • pthread_create(thread,NULL,worker,NULL)
  • ??pthread_create()?????????,??????????????
  • ??
  • ???????
  • ??????,???????NULL. ?????????????????????????,???
    ??,???????.
  • ????????
  • ????????????,??NULL.???????
  • ?????
  • ??????????
  • ???pthear_exit()??
  • ?????
  • ??????????
  • ??????exit()?,????????????.

16
  • pthread_join(pthread_t wait_for,void status)
  • ????????
  • ????????????,???wait_for???????
  • ????????????????????(????????)
  • ???
  • 0 ????
  • ESRCH ??wait_for???????????????
  • EINVAL ??????
  • EDEADLK ??????.
  • ???????????????????, ????????????,??????ESRCH??.

17
Hello World(2)
  • include ltpthread.hgt
  • include "stdio.h"
  • define numthrds 5
  • pthread_t tid
  • void worker()
  • main()
  • int i
  • tid (pthread_t) calloc(numthrds,sizeof(pthread
    _t))
  • for(i0iltnumthrdsi) pthread_create(tidi,NU
    LL,worker,NULL)
  • for(i0iltnumthrdsi) pthread_join(tidi,NULL)
  • void worker()
  • int myid
  • myid pthread_self() - tid0
  • printf("Hello World from thread d!\n",myid)

Hello World from thread 0! Hello World from
thread 1! Hello World from thread 2! Hello World
from thread 3! Hello World from thread 4!
18
Hello World(3)
void worker() int myid,num myid
pthread_self() - tid0 printf("d was added
to the sum in thread d\n",myid10,myid)
pthread_mutex_lock(mutex) sum
myid10 pthread_mutex_unlock(mutex) return
  • include ltpthread.hgt
  • include "stdio.h"
  • define numthrds 5
  • pthread_t tid
  • pthread_mutex_t mutex
  • int sum0
  • void worker()
  • main()
  • int i
  • tid (pthread_t) calloc(numthrds,sizeof(pthread
    _t))
  • pthread_mutex_init(mutex,NULL)
  • for(i0iltnumthrdsi) pthread_create(tidi,NU
    LL,worker,NULL)
  • for(i0iltnumthrdsi) pthread_join(tidi,NULL)
  • printf(The sum is d\n",sum)

19
????
  • 0 was added to the sum in thread 0
  • 10 was added to the sum in thread 1
  • 20 was added to the sum in thread 2
  • 30 was added to the sum in thread 3
  • 40 was added to the sum in thread 4
  • The sum is 100

20
????????PI??
21
  • include ltpthread.hgt
  • include "stdio.h"
  • pthread_mutex_t reduction_mutex
  • pthread_t tid
  • double pi,w
  • int n
  • int num_threads
  • double f(a)
  • double a
  • return (4.0/(1.0 aa))

void PIworker(void arg) int i,myid
double sum,mypi,x /set individual id to start
at 0 / myid pthread_self() - tid0
/integrate function/ sum0.0 for(i myid
1i lt n inum_threads) x w
((double)i - 0.5) sum f(x) mypi
w sum /reduce value/ pthread_mutex_lock(
reduction_mutex) pi mypi
pthread_mutex_unlock(reduction_mutex)
return(0)
22
gcc hello.c lpthread a.out 1000 5computed pi
3.1415927369231271
??Openmp
  • void main(argc,argv)
  • int argc
  • char argv
  • int i
  • /check command line /
  • if(argc ! 3)
  • printf("Usage s Num_intervals
    Num_threads\n",argv0)
  • exit(0)
  • /get num intervals and num threads from
    command line/
  • n atoi(argv1)
  • num_threads atoi(argv2)
  • w 1.0 / (double)n
  • pi 0.0
  • tid (pthread_t) calloc (num_threads,sizeof(pt
    hread_t))

/initilize lock/ if(pthread_mutex_init(reductio
n_mutex,NULL)) fprintf(stderr,"Cannot init
lock\n") exit(1) /create the threads/
for(i 0 iltnum_threads i) if(pthread_create
(tidi,NULL,PIworker,NULL))
fprintf(stderr,"Cannot create thread d\n",i)
exit(1) /join threads/ for(i
0 i lt num_threads i) pthread_join(tidi
,NULL) printf("computed pi .16f\n",pi)
23
?????????
  • pthread_create()??????????????????????
    ????????????? vs. ????????????????.
  • ?????????????
  • ???????,????????,?????????????
  • ????????,????????,???Pthreads?????
  • Pthreads?????????, ????????????,??????????????????
    ????????????????????????????????????????????????
    ?????????????????????????OpenMP

24
??????
  • ?????(Thread Library)
  • Win32 API.
  • POSIX threads????.
  • X3H5???????
  • ????(Compiler Directives)
  • OpenMP - portable shared memory parallelism.

25
www.openmp.org
  • An Industry Standard API for Shared Memory
    Programming
  • An API for Writing Multithreaded Applications
  • ?????????????
  • ??Fortran, C and C??????????

26
?X3H5???
  • X3H5?ANSI/X3????????,??????PCF(the Parallel
    Computing Forum)??????,?????????ANSI??.
    PCF??????????,??DO???????????????????,????????????
    ????.
  • OpenMP???????????,????????,????????????.

27
ANSI X3H5??????
  • ????????(ANSI??(1993))
  • ??????????????????X3H5,?X3H5?????????????????????.
    (???????OpenMP???!)
  • X3H5??C,Fortran77??Fortran90??.
  • X3H5?????????????????
  • ???(????Work Sharing)
  • ????
  • ???

parallel end parallel
psections end psections
pdo end pdo
psingle end psingle
28
X3H5????
  • program main !?????????
  • A !A????????
  • parallel !???????
  • B !B????????
  • psections !?????
  • section
  • C !??????C
  • section
  • D !???????D
  • end psections !??C?D???
  • psingle ?????????
  • E !E?????????
  • end psingle !??????
  • pdo I1,6 !??do????
  • F(i) !?????????
  • end pdo no wait !???????
  • G !???????
  • end parallel !??????
  • H !?????H

??
P
Q
R
??barrier
B
B
B
C
D
??barrier
E
??barrier
F(12)
F(34)
F(56)
???barrier
G
G
G
??barrier
H
?????????????? ???F(11),F(22),F(36)
29
X3H5????????
  • ?????????,???????????,??????????.?????parallel?,??
    ????????????????(???????).???????????????.????????
    ??????,??end parallel.??????????,??????????.
  • ?????????????????,??????????,?????????????.
  • ?????,?????
  • ????parallel, end parallel, end pdo?end
    psingle???barrier.????,??no wait
  • ????????????,??????????

????
fork
...
????
barrier
????

30
OpenMP ????
  • Fork-Join ????
  • ????????????????????.
  • ?????????????.

31
????OpenMP?
  • OpenMP????????
  • ????????.
  • ?????????.
  • ??????????????,?????,??????????,?????OpenMP???.

?OpenMP???????????????
void main() double Res1000 for(int
i0ilt1000i) do_huge_comp(Resi)
void main() double Res1000 pragma omp
parallel for for(int i0ilt1000i)
do_huge_comp(Resi)
????
????
32
????????
  • OpenMP ?????????.
  • ??????????.
  • ?????????race condition (????)
  • race condition?????,??????????(????????)?????????
    ,?????????????????????????,???????????????,???????
    ???
  • ????????????
  • ?????????????.

33
OpenMP??
  • ??OpenMP????????pragmas.
  • C?C?pragmas???
  • pragma omp construct clause clause
  • Fortran?,???????????
  • COMP CONSTRUCT clause clause
  • !OMP CONSTRUCT clause clause(????????)
  • OMP CONSTRUCT clause clause
  • ???????(??????)
  • C23456789
  • !OMP PARALLEL DO SHARED(A,B,C)
  • COMP PARALLEL DO
  • COMPSHARED(A,B,C)
  • COMP PARALLELDOSHARED(A,B,C)
  • ??OpenMP????????,????OpenMP???????OpenMP???????,??
    ????.

34
Structured blocks(????)
  • ??????
  • ?????????????????
  • ????????????????
  • ???Fortran??STOP???c/c??exit()???,???????.
  • ??OpenMP???????.

COMP PARALLEL 10 30 if() goto 20 go to
10 COMP END PARALLEL if() goto 30 20 print ,
id
COMP PARALLEL 10 if() goto 10 COMP END
PARALLEL print ,id
??????
???????
35
OpenMP??????
  • OpenMP?????????
  • ???Parallel Regions
  • ????Worksharing
  • ????Data Environment
  • ??Synchronization
  • ?????/????
  • ?Fortran,C/C?,OpenMP???????.

36
Parallel Regions(???)
  • ????OpenMP?????,???????????????.
  • ???????omp parallel?,??????,????????????????.???
    ???????,??????ID??,???ID?0.
  • ?????????????.
  • ?????4??????

double A1000 omp_set_num_threads(4) pragma
omp parallel int ID omp_thread_num()
worker(ID,A)
??????????ID ??????A????????????. ID(0,1,2,3).
37
????Lecical / dynamic extent??Orphaned ????
bar.f subroutine whoami external
omp_get_thread_num integer iam,
omp_get_thread_num iam omp_get_thread_num() COM
P CRITICAL print,Hello from , iam COMP END
CRITICAL return end
poo.f COMP PARALLEL call whoami COMP END
PARALLEL

Static/lexical extent????????????????.
Dynamic extent???????????(????)?????,????region.
Orphan???????????????,??????????,?????inline??
38
???????
double A1000 omp_set_num_threads(4) pragma
omp parallel int ID omp_thread_num()
worker(ID,A)
??????????,????.
Double A1000
opm_set_num_threads(4)
worker(2,A)
worker(1,A)
worker(3,A)
worker(0,A)
?????????(?,??barrier??)
??,?????????SPMD??.
39
Hello World(C)
include ltomp.hgt main() int
myid,numthreads pragma omp parallel
myid omp_get_thread_num()
numthreads omp_get_num_threads()
printf("Hello World from thread d of
d!\n",myid,numthreads)
40
Hello World(Fortran)
PROGRAM HELLO integer myid,numthreads integer
omp_get_num_threads,omp_get_thread_num !omp
parallel private(numthreads,myid)
numthreads omp_get_num_threads() myid
omp_get_thread_num() print , 'Hello
World from thread' ,myid,'of',numthreads !omp
end parallel stop end
41
OpenMP??????
  • ????OpenMP??????????????,???OpenMP??????.
  • IBM AIX xlc???,????????-qsmp
  • xlc file.c qsmp
  • xlf_r file.f -qsmp (xlf_r?IBM AIX4.3????????????)
  • ??3000OS AIX4.3 -qsmp AIX4.3?? OpenMP????
  • Intel C/C???icc, Intel Fortran??????-openmp
  • icc file.c openmp
  • ifc file.f openmp
  • ??4000LOSRedhat Linux 8.0
  • PGI C/C???icc, PGI Fortran??????-mp
  • pgcc file.c mp
  • pgf77 file.f mp
  • pgf90 file.f mp
  • ??4000AOSSuSE Linux 8.0 / Turbo Linux

42
????(?????)
  • C
  • pragma omp parallel clause clause ...
    new-line
  • structured-block
  • Fortran
  • !OMP PARALLEL clause, clause...
  • block
  • !OMP END PARALLEL
  • ??clause?????
  • if(expr)??expr???????????????
  • private(list)?????,???????
  • firstprivate(list)?????????????????
  • default(shared none)(C)
  • DEFAULT(PRIVATE SHARED NONE)(Fortran)
  • shared(list)????????????
  • copyin(list)??????threadprivate?????
  • reduction(operator list)????

43
OpenMP??????
  • OpenMP?????????
  • ???Parallel Regions
  • ????Worksharing
  • DO(Fortran)/for(C)????????????
  • Sections???????
  • Single?????????????????(?I/O)
  • ????Data Environment
  • ??Synchronization
  • ?????/????

OpenMP?????
44
????DO(Fortran)/for(C)??
  • Fortran
  • !OMP DO clause, clause...
  • do_loop
  • !OMP END DO NOWAIT(??)
  • C/C
  • pragma omp for clause clause ... new-line
  • for-loop

COMP PARALLEL COMP DO DO i0,n
pragma omp parallel pragma omp for for
(i0iltni)
?DO/for????????barrier????,?NO WAIT/no wait????.
45
??parrallel???for??
for(i0IltNi) ai ai bi
????
pragma omp parallel int id, i, Nthrds,
istart, iend id omp_get_thread_num()
Nthrds omp_get_num_threads() istart id
N / Nthrds iend (id1) N / Nthrds
for(iistartIltiendi) ai ai bi
?????????
pragma omp parallel pragma omp for
schedule(static) for(i0IltNi) ai ai
bi
????????????
??DO???PARALLEL???????,??????????????C??.
46
????????????
  • ?????????OpenMP??????????
  • ??????????????????,?????????????????
  • ??????????,??????????????????????????????.
  • ???????????,???????????????????
  • ??????!omp parallel
  • ???????do/for,section,?single??.

47
????for??
  • pragma omp for clause clause ... new-line
  • for-loop
  • Clause??????
  • private(list)
  • firstprivate(list)
  • lastprivate(list)
  • reduction(operator list)
  • ordered
  • schedule(kind, chunk_size)
  • nowait

???????
48
????DO??
  • !OMP DO clause, clause...
  • do_loop
  • !OMP END DO NOWAIT
  • PRIVATE(list)
  • FIRSTPRIVATE(list)
  • LASTPRIVATE(list)
  • REDUCTION(operatorintrinsic_procedure_namelist
    )
  • SCHEDULE(type,chunk)
  • ORDERED

49
DO/for??????
  • ????????DO?for??.
  • For??????????,??????break????.
  • ?Fortran?,????END DO????,???????DO???????.
  • ?????????.
  • Schedule, ordered,nowait????????.

50
schedule
  • Schedule?????????????????
  • schedule(dynamic,chunk)
  • ???????chunk_size?????,??????????,????,??????(????
    ??????chunk_size).(???)
  • ?chunk_size?????,???1.
  • schedule(static,chunk)
  • ??chunk_size???, ??????????????chunk??????,???????
    ????,???????.
  • ??chunk_size????,??????????????????????.
  • schedule(guided,chunk)
  • ?????????????,?????chunk????.
  • schedule(runtime)
  • ?????chunk??????????OMP_SCHEDULE???.
  • chunk????????.
  • ??????.

51
Schedule???
Work pool
Work
Work
..
..
..
..
..
.
.
.
????
.
.
.
Dynamic??
Static??
Guided??
52
????
  • ??????????.
  • ??
  • ???????,????????????.
  • ?????????????
  • ??????????????????(???????).
  • ??
  • ??????????????,?????????.
  • Guided??????for??
  • ??
  • ???????????qceiling(n/p)???,??nmax(n-q,pk),????
    ,??npk??.
  • ????????????,????????????????????.

53
NOWAIT??(Fortran)
??????i???????????
COMP PARALLEL COMP DO do i1,n
a(i) cos(a(i)) enddo COMP END DO COMP
DO do i1,n b(i)a(i)b(i)
enddo COMP END DO COMP END PARALLEL
COMP PARALLEL COMP DO do i1,n
a(i) cos(a(i)) enddo COMP END DO
NOWAIT COMP DO do i1,n
b(i)a(i)b(i) enddo COMP END DO COMP END
PARALLEL
?? BARRIER
No BARRIER
END DO????enddo,???.
54
no wait??(C)
pragma omp parallel pragma omp for
for(i1iltni) a(i)
cos(a(i)) pragma omp for
for(i1iltni) b(i)a(i)b(i)

pragma omp parallel pragma omp for no
wait for(i1iltni)
a(i) cos(a(i)) pragma omp for no wait
for(i1iltni)
b(i)a(i)b(i)
No BARRIER
?? BARRIER
????.
55
Sections??(?????)
  • Sections??????????????????????.(??10???10?????????
    ?,????.)

pragma omp sections no wait x_calculation()
pragma omp section y_calculation() pragma
omp section z_calculation()
x_calculation(),y_calculation()??z_calculation()??
?????????? ???????.?????????????.
??????omp sections????????barrier????.????
nowait ??????barrier??(???????????). ?for?????,O
penMP???parallel sections.
56
Single
  • Single?????????????????
  • pragma omp single clause clause ... new-line
  • structured-block
  • Clause is one of the following
  • private(list)
  • firstprivate(list)
  • nowait

57
Work sharing????
  • Fortran
  • DO
  • SECTIONS
  • SINGLE
  • WORKSHARE
  • C
  • for
  • sections
  • single

58
Binding?????????
  • for, sections, single, master, and
    barrier???????????????????,???????.

pragma omp parallel pragma omp for for
(I0IltNI) NEAT_STUFF(I)
pragma omp parallel for for (I0IltNI) NEAT_
STUFF(I)

59
Fortran
!OMP PARALLEL DO clause, clause... do_loop
!OMP END PARALLEL DO
!OMP PARALLEL SECTIONS clause,
clause... !OMP SECTION block !OMP
SECTION block . . . !OMP END PARALLEL SECTIONS
!OMP PARALLEL WORKSHARE clause,
clause... block !OMP END PARALLEL WORKSHARE
60
C
pragma omp parallel for clause clause ...
new-line for-loop
pragma omp parallel sections clause clause
... new-line pragma omp section
new-line structured-block pragma omp section
new-line structured-block . .
61
OpenMP??????
  • OpenMP?????????
  • ???Parallel Regions
  • ????Worksharing
  • ????Data Environment
  • ??Synchronization
  • ?????/????

62
??????
  • ????????
  • ????????????
  • ?????????
  • Fortran COMMON ?, SAVE ??, MODULE ??
  • C File scope variables, static
  • ????????????...
  • ???????????????????
  • ??????Auto???????.

63
????????
subroutine work common /input/ A(10) real
temp(10) integer count save count
program sort common /input/ A(10) integer
index(10) call input COMP PARALLEL call
work(index) COMP END PARALLEL print, index(1)
??A,index?cound???????. temp???????,????????.
64
??????
  • ???????? threadprivate?????????????????.
  • ?????????????????
  • shared(?????)
  • private
  • firstprivate
  • lastprivate??????????????????, ????????
  • ????????????
  • default (private shared none)

65
??????
  • ??????OpenMP??????????.
  • ?????
  • ???????
  • Copyin
  • Firstprivate
  • ??????
  • Reduction
  • Lastprivate
  • OpenMP??????????????????????????????.
  • OpenMP???????
  • ?????????
  • ?threadprivate ?????????
  • ?private, firstprivate,lastprivate, or reduction
    ???????
  • ??????.

66
Threadprivate?????????
  • parameter (N1000)
  • real A(N,N)
  • COMP THREADPRIVATE(/buf/)
  • common/buf/lft(N),rht(N)
  • COMP PARALLEL
  • call init
  • call scale
  • call order
  • COMP END PARALLEL

buf ????????
subroutine scale parameter
(N1000) COMP THREADPRIVATE(/buf/)
common/buf/lft(N),rht(N) do i1, N
lft(i) const A(i,iam) end do
return end
subroutine order parameter
(N1000) COMP THREADPRIVATE(/buf/)
common/buf/lft(N),rht(N) do i1, N
A(i,iam) lft(index) end do
return end
67
Private
  • private(list)??list??????????????.
  • ?????????(???????)
  • ????????,???????

main() int i 10000 pragma omp
parallel private(i) i -10000
printf("d\n",i) ?? 10000
main() int i 10000 pragma omp
parallel i -10000
printf("d\n",i) ?? -10000
68
  • Private???????????????,?????????????,????????????,
    ????????????.
  • ????????,??????????????????????????????????,?????
    ????????????????????????.
  • ?????????????????
  • ???????????(?????????)

69
?
  • For???

3 804398705 2 804398705 2 804398706 2 804398707 0
804398705 0 804398706 0 804398707 1 804398705 1 80
4398706 1 804398707 sum 10
main() int i,sum0,myid pragma
omp parallel for \
private(i,sum)
for(i0ilt10i) myid
omp_get_thread_num()
printf("d\n",myid) sum sum
1 printf("d\n",sum)
printf("sum
d\n",sum)
??sum ????.
70
Firstprivate
  • Firstprivate? private?????.
  • ????????????????????.

1 2 3 1 1 2 3 1 2 3
main() int i,sum0,myid pragma
omp parallel for firstprivate(i,sum)
for(i0ilt10i) sum sum 1
printf("d\n",sum)
printf("sum d\n",sum)

71
Lastprivate
  • Lastprivate??????????????????.

pragma omp parallel pragma omp for
lastprivate(i) for (i0 iltn-1 i) ai
bi bi1 aib0
for (i1iltn-1i) a(i) b(i1) a(i) b(0)
???? in-1
72
Default
  • ????default(shared)(???????)
  • ????default(private)
  • ????????????????,?????
  • ????????????private(list)
  • default(none) ????????????????
  • Fortran??default(private).
  • C/C ?default(private),??default(shared)
    ?default(none).

73
DEFAULT?(Fortran)
itotal 1000 COMP PARALLEL PRIVATE(np, each) np
omp_get_num_threads() each itotal/np COMP
END PARALLEL
itotal 1000 COMP PARALLEL DEFAULT(PRIVATE)
SHARED(itotal) np omp_get_num_threads() each
itotal/np COMP END PARALLEL
74
reduction
  • ????(?????????????????????)
  • reduction (op list).
  • List?????????????????.
  • ???????
  • Operator Initialization
  • 0
  • 1
  • 1
  • 0
  • ??????????

pragma omp parallel for private(i) shared(x, y,
n) reduction( a, b) for (i0 iltn i) a
a xi b b yi
75
??PI??
  • ?????????????PI????(????????).
  • ??OpenMP?????????.???????????,???????(????????????
    ???for??)
  • ??????????????SPMD????.
  • ????????????.
  • ??.

76
????
include "stdio.h" main(int argc,char argv)
n atoi(argv1) w 1.0 /
(double) n sum 0.0
for(i0iltni) x w ((double)i -
0.5) sum sum 4.0/(1.0xx)
pi w sum printf("Computed pi
.16f\n",pi)
77
???Parallel????????
  • include "stdio.h"
  • main(int argc, char argv)
  • n atoi(argv1)
  • w 1.0 / (double) n
  • sum 0.0
  • pragma omp parallel private(x)
    shared(w) reduction(sum)
  • myid omp_get_thread_num()
  • numthreads omp_get_num_threads()
  • for(imyidiltninumthreads)
  • x w ((double)i -
    0.5)
  • sum sum
    4.0/(1.0xx)
  • .

????????
78
??for????????
  • include "stdio.h"
  • main(int argc, char argv)
  • n atoi(argv1)
  • w 1.0 / (double) n
  • sum 0.0
  • pragma omp for reduction(sum)
  • for(i0iltni)
  • x w ((double)i -
    0.5)
  • sum sum
    4.0/(1.0xx)
  • pi w sum
  • printf("Computed pi
    .16f\n",pi)

?Pthreads??
79
OpenMP??????
  • OpenMP?????????
  • ???Parallel Regions
  • ????Worksharing
  • ????Data Environment
  • ??Synchronization
  • ?????/????

80
OpenMP??
  • ??????
  • ????I/O
  • OpenMP????????
  • atomic
  • critical section
  • barrier
  • flush
  • ordered
  • single
  • master

???????????????,?????????????????.
81
critical section
  • ??????????????.
  • pragma omp critical (name) new-line
  • structured-block

COMP PARALLEL DO PRIVATE(B) COMP
SHARED(RES) DO 100 I1,NITERS B DOIT(I) COMP
CRITICAL CALL CONSUME (B, RES) COMP END
CRITICAL 100 CONTINUE
82
atomic
  • Atomic???????,???????????.
  • pragma omp atomic new-line
  • expression-stmt
  • ??expression-stmt???????
  • x binop expr (bino??? - / ltlt
    gtgt )
  • x
  • x
  • x--
  • --x
  • ??????????(?????????X???)
  • COMP PARALLEL PRIVATE(B)
  • B DOIT(I)
  • COMP ATOMIC
  • X X B
  • COMP END PARALLEL

83
  • ????????????

pragma omp parallel for \ shared(x, y, index,
n) for (i0 iltn i) pragma omp
atomic xindexi work1(i) yi
work2(i)
pragma omp parallel for \ shared(x, y, index,
n) for (i0 iltn i) pragma omp
critical xindexi work1(i) yi
work2(i)
????
???
84
??????????
  • ?????????????,OpenMP????????????????????????????.
  • ??????????????.
  • ????????????
  • ??????????????,?????????.
  • ???????????,
  • ???????????,????????????

85
barrier
  • Barrier ???????????????????Barrier??.

pragma omp parallel shared (A, B, C)
private(id) idomp_get_thread_num() Aid
big_calc1(id) pragma omp barrier pragma omp
for for(i0iltNi) Cibig_calc3(I,A) prag
ma omp for nowait for(i0iltNi) Bibig_cal
c2(C, i) Aid big_calc3(id)
for?????barrier??
for?????barrier??
parallel????barrier??
86
ordered
  • Ordered????????????????????????????????.
  • ?????,????????ordered,?????????????
  • ???????????????
  • void worker(int k)
  • pragma omp ordered
  • printf(d ,k)
  • main()
  • int i
  • pragma omp parallel for schedule(dynamic)
  • for(i0ilt5i)
  • worker(i)
  • ????
  • 0 1 2 3 4

87
master
  • Master???????????????????. ????????.??????barrier?
    ?.

pragma omp parallel private (tmp) do_many_thin
gs() pragma omp master exchange_boundaries()
pragma barrier do_many_other_things()
88
single
  • Single??????????????????????(???????).
  • ?single???????barrier????.

pragma omp parallel pragma omp
single printf("Beginning work1.\n") work1()
pragma omp single printf("Finishing
work1.\n") pragma omp single
nowait printf("Finished work1 and beginning
work2.\n") work2()
????single????
??
??
???
89
flush
  • pragma omp flush (list) new-line
  • !OMP FLUSH (list)
  • Flush??????????.??????????,??????????????????????.
  • ?????????????????,????????.

90
Flush(Fortran)
  • ??flush
  • BARRIER
  • CRITICAL and END CRITICAL
  • END DO
  • END SECTIONS
  • END SINGLE
  • END WORKSHARE
  • ORDERED and END ORDERED
  • PARALLEL and END PARALLEL
  • PARALLEL DO and END PARALLEL DO
  • PARALLEL SECTIONS and END PARALLEL SECTIONS
  • PARALLEL WORKSHARE and END PARALLEL WORKSHARE
  • ???????flush
  • DO
  • MASTER and END MASTER
  • SECTIONS
  • SINGLE
  • WORKSHARE

91
Flush(C)
  • ??flush
  • barrier
  • Critical?????
  • Ordered?????
  • Parallel???
  • For???
  • Sections???
  • Single???
  • ???flush
  • no wait

92
????
  • ??OpenMP???????Barrier
  • end parallel
  • end do (?nowait??)
  • end sections (?nowait??)
  • end critical
  • end single (?nowait??)

93
Nesting???????
  • ??parallel?????????????parallel?.????????????,????
    ??????????????,??,?????????????.
  • ??parallel??for, sections, ? single?????????.
  • ??critical?????????.
  • for, sections, ? single ?????critical, ordered, ?
    master ??????.
  • barrier ??????? for, ordered, sections, single,
    master, ? critical ??????.
  • master ????? for, sections, ? single ??????.
  • ordered ????? critical??????.

94
OpenMP??????
  • OpenMP?????????
  • ???Parallel Regions
  • ????Worksharing
  • ????Data Environment
  • ??Synchronization
  • ?????/????

95
OpenMP??
  • ???
  • omp_init_lock(), omp_set_lock(),
    omp_unset_lock(), omp_test_lock(),
    omp_destroy_lock()
  • ???????
  • ????????
  • omp_set_num_threads(), omp_get_num_threads(),
    omp_get_thread_num(), omp_get_max_threads()
  • ?/????????
  • omp_set_nested(), omp_set_dynamic(),
    omp_get_nested(), omp_get_dynamic()
  • ????????????
  • omp_in_parallel()
  • ??????????
  • omp_get_num_procs()

96
?????
  • ??????????????
  • ?????omp_lock_t,????????????? omp_init_lock().
  • ????????????????????,???????,????????.
  • ?????,??????? omp_set_lock(),?????????omp_unset_l
    ock().
  • ??????????,????????omp_test_lock()??????,?????????
    ?.
  • ???????,?omp_destroy_lock()??.

97
  • include ltomp.hgt
  • int main()
  • omp_lock_t lck
  • int id
  • omp_init_lock(lck)
  • pragma omp parallel shared(lck) private(id)
  • id omp_get_thread_num()
  • omp_set_lock(lck)
  • printf("My thread id is d.\n", id)
  • omp_unset_lock(lck)
  • while (! omp_test_lock(lck))
  • skip(id) / we do not yet have the lock, so we
    must do something else /
  • work(id) / we now have the lock and can do
    the work /
  • omp_unset_lock(lck)
  • omp_destroy_lock(lck)

98
???????
omp_lock_t lck omp_init_lock(lck) pragma omp
parallel private (tmp) id omp_get_thread_num(
) tmp do_lots_of_work(id) omp_set_lock(lck)
printf(d d, id, tmp) omp_unset_lock(lck)

99
  • ??????,?????????(???????????????).

include ltomp.hgt void main() omp_set_dynamic(0
) omp_set_num_threads(4) pragma omp
parallel int idomp_get_thread_num() do_l
ots_of_stuff(id)
100
?????
  • omp_set_dynamic(0)
  • omp_set_num_threads(16)
  • pragma omp parallel shared(x, npoints)
    private(iam, ipoints)
  • if (omp_get_num_threads() ! 16) abort()
  • iam omp_get_thread_num()
  • ipoints npoints/16
  • do_by_16(x, iam, ipoints)

101
?????
  • int numthreads
  • numthreads omp_get_num_threads()
  • printf(d\n,numthreads)
  • pragma omp parallel private(i)
  • numthreads omp_get_num_threads()
  • printf(d\n,numthreads)
  • ??
  • 1
  • 4
  • 4
  • 4
  • 4

102
????
  • ????????
  • OMP_SCHEDULE SCHEDULE, CHUNK_SIZE
  • ???????
  • OMP_NUM_THREADS INT_LITERAL
  • ????????????????(???????)?
  • OMP_DYNAMIC TRUE FALSE
  • ???? (??)
  • ????????????,?????????????.
  • ??,???????omp_set_num_threads()??????,????????????
    ?????????.
  • ????
  • ???????????????.
  • ????????????????????????(????????????,?I/O???)
  • OMP_NESTED TRUE FALSE(???FALSE)

103
??
  • ???
  • parallel
  • ????
  • for
  • sections
  • Single
  • parallel for
  • parallel sections
  • ??
  • master
  • critical
  • barrier
  • atomic
  • flush
  • ordered
  • ???
  • parallel
  • ????
  • DO
  • SECTIONS
  • SINGLE
  • WORKSHARE
  • PARALLEL DO
  • PARALLEL SECTIONS
  • PARALLEL WORKSHARE
  • ??
  • MASTER
  • CRITICAL
  • BARRIER
  • ATOMIC
  • FLUSH
  • ORDERED

104
  • ????????
  • omp_set_num_threads
  • omp_get_num_threads
  • omp_get_max_threads
  • omp_get_thread_num
  • omp_get_num_procs
  • omp_in_parallel
  • omp_set_dynamic
  • omp_get_dynamic
  • omp_set_nested
  • omp_get_nested
  • OMP_SET_NUM_THREADS
  • OMP_GET_NUM_THREADS
  • OMP_GET_MAX_THREADS
  • OMP_GET_THREAD_NUM
  • OMP_GET_NUM_PROCS
  • OMP_IN_PARALLEL
  • OMP_SET_DYNAMIC
  • OMP_GET_DYNAMIC
  • OMP_SET_NESTED
  • OMP_GET_NESTED
  • OMP_GET_WTIME
  • OMP_GET_WTICK

105
  • ?????
  • omp_init_lock
  • omp_init_nest_lock
  • omp_destroy_lock
  • omp_destroy_nest_lock
  • omp_set_lock
  • omp_set_nest_lock
  • omp_unset_lock
  • omp_unset_nest_lock
  • omp_test_lock
  • omp_test_nest_lock

OMP_INIT_LOCK OMP_INIT_NEST_LOCK OMP_DESTROY_LOCK
OMP_DESTROY_NEST_LOCK OMP_SET_LOCK OMP_SET_NEST_LO
CK OMP_UNSET_LOCK OMP_UNSET_NEST_LOCK OMP_TEST_LO
CK OMP_TEST_NEST_LOCK
106
????(C?Fortran??) OMP_SCHEDULE OMP_NUM_THREADS
OMP_DYNAMIC OMP_NESTED
107
OpenMP????
  • ??C,C?Fortran
  • ????????????????,???????????????
  • ???????????????
  • ??????????????????,??????????????.
  • Orphan???????,??OpenMP???????????????????????

108
??!
About PowerShow.com