0% found this document useful (0 votes)
52 views3 pages

PDC Assignment

This document contains an assignment submission for a parallel and distributed computing course. It includes the sequential code for calculating the factorial of a number. The student is asked to develop a parallel version of this code using MPI that divides the task among multiple processors. In the parallel version, processor 0 would compute the factorial and return the result to processor 1. MPI would be used for communication between the processors. Dividing the task among processors creates a parallel implementation of calculating the factorial.

Uploaded by

Ahmad Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views3 pages

PDC Assignment

This document contains an assignment submission for a parallel and distributed computing course. It includes the sequential code for calculating the factorial of a number. The student is asked to develop a parallel version of this code using MPI that divides the task among multiple processors. In the parallel version, processor 0 would compute the factorial and return the result to processor 1. MPI would be used for communication between the processors. Dividing the task among processors creates a parallel implementation of calculating the factorial.

Uploaded by

Ahmad Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMSATS UNIVERSITY ISLAMABAD

ABBOTTABAD CAMPUS

PARALLEL AND DISTRIBUTED


COMPUTING
ASSIGNMENT 3

SUBMITTED BY: AHMAD REHMAN

REG NO: FA18-BCS-007

SUBMITTED TO: SIR MATEEN YAQOOB


Part 1: Develop parallel version of factorial calculation program for distributed memory using
MPI. The sequential version is given below to start with.

Answer
#include <stdio.h>

long fact (int n) {

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)

{ printf("Enter the number: ");

scanf("%d",&n); }

MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);

ans=fact(n);

if(r==0)

printf("Computed answer: %ld\n",ans);

MPI_Barrier(MPI_COMM_WORLD);

MPI_Reduce(&ans,&ans,1,MPI_LONG,MPI_SUM,0,MPI_COMM_WORLD);

if(r==0)

{ printf("Received answer: %ld\n",ans); }


MPI_Finalize();

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy