La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
Télécharger Lire

multicore days 2008 openmp tutorialx

De
21 pages
September 12, 2008A brief look at OpenMPMats BrorssonKTH School of Information and Communication Technology1Before we begin...• Who is mainly programming in:- C- C++- Java- C#- Other• Who has experience with parallelization:- With pthreads/java threads? - OpenMP?- Other models?2Multicore days 2008 1September 12, 2008OpenMP• A portable shared memory multiprocessing API based on compiler directivesFortran 77/90, C, C++Multi-vendor support for both Unix and NT• Standardises fine grain (loop, task) parallelism• Also support coarse grained algorithms• http://www.openmp.org• http://www.compunity.org• It is NOT automatic parallelization3The OpenMP model• All threads have the same access to the same globally shared memory• Data can be shared or private• Private data is accessible only by threads who owns it• Data transfer is transparant to programmers• Synchronization takes 4place but can be implicitMulticore days 2008 2September 12, 2008A first example• For-loop with • Parallelized versionindependent iterations #pragma omp parallel forfor (i = 0; i < n; i++) for (i = 0; i < n; i++)c[i] = a[i] + b[i]; c[i] = a[i] + b[i];Thr 1 Thr 2c[0]=... c[4]=...c[1]=... c[5]=...c[2]=... c[6]=...c[3]=... c[7]=...5OpenMP execution modelmaster threadjoinfork parallel regions• OpenMP has a fork-join execution model• The program starts as any sequential program• Threads are forked off at parallel regions• Threads join after a ...
Voir plus Voir moins
  
    
 
       
  
        ! "  # $ %  &' ( ")( " (
  

* +    *) +    , --&./0 0  1   + 2#  3    40 56  *     &&$$$77&&$$$77  3"  %
  
*         +   8  +    )   +  +  $ $  8      % 5  +  + 
    
  
,1 $   for (i = 0; i < n; i++) c[i] = a[i] + b[i]; Thr 1 Thr 2 c[0]=... c[4]=... c[1]=... c[5]=... c[2]=... c[6]=... c[3]=... c[7]=...
)%  #pragma omp parallel for for (i = 0; i < n; i++) c[i] = a[i] + b[i];
 "# 
master thread
n fork parallel regions joi ")   51' #       9    5      '    
!
 "
8 ; )  + 5   3+   5  %  81 7 8  ) '  3  "
  
: 3+    8 8  ' 3  *)  5  
$
    #pragma omp parallel if (n>limit) default(none) \ shared(n, a, b, c, x, y, z) private (f, i , scale) { f = 1.0; &"# '   #pragma omp for nowait for (i=0; i<n; i++) z = x #pragma[oim]pfor[in]ow+aiyt[i]; () "#" for (i=0; i<n; i++) a[i] = b[i] + c[i]; #pragma omp barrier *'"+... scale = sum(a,0,n) + sum(z,0,n) + f; }
%
  
 -  
;#     main(){ A; /* Master thread executes */ #pragma omp parallel { /* fork parallel threads */ B; /* Executes in parallel */ } /* join parallel threads */ C; /* Master thread executes */ } $    5 (
/  
    + /  +      + ; + setenv OMP NUM THREADS 8 _ _ <+  p_ _ _ reads(8); om set num th     num_threads(8)
,
.
  
(  "#"
for   sections   single  9 #
 0  
#pragma omp for for (i = 1; i < n; i++) b[i] = (a[i] + a[i-1]) / 2.0;  +        +     $   +(


  
 0  
#pragma omp for schedule(<schedule>, <chunk>) for (i = 1; i < n; i++) b[i] = (a[i] + a[i-1]) / 2.0; <schedule>     +  8    <chunk> 5 % 9    +  
*"# "


*" 0 #" 
#pragma omp sections { #pragma omp section { A; } #pragma omp section { B; } }
 +    *    #  
*1# "#   "
  
#pragma omp single { A; } #pragma omp master { B; }
 +    "    # * "   # 

!
  
2#"   0 * '    $  + 3   # 1 2        + 
*'"
 # *  
$
%
  
## "#
#pragma omp critical { sum = sum + local sum; _ }  +      +5      # +      
"
#pragma omp atomic sum += f(x); *    critical ;        46    #  4#6    "    $
,
.

  
 '"
#pragma omp barrier  +     ;  $        for 0 sections  single    $  +
(' 3
 $     $      
for (i=0; i < N; i++) a[i] = b[i] + c[i]; for (i=0; i < M; i++) // M < N d[i] = a[i] + b[i];
    $ $ 4 6  (


