The material developed for this lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this “credits” note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e- mail to: perrone[at]bucknell[dot]edu
Copy the following C source code to your local working directory for this lab:
~cs315/Labs/Lab3/summation.c
This program requires two command line arguments to execute:
The task performed by this code is simple: It creates the specified number of threads and assigns to each one of them an interval of integer numbers, say [a,b). Each thread computes a value s, which is the summation of the squares of the integers in its interval, that is:
a^2 + (a+1)^2 + (a+2)^2 + … + (b-2)^2 + (b-1)^2
Essentially, this program “parallelizes” the computation of the summation of the squares of integers in the interval [0, numthreads*increment) balancing the load equally among the number of threads specified. We say “parallelizes” in quotes because in a single-core machine, the threads would execute concurrently but not in parallel (remember the difference) splitting the time of one single CPU. When this code runs on a multi-core machine the threads may really run in parallel (this depends on the set up of the library and the OS).
IMPORTANT: The code given to you in the file summation.c is likely to compile with warnings. It is your task to ensure to you eliminate these warnings in the files you will generate using this code as starting point!
In order to compile and link this program, you cannot simply do
$ gcc -std=gnu99 -Wall summation.c -o summation
It turns out that this program uses functions that are defined in libraries that are not being linked with your code by default. The two libraries in question, for this program, are the math library (which, among many other functions, defines pow(3)) and the pthread library (which defines all the data types and functions for POSIX threads).
Read the manual page of pow(3) and find any text that says “link with…” Pass the flag you find to gcc, when compiling the code. (By the way, this is a linker flag!) This links the math library with your program.
In order to put this flag to use, you should simply append that little bit of text to the invocation of gcc above.
Now, append to the last invocation of gcc you used the flag -lpthread – the -l is telling gcc this is a linker flag, the pthread names the library to link. If all went as expected, you should now have an executable to run.
Create a rule in your Makefile to compile summation.c into an executable. This rule should be activated when one calls make without any command line arguments.
When you are done with this, you need to:
Run your summation executable few times, passing different values of number of threads and increment. Start with two threads and a somewhat large increment. Then keeping the increment value fixed, increase the number of threads to see how things might change.
Read the source summation.c carefully and do your best to understand how it works. Now, respond to the questions below in your lab3.txt file, writing as concisely and clearly as possible.
When you are done with this, you need to:
Copy your summation.c to a new file summation3.c, which will compute the time it takes to execute to completion. To accomplish this, you should instrument the program to call gettimeofday(2) right when starts and then again when it ends.
With a little bit of arithmetic, using the two struct timeval values returned, you can compute the time it took the program to run. Be careful to do the right arithmetic when you subtract two time values. Have this time printed to the standard output exactly before the program terminates.
Update your Makefile so it will compile summation3.c properly.
When you are done with this, you need to:
Copy your summation3.c to a new file summation4.c. In this new version, each thread will compute the time that it takes to execute individually and print the result to the standard output. Furthermore, have each thread print to the standard output the current time before it returns to the main thread. Note that you will have to use a combination of gettimeofday(2) and ctime(3) to produce a human readable date.
Update your Makefile so it will compile summation4.c properly.
Additionally, write in lab3.txt the responses to the following questions.
When you are done with this, you need to:
Before turning in your work for grading, create a text file in your Lab 3 directory called submission.txt. In this file, provide a list to indicate to the grader, problem by problem, if you completed the problem and whether it works to specification. Wrap everything up by turning in this file:
Note: [up to -10 points] An incorrect or incomplete Makefile to build all programs in lab and pre-lab.