OpenMP Examples
OpenMP Examples
https://computing.llnl.gov/tutorials/openMP/
#include <omp.h>
main () {
Serial code
.
.
.
Format:
#pragma
directive-name [clause, ...] newline
omp
Required for A valid OpenMP Optional. Clauses Required.
all OpenMP directive. Must can be in any order, Precedes the
C/C++ appear after the and repeated as structured block
directives. pragma and before necessary unless which is enclosed
any clauses. otherwise restricted. by this directive.
Example:
structured_block
Notes:
When a thread reaches a PARALLEL directive, it creates a team of
threads and becomes the master of the team. The master is a member of
that team and has thread number 0 within that team.
Starting from the beginning of this parallel region, the code is
duplicated and all threads will execute that code.
There is an implied barrier at the end of a parallel section. Only the
master thread continues execution past this point.
If any thread terminates within a parallel region, all threads in the team
will terminate, and the work done up until that point is undefined.
main () {
}
OpenMP Exercise 1
Getting Started
Overview:
https://computing.llnl.gov/tutorials/openMP/exercise.html
OpenMP Exercise
https://computing.llnl.gov/tutorials/openMP/exercise.html
Exercise 1
1. Login to the workshop machine
Workshops differ in how this is done. The instructor will go over this
beforehand.
C: cp /usr/global/docs/training/blaise/openMP/C/*
~/openMP
https://computing.llnl.gov/tutorials/openMP/exercise.html
/
**********************************************************************
FILE: omp_hello.c Hello world
* DESCRIPTION:
* OpenMP Example - Hello World - C/C++ Version
* In this simple example, the master thread forks a parallel region.
* All threads in the team obtain their unique thread number and
print it.
* The master thread only prints the total number of threads. Two
OpenMP
* library routines are used to obtain the number of threads and each
* thread's number.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 04/06/05
**********************************************************************
********/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
Using your choice of compiler (see above section 4), compile your hello world
OpenMP program. This may take several attempts if there are any code
errors. For example:
gcc -fopenmp omp_hello.c -o hello
EXAMPLE 2 – workShare1
/*********************************************************************
* FILE: omp_workshare1.c Loop work-sharing
* DESCRIPTION:
* OpenMP Example - Loop Work-sharing - C/C++ Version
* In this example, the iterations of a loop are scheduled
dynamically
* across the team of threads. A thread will perform CHUNK
iterations
* at a time before being scheduled for the next CHUNK of work.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 04/06/05
**********************************************************************
********/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define CHUNKSIZE 10
#define N 100
/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
chunk = CHUNKSIZE;
EXAMPLE 3 - workShare2
/*********************************************************************
* FILE: omp_workshare2.c
* DESCRIPTION:
* OpenMP Example - Sections Work-sharing - C Version
* In this example, the OpenMP SECTION directive is used to assign
* different array operations to each thread that executes a SECTION.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 07/16/07
**********************************************************************
/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define N 50
/* Some initializations */
for (i=0; i<N; i++) {
a[i] = i * 1.5;
b[i] = i + 22.35;
c[i] = d[i] = 0.0;
}
} /* end of sections */
printf("Thread %d done.\n",tid);
EXAMPLE
/*********************************************************************
* FILE: omp_mm.c Matrix multiply
* DESCRIPTION:
* OpenMp Example - Matrix Multiply - C Version
* Demonstrates a matrix multiply using OpenMP. Threads share row
iterations
* according to a predefined chunk size.
* AUTHOR: Blaise Barney
* LAST REVISED: 06/28/05
**********************************************************************
/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
https://computing.llnl.gov/tutorials/parallel_comp/
Parallel Computing:
OPENMP
C Examples of Parallel Programming with OpenMP
https://people.sc.fsu.edu/~jburkardt/c_src/openmp/openmp.html
OpenMP Exercise
https://computing.llnl.gov/tutorials/openMP/exercise.html
http://www.ibm.com/developerworks/br/aix/library/au-aix-openmp-framework/#list2