Please read full question before answering. I keep
getting reposts of wrong answer.
2. Summing up to π
(50 pts.)
Write a parallel program pie.c in C or C++ (pie.cc) for Linux that
computes an approximation of the
number π using a series with N+1 terms. The series sum is
partitioned in T non-overlapping partial
sums, each computed by T separate child processes created with the
fork() library function.
This program demonstrates data parallelism and interprocess
communication using pipes. Each child
process could perform a (potentially) long computation on a
separate CPU (or core). Depending on
the computer architecture, the entire computation could reach a
speedup close to T.
Numbers N and T are passed from the shell to the pie program as
command line parameters. The
main(argc, argv[]) function call gets these parameters (T, N) in
argv[1] and argv[2] as strings,
respectively. (Element argv[0] contains the program name and we
don’t need it.)
The first parameter, N, is the upper limit for i in the formula
above. The second parameter, T, is
the number of child processes that compute partial sums. N should
always be greater than T, N>T.
Otherwise the program should display an error message and call
exit(1).
Use the Nilakantha approximation formula for π
(http://en.wikipedia.org/wiki/Pi):
π = 3 + 4/(2*3*4) – 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + …. +
k*4/((2*i)* (2*i+1)*( 2*i+2))+...
where k = 1 if i is odd and k = -1 if i is even and i goes from 1
to N. The program can be run like
this from the shell:
./pie N T
For instance,
./pie 100 4
This command computes the approximation with 101 terms (starting
with term 3) with 4 child
processes.
Here is a description of the algorithm to be implemented.
The parent process iterates with an index variable of type int
called j from 0 to T (0≤j<T). During
iteration with index j, the parent process ...
1. creates a pipe associated with child process with index j
2. creates a child process with fork()
3. writes the binary values of N, T, and j to the pipe
After ending the loop (above) that created the T child
processes, the parent process starts a new
loop, with index j from 0 to T (0≤j<T). In iteration with index
j, the parent process reads from
the pipe associated with child j the value of the partial sum
computed by the child and adds it to
an accumulator double variable.
After the second loop ends, the parent process:
1. displays the sum approximating the value of π stored in the
accumulator, with a message that
reads like:
2. The
approximation of pi with N=%d and T=%d processes is %f.
(this is a C printf format string, make sure the code displays the
actual numbers!)
3. waits for all child processes to end
4. exits with code 0.
The child process with index j runs its code in a function
called computePartialSum. In this
function the child process does this:
1. closes any unnecessary file descriptors (which ones?)
2. reads the values for N,T, and j from the pipe that were written
by the parent process
3. computes the partial sum of the series (as described
below)
4. writes the partial sum to the pipe
5. calls exit(0).
NOTE: for this homework the child process should NOT rely on global
variables to access N, T, and
j.
These values must be read from the parent process using a pipe.
Doing otherwise is considered a
mistake and points will be cut.
For example, if the user runs command
./pie 100 4
then :
child process #0 computes the partial sum for i going from 1 to
25 child process #1 computes the
partial sum for i going from 26 to 50 child process #2 computes the
partial sum for i going from 51
to 75 child process #3 computes the partial sum for i going from 76
to 100
Important:
1. A child process with index j (with 0≤j<T) will
compute a partial sum for i going from N*j/T+1
to (N/T)*(j+1). Note that * and / are used on ints.
2. Make sure you check for errors when making library calls, like
fork(), pipe(), etc.
Here are some suggestions:
- first write a program that computes the
approximation of π with one for loop, with no
multiprocessing (no forks), and make sure first this approximation
code works
- don't forget the term 3 from the formula
- after the completion of the loop, the parent
process should wait for all processes to complete
using
waitpid().
Please read full question before answering. I keep getting reposts of wrong answer. 2. Summing up to π (50 pts.) Write a
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am