find attached the instructions of the assignment
1
Question 2 (Lecture 2): The purpose of this question is to practice MPI_Bcast. write a program
using MPI to accomplish the following:
1. Process0 generate a random number for variable n in the range 10 to 99 inclusive and
broadcast to all other processes.
Note that, since process0 is broadcasting, we should not include: MPI_Bcast in a conditional
statement such as ” if (my_rank == 0){…}”.
2. All the processes use the built-in function: malloc to allocate n consecutive locations for the
array variable: receive_vector.
3. Process0 generates n random integers in the range 0 to 99 inclusive and stores them into its
own array: receive_vector.
4. Process0 broadcast the content of its array: receive_vector to the receive_vector of all other
processes.
5. All the processes obtain and display the sum of the numbers of their own array:
receive_vector.
The following is a sample dialog of my program:
mehdi@ubuntu:~/cs610$ mpicc -g -Wall a.c
mehdi@ubuntu:~/cs610$ mpiexec -n 10 ./a.out
In process 0 the sum is: 4593
In process 4 the sum is: 4593
In process 5 the sum is: 4593
In process 8 the sum is: 4593
In process 9 the sum is: 4593
In process 2 the sum is: 4593
In process 1 the sum is: 4593
In process 3 the sum is: 4593
In process 6 the sum is: 4593
In process 7 the sum is: 4593
Answer:
Question 3 (Lecture 3): Complete the following parallel program using the sequent simulator
(sequent.h) to display the approximate area under a curve. Each process (including process0)
calculates the area of one trapezoid.
Note: The variable: n gets the number of processes from the keyboard.
Note: The number of processes cannot go beyond 9. I made this restriction in my software.
Note: A version of this question is in lecture 2.
Note: The area of each trapezoid is added to a shared memory location.
2
Note: You must only guard the smallest section of the code with m_lock() and m_unlock(),
where processes try to changed the content of a shared memory location.
Note: You must not use MPI.
Note: If you get error with respect to shared memory enter the following on the prompt:
ipcrm -a
For more information on this command enter: man ipcrm.
//File name: a.c
#include “sequent.h”
#include
float *approx;
float a = 3, b = 6;
int n;
float f(float x);
void eachArea();
//To add each area to.
//Area under the curve from: x = a to: x = b.
//For the function x2.
//Each process calls this function to obtain the area of one trapezoid.
int main()
{ //Complete this function
initialize();
approx = //Complete this line
printf(“Enter the number of processes: “);
//Write instruction for this line
m_set_procs(n);
m_fork(eachArea);
m_kill_procs();
printf(“The precise area: %.2f\n”,b*b*b/3 – a*a*a/3);
printf(“The area is: %.2f\n”, approx[0]);
clean();
return 0;
}
float f(float x)
{//This function is complete. Do not change it.
return x * x;
}
void eachArea()
{//Complete this function
}
Answer: