2. Summing up to TT (50 pts.) Write a parallel program pie.c in Cor C++ (pie.cc)
ID: 3845123 • Letter: 2
Question
2. Summing up to TT (50 pts.) Write a parallel program pie.c in Cor C++ (pie.cc) for Linux that computes an approximation of the number Tu 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 fork0library 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), thus, the entire computation could reach a speedup close to T. Numbers N and Tare passed from the shell to the pie program as command line parameters. The main(argc, argvll) function call gets these parameters T, N) in argvlil and argv12] as strings, respectively. (Element argvloj 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,Tis the number of child processes that compute partial sums. N should always be greater than N T otherwise the program should display an error message and call exit(1) Use the Nilakantha approximation formula for Tu (htt n wiki wiki/Pi ia 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+20)+. 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 NT 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 toT(0sj*). During iteration with index j, the parent pr 1. creates a pipe associated with child process with index j 2. creates a child process with fork0 3. writes the values N, T and j to the pipeExplanation / Answer
int main(int argc, char *argv[]) {
if (argc != 1) {
printf("Program only takes one argument - nth tern in the Nilakantha series to use");
return -1;
}
int precision = (int) argv[0]
if (precision <= 0) {
printf("%s", "Non-negative input required");
return -1;
}
double sum = 3;
int i = 1;
for (i <= precision; i++) {
sum += nthNilakanthaNum(i);
}
}
double nthNilakanthaNum(int n) {
double denominator = 2 * n;
int i
for (i = 0; i < 2; i++) {
denominator *= denominator + 1;
}
double value = 4 / denominator;
if ((n & 1) ^ 1) { //n is odd
return value * -1;
}
return value;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.