layout | title | release_number | author | tutorial |
---|---|---|---|---|
tutorial_page |
Exercise 1 |
UCRL-MI-133316 |
Blaise Barney, Lawrence Livermore National Laboratory |
OpenMP |
- Login to the workshop cluster using your workshop username and OTP token
- Copy the exercise files to your home directory
- Familiarize yourself with LC's OpenMP environment
- Write a simple "Hello World" OpenMP program
- Successfully compile your program
- Successfully run your program
- Modify the number of threads used to run your program
Workshops differ in how this is done. The instructor will go over this beforehand.
- In your home directory, create a subdirectory for the example codes and then cd to it.
mkdir openMP
cd openMP
- Then, copy either the Fortran or the C version of the parallel OpenMP exercise files to your openMP subdirectory:
cp /usr/global/docs/training/blaise/openMP/C/* ~/openMP
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.
{% raw %}
<style type="text/css"> .tg {border-collapse:collapse;border-spacing:0;} .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;} .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;} .tg .tg-oqcz{background-color:#869AC3;font-weight:bold;text-align:left;vertical-align:top} .tg .tg-0lax{text-align:left;vertical-align:top} </style>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 | |
ser_pi_calc.c | ser_pi_calc.f | Serial Pi calculation | |
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 |
{% endraw %}
-
Visit the Compilers at LC webpage.
-
You can also view the available compilers in the Compilers section of the Linux Clusters Overview tutorial.
-
Now, in your cluster login window, try the
module avail
command to display available compilers. You should see GNU, Intel and PGI compilers - several versions of each.- Question: Which version is the default version?
- Answer: Look for the "(D)".
1. Using your favorite text editor (vi/vim, emacs, nedit, gedit, nano...) open a new file - call it whatever you'd like.
2. Create a simple OpenMP program that does the following:
- Creates a parallel region
- Has each thread in the parallel region obtain its thread id
- Has each thread print "Hello World" along with its unique thread id
- Has the master thread only, obtain and then print the total number of threads
If you need help, see the provided omp_hello.c or omp_hello.f file.
3. 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:
<style type="text/css"> .tg {border-collapse:collapse;border-spacing:0;} .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;} .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;} .tg .tg-oqcz{background-color:#869AC3;font-weight:bold;text-align:left;vertical-align:top} .tg .tg-0lax{text-align:left;vertical-align:top} </style>C: | Fortran: |
---|---|
icc -openmp omp_hello.c -o hello pgcc -mp omp_hello.c -o hello gcc -fopenmp omp_hello.c -o hello |
ifort -openmp omp_hello.f -o hello pgf90 -mp omp_hello.f -o hello gfortran -fopenmp omp_hello.f -o hello |
When you get a clean compile, proceed.
4. Run your hello executable and notice its output.
- Is it what you expected? As a comparison, you can compile and run the provided omp_hello.c or omp_hello.f example program.
- How many threads were created? By default, the Intel and GNU compilers will create 1 thread for each core. The PGI compiler will create only 1 thread total.
5. Notes:
- For the remainder of this exercise, you can use the compiler command of your choice unless indicated otherwise.
- Compilers will differ in which warnings they issue, but all can be ignored for this exercise. Errors are different, of course.
Explicitly set the number of threads to use by means of the OMP_NUM_THREADS
environment variable:
setenv OMP_NUM_THREADS 8
Your output should look something like below.
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
Run your program several times and observe the order of print statements. Notice that the order of output is more or less random.