OPENMP
                       A Brief Introduction to OpenMP


                                  Û[U/ Jerry He
           ...
̇SN




     1    OpenMP0



     2    OpenMPA5




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   2 /...
̇SN




     1    OpenMP0



     2    OpenMPA5




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   2 /...
0
                                 OpenMP0




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   3 / 31
0
                                 OpenMP0



    OpenMP


OpenMP´dOpenMP Architecture Review BoardVÞJÑ
  §¿® 2• É §^u •S•...
0
                                 OpenMP0



    OpenMP




dã´˜‡;. OpenMP§S «¿ã§·‚Œ±w §´dG1“
èÚ¿1“è †|¤ §¿1“è «•·‚r§ ‰/¿...
0
                                 OpenMP0



    XÛ3“襦^


?Èì ˜
     MS VS2008: 3Property–>language¥§support openmp.
  ...
0
                                 OpenMP0



    OpenMPS•                     .




                                     ...
0
                                 OpenMP0



    OpenMPS•                     .



     éA OpenMP§Sp§z‡êâÑk˜‡–¯á5"
      ...
0
                                 OpenMP0



    OpenMP$1Å›




Jerry He (Tsinghua University)   A Brief Introduction to ...
0
                                 OpenMP0



    ¿1«Vg




                                                   M¿1«
   ...
0
                                 OpenMP0



    ¿1«Vg




                                                   ››¿1«¥‚§ êþ...
OpenMPA 5
                                              A



         OpenMP’s Hello world Program


1   #i n c l u d e <o...
OpenMPA 5
                                               A



         OpenMP’s for


          #pragma omp for ¦^ù‡Šé§·‚Ò...
OpenMPA 5
                                               A



         OpenMP’s for


          #pragma omp for ¦^ù‡Šé§·‚Ò...
OpenMPA 5
                                                 A



           if/shared/privateŠ{

     c¡®²J                ...
OpenMPA 5
                                                 A



           if/shared/privateŠ{

     c¡®²J                ...
OpenMPA 5
                                                 A



           if/shared/privateŠ{

     c¡®²J                ...
OpenMPA 5
                                                 A



           default(none|shared)

 1        int x [5]={1 ,2...
OpenMPA 5
                                                 A



           default(none|shared)

 1        int x [5]={1 ,2...
OpenMPA 5
                                                A



          firstprivate,lastprivate

1        i n t a =10 , ...
OpenMPA 5
                                                A



          firstprivate,lastprivate

1        i n t a =10 , ...
OpenMPA 5
                                                 A



          8        reduction

                            ...
OpenMPA 5
                                                 A



          8        reduction

                            ...
OpenMPA 5
                                                 A



          8        reduction

                            ...
OpenMPA 5
                                              A



              .«critical


1   f l o a t d o t p r o d u c t ...
OpenMPA 5
                                              A



              .«critical


1   f l o a t d o t p r o d u c t ...
OpenMPA 5
                                       A



    ?ÖNÝschedule



                                                ...
OpenMPA 5
                                       A



    ?ÖNÝschedule



                                                ...
OpenMPA 5
                                       A



    ?ÖNÝschedule



                                                ...
OpenMPA 5
                                       A



    ?ÖNÝschedule



                                                ...
OpenMPA 5
                                       A



    ?ÖNÝschedule



                                                ...
OpenMPA 5
                                                  A



         ?ÖNÝschedule



    schedule(<type> [,<chunk>])•...
OpenMPA 5
                                          A



      section

  1   #pragma omp s e c t i o n s [ c l a u s e ( ...
OpenMPA 5
                                          A



      section

  1   #pragma omp s e c t i o n s [ c l a u s e ( ...
OpenMPA 5
                                               A



          barrier,nowait


 1   #pragma omp p a r a l l e l
...
OpenMPA 5
                                               A



          barrier,nowait


 1   #pragma omp p a r a l l e l
...
OpenMPA 5
                                            A



         single,master


    4,¬“è•k˜‡‚§‰1"

1   #pragma omp p ...
OpenMPA 5
                                            A



         single,master


    4,¬“è•k˜‡‚§‰1"

1   #pragma omp p ...
OpenMPA 5
                                            A



         single,master


    4,¬“è•k˜‡‚§‰1"

1   #pragma omp p ...
OpenMPA 5
                                       A



    ~^             Û¼ê




     omp get num procs§ˆ£$1              ...
OpenMPA 5
                                       A



    OpenMP 3.0


            Task
                                  ...
OpenMPA 5
                                       A



    OpenMP 3.0


            Task
            ¿1i@




Jerry He (Tsi...
OpenMPA 5
                                       A



    OpenMP 3.0



            Task
            ¿1i@




Jerry He (Ts...
OpenMPA 5
                                            A



     Performance:Amdahl’s law



Definition
                   ...
OpenMPA 5
                                            A



     Performance:Amdahl’s law
Definition
                      ...
OpenMPA 5
                                                A



       One Myth


A program use all cores can scale.

     ...
OpenMPA 5
                                                A



       One Myth


A program use all cores can scale.

     ...
OpenMPA 5
                                                A



       One Myth


A program use all cores can scale.

     ...
OpenMPA 5
                                                A



       One Myth


A program use all cores can scale.

     ...
OpenMPA 5
                                       A



    OpenMP




     Demo




Jerry He (Tsinghua University)   A Brie...
OpenMPA 5
                                       A



    o(




     OpenMPJø˜‡                  §%rŒ ¿1 ."
     Œ^u?¿Œ  ...
OpenMPA 5
                                       A



    ë•]




     OpenMP(• Õ
     Free lunch is over
     Ruud van de...
OpenMPA 5
                                       A




                   Thanks for your attention!
                     ...
Upcoming SlideShare
Loading in …5
×

Biref Introduction to OpenMP

801
-1

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
801
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Biref Introduction to OpenMP

  1. 1. OPENMP A Brief Introduction to OpenMP Û[U/ Jerry He hejw06@gmail.com June 19, 2010 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 1 / 31
  2. 2. ̇SN 1 OpenMP0 2 OpenMPA5 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 2 / 31
  3. 3. ̇SN 1 OpenMP0 2 OpenMPA5 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 2 / 31
  4. 4. 0 OpenMP0 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 3 / 31
  5. 5. 0 OpenMP0 OpenMP OpenMP´dOpenMP Architecture Review BoardVÞJÑ §¿® 2• É §^u •S•¿1XÚ õ‚§ §S O ˜@• 55º(Compiler Directive)"OpenMP|± ?§Šó•)CŠ ó!C++ÚFortran¶ |±OpenMP ?Èì•)Sun Compiler§GNU CompilerÚIntel Compiler "OpenMPJ ø é¿1Ž{ p Ä–£ã§§S ÏL3 “è ¥;^ pragma5•²gC ¿ã§dd?ÈìŒ ±gÄò§S?1¿1z§¿37‡ƒ?ÓÚp½± 9Ï&" ÀJ Ñù pragma§½ö?ÈìØ| ±OpenMP ž§§SqŒòz•Ï~ §S(˜„•G 1)§“èE,Œ± ~$Š§•´ØU|^õ‚§5 „§S‰1" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 4 / 31
  6. 6. 0 OpenMP0 OpenMP dã´˜‡;. OpenMP§S «¿ã§·‚Œ±w §´dG1“ èÚ¿1“è †|¤ §¿1“è «•·‚r§ ‰/¿1«0" Ì‚§˜ ?¿1«§ÒgÄ )Ñõ‡‚§§5¿1 ‰1" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 5 / 31
  7. 7. 0 OpenMP0 XÛ3“襦^ ?Èì ˜ MS VS2008: 3Property–>language¥§support openmp. gcc: -fopenmp 'u•õ?Èì |±ë„OpenMP Compilers “èCz <omp.h>Þ©‡ ¿ ˜ #pragma•-=Œ" “è3šopenmpeEU?È" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 6 / 31
  8. 8. 0 OpenMP0 OpenMPS• . ¤k ‚§é kêâÑkƒ Ó •µŒ„!ŒÖ!Œ " ˜¬ê⌱úk§Œ±h k" úkêâ ¤k‚§¦^" hkêâ= T‚§¦^" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 7 / 31
  9. 9. 0 OpenMP0 OpenMPS• . éA OpenMP§Sp§z‡êâÑk˜‡–¯á5" k2«a.µ Shared •kdêâ ˜‡¢~" ¤k‚§ÓÚÖ " dêâ UCéÙ§‚§ÑŒ„" Private z‡‚§ÑÕkdêâ" Ù§‚§ØU–¯dêâ" UC•=éd‚§Œ„" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 8 / 31
  10. 10. 0 OpenMP0 OpenMP$1Å› Jerry He (Tsinghua University) A Brief Introduction to OpenMP 9 / 31
  11. 11. 0 OpenMP0 ¿1«Vg M¿1« O˜1“è#pragma omp parallel ^s)ÒrI‡˜3¿1 «S Šé)å5 ¿1«pz‡‚§Ñ¬ ‰1¿ 1«¥ “è" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 10 / 31
  12. 12. 0 OpenMP0 ¿1«Vg ››¿1«¥‚§ êþ %@œ¹e§¿1«S‚§ ê=XÚ¥Ø ‡ê •{˜µset OMP NUM THREADS=4 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 11 / 31
  13. 13. OpenMPA 5 A OpenMP’s Hello world Program 1 #i n c l u d e <omp . h> 2 #i n c l u d e < s t d i o . h> 3 i n t main ( ) { 4 #pragma omp p a r a l l e l // come i n t o p a r a l l e l i z a t i o n 5 p r i n t f ( ” H e l l o w o r l d from t h r e a d %d , n t h r e a d s %dn” , 6 omp get thread num () , omp get num threads () ) ; 7 } // come o u t o f p a r a l l e l i z a t i o n ÑÑ(J 1 H e l l o w o r l d from t h r e a d 0 , n t h r e a d s 2 2 H e l l o w o r l d from t h r e a d 1 , n t h r e a d s 2 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 12 / 31
  14. 14. OpenMPA 5 A OpenMP’s for #pragma omp for ¦^ù‡Šé§·‚ÒŒ±r˜‡forÌ‚ óŠ þ£~Xµ1...N¤© ‰ØÓ‚§"ù‡Šé ¡7L;‹˜ ‡forÌ‚§¦UédforÌ‚ óŠþ?1© " du 21openmp pragma¢3k 憧·‚Œ±r21½õ 1openmp pragmaÜ¿¤˜1" 1 #pragma omp p a r a l l e l 1 #pragma omp p a r a l l e l f o r 2 #pragma omp f o r 2 f o r ( i =0; i <MAX;++ i ) { 3 f o r ( i =0; i <MAX;++ i ) { 3 cout <<i <<e n d l ; 4 cout <<i <<e n d l ; 4 } 5 } Jerry He (Tsinghua University) A Brief Introduction to OpenMP 13 / 31
  15. 15. OpenMPA 5 A OpenMP’s for #pragma omp for ¦^ù‡Šé§·‚ÒŒ±r˜‡forÌ‚ óŠ þ£~Xµ1...N¤© ‰ØÓ‚§"ù‡Šé ¡7L;‹˜ ‡forÌ‚§¦UédforÌ‚ óŠþ?1© " du 21openmp pragma¢3k 憧·‚Œ±r21½õ 1openmp pragmaÜ¿¤˜1" 1 #pragma omp p a r a l l e l 1 #pragma omp p a r a l l e l f o r 2 #pragma omp f o r 2 f o r ( i =0; i <MAX;++ i ) { 3 f o r ( i =0; i <MAX;++ i ) { 3 cout <<i <<e n d l ; 4 cout <<i <<e n d l ; 4 } 5 } Jerry He (Tsinghua University) A Brief Introduction to OpenMP 13 / 31
  16. 16. OpenMPA 5 A if/shared/privateŠ{ c¡®²J OpenMP S• .§ùA‡'…i^5 ˜êâ –¯ a." 1 int x [5]={1 ,2 ,3 ,4 ,5}; 2 int y [5]={2 ,3 ,4 ,5 ,6}; 3 int c [5]; 4 int a ,b, i ; 5 #pragma omp p a r a l l e l f o r p r i v a t e ( a , b , i ) s h a r e d ( x , y , c ) 6 f o r ( i =0; i <5;++ i ) { 7 a=x [ i ] ; b=y [ i ] ; 8 c [ i ]= a+b ; 9 } 10 f o r ( i =0; i <5;++ i ) 11 p r i n t f ( ”%d ” , c [ i ] ) ; 1 3 5 7 9 11 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 14 / 31
  17. 17. OpenMPA 5 A if/shared/privateŠ{ c¡®²J OpenMP S• .§ùA‡'…i^5 ˜êâ –¯ a." 1 int x [5]={1 ,2 ,3 ,4 ,5}; 2 int y [5]={2 ,3 ,4 ,5 ,6}; 3 int c [5]; 4 int a ,b, i ; 5 #pragma omp p a r a l l e l f o r p r i v a t e ( a , b , i ) s h a r e d ( x , y , c ) 6 f o r ( i =0; i <5;++ i ) { 7 a=x [ i ] ; b=y [ i ] ; 8 c [ i ]= a+b ; 9 } 10 f o r ( i =0; i <5;++ i ) 11 p r i n t f ( ”%d ” , c [ i ] ) ; 1 3 5 7 9 11 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 14 / 31
  18. 18. OpenMPA 5 A if/shared/privateŠ{ c¡®²J OpenMP S• .§ùA‡'…i^5 ˜êâ –¯ a." 1 int x [5]={1 ,2 ,3 ,4 ,5}; 2 int y [5]={2 ,3 ,4 ,5 ,6}; 3 int c [5]; 4 int a ,b, i ; 5 #pragma omp p a r a l l e l f o r p r i v a t e ( a , b , i ) s h a r e d ( x , y , c ) 6 f o r ( i =0; i <5;++ i ) { 7 a=x [ i ] ; b=y [ i ] ; 8 c [ i ]= a+b ; 9 } 10 f o r ( i =0; i <5;++ i ) 11 p r i n t f ( ”%d ” , c [ i ] ) ; 1 3 5 7 9 11 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 14 / 31
  19. 19. OpenMPA 5 A default(none|shared) 1 int x [5]={1 ,2 ,3 ,4 ,5}; 2 int y [5]={2 ,3 ,4 ,5 ,6}; 3 int c [5]; 4 int a ,b, i ; 5 #pragma omp p a r a l l e l f o r d e f a u l t ( s h a r e d ) p r i v a t e ( a , b , i ) 6 f o r ( i =0; i <5;++ i ) { 7 a=x [ i ] ; b=y [ i ] ; 8 c [ i ]= a+b ; 9 } 10 f o r ( i =0; i <5;++ i ) 11 p r i n t f ( ”%d ” , c [ i ] ) ; %@•default(shared)§Øparallel¬S½Â CþÙ§CþÑ •shareda." 핦^default(private) Jerry He (Tsinghua University) A Brief Introduction to OpenMP 15 / 31
  20. 20. OpenMPA 5 A default(none|shared) 1 int x [5]={1 ,2 ,3 ,4 ,5}; 2 int y [5]={2 ,3 ,4 ,5 ,6}; 3 int c [5]; 4 int a ,b, i ; 5 #pragma omp p a r a l l e l f o r d e f a u l t ( s h a r e d ) p r i v a t e ( a , b , i ) 6 f o r ( i =0; i <5;++ i ) { 7 a=x [ i ] ; b=y [ i ] ; 8 c [ i ]= a+b ; 9 } 10 f o r ( i =0; i <5;++ i ) 11 p r i n t f ( ”%d ” , c [ i ] ) ; %@•default(shared)§Øparallel¬S½Â CþÙ§CþÑ •shareda." 핦^default(private) Jerry He (Tsinghua University) A Brief Introduction to OpenMP 15 / 31
  21. 21. OpenMPA 5 A firstprivate,lastprivate 1 i n t a =10 , b =20 , c =30 , d =40; 2 int i ; 3 p r i n t f ( ” a=%d , b=%d , c=%d , d=%dn” , a , b , c , d ) ; 4 #pragma omp p a r a l l e l f o r p r i v a t e ( a ) f i r s t p r i v a t e ( b , d ) la stp riv ate (c , d) 5 f o r ( i =0; i <1;++ i ) { 6 p r i n t f ( ” a=%d , b=%d c=%d , d=%dn” , a , b , c , d ) ; 7 a++;b++;c++;d++; 8 } 9 p r i n t f ( ” a=%d , b=%d , c=%d , d=%dn” , a , b , c , d ) ; (J 1 a =10 , b =20 , c =30 , d=40 2 a =2,b=20 c =4072624 , d=40 3 a =10 , b =20 , c =4072625 , d=41 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 16 / 31
  22. 22. OpenMPA 5 A firstprivate,lastprivate 1 i n t a =10 , b =20 , c =30 , d =40; 2 int i ; 3 p r i n t f ( ” a=%d , b=%d , c=%d , d=%dn” , a , b , c , d ) ; 4 #pragma omp p a r a l l e l f o r p r i v a t e ( a ) f i r s t p r i v a t e ( b , d ) la stp riv ate (c , d) 5 f o r ( i =0; i <1;++ i ) { 6 p r i n t f ( ” a=%d , b=%d c=%d , d=%dn” , a , b , c , d ) ; 7 a++;b++;c++;d++; 8 } 9 p r i n t f ( ” a=%d , b=%d , c=%d , d=%dn” , a , b , c , d ) ; o( privateCþ´™½Â " firstprivateCþ3?¿1«c¬l ¡Ó¶CþDŠ" lastprivateCþ3Ñ¿1«c¬DŠ ¡Ó¶Cþ" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 16 / 31
  23. 23. OpenMPA 5 A 8 reduction 1 f l o a t dot product ( f l o a t ∗ a , 1 f l o a t dot product ( f l o a t ∗ a , f l o a t ∗b , i n t N) { f l o a t ∗b , i n t N) { 2 f l o a t sum = 0 . 0 ; 2 f l o a t sum = 0 . 0 ; 3 int i ; 3 int i ; 4 #pragma omp p a r a l l e l f o r 4 #pragma omp p a r a l l e l f o r r e d u c t i o n (+: sum ) p r i v a t e ( s h a r e d ( sum ) p r i v a t e ( i ) i) 5 f o r ( i =0; i <N;++ i ) 5 f o r ( i =0; i <N;++ i ) 6 sum+=a [ i ] ∗ b [ i ] ; 6 sum+=a [ i ] ∗ b [ i ] ; 7 r e t u r n sum ; 7 r e t u r n sum ; 8 } 8 } |± 8 öŠ +, −, ∗, /, · · · Jerry He (Tsinghua University) A Brief Introduction to OpenMP 17 / 31
  24. 24. OpenMPA 5 A 8 reduction 1 f l o a t dot product ( f l o a t ∗ a , 1 f l o a t dot product ( f l o a t ∗ a , f l o a t ∗b , i n t N) { f l o a t ∗b , i n t N) { 2 f l o a t sum = 0 . 0 ; 2 f l o a t sum = 0 . 0 ; 3 int i ; 3 int i ; 4 #pragma omp p a r a l l e l f o r 4 #pragma omp p a r a l l e l f o r r e d u c t i o n (+: sum ) p r i v a t e ( s h a r e d ( sum ) p r i v a t e ( i ) i) 5 f o r ( i =0; i <N;++ i ) 5 f o r ( i =0; i <N;++ i ) 6 sum+=a [ i ] ∗ b [ i ] ; 6 sum+=a [ i ] ∗ b [ i ] ; 7 r e t u r n sum ; 7 r e t u r n sum ; 8 } 8 } |± 8 öŠ +, −, ∗, /, · · · Jerry He (Tsinghua University) A Brief Introduction to OpenMP 17 / 31
  25. 25. OpenMPA 5 A 8 reduction 1 f l o a t dot product ( f l o a t ∗ a , 1 f l o a t dot product ( f l o a t ∗ a , f l o a t ∗b , i n t N) { f l o a t ∗b , i n t N) { 2 f l o a t sum = 0 . 0 ; 2 f l o a t sum = 0 . 0 ; 3 int i ; 3 int i ; 4 #pragma omp p a r a l l e l f o r 4 #pragma omp p a r a l l e l f o r r e d u c t i o n (+: sum ) p r i v a t e ( s h a r e d ( sum ) p r i v a t e ( i ) i) 5 f o r ( i =0; i <N;++ i ) 5 f o r ( i =0; i <N;++ i ) 6 sum+=a [ i ] ∗ b [ i ] ; 6 sum+=a [ i ] ∗ b [ i ] ; 7 r e t u r n sum ; 7 r e t u r n sum ; 8 } 8 } |± 8 öŠ +, −, ∗, /, · · · Jerry He (Tsinghua University) A Brief Introduction to OpenMP 17 / 31
  26. 26. OpenMPA 5 A .«critical 1 f l o a t d o t p r o d u c t ( f l o a t ∗ a , f l o a t ∗b , i n t N) { 2 f l o a t sum = 0 . 0 ; 3 int i ; 4 #pragma omp p a r a l l e l f o r s h a r e d ( sum ) p r i v a t e ( i ) 5 f o r ( i =0; i <N;++ i ) 6 #pragma omp c r i t i c a l 7 sum+=a [ i ] ∗ b [ i ] ; 8 r e t u r n sum ; 9 } „Œ±¦^·¶ .«§½•”þ4 atomic" ë„OpenMP(•© " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 18 / 31
  27. 27. OpenMPA 5 A .«critical 1 f l o a t d o t p r o d u c t ( f l o a t ∗ a , f l o a t ∗b , i n t N) { 2 f l o a t sum = 0 . 0 ; 3 int i ; 4 #pragma omp p a r a l l e l f o r s h a r e d ( sum ) p r i v a t e ( i ) 5 f o r ( i =0; i <N;++ i ) 6 #pragma omp c r i t i c a l 7 sum+=a [ i ] ∗ b [ i ] ; 8 r e t u r n sum ; 9 } „Œ±¦^·¶ .«§½•”þ4 atomic" ë„OpenMP(•© " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 18 / 31
  28. 28. OpenMPA 5 A ?ÖNÝschedule kn «static,dynamic,guidedn« NÝ•ª" static²þ© §?Èž( ½" dynamic§ ¤˜¬§2©˜ ¬" Ódynamic§ O3u¬ Œ ¬•ê~ " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 19 / 31
  29. 29. OpenMPA 5 A ?ÖNÝschedule kn «static,dynamic,guidedn« NÝ•ª" static²þ© §?Èž( ½" dynamic§ ¤˜¬§2©˜ ¬" Ódynamic§ O3u¬ Œ ¬•ê~ " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 19 / 31
  30. 30. OpenMPA 5 A ?ÖNÝschedule kn «static,dynamic,guidedn« NÝ•ª" static²þ© §?Èž( ½" dynamic§ ¤˜¬§2©˜ ¬" Ódynamic§ O3u¬ Œ ¬•ê~ " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 19 / 31
  31. 31. OpenMPA 5 A ?ÖNÝschedule kn «static,dynamic,guidedn« NÝ•ª" static²þ© §?Èž( ½" dynamic§ ¤˜¬§2©˜ ¬" Ódynamic§ O3u¬ Œ ¬•ê~ " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 19 / 31
  32. 32. OpenMPA 5 A ?ÖNÝschedule kn «static,dynamic,guidedn« NÝ•ª" static²þ© §?Èž( ½" dynamic§ ¤˜¬§2©˜ ¬" Ódynamic§ O3u¬ Œ ¬•ê~ " Jerry He (Tsinghua University) A Brief Introduction to OpenMP 19 / 31
  33. 33. OpenMPA 5 A ?ÖNÝschedule schedule(<type> [,<chunk>])•½Ì‚NÝ•ª" 1 #pragma omp p a r a l l e l f o r s c h e d u l e ( s t a t i c ) 2 ... 3 #pragma omp p a r a l l e l f o r s c h e d u l e ( dynamic , chunk ) 4 ... 5 #pragma omp p a r a l l e l f o r s c h e d u l e ( g u i d e d , chunk ) 6 ... 7 #pragma omp p a r a l l e l f o r s c h e d u l e ( a u t o ) 8 ... // o n l y s u p p o r t by OpenMP 3 . 0 . Jerry He (Tsinghua University) A Brief Introduction to OpenMP 20 / 31
  34. 34. OpenMPA 5 A section 1 #pragma omp s e c t i o n s [ c l a u s e ( s ) ] 2 { 3 #pragma omp s e c t i o n 4 <c o d e b l o c k 1 > 5 #pragma omp s e c t i o n 6 <c o d e b l o c k 2 > 7 #pragma omp s e c t i o n 8 <c o d e b l o c k 3 > 9 ... 10 } |± clauseskµ private firstprivate lastprivate reduction nowait Jerry He (Tsinghua University) A Brief Introduction to OpenMP 21 / 31
  35. 35. OpenMPA 5 A section 1 #pragma omp s e c t i o n s [ c l a u s e ( s ) ] 2 { 3 #pragma omp s e c t i o n 4 <c o d e b l o c k 1 > 5 #pragma omp s e c t i o n 6 <c o d e b l o c k 2 > 7 #pragma omp s e c t i o n 8 <c o d e b l o c k 3 > 9 ... 10 } |± clauseskµ private firstprivate lastprivate reduction nowait Jerry He (Tsinghua University) A Brief Introduction to OpenMP 21 / 31
  36. 36. OpenMPA 5 A barrier,nowait 1 #pragma omp p a r a l l e l 2 { 3 print (a) ; 4 #pragma omp b a r r i e r 5 print (b) ; 6 #pragma omp f o r 7 ... 8 print (c) ; 9 #pragma omp f o r n o w a i t 10 ... 11 } Øfor §Nõ•- ¡Û¹kbarrier" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 22 / 31
  37. 37. OpenMPA 5 A barrier,nowait 1 #pragma omp p a r a l l e l 2 { 3 print (a) ; 4 #pragma omp b a r r i e r 5 print (b) ; 6 #pragma omp f o r 7 ... 8 print (c) ; 9 #pragma omp f o r n o w a i t 10 ... 11 } Øfor §Nõ•- ¡Û¹kbarrier" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 22 / 31
  38. 38. OpenMPA 5 A single,master 4,¬“è•k˜‡‚§‰1" 1 #pragma omp p a r a l l e l 2 { 3 print (a) ; 4 #pragma omp s i n g l e 5 print (b) ; 6 #pragma omp m a s t e r 7 print (c) ; 8 } singleÚmaster «O" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 23 / 31
  39. 39. OpenMPA 5 A single,master 4,¬“è•k˜‡‚§‰1" 1 #pragma omp p a r a l l e l 2 { 3 print (a) ; 4 #pragma omp s i n g l e 5 print (b) ; 6 #pragma omp m a s t e r 7 print (c) ; 8 } singleÚmaster «O" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 23 / 31
  40. 40. OpenMPA 5 A single,master 4,¬“è•k˜‡‚§‰1" 1 #pragma omp p a r a l l e l 2 { 3 print (a) ; 4 #pragma omp s i n g l e 5 print (b) ; 6 #pragma omp m a s t e r 7 print (c) ; 8 } singleÚmaster «O" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 23 / 31
  41. 41. OpenMPA 5 A ~^ Û¼ê omp get num procs§ˆ£$1 ‚§ õ?nÅ ?nì‡ê omp get num threads§ˆ£ c¿1«¥¹Ä ‚§‡ê omp get thread num§ˆ£‚§Ò§XJkt‡¹Ä‚§§‚§Ò •0 t-1" omp set num threads§?U¿1‰1“èž‚§ ‡ê"‚§êŒ U‡LŒ^ ?nìê§džõ‡‚§¬ N Ó˜‡?nì þ"d¼ê7L3G1Ü© N^" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 24 / 31
  42. 42. OpenMPA 5 A OpenMP 3.0 Task 1 v e c t o r vec (100) ; ¿1i@ 2 vector : : iterator i t ; 3 #pragma omp f o r d e f a u l t ( none ) s h a r e d ( v e c ) 4 f o r ( i t =v e c . b e g i n ( ) ; i t < v e c . end ( ) ; i t ++) 5 // do y o u r work . Jerry He (Tsinghua University) A Brief Introduction to OpenMP 25 / 31
  43. 43. OpenMPA 5 A OpenMP 3.0 Task ¿1i@ Jerry He (Tsinghua University) A Brief Introduction to OpenMP 25 / 31
  44. 44. OpenMPA 5 A OpenMP 3.0 Task ¿1i@ Jerry He (Tsinghua University) A Brief Introduction to OpenMP 25 / 31
  45. 45. OpenMPA 5 A Performance:Amdahl’s law Definition 1 speedup(p) = (1) s + (1 − s)/p p: core nums s: serial code 1-s: can parallel code Jerry He (Tsinghua University) A Brief Introduction to OpenMP 26 / 31
  46. 46. OpenMPA 5 A Performance:Amdahl’s law Definition 1 speedup(p) = (1) s + (1 − s)/p p: core nums s: serial code 1-s: can parallel code Jerry He (Tsinghua University) A Brief Introduction to OpenMP 26 / 31
  47. 47. OpenMPA 5 A One Myth A program use all cores can scale. éuG1“èkvk•Ð `z" 1 double a [ 1 0 0 0 ] [ 1 0 0 0 ] ; 2 f o r ( i n t i =0; i <1000; i ++) 3 f o r ( i n t j =0; j <1000; j ++) 4 a [ i ] [ j ] = 0 . 0 // o r a [ j ] [ i ] = 0 . 0 5 // c++’ s a r r a y i s row− f i r s t . ´Ä• zÜ© Ñž" S•§– •. . . Jerry He (Tsinghua University) A Brief Introduction to OpenMP 27 / 31
  48. 48. OpenMPA 5 A One Myth A program use all cores can scale. éuG1“èkvk•Ð `z" 1 double a [ 1 0 0 0 ] [ 1 0 0 0 ] ; 2 f o r ( i n t i =0; i <1000; i ++) 3 f o r ( i n t j =0; j <1000; j ++) 4 a [ i ] [ j ] = 0 . 0 // o r a [ j ] [ i ] = 0 . 0 5 // c++’ s a r r a y i s row− f i r s t . ´Ä• zÜ© Ñž" S•§– •. . . Jerry He (Tsinghua University) A Brief Introduction to OpenMP 27 / 31
  49. 49. OpenMPA 5 A One Myth A program use all cores can scale. éuG1“èkvk•Ð `z" 1 double a [ 1 0 0 0 ] [ 1 0 0 0 ] ; 2 f o r ( i n t i =0; i <1000; i ++) 3 f o r ( i n t j =0; j <1000; j ++) 4 a [ i ] [ j ] = 0 . 0 // o r a [ j ] [ i ] = 0 . 0 5 // c++’ s a r r a y i s row− f i r s t . ´Ä• zÜ© Ñž" S•§– •. . . Jerry He (Tsinghua University) A Brief Introduction to OpenMP 27 / 31
  50. 50. OpenMPA 5 A One Myth A program use all cores can scale. éuG1“èkvk•Ð `z" 1 double a [ 1 0 0 0 ] [ 1 0 0 0 ] ; 2 f o r ( i n t i =0; i <1000; i ++) 3 f o r ( i n t j =0; j <1000; j ++) 4 a [ i ] [ j ] = 0 . 0 // o r a [ j ] [ i ] = 0 . 0 5 // c++’ s a r r a y i s row− f i r s t . ´Ä• zÜ© Ñž" S•§– •. . . Jerry He (Tsinghua University) A Brief Introduction to OpenMP 27 / 31
  51. 51. OpenMPA 5 A OpenMP Demo Jerry He (Tsinghua University) A Brief Introduction to OpenMP 28 / 31
  52. 52. OpenMPA 5 A o( OpenMPJø˜‡ §%rŒ ¿1 ." Œ^u?¿Œ •S•ªXÚ¥" A Ü?ÈìÑ|±OpenMP" 3 ¡|±OpenMP 3.0" OpenMP 3.0¥ taskA5¦ÙA^•2" Sun CompileréOpenMP|±•2" Jerry He (Tsinghua University) A Brief Introduction to OpenMP 29 / 31
  53. 53. OpenMPA 5 A ë•] OpenMP(• Õ Free lunch is over Ruud van der Pas: An overview of OpenMP 3.0 Wikipedia OpenMP OpenMP Application Program Interface Version 3.0 May 2008 Summary of OpenMP 3.0 C/C++ Syntax Jerry He (Tsinghua University) A Brief Introduction to OpenMP 30 / 31
  54. 54. OpenMPA 5 A Thanks for your attention! Q&A Jerry He (Tsinghua University) ã: A Brief Introduction to OpenMP 31 / 31

×