To get more experience with pthreads, and also to use timing functions in C.
An important part of parallel programming is load balancing. Ideally, each thread will spend the same amount of time working as each of the others. For this lab, you will analyze the load balance between the threads of the π estimation program.
To do this, simply have each thread time the amount of time it takes to execute. In order to figure out how long a thread takes, we will need to get the time at two points, once at the start of the thread and once at the end. Then we can subtract the two values to find the elapsed time.
time() function unfortunately has only second resolution.
In order to have more accurate numbers, we will find the number milliseconds
since the epoch, which is January 1st, 1970.
To do this, first include the header file
Next create an object of type
struct timeval current;
Then pass this object into the function
The second parameter is for passing time zone information, and should be NULL.
The timeval structure contains two fields,
tv_usec which contain the number of seconds and
To get the number of milliseconds, we can use the following:
unsigned long ms = (current.tv_sec * 1000) + (current.tv_usec / 1000);
When you're finished, submit both the source code and the analysis (in a text document of some kind, or as a comment) to Canvas.
Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.