In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class on Monday February 13.
Image from: http://michaeljswart.com/2009/07/deadlocks-explained/
Lets spend a few moments reviewing what we have learned about OpenMP by doing a code review of the following project.
Review the Pandemic code in your groups and figure out what it is doing.
Disscuss in your groups and share with the class.
Try to find and suggest a fix for the race condition in the following code.
Example from: https://github.com/ofloveandhate/adv_sci_comp_spring2017/tree/master/openmp/problems
#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;
}
Try to find and suggest a fix for the deadlock in the following code.
Example from: https://github.com/ofloveandhate/adv_sci_comp_spring2017/tree/master/openmp/problems
#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;
}
If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asynchronously, turn in your assignment using D2L.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.