SlideShare a Scribd company logo
1 of 54
Download to read offline
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

More Related Content

Viewers also liked

Viewers also liked (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
 

Recently uploaded

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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...
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

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