Exercise 1

Overview

1. Login to the workshop machine

Workshops differ in how this is done. The instructor will go over this beforehand.

2. Copy the example files

  1. In your home directory, create a subdirectory for the example codes and then cd to it.
     mkdir openMP
     cd  openMP 
    
  2. Then, copy either the Fortran or the C version of the parallel OpenMP exercise files to your openMP subdirectory:

    C:

     cp  /usr/global/docs/training/blaise/openMP/C/*  ~/openMP
    

    Fortran:

     cp  /usr/global/docs/training/blaise/openMP/Fortran/*  ~/openMP
    

3. List the contents of your OpenMP subdirectory

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
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

4. Compilers - What’s Available?

  1. Visit the Compilers at LC webpage.

  2. You can also view the available compilers in the Compilers section of the Linux Clusters Overview tutorial.

  3. 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)”.

5. Create, compile and run an OpenMP “Hello world” program

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:

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:

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.

5. Notes:

6. Vary the number of threads and re-run Hello World

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.