|
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:
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_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 |
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 less than a second.
C: | xlc_r -q64 -O2 -qsmp=omp omp_hello.c -o hello |
Fortran: | xlf_r -q64 -O2 -qsmp=omp omp_hello.f -o hello |
setenv OMP_NUM_THREADS 4
hello
Hello World from thread = 0 Number of threads = 4 Hello World from thread = 3 Hello World from thread = 1 Hello World from thread = 2 |
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.
C: | xlc_r -q64 -O2 -qsmp=omp omp_workshare1.c -o workshare1
workshare1 | sort |
Fortran: | xlf_r -q64 -O2 -qsmp=omp omp_workshare1.f -o workshare1
workshare1 | sort |
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: | xlc_r -q64 -O2 -qsmp=omp omp_workshare2.c -o workshare2
workshare2 |
Fortran: | xlf_r -q64 -O2 -qsmp=omp 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: | xlc_r -q64 -O2 -qsmp=omp omp_orphan.c -o orphan
orphan | sort |
Fortran: | xlf_r -q64 -O2 -qsmp=omp omp_orphan.f -o orphan
orphan | sort |
This example performs a matrix multiple by distributing the iterations of the operation between available threads.
C: | xlc_r -q64 -O2 -qsmp=omp omp_mm.c -o matmult
matmult |
Fortran: | xlf_r -q64 -O2 -qsmp=omp 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?
NOTE: IBM implements all of the necessary Fortran functions as integer instead of logical as the standard specifies.
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.
Compile each code as you did with the previous examples (C or Fortran).
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. | |
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 | Wrong result |
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?