Review, compile and run the dotprod_serial.c program. As its
name implies, it is serial - no threads are created.
Now review, compile and run the dotprod_mutex.c program. This
version of the dotprod program uses threads and requires a mutex
to protect the global sum as each thread updates it with their
Execute the dotprod_mutex program several times and notice that
the order in which threads update the global sum varies.
Run it several times and notice what the global sum is each
time? See if you can figure out why and fix it. The explanation
is provided in the bug examples table above, and an example
solution is provided by the bug6fix.c program.
The arrayloops.c program is another example of using a mutex to
protect updates to a global sum. Feel free to review, compile
and run this example code as well.
2. Condition Variables
Review, compile and run the condvar.c program. This example is
essentially the same as the shown in the tutorial. Observe the
output of the three threads.
Now, review, compile and run the bug1.c program. Observe the
output of the five threads. What happens? See if you can
determine why and fix the problem. The explanation is provided
in the bug examples table above, and an example solution is
provided by the bug1fix.c program.
The bug4.c program is yet another example of what can go wrong
when using condition variables. Review, compile (for gcc include
the -lm flag) and run the code. Observe the output and then see
if you can fix the problem. The explanation is provided in the
bug examples table above, and an example solution is provided by
the bug4fix.c program.
3. Hybrid MPI with Pthreads
Your pthreads directory should contain the following 5 codes:
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.
Review each of the codes. The order of the listing above shows
Use the provided makefile to compile all of the codes at once.
The makefile uses the gcc compiler - feel free to modify it and
use a different compiler.
make -f mpithreads.makefile
Run each of the codes and observe their output:
Serial version - no threads or MPI
Threads only version of the code using 8 threads
srun -n8 -ppReserved mpithreads_mpi
MPI only version with 8 tasks running on a single node in the special workshop pool
srun -N4 -ppReserved mpithreads_both
MPI with threads using 4 tasks running on 4 different nodes, each of which spawns 8 threads, running in special workshop pool
Lawrence Livermore National Laboratory
7000 East Avenue • Livermore, CA 94550 | LLNL-WEB-458451
Operated by the Lawrence Livermore National Security, LLC for the
Department of Energy's National Nuclear Security Administration
Learn about the Department of Energy's Vulnerability Disclosure Program