SlideShare una empresa de Scribd logo
1 de 54
Descargar para leer sin conexión
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
̇SN




     1    OpenMP0



     2    OpenMPA5




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




     1    OpenMP0



     2    OpenMPA5




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   2 / 31
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•¿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
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
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
0
                                 OpenMP0



    OpenMPS•                     .




                                                         ¤k ‚§é kêâÑkƒ
                                                         Ó  •µŒ„!ŒÖ!Œ
                                                          "
                                                         ˜¬ê⌱úk§Œ±h
                                                         k"
                                                         úkêâ ¤k‚§¦^"
                                                         hkêâ= T‚§¦^"




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP     7 / 31
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
0
                                 OpenMP0



    OpenMP$1Å›




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   9 / 31
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
OpenMPA 5
                                       A



    OpenMP 3.0


            Task
            ¿1i@




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   25 / 31
OpenMPA 5
                                       A



    OpenMP 3.0



            Task
            ¿1i@




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   25 / 31
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
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
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
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
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
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
OpenMPA 5
                                       A



    OpenMP




     Demo




Jerry He (Tsinghua University)   A Brief Introduction to OpenMP   28 / 31
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
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
OpenMPA 5
                                       A




                   Thanks for your attention!
                            Q&A




Jerry He (Tsinghua University)               ã:
                                 A Brief Introduction to OpenMP   31 / 31

Más contenido relacionado

Destacado (6)

Openmp combined
Openmp combinedOpenmp combined
Openmp combined
 
Wolfgang Lehner Technische Universitat Dresden
Wolfgang Lehner Technische Universitat DresdenWolfgang Lehner Technische Universitat Dresden
Wolfgang Lehner Technische Universitat Dresden
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel Programming
 
Open mp intro_01
Open mp intro_01Open mp intro_01
Open mp intro_01
 
Form i 983 sample
Form i 983 sampleForm i 983 sample
Form i 983 sample
 
OpenMP
OpenMPOpenMP
OpenMP
 

Último

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Último (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Biref Introduction to OpenMP

  • 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. ̇SN 1 OpenMP0 2 OpenMPA5 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 2 / 31
  • 3. ̇SN 1 OpenMP0 2 OpenMPA5 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 2 / 31
  • 4. 0 OpenMP0 Jerry He (Tsinghua University) A Brief Introduction to OpenMP 3 / 31
  • 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. 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. 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. 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. 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. 0 OpenMP0 OpenMP$1Å› Jerry He (Tsinghua University) A Brief Introduction to OpenMP 9 / 31
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. OpenMPA 5 A OpenMP 3.0 Task ¿1i@ Jerry He (Tsinghua University) A Brief Introduction to OpenMP 25 / 31
  • 44. OpenMPA 5 A OpenMP 3.0 Task ¿1i@ Jerry He (Tsinghua University) A Brief Introduction to OpenMP 25 / 31
  • 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. 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. 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. 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. 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. 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. OpenMPA 5 A OpenMP Demo Jerry He (Tsinghua University) A Brief Introduction to OpenMP 28 / 31
  • 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. 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. OpenMPA 5 A Thanks for your attention! Q&A Jerry He (Tsinghua University) ã: A Brief Introduction to OpenMP 31 / 31