Lab Assesment 9 Parallel & Distributed Computing (L31+32) : Dated: 16/10/2020 Assessment 9 Muskan Agrawal 18BCE0707

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Dated: 16/10/2020 MUSKAN AGRAWAL

ASSESSMENT 9 18BCE0707

LAB ASSESMENT 9

PARALLEL
&
DISTRIBUTED COMPUTING
(L31+32)

AIM: Write a C program to use MPI_Reduce that divides the processors


into the group to find the addition independently.
Dated: 16/10/2020 MUSKAN AGRAWAL
ASSESSMENT 9 18BCE0707

CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<mpi.h>
int main(int argc, char* argv[]){
int rank,numprocs,i,sum=0;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int localsum=0;
int globalsum=0;
int a[]={7,2,9,4};
char o;
if(rank==0){
printf("Addition of elements using MPI_Reduce \n");
}
for(i=rank;i<4;i+=numprocs){
sum+=a[i];
}
localsum=sum;

MPI_Reduce(&localsum,&globalsum,1,MPI_INT,MPI_SUM,0,MPI_
COMM_WORLD);
if (rank==0){
printf("Global Sum = %d\n",globalsum);
}
MPI_Finalize();
return 0;
}
Dated: 16/10/2020 MUSKAN AGRAWAL
ASSESSMENT 9 18BCE0707

CODE SNIPPET:

EXECUTION:
Dated: 16/10/2020 MUSKAN AGRAWAL
ASSESSMENT 9 18BCE0707

REMARKS:

1. MPI_Reduce takes an array of input elements on each process and returns an array of
output elements to the root process. The output elements contain the reduced result.
2. The send_data parameter is an array of elements of type datatype that each process
wants to reduce. The recv_data is only relevant on the process with a rank of root.
The recv_data array contains the reduced result and has a size of sizeof(datatype) *
count.
3. The op parameter is the operation that you wish to apply to your data. MPI contains a
set of common reduction operations that can be used. The reduction operations defined
by MPI include
4. MPI_MAX - Returns the maximum element.

5. MPI_MIN - Returns the minimum element.

6. MPI_SUM - Sums the elements.

7. MPI_PROD - Multiplies all elements.

8. MPI_LAND - Performs a logical and across the elements.

9. MPI_LOR - Performs a logical or across the elements.

10. MPI_BAND - Performs a bitwise and across the bits of the elements.

11. MPI_BOR - Performs a bitwise or across the bits of the elements.

12. MPI_MAXLOC - Returns the maximum value and the rank of the process that owns it.

13. MPI_MINLOC - Returns the minimum value and the rank of the process that owns it.

You might also like