Date post: | 05-Apr-2018 |
Category: |
Documents |
Upload: | sadhanamca1 |
View: | 216 times |
Download: | 0 times |
of 61
7/31/2019 Openmp Berkeley Chapman Slides 0
1/61
! !
!"#$%&'"()"*+',-.
"!#$%&'!()!*+',-.
./'0',1'&!234
5/6!27/87/7!9:7+;7,
9/'71'&!234
2/'11!
7/31/2019 Openmp Berkeley Chapman Slides 0
2/61
! !
!"#$%&'"()"*+',-.
",!'!B:7/'&!-';)/3!B301';!E!97C:'!=%'/7/C:3
FG FG-7%,!-';)/3
C+$!H
C+$!I C+$!J
C+$!G
FI
FI
FI
FI
7/31/2019 Openmp Berkeley Chapman Slides 0
3/61
! !
!"#$%&'"()"*+',-.
-7%,!-';)/3
cpu 1
cpu 2
cpu 3
cpu 0
cpu 0
*+',-.K0!!L!/)01"2"3)%,
M
1J
1G
1I
1H
1J
1G
1I
Do stuff 1H
Do stuff
Do stuff
Do stuff
N 1H1H
process 0
(:/'7&!H!%0!),!9.?!HO!71!1:'!A)/PQ!J!,'R!1:/'7&0!7/'!C/'71'&7,&!7/'!&%01/%8$1'&!1)!1:'!/';7%,%,S!J!9.?06
7/31/2019 Openmp Berkeley Chapman Slides 0
4/61
! !
!"#$%&'"()"*+',-.
T:71!%0!*+',-.U
! ",!%,&$01/3!017,&7/&!A)/!0:7/'&!;';)/3!+7/7>>'>!+/)S/7;;%,S
" *+',-.!"/C:%1'C1$/'!V'@%'R!2)7/&
" "-5Q!W,1'>Q!W2-Q!=.Q!-%C/)0)A1Q!B$,X*/7C>'Q!M$Y%10$Q!Z
7/31/2019 Openmp Berkeley Chapman Slides 0
5/61
! !
!"#$%&'"()"*+',-.
(:'!",71);3!)A!7,!*+',-.!./)S/7;
#include #include
int main (int argc, char *argv[]) {int tid, numt;numt = omp_get_num_threads();#pragma omp parallel private(tid) shared(numt){
tid = omp_get_thread_num();printf("hi, from %d\n", tid);
#pragma omp barrierif ( tid == 0 ) {printf("%d threads say hi!\n",numt);
}} return 0;}
parallel!]A)/P^&%/'C1%@'
/$,1%;'!A$,C1%),
01/$C1$/'&!parallel!!8>)CP!
&%/'C1%@'!]1:/'7&!barrier^
C>7$0'0
7/31/2019 Openmp Berkeley Chapman Slides 0
6/61
! !
!"#$%&'"()"*+',-.
*+',-.!%0!V'7>>3!7!B17,&7/&!B+'C%_C71%),
(:'!1%;'>%,'!)A!1:'!*+',-.!B17,&7/&!B+'C%_C71%),
7/31/2019 Openmp Berkeley Chapman Slides 0
7/61
! !
!"#$%&'"()"*+',-.
B);'!2','_10!)A!$0%,S!*+',-.
! W1K0!!"#$%&'(Q!0$++)/1'&!83!;)01!9X9\\!E!M)/1/7,!C);+%>'/0
! *A1',Q!;$C:!)A!0'`$',1%7>!C)&'!C7,!8'!>'A1!$,1)$C:'&!
! (:'!&'@'>)+;',1!C3C>'!%0!7!A/%',&>3!),'
"
97,!8'!%,1/)&$C'&!)$(#%$)*('+!%,1)!'D%01%,S!C)&'" 9)//'C1,'00!C7,!8'!@'/%_'&!7>),S!1:'!R73
" F%P'R%0'Q!+'/A)/;7,C'!8','_10!C7,!8'!S7$S'&
! *+1%;%a%,S!;';)/3!7CC'00!%,!1:'!0'/%7>!+/)S/7;!R%>>!8','_1!
1:'!1:/'7&'&!@'/0%),!]'6S6Q!A7>0'!0:7/%,SQ!'1C^! W1!C7,!8'!A$,!1)!$0'!]%;;'&%71'!S/71%_C71%),^
7/31/2019 Openmp Berkeley Chapman Slides 0
8/61
! !
!"#$%&'"()"*+',-.
T:71!5)'0!*+',-.!./)@%&'U
! ",!7801/7C1%),!78)@'!>)R!>'@'>!1:/'7&!>%8/7/%'0
! 5%/'C1%@'0Q!:%&&',!%,0%&'!)A!01/$C1$/'&!C);;',10
! "!0$,(%5'!>%8/7/3!1:71!;7,7S'0!'D'C$1%),!&3,7;%C7>>3
!
"&&%1%),7>!C),1/)>!@%7!',@%/),;',1!@7/%78>'0!E!7!0$,(%5'!".W
! %13O
7/31/2019 Openmp Berkeley Chapman Slides 0
9/61
! !
!"#$%&'"()"*+',-.
T:71!9);+%>'/0!B$++)/1!*+',-.U
!"#$%& '(#)*()"+ ,*--%&."$/,-"01210(.1%#
!"# $%$&&'()*(+,-./0/12'(3*(+ -45563*)67488./0
942%:/1;5< $%$&&,-./0/12'(=*(+ -45563*)67488./0
!20
7/31/2019 Openmp Berkeley Chapman Slides 0
10/61
! !
!"#$%&'"()"*+',-.
9);+%>%,S!7,&!
7/31/2019 Openmp Berkeley Chapman Slides 0
11/61
! !
!"#$%&'"()"*+',-.
*+',-.!5%/'C1%@'0!E!9),01/$C10
! 9),17%,'&!%,0%&'!9(0$4($0'&"4)55',(9
9X9\\4!!!
#pragma omp
M)/1/7,4!
!$OMP
! *+',-.!C);+>%7,1!C);+%>'/0!_,&!7,&!+7/0'!&%/'C1%@'0
! Z),[C);+>%7,1!9:)$;&!07A'>3!%S,)/'!1:';!70!C);;',10
! "!4),9(0$4(!%0!7!&%/'C1%@'!1:71!7e'C10!1:'!',C>)0%,S!C)&'
! W;+'/71%@'!]017,&7>),'^!&%/'C1%@'0!'D%01
! 7$0'0!:70!,)!8'7/%,S!),!'e'C1
7/31/2019 Openmp Berkeley Chapman Slides 0
12/61
! !
!"#$%&'"()"*+',-.
B$;;7/3!)A!*+',-.K0!5%/'C1%@'0
! M)/P%,S!(:/'7&0
parallel
! 5%01/%8$1%,S!T)/P
for]9X9\\^!
DO!]M)/1/7,^
sections/section
WORKSHARE]M)/1/7,^
! B%,S>%,S!*$1!(:/'7&0
singleMaster
! -$1$7>!$0%),
" critical
"
atomic
! B3,C:/),%a71%),
barrier
flush
ordered
taskwait! "03,C:/),)$0!(70P%,S
task
! 5717!
7/31/2019 Openmp Berkeley Chapman Slides 0
13/61
! !
!"#$%&'"()"*+',-.
?0'A$>!*+',-.!
7/31/2019 Openmp Berkeley Chapman Slides 0
14/61
! !
!"#$%&'"()"*+',-.
J!(3+'0!)A!V$,1%;'!M$,C1%),0
'&"5$($8;"'?4;$9%),^O!'6S6Q
L omp_{init,set,test,unset,destroy}_lock
" omp_{...}_nest_lock
(%;%,S!/)$1%,'0O!'6S6QL omp_get_wtime
L omp_get_wtick
* 9 %> &U * & %
7/31/2019 Openmp Berkeley Chapman Slides 0
15/61
! !
!"#$%&'"()"*+',-.
=)R!W0!7,!*+',-.!./)S/7;!9);+%>'&U!='/'K0!=)R!*+',?=!&)'0!%16
@%8)A"'(B"8;B4!:11+4XXRRRG6C06$:6'&$XgC)++'/X)+',$:6+&A
.7/0'!%,1)!f'/3!=%S:
!T=WVF
.$1!!*+',-.!C),01/$C10!%,1)!017,&7/&!A)/;71!
=%S:!>'@'>Q!0'/%7>!+/)S/7;)+1%;%a71%),0
F))+!>'@'>!0'/%7>!+/)S/7;)+1%;%a71%),0
(/7,0A)/;!*+',-.!%,1)1:/'7&'&!C)&'
*+1%;%a'Q!,)RQ!+)1',1%7>>3Q1:/'7&'&!C)&'
*$1+$1!,71%@'!W"hi!%,01/6)/!%,1'/A7C'!R%1:!,71%@'C);+%>'/!R%1:!0)$/C'!1)0)$/C'
T: 1 5 1: ( A & 9 & F P >%P U
http://www2.cs.uh.edu/~copper/openuh.pdfhttp://www2.cs.uh.edu/~copper/openuh.pdf7/31/2019 Openmp Berkeley Chapman Slides 0
16/61
! !
!"#$%&'"()"*+',-.
T:71!5)'0!1:'!(/7,0A)/;'&!9)&'!F))P!>%P'U
! W,1'/;'&%71'!C)&'QjTG9k![!T=WVF!1)!9
" uhcc -mp -gnu3 -CLIST:emit_nested_pu simple.c
" :11+4XXRRRG6C06$:6'&$Xg'01/78&X*+',-.X0%;+>'X!
static void __omprg_main_1(__ompv_gtid_a, __ompv_slink_a)_INT32 __ompv_gtid_a;_UINT64 __ompv_slink_a;
{register _INT32 _w2c___comma;_UINT64 _temp___slink_sym0;_INT32 __ompv_temp_gtid;_INT32 __mplocal_my_id;
/*Begin_of_nested_Program_Unit(s)*/
_temp___slink_sym0 = __ompv_slink_a;__ompv_temp_gtid = __ompv_gtid_a;_w2c___comma = omp_get_thread_num();__mplocal_my_id = _w2c___comma;
printf("hello from %d\n", __mplocal_my_id);return;} /* __omprg_main_1 */
#include int main(int argc, char *argv[]) {
int my_id;#pragma omp parallel default(none) private(my_id){
my_id = omp_get_thread_num();printf("hello from %d\n",my_id);
}return 0;
}
(:'!)/%S%,7>!main()
+7/7>>'>!/'S%),!%,!main!%0!)$1>%,'&!1)!__omprg_main_1()
" * -. jC (% D @%E FCG@H
http://www2.cs.uh.edu/~estrabd/OpenMP/simple/http://www2.cs.uh.edu/~estrabd/OpenMP/simple/7/31/2019 Openmp Berkeley Chapman Slides 0
17/61
! !
!"#$%&'"()"*+',-.
",!*+',-.!jC$,(%5'D"@%E0806"FCG@H
! (:'!j0$,(%5'D"!;7,7S'0!1:'!;$>1%[1:/'7&'&!'D'C$1%),4
" W1K0!$0'&!83!1:'!/'0$>1%,S!'D'C$178>'!*+',-.!+/)S/7;
" W1K0!R:71!0+7R,0!1:/'7&0!]'6S6Q!C7>>0!+1:/'7&0^!
" W1K0!R:71!;7,7S'0!0:7/'&!E!+/%@71'!;';)/3
" W1K0!R:71!&%01/%8$1'0!]0:7/'0^!R)/P!7;),S!1:/'7&0
" W1K0!R:71!03,C:/),%a'0!1:/'7&0!E!170P0
" W1K0!R:71!/'&$C'0!@7/%78>'0!7,&!P''+0!lastprivate
" W1K0!R:71!%0!%,l$',C'&!83!',@7/0!E!1:'!$0'/!>'@'>!".W
! 5)D3S',!&)C0!)A!*+',?=K0!*+',-.!V(FQ!libopenmp
" :11+4XXRRRG6C06$:6'&$Xg'01/78&X*+',?=X/mdJX:1;>[>%8)+',;+X
! (:'!5)D3S',!C7>>!S/7+:!A)/!cc);+cA)/P!%,!>%8)+',;+X1:/'7&06C
" __omp_fork(...) call graph
(:' ,'R i ()
http://www2.cs.uh.edu/~estrabd/OpenUH/r593/html-libopenmp/http://www2.cs.uh.edu/~estrabd/OpenUH/r593/html-libopenmp/dc/d87/a00032_acb1e4cd1e1a77010203f0ae270c3ace3_cgraph.pnghttp://www2.cs.uh.edu/~estrabd/OpenUH/r593/html-libopenmp/http://www2.cs.uh.edu/~estrabd/OpenUH/r593/html-libopenmp/dc/d87/a00032_acb1e4cd1e1a77010203f0ae270c3ace3_cgraph.png7/31/2019 Openmp Berkeley Chapman Slides 0
18/61
! !
!"#$%&'"()"*+',-.
(:'!,'R!main()
extern _INT32 main() {register _INT32 _w2c___ompv_ok_to_fork;register _UINT64 _w2c_reg3;register _INT32 _w2c___comma;
_INT32 my_id;_INT32 __ompv_gtid_s1;/*Begin_of_nested_PU(s)*/
_w2c___ompv_ok_to_fork = 1;if(_w2c___ompv_ok_to_fork){
_w2c___ompv_ok_to_fork = __ompc_can_fork();}if(_w2c___ompv_ok_to_fork){
__ompc_fork(0, &__omprg_main_1, _w2c_reg3);}else{
__ompv_gtid_s1 = __ompc_get_local_thread_num();__ompc_serialized_parallel();_w2c___comma = omp_get_thread_num();
my_id = _w2c___comma;printf("hello from %d\n", my_id);__ompc_end_serialized_parallel();
}return 0;
} /* main */
C7>>0!V(F!A)/P!7,&!+700'0A$,C1%),!+)%,1'/!1)!)$1>%,'&!main()
0'/%7>!@'/0%),
__omprg_main_1K0!A/7;'!+)%,1'/
Z)8)&3!R7,10!1)!C)&'!>%P'!1:%0Q!0)!>'1!1:'!C);+%>'/!7,&!/$,1%;'!&)!;)01!7>>!1:%0!1'&%)$0!R)/Pn
7/31/2019 Openmp Berkeley Chapman Slides 0
19/61
! !
!"#$%&'"()"*+',-.
!
./)S/7;;%,S!R%1:!*+',-.!J6H
(:' parallel 9),01/$C1
7/31/2019 Openmp Berkeley Chapman Slides 0
20/61
! !
!"#$%&'"()"*+',-.
(:'!parallel!9),01/$C1
! T:'/'!1:'!jA)/Pk!)CC$/0!]'6S6Q__ompc_fork(...)^
! )0'0!7>>!)1:'/!*+',-.!C),01/$C10!E!&%/'C1%@'0
! (:%0!C),01/$C1!7CC'+10!1:'!A)>>)R%,S!C>7$0'04!if,num_threads, private, firstprivate, shared,default, copyin, reduction
! 97,!C7>>!A$,C1%),0!1:71!C),17%,!j)/+:7,k!C),01/$C10
" B171%C7>>3!)$10%&'!)A!+7/7>>'>Q!8$1!&3,7;%C7>>3!%,0%&'!&$/%,S!/$,1%;'
! 97,!8'!,'01'&
" B%;+>' *+',-. '
7/31/2019 Openmp Berkeley Chapman Slides 0
21/61
! !
!"#$%&'"()"*+',-.
"!B%;+>'!*+',-.!'
#include #include
int main (int argc, char *argv[]) {int tid, numt;numt = omp_get_num_threads();#pragma omp parallel private(tid) shared(numt){
tid = omp_get_thread_num();printf("hi, from %d\n", tid);
#pragma omp barrier
if ( tid == 0 ) {printf("%d threads say hi!\n",numt);}
} return 0;}
9X9\\
*$1+$1!$0%,S!i!(:0'8&94
hi, from 3hi, from 0hi, from 2hi, from 14 threads say hi!
Z)1'Q!1:/'7&!)/&'/,)1!S$7/7,1''&n
S'1!,$;8'/!)A!1:/'7&0
A)/P
R7%1!A)/!7>>!1:/'7&0
S'1!1:/'7&!%&
Y)%,!]%;+>%C%1!87//%'/Q!7>>!R7%1^
(:' M)/1/7, f'/0%),
7/31/2019 Openmp Berkeley Chapman Slides 0
22/61
! !
!"#$%&'"()"*+',-.
(:'!M)/1/7,!f'/0%),
#include #include
int main (int argc, char *argv[]) {int tid, numt;numt = omp_get_num_threads();#pragma omp parallel private(tid) shared(numt){
tid = omp_get_thread_num();printf("hi, from %d\n", tid);
#pragma omp barrier
if ( tid == 0 ) {printf("%d threads say hi!\n",numt);}
} return 0;}
program hello90use omp_libinteger:: tid, numtnumt = omp_get_num_threads()
!$omp parallel private(id) shared(numt)tid = omp_get_thread_num()
write (*,*) 'hi, from', tid!$omp barrier if ( tid == 0 ) then
write (*,*) numt,'threads say hi!'end if
!$omp end parallelend program
9X9\\ MdH
*$1+$1!$0%,S!i!(:0'8&94
hi, from 3hi, from 0hi, from 2hi, from 14 threads say hi!
Z)1'Q!1:/'7&!)/&'/,)1!S$7/7,1''&n
Z)R N$01 1:' .7/7>>'>%a'& 9)&'
7/31/2019 Openmp Berkeley Chapman Slides 0
23/61
! !
!"#$%&'"()"*+',-.
Z)RQ!N$01!1:'!.7/7>>'>%a'&!9)&'
#include #include
int main (int argc, char *argv[]) {int tid, numt;numt = omp_get_num_threads();#pragma omp parallel private(tid) shared(numt){
tid = omp_get_thread_num();printf("hi, from %d\n", tid);
#pragma omp barrier
if ( tid == 0 ) {printf("%d threads say hi!\n",numt);}
}return 0;
}}
program hello90use omp_libinteger:: tid, numtnumt = omp_get_num_threads()
!$omp parallel private(id) shared(numt)tid = omp_get_thread_num()
write (*,*) 'hi, from', tid!$omp barrier if ( tid == 0 ) then
write (*,*) numt,'threads say hi!'end if
!$omp end parallelend program
9X9\\ MdH
*$1+$1!$0%,S!i!(:0'8&94
hi, from 3hi, from 0hi, from 2hi, from 14 threads say hi!
Z)1'Q!1:/'7&!)/&'/,)1!S$7/7,1''&n
(/7C' )A (:'
7/31/2019 Openmp Berkeley Chapman Slides 0
24/61
! !
!"#$%&'"()"*+',-.
(/7C'!)A!(:'!>!1:/'7&0!C7>>!printf
1:/'7&!87//%'/
only thread withtid == 0 does this
)1:'/!1:/'7&0!R7%1
Y)%,
9),1/)>>%,S!1:'!M)/P!71!V$,1%;'
7/31/2019 Openmp Berkeley Chapman Slides 0
25/61
! !
!"#$%&'"()"*+',-.
9) ) S ' ) 7 $ '
! (:'!jifk!C>7$0'!C),17%,0!7!C),&%1%),7>!'D+/'00%),6
! WA!(V?7$0'!%0!7,)1:'/!R73!1)!C),1/)>!1:'!,$;8'/!)A!1:/'7&0!7C1%@'!%,!7!parallel!C),1/$C1
int n = some_func();#pragma omp parallel if(n>5){
do stuff in parallel}
int n = some_func();#pragma omp parallel num_threads(n){ do stuff in parallel
}
(:'!5717!
7/31/2019 Openmp Berkeley Chapman Slides 0
26/61
! !
!"#$%&'"()"*+',-.
S
! default([shared]|none|private)! shared(list,)![!0$++)/1'&!83!parallel!C),01/$C1!),>3
! private(list,)
! firstprivate(list,)
! lastprivate(list,)![!0$++)/1'&!83!>))+!E!sections!C),01/$C10!),>3
! reduction(:list,)
! copyprivate(list,)![!0$++)/1'&!83!single!C),01/$C1!),>3
! threadprivate!L!7!017,&7>),'!&%/'C1%@'Q!,"$!7!C>7$0'
#pragma omp threadprivate(list,)
!$omp threadprivate(list,)
! copyin(list,)![!0$++)/1'&!83!parallel!C),01/$C1!),>3
private!E!shared!%,!1:71!B%;+>'!*+',-.!'
7/31/2019 Openmp Berkeley Chapman Slides 0
27/61
! !
!"#$%&'"()"*+',-.
p + + +
#include #include
int main (int argc, char *argv[]) {int tid, numt;numt = omp_get_num_threads();#pragma omp parallel private(tid) shared(numt){
tid = omp_get_thread_num();printf("hi, from %d\n", tid);
#pragma omp barrier
if ( tid == 0 ) {printf("%d threads say hi!\n",numt);}
} return 0;}
program hello90use omp_libinteger:: tid, numtnumt = omp_get_num_threads()
!$omp parallel private(id) shared(numt)tid = omp_get_thread_num()
write (*,*) 'hi, from', tid!$omp barrier if ( tid == 0 ) then
write (*,*) numt,'threads say hi!'end if
!$omp end parallelend program
9X9\\ MdH
*$1+$1!$0%,S!i!(:0'8&94
hi, from 3
hi, from 0hi, from 2hi, from 14 threads say hi!
Z)1'Q!1:/'7&!)/&'/,)1!S$7/7,1''&n
-';)/3!9),0%01',C3!-)&'>
7/31/2019 Openmp Berkeley Chapman Slides 0
28/61
! !
!"#$%&'"()"*+',-.
! *+',-.!$0'0!7!j/'>7D'&!C),0%01',C3k!;)&'>
" (:/'7&0!;73!1';+)/7/%>3!j0''k!&%e'/',1!@7>$'0!A)/!1:'!07;'!0:7/'&!@7/%78>'!
" 9)/'0!;73!:7@'!)$1!)A!&71'!@7>$'0!%,!1:'%/!C7C:'
! -)01!C),01/$C10!%;+>3!7!jflushk!)A!'7C:!1:/'7&K0!C7C:'
! (/'71'&!70!7!;';)/3!jA',C'k!83!C);+%>'/0!R:',!%1!C);'0!1)!/')/&'/%,S!)+'/71%),0
! *+',-.!+/)@%&'0!7,!'D+>%C%1!l$0:!&%/'C1%@'
#pragma flush (list,)
!$OMP FLUSH(list,)
-$>1%+>'!(:/'7&0!-73!=7@'!9)+%'0!)A!B:7/'&!f7/%78>'0!%,!97C:'!
7/31/2019 Openmp Berkeley Chapman Slides 0
29/61
! !
!"#$%&'"()"*+',-.
process 3
-7%,!-';)/3
cpu 1
cpu 2
cpu 3
cpu 0 Thread 0
Thread 1
Thread 2
Thread 3
p0
",!'D+>%C%1!barrier!%,!1:71!B%;+>'!*+',-.!'
7/31/2019 Openmp Berkeley Chapman Slides 0
30/61
! !
!"#$%&'"()"*+',-.
#include #include
int main (int argc, char *argv[]) {int tid, numt;numt = omp_get_num_threads();#pragma omp parallel private(tid) shared(numt){
tid = omp_get_thread_num();printf("hi, from %d\n", tid);
#pragma omp barrier
if ( tid == 0 ) {printf("%d threads say hi!\n",numt);}
} return 0;}
program hello90use omp_libinteger:: tid, numtnumt = omp_get_num_threads()
!$omp parallel private(id) shared(numt)tid = omp_get_thread_num()
write (*,*) 'hi, from', tid!$omp barrier if ( tid == 0 ) then
write (*,*) numt,'threads say hi!'end if
!$omp end parallelend program
9X9\\ MdH
*$1+$1!$0%,S!i!(:0'8&94
hi, from 3
hi, from 0hi, from 2hi, from 14 threads say hi!
(/7C'!)A!(:'!
7/31/2019 Openmp Berkeley Chapman Slides 0
31/61
! !
!"#$%&'"()"*+',-.
0 B 0hi, from 0 0 == 04 threads say hi!
1 B 1hi, from 1 1 != 0
2 B 2hi, from 2 2 !=0
3 B 3hi, from 3 3 != 0
M N
B!o!R7%1!A)/!7>>!1:/'7&0!p!barrier!8'A)/'!+/)S/'00%,S!A$/1:'/6#pragma omp barrier
"!reduction!'!
7/31/2019 Openmp Berkeley Chapman Slides 0
32/61
! !
!"#$%&'"()"*+',-.
#include #include
int main (int argc, char *argv[]) {int t, i;i = 0;#pragma omp parallel private(t) reduction(+,i){
t = omp_get_thread_num();i = t + 1;printf("hi, from %d\n", t);
#pragma omp barrierif ( t == 0 ) {int numt = omp_get_num_threads();printf("%d threads say hi!\n",numt);
}}printf(i is reduced to %d\n,i);
return 0;}
program hello90use omp_libinteger:: t, i, numti = 0
!$omp parallel private(t) reduction(+:i)t = omp_get_thread_num()i = t + 1;
write (*,*) 'hi, from', t!$omp barrier if ( t == 0 ) then
numt = omp_get_num_threads()
write (*,*) numt,'threads say hi!'end if!$omp end parallel
write (*,*) 'i is reduced to ', iend program
9X9\\ MdH
*$1+$1!$0%,S!i!(:0'8&94
hi, from 3hi, from 0hi, from 2hi, from 14 threads say hi!i is reduced to 10
(/7C'!)A!7!@7/%78>'!/'&$C1%),
7/31/2019 Openmp Berkeley Chapman Slides 0
33/61
! !
!"#$%&'"()"*+',-.
0 i0 = 0 + 1
1 i1 = 1 + 1
2 i2 = 2 + 1
3 i3 = 3 + 1
M
i = 0
N
i = i+i0+i
1+i
2+i
3...
i = 1+2+3+4...
i = 10i = 10
7!0:7/'&!@7/%78>'!%0!%,%1%7>%a'&
'7C:!1:/'7&!R/%1'0!1)!%10
+/%@71'!@7/%78>'
/'&$C1%),!)CC$/071!',&!)A!1:'!C),01/$C1
_,7>!@7>$'!)A!i!%07@7%>78>'!7A1'/!',&)A!C),01/$C1
f7>%&!*+'/71%),0!A)/!1:'!reduction!9>7$0'
7/31/2019 Openmp Berkeley Chapman Slides 0
34/61
! !
!"#$%&'"()"*+',-.
! V'&$C1%),!)+'/71%),0!%,!9X9\\4
" "/%1:;'1%C4!"!#!$
" 2%1R%0'4!!!!!!!%!&!'
" F)S%C7>4!!!!!!!%%!!''
! ()*+,-./0!/1)23-./04!.0!5/2-230
" 67+.839)0-!32.-:;)-.,.4)
" min
! @4)2!*)A.0)*!2)*+,-./04!B@C(D!.4!30!32)3!/A!,+22)0-!2)4)32,:
! E/-)F!.0.-.39.G)*!839+)!;3--)24H!!
Z'01'&!parallel!9),01/$C10
7/31/2019 Openmp Berkeley Chapman Slides 0
35/61
! !
!"#$%&'"()"*+',-.
!
97,!8'!,'01'&Q!8$1!0+'C%_C71%),!;7P'0!%1!)+1%),7>" OMP_NESTED={true,false}
" OMP_MAX_ACTIVE_LEVELS={1,2,..}
" omp_{get,set}_nested()
" omp_get_level()
" omp_get_ancestor_thread_num(level)
!
7/31/2019 Openmp Berkeley Chapman Slides 0
36/61
! !
!"#$%&'"()"*+',-.
H
G I H
G I H G I H G I H
Z'01!F'@'>!H
Z'01!F'@'>!I
Z'01!F'@'>!G
__ompc_fork(..)
__ompc_fork(..) __ompc_fork(..) __ompc_fork(..)
2'C7$0'!)A!1:'!1/''!01/$C1$/'Q!'7C:!1:/'7&!C7,!8'!$,%`$'>3!%&',1%_'&!83!%10!A$>>!+71:!A/);!1:'!/))1!)A!%10!0$8[1/''O
(:%0!+71:!1$+>'!C7,!8'!C7>C$>71'&!%,!-F;'I';H!$0%,S!omp_get_level!7,&omp_get_ancestor_thread_num!!%,!C);8%,71%),6
qHQHQGrqHQGQGr
(:/'7&!,$;8'/0!7/'!,)1!$,%`$'O!+71:0!1)!'7C:!1:/'7&!7/'6
T)/P!B:7/%,S!9),01/$C10
7/31/2019 Openmp Berkeley Chapman Slides 0
37/61
! !
!"#$%&'"()"*+',-.
!
(:/'7&0!;$01!0:7/'!%,!1:'!R)/P!)A!+/)S/7;!! *+',-.!+/)@%&'0!jR)/P!0:7/%,Sk!C),01/$C10
! B+'C%_'0!1:'!R)/P!1)!8'!&%01/%8$1'&!7,&!:)R
!
(:'0'!C),01/$C10!%,C>$&'4" >))+0!(for, DO)
" sections
" WORKSHARE!]M)/1/7,!),>3^
" single, master
*+',-.!.7/7>>'>%a'0!F))+0!83!5%01/%8$1%,S!W1'/71%),0!1)!
7/31/2019 Openmp Berkeley Chapman Slides 0
38/61
! !
!"#$%&'"()"*+',-.
thread 1i = 34 thru 67
thread 2i = 68 thru 99
thread 0i = 0 thru 33
!"#$!%&'()*+)$,+'$-,($$-,($.!/0%!$1/$22%$!334$5$$$$66$7,$8#9--$$$:
-,($.!/0%!$1/$;;%$!334$5$$66$7,$8#9--:
-,($.!/;%$!334$5$$66$7,$8#9--:
-,($.!/=?%!$1/$22%$!334$5$$66$7,$8#9--:
.7/7>>'>%a%,S!F))+0![!9X9\\
7/31/2019 Openmp Berkeley Chapman Slides 0
39/61
! !
!"#$%&'"()"*+',-.
:11+4XX&'@'>)+'/060$,6C);X0)>7/%0X7/1%C>'0X01$&%)c)+',;+6:1;>
#include #include #define N 100
int main(void){float a[N], b[N], c[N];int i;omp_set_dynamic(0); // ensures use of all available threadsomp_set_num_threads(20); // sets number of all available threads to 20
/* Initialize arrays a and b. */for (i = 0; i < N; i++){
a[i] = i * 1.0;b[i] = i * 2.0;}
/* Compute values of array c in parallel. */
#pragma omp parallel shared(a, b, c) private(i){
#pragma omp for[nowait]for (i = 0; i < N; i++)
c[i] = a[i] + b[i];}
printf ("%f\n", c[10]);}
.7/7>>'>%a%,S!F))+0![!9X9\\
7/31/2019 Openmp Berkeley Chapman Slides 0
40/61
! !
!"#$%&'"()"*+',-.
:11+4XX&'@'>)+'/060$,6C);X0)>7/%0X7/1%C>'0X01$&%)c)+',;+6:1;>
{a[i] = i * 1.0;b[i] = i * 2.0;
}
/* Compute values o#include #include #define N 100
int main(void){float a[N], b[N], c[N];int i;omp_set_dynamic(0); // ensures use of all available threads
omp_set_num_threads(20); // sets number of all available threads to 20/* Initialize arrays a and b. */for (i = 0; i < N; i++)f array c in parallel. */
#pragma omp parallel shared(a, b, c) private(i){
#pragma omp for[nowait]for (i = 0; i < N; i++)
c[i] = a[i] + b[i];}printf ("%f\n", c[10]);
}
jnowaitk!%0!)+1%),7>
! # %& * -.
.7/7>>'>%a%,S!F))+0![!M)/1/7,
7/31/2019 Openmp Berkeley Chapman Slides 0
41/61
! !
!"#$%&'"()"*+',-.
:11+4XX&'@'>)+'/060$,6C);X0)>7/%0X7/1%C>'0X01$&%)c)+',;+6:1;>
PROGRAM VECTOR_ADDUSE OMP_LIBPARAMETER (N=100)INTEGER N, I
REAL A(N), B(N), C(N)CALL MP_SET_DYNAMIC (.FALSE.) !ensures use of all available threadsCALL OMP_SET_NUM_THREADS (20) !sets number of available threads to 20
! Initialize arrays A and B.DO I = 1, NA(I) = I * 1.0B(I) = I * 2.0
ENDDO! Compute values of array C in parallel.
!$OMP PARALLEL SHARED(A, B, C), PRIVATE(I)!$OMP DODO I = 1, NC(I) = A(I) + B(I)
ENDDO!$OMP END DO[nowait]
! ... some more instructions!$OMP END PARALLEL
PRINT *, C(10)
END
! # %&' () *+',-.
.7/7>>'>%a%,S!F))+0![!M)/1/7,
7/31/2019 Openmp Berkeley Chapman Slides 0
42/61
! !
!"#$%&'"()"*+',-.
:11+4XX&'@'>)+'/060$,6C);X0)>7/%0X7/1%C>'0X01$&%)c)+',;+6:1;>
PROGRAM VECTOR_ADDUSE OMP_LIBPARAMETER (N=100)INTEGER N, I
REAL A(N), B(N), C(N)CALL MP_SET_DYNAMIC (.FALSE.) !ensures use of all available threadsCALL OMP_SET_NUM_THREADS (20) !sets number of available threads to 20
! Initialize arrays A and B.DO I = 1, NA(I) = I * 1.0B(I) = I * 2.0
ENDDO! Compute values of array C in parallel.
!$OMP PARALLEL SHARED(A, B, C), PRIVATE(I)!$OMP DODO I = 1, NC(I) = A(I) + B(I)
ENDDO!$OMP END DO[nowait]
! ... some more instructions!$OMP END PARALLEL
PRINT *, C(10)
END
jnowaitk!%0!)+1%),7>!E!7&&'&1)!1:'!C>)0%,S!&%/'C1%@'
! #$%&' () *+',-.
.7/7>>'>!F))+!BC:'&$>%,S
7/31/2019 Openmp Berkeley Chapman Slides 0
43/61
! !
!"#$%&'"()"*+',-.
! BC:'&$>%,S!/'A'/0!1)!:)R!%1'/71%),0!7/'!700%S,'&!1)!7!+7/1%C$>7/!1:/'7&O
! (:'/'!7/'!m!13+'04
" 9(8(%4!L!'7C:!1:/'7&!%0!78>'!1)!C7>C$>71'!%10!C:$,P!
" &6,85%4!L!_/01!C);'Q!_/01!0'/@'!;7,7S'&!83!/$,1%;'" =$%&'&!L!&'C/'70%,S!C:$,P!0%a'0Q!%,C/'70%,S!R)/P
" 8$()!L!&'1'/;%,'&!7$1);71%C7>>3!83!C);+%>'/!)/!/$,1%;'
" 0$,(%5'!L!&'_,'&!83!OMP_SCHEDULE!)/!omp_set_schedule
! F%;%171%),0" ),>3!),'!0C:'&$>'!13+'!;73!8'!$0'&!A)/!7!S%@',!>))+
" 1:'!C:$,P!0%a'!7++>%'0!1)!8;;!1:/'7&0
! #$%&' () *+',-.
.7/7>>'>!F))+!BC:'&$>%,S![!'
7/31/2019 Openmp Berkeley Chapman Slides 0
44/61
! !
!"#$%&'"()"*+',-.
!$OMP PARALLEL SHARED(A, B, C) PRIVATE(I)!$OMP DO SCHEDULE (DYNAMIC,4)
DO I = 1, NC(I) = A(I) + B(I)
ENDDO!$OMP END DO [nowait]
!$OMP END PARALLEL
#pragma omp parallel shared(a, b, c) private(i){
#pragma omp for schedule (guided,4) [nowait]
for (i = 0; i < N; i++)c[i] = a[i] + b[i];}
M)/1/7,
9X9\\
0C:'&$>' C:$,P!0%a'
! #$%&' () *+',-.
"!section!9),01/$C1!'
7/31/2019 Openmp Berkeley Chapman Slides 0
45/61
! !
!"#$%&'"()"*+',-.
#include #include
int square(int n){return n*n;
}int main(void){int x, y, z, xs, ys, zs;omp_set_dynamic(0);omp_set_num_threads(3);x = 2; y = 3; z = 5;
#pragma omp parallel shared(xs,ys,zs) firstprivate (x, y, z){#pragma omp sections
{#pragma omp section{ xs = square(x);printf ("id = %d, xs = %d\n", omp_get_thread_num(), xs);
}#pragma omp section
{ ys = square(y);printf ("id = %d, ys = %d\n", omp_get_thread_num(), ys);
}#pragma omp section
{ zs = square(z);printf ("id = %d, zs = %d\n", omp_get_thread_num(), zs);}
}}return 0;
}
:11+4XX&'@'>)+'/060$,6C);X0)>7/%0X7/1%C>'0X01$&%)c)+',;+6:1;>
!"#$%&'"()"*+',-.
"!section!9),01/$C1!'
7/31/2019 Openmp Berkeley Chapman Slides 0
46/61
! !
+
#pragma omp sections{
#pragma omp section{ xs = square(x);printf ("id = %d, xs = %d\n", omp_get_thread_num(), xs);
}#pragma omp section{ ys = square(y);printf ("id = %d, ys = %d\n", omp_get_thread_num(), ys);
}#pragma omp section
{ zs = square(z);printf ("id = %d, zs = %d\n", omp_get_thread_num(), zs);
}}
1:/'7&!H
1:/'7&!I
1:/'7&!G
(%;'
1oH 1oI
!"#$%&'"()"*+',-.
(:'!task!9),01/$C1
7/31/2019 Openmp Berkeley Chapman Slides 0
47/61
! !
+
!
(70P0!R'/'!7&&'&!%,!J6H!1)!:7,&>'!&3,7;%C!7,&!$,01/$C1$/'&!7++>%C71%),0
" V'C$/0%),
" (/''!E!S/7+:!1/7@'/07>0
! *+',-.K0!'D'C$1%),!;)&'>!870'&!),!1:/'7&0!R70!/'&'_,'&
! "!1:/'7&!%0!C),0%&'/'&!1)!8'!7,!%5+;%4%(!170P
! (:'!task!C),01/$C1!&'_,'0!0%,S$>7/!170P0!'D+>%C%1>3
! F'00!)@'/:'7&!1:7,!,'01'&!parallel!/'S%),0
!"#$%&'"()"*+',-.
7/31/2019 Openmp Berkeley Chapman Slides 0
48/61
! !
process 3
-7%,!-';)/3
cpu 1
cpu 2
cpu 3
cpu 0 Implicit task 0
Implicit task 1
Implicit task 2
Implicit task 3
p0tied (private)untied (public)
tied (private)
untied (public)
tied (private)
untied (public)
tied (private)
untied (public)
!"#$%&'"()"*+',-.
(:'!task!9),01/$C1
7/31/2019 Openmp Berkeley Chapman Slides 0
49/61
! !
!
9>7$0'0!0$++)/1'&!7/'4!if, default, private,firstprivate shared, tied/untied
! 23!&'A7$>1Q!7>>!@7/%78>'0!7/'!firstprivate
! (70P0!C7,!8'!,'01'&!03,17C1%C7>>3Q!8$1!7/'!01%>>!
703,C:/),)$0! (:'!170PR7%1!&%/'C1%@'!C7$0'0!7!170P!1)!R7%1!$,1%>!7>>!%10!
C:%>&/',!:7@'!C);+>'1'&
!"#$%&'"()"*+',-.
"!task!9),01/$C1!9X9\\!'
7/31/2019 Openmp Berkeley Chapman Slides 0
50/61
! !
struct node {struct node *left;struct node *right;
};
extern void process(struct node *);
void traverse( struct node *p ) {if (p->left)
#pragma omp task // p is firstprivate by default traverse(p->left);if (p->right)
#pragma omp task // p is firstprivate by default traverse(p->right);process(p);
}
!"#$%&'"()"*+',-.
-$1$7>!$0%),4!criticalQ!atomicQ!7,&!omp_lock_t
7/31/2019 Openmp Berkeley Chapman Slides 0
51/61
! !
!
B);'!C)&'!;$01!8'!'D'C$1'&!83!),'!1:/'7&!71!7!1%;'! 3!0'/%7>%a'0!1:'!1:/'7&0
! ">0)!C7>>'&!C/%1%C7>!0'C1%),0
! *+',-.!+/)@%&'0!J!R730!1)!7C:%'@'!;$1$7>!'DC>$0%),
" (:'!critical!C),01/$C1!',C>)0'0!7!C/%1%C7>!0'C1%),
" (:'!atomic!C),01/$C1!',C>)0'!$+&71'0!1)!0:7/'&!@7/%78>'0
" "!>)R!>'@'>Q!S','/7>!+$/+)0'!>)CP%,S!;'C:7,%0;
!"#$%&'"()"*+',-.
"!critical!9),01/$C1!'
7/31/2019 Openmp Berkeley Chapman Slides 0
52/61
! !
#pragma omp parallel shared(c) private(a, b, i){
#pragma omp critical{ for (i = 0; i < N; i++)
C[ 0] += a[i] + b[i];printf("%f\n",c[0]);
}}
1:/'7&!I 1:/'7&!H 1:/'7&!G
1!o!H 1!o!I 1!o!G
(%;'Z)1'4
7/31/2019 Openmp Berkeley Chapman Slides 0
53/61
! !
#pragma omp critical(a){// some code
}#pragma omp critical(b)
{// some code
}#pragma omp critical(c)
{// some code
}
thread 1
1!o!H
(%;'
Z)1'4C$>71%,S!s
t ti l t 100000
7/31/2019 Openmp Berkeley Chapman Slides 0
54/61
! !
static long num_steps = 100000;double step;
void main (){ int i; double x, pi, sum = 0.0;
step = 1.0/(double) num_steps;for (i=0;i
7/31/2019 Openmp Berkeley Chapman Slides 0
55/61
! !
#include static long num_steps = 100000; double step;#define NUM_THREADS 2void main (){
int i, id, nthreads; double x, pi, sum[NUM_THREADS];step = 1.0/(double) num_steps;omp_set_num_threads(NUM_THREADS);
#pragma omp parallel private (i, id, x){
id = omp_get_thread_num();#pragma omp single
nthreads = omp_get_num_threads();for (i=id, sum[id]=0.0;i< num_steps; i=i+nthreads){
x = (i+0.5)*step;sum[id] += 4.0/(1.0+x*x);}
}for(i=0, pi=0.0;i0"50$6(014/%##%+0?)''0%@(2$0
!(#5"#/%,2(
!"#$%&'"()"*+',-.
"!B>%S:1>3!-)/'!!./)S/7;![!97>C$>71%,S! s
#include
7/31/2019 Openmp Berkeley Chapman Slides 0
56/61
! !
#include static long num_steps = 100000; double step;#define NUM_THREADS 2void main (){
int i, id, nthreads; double x, pi, sum;step = 1.0/(double) num_steps;omp_set_num_threads(NUM_THREADS);
#pragma omp parallel private (i, id, x, sum) {
id = omp_get_thread_num();#pragma omp single
nthreads = omp_get_num_threads();for (i=id, sum=0.0;i< num_steps; i=i+nthreads){
x = (i+0.5)*step;sum += 4.0/(1.0+x*x);}
#pragma omp critical pi += sum * step; }}
A"0%##%+B01"0,"05%'1(016%#),>
A"$(C0$6)10/($6"30"502"/&),),>0!%#$)%'014/103"(1,8$012%'(0*(#+0?('''!E!!./)S/7;![!97>C$>71%,S!s
#include
7/31/2019 Openmp Berkeley Chapman Slides 0
57/61
! !
#include static long num_steps = 100000; double step;#define NUM_THREADS 2void 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 private(x) reduction(+:sum) for (i=0;i>'>!+/)S/7;!%0!C/'71'&!R%1:)$1!C:7,S%,S!7,3!'D%01%,S!C)&'!7,&!7&&%,S!i!0%;+>'!>%,'06
!"#$%&'"()"*+',-.
#include
(:'!M%,7>!.7/7>>'>!./)S/7;![!97>C$>71%,S!s
7/31/2019 Openmp Berkeley Chapman Slides 0
58/61
! !
#include static long num_steps = 100000; double step;#define NUM_THREADS 2void 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 private(x) reduction(+:sum) for (i=0;i>3!0'1!$0%,S!1:'!',@%/),;',1!@7/%78>'Q!OMP_NUM_THREADS6
)0!#)*%$(0&+03(5%4'$
="#0>""30-!(,D.0)/!'(/(,$%$)",1B0reduction0)10/"#(012%'%&'(0$6%,0%0
critical02",1$#42$
!"#$%&'"()"*+',-.
#','/7>!./)S/7;;%,S!(%+0
7/31/2019 Openmp Berkeley Chapman Slides 0
59/61
! !
!
-%,%;%a'!parallel!C),01/$C10! ?0'!4)5E%,'&!C),01/$C10Q!%A!%1!&)'0,K1!@%)>71'!1:'!78)@'
! -%,%;%a'!0:7/'&!@7/%78>'0Q!;7D%;%a'!+/%@71'
! -%,%;%a'!87//%'/0Q!8$1!&),K1!07C/%_C'!07A'13
! T:',!%,0'/1%,S!*+',-.!%,1)!'D%01%,S!C)&'
" ?0'!7!&%0C%+>%,'&Q!%1'/71%@'!&'@'>)+;',1!C3C>'!L!1'01!7S7%,01!0'/%7>!@'/0%),
" ?0'!87//%'/0!>%8'/7>>3
" *+1%;%a'!*+',-.!E!703,C:/),%a'!'%1$
! T:',!017/1%,S!A/);!0C/71C:
" B17/1!R%1:!7,!)+1%;%a'&!0'/%7>!@'/0%),
!"#$%&'"()"*+',-.
(:'!M$1$/'!)A!*+',-.
7/31/2019 Openmp Berkeley Chapman Slides 0
60/61
! !
!
f',&)/!8$3[%,!7,&!VE5!0$++)/1!%0!70!01/),S!70!'@'/! -$01!/';7%,!/'>'@7,1
! "C1%@'!7/'70!)A!/'0'7/C:4
" V'_,';',1!1)!170P%,S!;)&'>!]0C:'&$>%,SQ!'1C^
" ?0'/!&'_,'&!/'&$C1%),0!]?5V0^" "CC'>'/71)/0!E!:'1'/)S',')$0!',@%/),;',10
" %,S
" =38/%&!;)&'>0
!
BC7>%,S!%00$'0!8'%,S!7&&/'00'&4" (:)$07,&0!)A!1:/'7&0
" 5717!>)C7>%13
" -)/'!l'D%8>'!E!'vC%',1!03,C:/),%a71%),
!"#$%&'"()"*+',-.
"&&%1%),7>!V'0)$/C'0
7/31/2019 Openmp Berkeley Chapman Slides 0
61/61
! !
!
:11+4XXRRR6C06$:6'&$Xg:+C1))>0! :11+4XXRRR6C);+$,%136)/S
! :11+4XXRRR6)+',;+6)/S
" B+'C%_C71%),!J6H
" -)/'!/'0)$/C'0
! j?0%,S!*+',-.kQ!9:7+;7,Q!'16!7>6