In order to successfully complete this assignment you need to participate both individually and in groups during class. Have one of the instructors check your notebook and sign you out before leaving class. Turn in your assignment using D2L.
ICA 16: Debugging Parrallel Problems#

Image from: http://michaeljswart.com/2009/07/deadlocks-explained/
Agenda for today’s class (70 minutes)#
(20 minutes) Pre class Review
(20 minutes) Quiz2 Review
(20 minutes) Code Review
(10 minutes) Deadlock
1. Pre class Review#
✅ QUESTION: Discuss the importance of locks with your group and explain the difference between a lock and a critical section.
Summarize your discussion here
2. Quiz2 Review#
As a class we will review quiz 2.
3. Code Review#
Lets spend a few moments reviewing what we have learned about OpenMP by doing a code review of the following projects.
Getexample Example#
Download the openmp_exercise
getexample. Review the code in your groups and figure out what it is doing.
Disscuss in your groups and share with the class.
Pandemic Example#
Review the Pandemic code in your groups and figure out what it is doing.
Disscuss in your groups and share with the class.
Race Condition Example#
Try to find and suggest a fix for the race condition in the following code.
Example from: ofloveandhate/adv_sci_comp_spring2017
#include <omp.h>
#include <vector>
#include <iostream>
int main()
{
int counter = 0;
int size = 1000;
#pragma omp parallel for
for (int ii=0; ii<size; ++ii)
{
if (ii%2)
++counter;
}
std::cout << counter << '\n';
return 0;
}
4. Deadlock#
Try to find and suggest a fix for the deadlock in the following code.
Example from: ofloveandhate/adv_sci_comp_spring2017
#include <omp.h>
#include <vector>
#include <iostream>
// uses unspecified name for critical region.
void foo()
{
#pragma omp parallel
#pragma omp critical
{
auto id = omp_get_thread_num();
std::cout << "o,hai there, i'm " << id << '\n';
}
}
// goes with foo above.
// uses an unspecified name for critical region.
void deadlockA()
{
int sum = 0;
#pragma omp parallel for
for (int ii=0; ii<100; ++ii)
{
#pragma omp critical
{
sum += ii;
foo();
}
}
}
// slightly different than A. causes deadlock
// even when using names for the critical sections
void deadlockB()
{
#pragma omp parallel
{
#pragma omp critical(A)
{
#pragma omp critical(B)
{
std::cout << "whassup\n";
}
}
#pragma omp critical(B)
{
#pragma omp critical(A)
{
std::cout << "ahoy\n";
}
}
}
}
int main()
{
deadlockB();
return 0;
}
Congratulations, we’re done!#
Have one of the instructors check your notebook and sign you out before leaving class. Turn in your assignment using D2L.
Written by Dr. Dirk Colbry, Michigan State University (Updated by Dr. Nathan Haut in Spring 2025)
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.