Link to this document's Jupyter Notebook

In order to successfully complete this assignment you must do the required reading, watch the provided videos and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on Tuesday March 16. Students must come to class the next day prepared to discuss the material covered in this assignment.


Pre-Class Assignment: Parallel Python

Goals for today's pre-class assignment

  1. Matrix Multiply Example
  2. Parallel Python example
  3. The Python GIL (Global Interface Lock))
  4. Getting around the GIL
  5. Assignment wrap up

1. Matrix Multiply Example

The following is a simple implementation of a matrix multiply written in python. Review the code try to understand what it is doing.

Lets compare this to the numpy result:

For this example, numpy result are most likely slower than the simple result. Think about why this might be. We will discuss this later.

DO THIS: See if you can write a loop to do a scaling study for the above code. Loop over the value of $n$ such that $n$ is 4, 16, 32, 64, 128 and 256. For each iteration generate two random matrices (as above) with $m = d = n$. Then time the matrix multiply for the provided function and again for the numpy function. Graph the results as size of $n$ vs time.

DO THIS: Explore the Internet for ways to speed up Python (There are a lot of them). Save some of your search results in the cell below and come to class prepaired to discuss what you found.

Put your search results here.


2. Parallel Python example

Here is an example for running parallel python using the multiprocessing library.

https://stackoverflow.com/questions/10415028/how-can-i-recover-the-return-value-of-a-function-passed-to-multiprocessing-proce

Lets try to make a parallel matrix multiply

The following is the instructor's attempt at using multiprocessing to do matrix multiply. First lets start with a serial method.

Lets compare this to the numpy result:

On some systems the numpy result may be slower than the simple result. Think about why this might be. We will discuss this later.

Now lets use multiprocessing to try and do a parallel method

QUESTION: Why do you think the parallel version was so much slower than Python?

Put your answer to the above question here.


3. The Python GIL (Global Interface Lock)

DO THIS: Read the following blog post and answer the questions: https://wiki.python.org/moin/GlobalInterpreterLock

QUESTION: Why was the GIL introduced to the Python programming language?

Put your answer to the above question here.

QUESTION: How does the GIL help avoid race conditions?

Put your answer to the above question here.

QUESTION: How does the GIL help avoid deadlock?

Put your answer to the above question here.

QUESTION: Why is the GIL problematic to parallel libraries like the "thread" and "multiprocessing" libraries?

Put your answer to the above question here.


4. Getting around the GIL

Fortunately there are ways to get around the GIL. In fact, Python has libraries that do shared memory parallelization, shared network parallelization and GPU acceleration. Do some research and answer the following questions:

QUESTION: Some of numpy library can run in parallel. How does numpy get around the GIL?

Put your answer to the above question here.

QUESTION: The numba library can also run in parallel. How does numba get around the GIL?

Put your answer to the above question here.

QUESTION: What python library can be used to program GPUs?

Put your answer to the above question here.

QUESTION: What python library can be used to run shared network parallelization such as the Message Passing Interface (MPI)?

Put your answer to the above question here.

QUESTION: There seem to be a lot of solutions for running Python in parallel. Provide an argument(s) as to why you would bother with an "older" language such as C/C++ or Fortran?

Put your answer to the above question here.


5. Assignment wrap-up

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credits for the assignment!

Direct Link to Google Form

If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.

Put your answer to the above question here

QUESTION: Summarize what you did in this assignment.

Put your answer to the above question here

QUESTION: What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

Put your answer to the above question here

QUESTION: How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

Put your answer to the above question here

QUESTION: What was the most challenging part of this assignment for you?

Put your answer to the above question here

QUESTION: What was the least challenging part of this assignment for you?

Put your answer to the above question here

QUESTION: What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?

Put your answer to the above question here

QUESTION: Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer to the above question here

QUESTION: Approximately how long did this pre-class assignment take?

Put your answer to the above question here


Congratulations, we're done!

To get credit for this assignment you must fill out and submit the above survey from on or before the assignment due date.

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.