Tutorials | Exercises | Abstracts | LC Workshops | Comments | Search | Privacy & Legal Notice


POSIX Threads Programming Exercise

  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

    In your home directory, create a pthreads subdirectory for the example codes, copy the example codes and then cd into your pthreads subdirectory and cd to it. Then copy the Pthreads example codes.

    mkdir pthreads 
    cp  /usr/global/docs/training/blaise/pthreads/*   ~/pthreads
    cd  pthreads
  3. List the contents of your pthreads subdirectory

    You should notice quite a few files. The table below lists and describes these files.

    File Name Description
    arrayloops.c
    arrayloops.f
    Data decomposition by loop distribution. Fortran example only works under IBM AIX: see comments in source code for compilation instructions.
    condvar.c
    
    Condition variable example file. Similar to what was shown in the tutorial
    detached.c
    Demonstrates how to explicitly create pthreads in a detached state.
    dotprod_mutex.c
    dotprod_serial.c
    Mutex variable example using a dot product program. Both a serial and pthreads version of the code are available.
    hello.c
    Simple "Hello World" example
    hello32.c
    "Hello World" pthreads program demonstrating thread scheduling behavior.
    hello_arg1.c
    One correct way of passing the pthread_create() argument.
    hello_arg2.c
    Another correct method of passing the pthread_create() argument, this time using a structure to pass multiple arguments.
    join.c
    Demonstrates how to explicitly create pthreads in a joinable state for portability purposes. Also shows how to use the pthread_exit status parameter.
    mpithreads_both.c
    mpithreads.makefile
    mpithreads_mpi.c
    mpithreads_serial.c
    mpithreads_threads.c
    
    A "series" of programs which demonstrate the progression for a serial dot product code to a hybrid MPI/Pthreads implementation. Files include the serial version, Pthreads version, MPI version, hybrid version and a makefile.
    bug1.c
    bug1fix.c
    bug2.c
    bug2fix.c
    bug3.c
    bug4.c
    bug4fix.c
    bug5.c
    bug6.c
    bug6fix.c
    
    Examples with bugs.


  4. Review and compile hello.c

    1. Begin with the simplest exercise code. After reviewing and understanding what hello.c does, use the thread-safe, IBM C compiler to compile the source code:
      xlc_r -q64 -O2 -o hello hello.c
    2. Run the hello executable and notice its output.

      hello

      A few notes:

      • The _r (thread-safe) version of the IBM xlc compiler is being invoked, however at LC the xlc compiler command is also thread safe and can be used.
      • The IBM compiler does not need any special compile/link flags for pthreads - however most other compilers do.

  5. Thread Scheduling

    1. Review the example code hello32.c. Note that it will create 32 threads. A sleep(); statement has been introduced to help insure that all threads will be in existence at the same time. Also, each thread performs actual work to demonstrate how the AIX scheduler behavior determines the order of thread completion.

    2. Compile and run the program. Notice the order in which thread output is displayed. Is it ever in the same order? How is this explained?

  6. Argument Passing

    1. Review the hello_arg1.c and hello_arg2.c example codes. Notice how the single argument is passed and how to pass multiple arguments through a structure.

    2. Compile and run both programs.

  7. Review, compile and run the other Pthreads example codes

    Try these other pthreads codes before moving on to the mpithreads or bugX.c sets of codes. Compile them as you did for hello.c. Some of these are similar to what was shown in the tutorial and some aren't.

  8. When things go wrong...

    Part of the learning process is to see what happens when things go wrong. There are many things that can go wrong when developing pthreads programs. The bugX.c series of programs demonstrate just a few. See if you can figure out what the problem is with each case and then fix it.

    Use xlc_r -q64 -O2 to compile each code as appropriate.

    The buggy behavior will differ for each example. Some hints are provided below.

    Code Behavior Hints/Notes
    bug1.c
    bug1fix.c
    Hangs
    bug2.c
    bug2fix.c
    Seg fault/coredump
    bug3.c Wrong answers
    bug4.c
    bug4fix.c
    Hangs (usually)
    bug5.c Threads die and never get to do their work
    bug6.c
    bug6fix.c
    Wrong answer - run it several times to prove this

  9. Try the mpithreads series of codes

    1. Your pthreads directory should contain the following 5 codes:

      mpithreads_serial.c
      mpithreads_threads.c
      mpithreads_mpi.c
      mpithreads_both.c
      mpithreads.makefile

      These codes implement a dot product calculation and are designed to show the progression of developing a hybrid MPI / Pthreads program from a a serial code. The problem size increases as the examples go from serial, to threads/mpi to mpi with threads.

      Suggestion: simply making and running this series of codes is rather unremarkable. Using the available lab time to understand what is actually happening is the intent. The instructor is available for your questions.

    2. As time permits, review each of the codes. (The order of the listing above shows the "progression").

    3. Use the provided makefile to compile all of the codes at once:

      make -f mpithreads.makefile

      Be sure to examine the makefile so that you are familiar with the actual compiler commands used.

    4. Run each of the codes. Note: in order to run the two codes which use MPI (mpithreads_mpi and mpithreads_both), you must first have the necessary POE environment variables set, as shown below.

      Environment Variable Setting Description
      setenv MP_PROCS 4
      The number of MPI tasks will be 4
      setenv MP_NODES 4
      Only one MPI task per node. For the mpithreads_both example, the threads will utilize the extra cpus.
      setenv MP_RMPOOL pbatch
      Set it to the workshop node pool


This completes the exercise.

Evaluation Form       Please complete the online evaluation form if you have not already done so for this tutorial.

Where would you like to go now?


https://computing.llnl.gov/tutorials/pthreads/exercise.html
Last Modified: 09/13/2007 15:33:00 blaiseb@llnl.gov
UCRL-MI-133316