CS31 In-class Exercise: parallel max

parallel max: writing a pthread program
Find the other students in your group of 3 students (we hope one of whom has a laptop).
The number on the back of the page is your group number

In your group you are going to do the following:

  1. First come up with an algorithm for parallelizing finding the max value in an array of size N using M threads. Assume that you are starting with an already initialized array of N values, and you can start out assuming that M evenly divides N (the case when it doesn't is an extention to try after you get this case working).

    Some questions to think about as you determine how to parallelize computing max:

    • what part of the cumulative operation does each thread do?
    • how does a thread know which work it will do?
    • do threads need to coordinate/synchronize their actions in some way? if so, when and how and how frequently?
    • what are the limits on concurrency?
    • what global state will you need? what local state?
    Make sure that every group member gets a chance to participate in answering these questions. Every group member should understand your group's solution before moving on. This includes asking questions about specific parts of your group's solution you don't understand, and taking care that everyone in your group can explain the ideas and details of your solution. It also means that after you suggest a solution to some part, you should then take time for your group members to ask questions about it and/or to suggest alternatives that you consider as a group.

  2. when you are happy with your psuedo-code algorithm talk your algorithm through with a professor or ninja before moving on to the next step

  3. Together, try implementing your algorithm in pthreads.
    • ssh into your cs account
      (if you have a Windows laptop, you need to install and use some software to ssh. see: CS Lab help about remote access using ssh):
      ssh you@lab.cs.swarthmore.edu
      

    • Once ssh'ed in, copy over some starting point code into cs31/inclass/max
      cd cs31/inclass
      cp -r ~newhall/public/cs31/inclass/max .
      cd max
      ls
      make
      
      To run:  ./max N M      
      example: ./max 10000 16
      
    • There is starting point code in the max.c file to get the command line arguments and to initialize an array of N values to random long values.

    • You should add all the pthread code to implement your parallel max algorithm.

    • Use the hello.c program as an example to help you with pthread functions and syntax

      You can also use hello.c as an example to see thread exectution info from top. (uncomment the section of its code with a long running loop to make it run longer first).
      Run ./hello 16 in one terminal and top -H in another to see the threads executing on different cores.

    • look at the man pages for pthread functions:
      man pthread_create
      man pthread_join
      man pthread_mutex_lock
      

    • look at the weekly lab page for other pthread examples

  4. Before the end of class, share your joint solution with your team mates. Here is one way to do this from the cs machine you are ssh'ed into:
     
    % mail username1 < max.c
    % mail username2 < max.c
    % mail username3 < max.c
    
    Another is to use scp:
    scp max.c username1@cs.swarthmore.edu:.          
    # username1 needs to enter his/her password
    
    Then team mates can mv max.c from her/his homedirectory into their cs31/inclass/12 subdirectory after they copy this over from my public directory.

Extentions

If you get your solution to this working and debugged, think about a slightly different version of this problem that does two things:
  1. compute max in paralllel when the number of threads, M, does not evenly divide the number of elements in the array, N.
  2. compute the number of times the max value occurs in the array (also in parallel).
Your solution should only spawn off worker threads one time and only join them when all parallel computation from both parts is done.