CS31 In-class Exercise: Week 12

parallel max: writing a pthread program
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.

    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?

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

  3. Together, try implementing your algorithm in pthreads (ssh into your cs account and run update31 to get the starting point code).

    ssh into your cs account:

    # MacOS: open a terminal and type:
    ssh you@lab.cs.swarthmore.edu
    
    On windows: need to use putty

    Once ssh'ed in, run update31, to get the starting point code in cs31/inclass/12

    update31
    cd cs31/inclass/12
    ls
    Makefile max.c
    
    To run:  ./max N M      
    example: ./max 10000 16
    
    There is already code in this 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 cs31/inclass/11/hello.c example to help you with pthread functions and syntax

    look at the man pages for pthread functions:

    man pthread_create
    man pthread_join
    man pthread_mutex_lock
    
  4. Before the end of class, share your joint solution with your team mates. Here is one way to do this:
     
    % mail username1 < max.c
    % mail username2 < max.c
    % mail username3 < max.c
    

    If you get your solution to this working and debugged, think about a slightly different version of this problem:
    1. compute max in paralllel
    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.