Computer Science 400
Parallel Processing and High Performance Computing
Fall 2017, Siena College
In this lab, we will begin to consider another major paradigm for parallelism: multithreading. So far, we have been working with message passing. In that case, each process has its own private address space. That is, each process has its own variables and any information to be exchanged among processes needs to be done with explicit message passing. Multithreading usually allows for the use of shared memory. Many operating systems provide support for threads, and a standard interface has been developed: POSIX Threads or pthreads.
You may work alone or with a partner on this lab.
Getting Set Up
You will receive an email with the link to follow to set up your GitHub repository pthreads-yourgitname for this Lab. One member of the group should follow the link to set up the repository on GitHub, then that person should email the instructor with the other group members' GitHub usernames so they can be granted access. This will allow all members of the group to clone the repository and commit and push changes to the origin on GitHub. At least one group member should make a clone of the repository to begin work.
Readings
Before starting, read through Pacheco Sections 4.1 and 4.2.
pthreads Basics
The basic idea is that we can create and destroy threads of execution in a program, on the fly, during its execution. These threads can then be executed in parallel by the operating system scheduler. If we have multiple processors, we should be able to achieve a speedup over the single-threaded equivalent.
We start with a look at a pthreads "Hello, world" program, which is in the pthreadhello directory of your repository for this lab.
The most basic functionality involves the creation and destruction of threads:
Prototypes for pthread functions are in pthread.h and programs need to link with libpthread.a (use -lpthread at link time).
Any global variables in your program are accessible to all threads. Local variables are directly accessible only to the thread in which they were created, though the memory can be shared by passing a pointer as part of the last argument to pthread_create().
A slightly more interesting example is in proctree_threads.
This example builds a "tree" of threads to a depth given on the command line. It includes calls to pthread_self(). This function returns the thread identifier of the calling thread.
Try it out and study the code to make sure you understand how it works.
split_proc
function
for a tree of depth n? (3 points)Multithreaded Pi
pthread_pi.c
. See
below for some tips. (20 points)pthread_create
call. That thread
will be responsible for accumulating its own count in that
variable. Then the main thread will have all of those values in its
array when the threads have completed.
Submitting
Your submission requires that all required deliverables are committed and pushed to the master for your repository on GitHub.
Grading
This assignment is worth 35 points, which are distributed as follows:
> Feature | Value | Score |
pthreadhello-more.c | 10 | |
Lab Question 1 | 1 | |
Lab Question 2 | 3 | |
Lab Question 3 | 1 | |
pthread-pi.c | 20 | |
Total | 35 | |