Link to this document's Jupyter Notebook

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 22. If you are attending asynchronously, turn in your assignment using D2L no later than _11:59pm on Monday February 22.


In-Class Assignment: Debugging Parrallel Problems

cartoon picture showing two archers trying to borrow each others bow as a humourous example of deadlock. Ultimately one of them gets shot as a quote ultimately victim of deadlock.

Image from: http://michaeljswart.com/2009/07/deadlocks-explained/

Agenda for today's class (70 minutes)

  1. (20 minutes) Pre class Review
  2. (20 minutes) Quiz2 Review
  3. (20 minutes) Code Review
  4. (10 minutes) Deadlock

1. Pre class Review

0221--OMP_weeds_pre-class-assignment


2. Quiz2 Review


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: 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;
}

4. Deadlock

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

Congratulations, we're done!

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.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.