Tuesday, 8 April 2014

Thread Programming Graphical Analysis

This post talks about the graphical analysis of thread programs using bash script and gnuplot.
You'll need to work on ubuntu for this.

Check if g++ is installed.
To check open the terminal (Ctrl+Alt+T) and try the following command:
$ g++ --version

If the output is something like:
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This means you have g++ installed, otherwise try the following command:
$ sudo apt-get install g++

Once you have installed the g++, then lets see how to compile and run threads programs using g++.
For illustration lets save pi with mutex thread program as pi_mutex.cpp
Make sure to make these changes in the pi_mutex program.

1) Replace all the headers with #include<bits/stdc++.h>

2) In the main() add this line
freopen ("output.txt","a",stdout);

3) Replace the cout statement with:
printf("%d %lf\n",thcnt,((double)t)/CLOCKS_PER_SEC);

Now in order to compile change the directory to where the program is stored and use the command:
$ g++ pi_mutex.cpp -lpthread

Here we are linking pthread library while compilation.

Then execute the program using the command:
$ ./a.out 1000 1000000000

This runs the program with 1000 threads and calculates upto 1000000000 terms.
You'll notice that a file output.txt has been made which contains the number of threads used and time taken to complete the execution.

Now we'll run the program by varying the number of threads and record the time of execution for each configuration.
For that save the following script as threads.sh

#!/bin/bash
g++ pi_mutex.cpp -lpthread
for (( c=1; c<=1000000; c=c*2 ))
do
    ./a.out $c 1000000000

done

This script runs the pi_mutex program by varying threads and the number of terms constant as 1000000000, while the c++ programs appends the execution time for each run in output.txt.

Before executing the script delete the output.txt file that was created earlier.
Now run this script using the command:
$ ./threads.sh

It might take a few minutes to complete the execution.

Now we need to plot the output graphically.
For that we will be using gnuplot.

Lets install gnuplot then by using the following command:
$ sudo apt-get install gnuplot

Once gnuplot is insatlled then enter the command 
$ gnuplot

Now follow the commands to plot the graph:

gnuplot> clear                                                                  
gnuplot> reset
gnuplot> unset key
gnuplot> set xtics rotate out
gnuplot> set style data histogram
gnuplot> set style fill solid border
gnuplot> set style histogram clustered                                          
gnuplot> plot for [COL=2:2] 'output.txt' using COL:xticlabels(1) title columnheader

Now the graph should appear which depicts the variation in running time by varying the number of threads.
Observe the pattern and think of the reason for such trends.

Try plotting other programs on similar lines.

By-
Tusshar Singh

3 comments:

  1. Hey..
    I m runnung ubuntu in vw ware..
    Do we req or have to do anything specific before the apt get command ??

    ReplyDelete
  2. 1. Before executing "./threads.sh" command make sure it has execute permissions. If not type the following command :-
    $ chmod +x threads.sh
    2. After executing the 'gnuplot' command, if there is a message :-
    terminal type set to 'unknown'
    So, fire up your terminal and enter this command :-
    $ sudo apt-get install gnuplot-x11

    ReplyDelete