PDC Assignment
PDC Assignment
ABBOTTABAD CAMPUS
Answer
#include <stdio.h>
if(n<=1)
return 1;
else
return n*fact(n-1); }
int main()
{ int n,r,size;
long ans;
MPI_Init (NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(r==0)
scanf("%d",&n); }
MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
ans=fact(n);
if(r==0)
MPI_Barrier(MPI_COMM_WORLD);
MPI_Reduce(&ans,&ans,1,MPI_LONG,MPI_SUM,0,MPI_COMM_WORLD);
if(r==0)
return 0; }
Explanation:
The code calculates the factorial of a range of the use of the MPI library. The variety is entered with
the aid of using the person on processor zero, and the factorial is calculated on that processor. The
result is then broadcast to processor 1, which prints it out. Finally, the 2 processors synchronize the
use of MPI_Barrier and processor zero prints out the obtained answer.
Part 2: Also, comment on how you will develop the parallel version
The parallel model of this system may be advanced via way of means of dividing the venture of
calculating the factorial of the enter wide variety most of the one-of-a-kind processors. The
processor zero can compute the factorial of the enter wide variety and go back the solution to
processor 1. The conversation among processor zero and 1 may be achieved the usage of MPI. The
computed result may be revealed first at processor zero after which the acquired solution may be
revealed at processor 1. By dividing the venture of calculating the factorial of the enter wide variety
most of the one-of-a-kind processors. The processor zero can compute the factorial of the enter
wide variety and go back the solution to processor 1. The conversation among processor zero and 1
may be achieved the usage of MPI. The computed result may be revealed first at processor zero
after which the acquired solution may be revealed at processor 1.
Explanation:
By dividing the obligation of computing the factorial of the enter quantity a few of the numerous
processors, a parallel model of this system can be built. The factorial of the enter quantity may be
computed through processor zero and lower back to processor 1. MPI may be used to speak among
processors zero and 1. Processor zero can put up the computed end result first, observed through
processor 1 printing the acquired answer. The obligation of computing the factorial of the enter
quantity is split a few of the many processors. The factorial of the enter quantity may be computed
through processor zero and lower back to processor 1. MPI may be used to speak among processors
zero and 1. Processor zero can put up the computed result first, observed through processor 1
printing the acquired answer.