Professional Documents
Culture Documents
Lab Assesment 9 Parallel & Distributed Computing (L31+32) : Dated: 16/10/2020 Assessment 9 Muskan Agrawal 18BCE0707
Lab Assesment 9 Parallel & Distributed Computing (L31+32) : Dated: 16/10/2020 Assessment 9 Muskan Agrawal 18BCE0707
Lab Assesment 9 Parallel & Distributed Computing (L31+32) : Dated: 16/10/2020 Assessment 9 Muskan Agrawal 18BCE0707
ASSESSMENT 9 18BCE0707
LAB ASSESMENT 9
PARALLEL
&
DISTRIBUTED COMPUTING
(L31+32)
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.
10. MPI_BAND - Performs a bitwise and 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.