|
Workshops differ in how this is done. The instructor will go over this beforehand.
mkdir openMP cd openMP
C: | cp /usr/global/docs/training/blaise/openMP/C/* ~/openMP |
Fortran: | cp /usr/global/docs/training/blaise/openMP/Fortran/* ~/openMP |
You should notice the following files. Note: Most of these are simple example files. Their primary purpose is to demonstrate the basics of how to parallelize a code with OpenMP. Most execute in a second or two.
C Files | Fortran Files | Description | |
---|---|---|---|
omp_hello.c | omp_hello.f | Hello world | |
omp_workshare1.c | omp_workshare1.f | Loop work-sharing | |
omp_workshare2.c | omp_workshare2.f | Sections work-sharing | |
omp_reduction.c | omp_reduction.f | Combined parallel loop reduction | |
omp_orphan.c | omp_orphan.f | Orphaned parallel loop reduction | |
omp_mm.c | omp_mm.f | Matrix multiply | |
omp_getEnvInfo.c | omp_getEnvInfo.f | Get and print environment information | |
omp_dotprod_serial.c
omp_dotprod_openmp.c omp_dotprod_mpi.c omp_dotprod_hybrid.c |
omp_dotprod_serial.f
omp_dotprod_openmp.f omp_dotprod_mpi.f omp_dotprod_hybrid.f |
Demonstrates progression from a serial program, to openMP, to MPI and to a hybrid MPI+OpenMP program. Uses a dot product calculation for the example. | |
omp_bug1.c
omp_bug1fix.c omp_bug2.c omp_bug3.c omp_bug4.c omp_bug4fix omp_bug5.c omp_bug5fix.c omp_bug6.c |
omp_bug1.f
omp_bug1fix.f omp_bug2.f omp_bug3.f omp_bug4.f omp_bug4fix omp_bug5.f omp_bug5fix.f omp_bug6.f |
Programs with bugs |
Quartz | Cab | module avail gcc
module avail intel
module avail pgi
module avail clang
|
use -l gcc
use -l icc
use -l pgi
use -l clang
|
If you need help, see the provided or file.
Example compiles:
C: | icc -qopenmp omp_hello.c -o hello
gcc -fopenmp omp_hello.c -o hello
pgcc -mp omp_hello.c -o hello
clang -fopenmp omp_hello.c -o hello |
Fortran: | ifort -qopenmp omp_hello.f -o hello
gfortran -fopenmp omp_hello.f -o hello
pgf90 -mp omp_hello.f -o hello
|
This may take several attempts if there are any code errors. When you get a clean compile, proceed.
setenv OMP_NUM_THREADS 8
Hello World from thread = 0 Hello World from thread = 3 Hello World from thread = 2 Number of threads = 8 Hello World from thread = 6 Hello World from thread = 1 Hello World from thread = 4 Hello World from thread = 7 Hello World from thread = 5 |
This example demonstrates use of the OpenMP loop work-sharing construct. Notice that it specifies dynamic scheduling of threads and assigns a specific number of iterations to be done by each thread.
setenv OMP_NUM_THREADS 4
C: | icc -qopenmp omp_workshare1.c -o workshare1
workshare1 | sort |
Fortran: | ifort -qopenmp omp_workshare1.f -o workshare1
workshare1 | sort |
This example performs a matrix multiple by distributing the iterations of the operation between available threads.
C: | icc -qopenmp omp_mm.c -o matmult
matmult |
Fortran: | ifort -qopenmp omp_mm.f -o matmult
matmult |
matmult | sort | grep Thread
Do the loop iterations match the SCHEDULE(STATIC,CHUNK) directive for the matrix multiple loop in the code?
This example demonstrates use of the OpenMP SECTIONS work-sharing construct Note how the PARALLEL region is divided into separate sections, each of which will be executed by one thread.
C: | icc -qopenmp omp_workshare2.c -o workshare2
workshare2 |
Fortran: | ifort -qopenmp omp_workshare2.f -o workshare2
workshare2 |
This example computes a dot product in parallel, however it differs from previous examples because the parallel loop construct is orphaned - it is contained in a subroutine outside the lexical extent of the main program's parallel region.
C: | icc -qopenmp omp_orphan.c -o orphan
orphan | sort |
Fortran: | ifort -qopenmp omp_orphan.f -o orphan
orphan | sort |
OpenMP by itself is constrained to a single node. In the world of HPC, the motivation for using OpenMP is possible performance gains when combined with MPI. This example demonstrates how to begin with a serial code, and develop it into a hybrid OpenMP + MPI program. MPI is covered in the MPI tutorial.
omp_dotprod_serial.* omp_dotprod_openmp.* omp_dotprod_mpi.* omp_dotprod_hybrid.*
C: | icc omp_dotprod_serial.c -o dotserial
icc -qopenmp omp_dotprod_openmp.c -o dotopenmp mpicc omp_dotprod_mpi.c -o dotmpi mpicc -qopenmp omp_dotprod_hybrid.c -o dothybrid |
Fortran: | ifort omp_dotprod_serial.f -o dotserial
ifort -qopenmp omp_dotprod_openmp.f -o dotopenmp mpifort omp_dotprod_mpi.f -o dotmpi mpifort -qopenmp omp_dotprod_hybrid.f -o dothybrid |
dotserial
dotopenmp srun -n4 -ppReserved dotmpi srun -n4 -ppReserved dothybrid |
There are many things that can go wrong when developing OpenMP programs. The omp_bugX.X series of programs demonstrate just a few. See if you can figure out what the problem is with each case and then fix it.
The buggy behavior will differ for each example. Some hints are provided below.
Code | Behavior | Hints/Notes |
---|---|---|
omp_bug1
omp_bug1fix |
Fails compilation. Solution provided - must compile solution file. | |
omp_bug2 | Thread identifiers are wrong. Wrong answers. | |
omp_bug3 | Run-time error, hang. | |
omp_bug4
omp_bug4fix |
Causes a segmentation fault. Solution provided - note that it is a script and will need to be "sourced". For example: "source omp_bug4fix". Be sure to examine the solution file to see what's going on - especially the last line, where you may need to change the name of the executable to match yours. | |
omp_bug5
omp_bug5fix |
Program hangs. Solution provided - must compile solution file. | |
omp_bug6 | Failed compilation |
This completes the exercise.
![]() |
Please complete the online evaluation form if you have not already done so for this tutorial. |
Where would you like to go now?