The serial version of our program to compute PI, written in C, is displayed below:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const double pi = 3.1415926535897932385;
int main( int argc, char *argv[] ) {
/* Arguments required for executing serialPi: */
/* 0 -> serialPi */
/* 1 -> numberOfIntervals */
int numberOfIntervals, interval;
double intervalWidth, intervalMidPoint, area = 0.0;
/* The number of intervals is a command line argument. */
numberOfIntervals = atoi( argv[1] );
/* Compute the interval width. */
intervalWidth = 1.0 / numberOfIntervals;
/* Now compute the area. */
for ( interval = 0; interval < numberOfIntervals; interval++ ) {
intervalMidPoint = (interval + 0.5) * intervalWidth;
area += 4.0 / ( 1.0 + intervalMidPoint*intervalMidPoint );
}
area *= intervalWidth;
/* Print the results. */
printf( "The computed value of the integral is %.15f\n", area );
printf( "The exact value of the integral is %.15f\n", pi );
return 0;
}
Note: If you did not copy example programs from HPC's account in the initial section of this workshop, then copy the above source code as serialPi.c to your SerialPi directory.
Exercise: Do this!
To compile the serialPi program, at your shell prompt in the SerialPi directory, do:
[agopu@bh2 agopu]$ cd ~/MPI_Tutorial/SerialPi/ [agopu@bh2 SerialPi]$ icc -o serialPi serialPi.c
Then to run the program on interactive nodes you got through qsub -I (bcXX), do:
[agopu@bc81 agopu]$ cd ~/MPI_Tutorial/SerialPi/ [agopu@bc81 SerialPi]$ ./serialPi 20000 The computed value of the integral is 3.141592654423135 The exact value of the integral is 3.141592653589793
Since the command line argument used above is 20000, the domain would have been divided into 20,000 intervals.
| Previous: A Naturally Parallelizable... | Up: Table of Contents | Next: Computing PI using Parallel Code |
|---|