Documents

omp examples

Description
omp examples
Categories
Published
of 10
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  CONCURRENT AND PARALLELPROGRAMMING Wojciech Mikanik, PhDwojciech.mikanik@polsl.plroom 503February 2008 Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Part IOpenMP Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Outline 1. Matrix multiplication example  sequential solution  pthreads solution  OpenMP solution 2. Introduction to OpenMP3. Expressing parallel execution4. Work sharing5. Data storage attributes6. Synchronization7. Nested parallelism8. Using third party libraries Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication Sequential version: pseudocode for i := 1 to n dofor j := 1 to n doC[i][j] := 0;for s := 1 to n doC[i][j] := C[i][j] + A[i][s] * B[s][j];end for;end for;end for; Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING  Matrix multiplication Sequential version: C implementation void matrix_mult (double a[N][N],double b[N][N],double c[N][N]){int i, j, s;for (i = 0; i < N; i ++){for (j = 0; j < N; j ++){c[i][j] = 0.0;for (s = 0; s < N; s ++)c[i][j] += a[i][s] * b[s][j];}}} File  matrix-seq.c . Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication PRAM CREW parfor i := 1 to n doparfor j := 1 to n doC[i][j] := 0;for s := 1 to n doC[i][j] := C[i][j] + A[i][s] * B[s][j];end for;end parfor;end parfor; Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication (AGAIN) Sequential version: pseudocode for i := 1 to n dofor j := 1 to n doC[i][j] := 0;for s := 1 to n doC[i][j] := C[i][j] + A[i][s] * B[s][j];end for;end for;end for; Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication Parallel version: C + pthread library Facts   Reasonable size of matrices ( n  ≥  64)   Thread creation is not free   Computing power comes from a CPU not from a thread   Computing system with small to medium number of CPUs Decision Thread  i   computes rows  i  , i   +  NT  , i   +  2 NT  , i   +  3 NT  , . . .  of theresult matrix  C  (NT — Number of Threads) Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING  Matrix multiplication Parallel version: C + pthread library (part I). File  matrix-pthreads.c . typedef struct {double (*a) [N];double (*b) [N];double (*c) [N];sem_t * s;int id;} thread_args;void * thread_proc (void * a){thread_args * arg = a;int i, j, s;for (i = arg->id; i < N; i += NT)for (j = 0; j < N; j ++){arg->c[i][j] = 0.0;for (s = 0; s < N; s ++)arg->c[i][j] += arg->a[i][s] * arg->b[s][j];}assert (sem_post (arg->s) == 0);return NULL;} Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication Parallel version: C + pthread library (part II) void matrix_mult (double a [N][N], double b[N][N], double c[N][N]){int i;thread_args ta[NT];sem_t s;pthread_t p;assert (sem_init (&s, 0, 0) == 0);for (i = 0; i < NT; i ++){thread_args x = { a, b, c, &s, i};ta[i] = x;assert (0 == pthread_create (&p, NULL, thread_proc, ta + i));}for (i = 0; i < NT; i ++)assert (sem_wait (&s) == 0);} . Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication Parallel version: C + OpenMP void matrix_mult (double a[N][N],double b[N][N],double c[N][N]){int i, j, s;#pragma omp parallel for private(j, s)for (i = 0; i < N; i ++){for (j = 0; j < N; j ++){c[i][j] = 0.0;for (s = 0; s < N; s ++)c[i][j] += a[i][s] * b[s][j];}}} File:  matrix-omp.c Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Matrix multiplication Sequential version: C implementation void matrix_mult (double a[N][N],double b[N][N],double c[N][N]){int i, j, s;for (i = 0; i < N; i ++){for (j = 0; j < N; j ++){c[i][j] = 0.0;for (s = 0; s < N; s ++)c[i][j] += a[i][s] * b[s][j];}}} File:  matrix-seq.c Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING  OpenMP   A way to do multithreaded programming   Focus: parallel execution of loops   OpenMP defines  set of compiler directives  (small) set of subroutines  bindings for programming languages (Fortran, C, C++)   OpenMP extends, but does not modify, a base programminglanguage Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING OpenMP   Goals  Scalable parallel applications for shared memory computers  Portability of performance  Fast and gradual development of parallel versions of existingsequential code  Easy simultaneous maintenance of parallel and sequentialsource code  Development of new parallel applications from scratch   History  Nov 1996 — spec. ANSI X3H5  Oct 1997 — spec. 1.0 OpenMP (FORTRAN)  1998 — spec. 1.0 OpenMP (C/C++)  1999 — spec. 1.1 OpenMP (FORTRAN)  2006 — spec. 2.5 compilers available (FORTRAN, C, C++)  Feb 2008 — spec. 3.0 defined   OpenMP homepage:  www.openmp.org   OpenMP user group:  www.cOMPunity.org Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING OpenMP enabled compilers   Intel (Linux, MSWindows)  C++ compiler ver 9.x: spec. 2.5 OpenMP  FORTRAN compiler   Microsoft Visual Studio 2005, Visual .NET: spec. 2.0OpenMP (MSWindows)   GNU C++ ver. 4.2.0 (TBA)   Sun Studio ver 11 (Solaris)   PGF77 and PGF90 Compilers from The Portland Group, Inc.(PGI) (Intel Linux, Intel Solaris, Intel Windows/NT)   IBM XL Fortran and C (IBM AIX)   HP, SGI, Fujitsu Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING Execution of OpenMP programs void matrix_mult (double a[N][N],double b[N][N],double c[N][N]){int i, j, s;#pragma omp parallel for private(j, s)for (i = 0; i < N; i ++){for (j = 0; j < N; j ++){c[i][j] = 0.0;for (s = 0; s < N; s ++)c[i][j] += a[i][s] * b[s][j];}}} File:  matrix-omp.c Wojciech Mikanik, PhD CONCURRENT AND PARALLEL PROGRAMMING
Search
Tags
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks