0% found this document useful (0 votes)
4 views

Cluster Lab Session 04(Examples)

Uploaded by

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

Cluster Lab Session 04(Examples)

Uploaded by

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

Cluster Lab Session 03

Example for bcast,scatter and gather in group communication

Bcast.c

#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv[])


{

int rank;

int data=0;

MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank==0)
{

data=10;
}

printf("\nbefore bcast data in process %d:%d",rank,data);

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

printf("\nafter bcast data in process %d:%d",rank,data);

MPI_Finalize();
return 0;
}

--------------------------------------
scatter.c

#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv[])
{

int rank;

int data=0;
int *buf=NULL;

MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
if(rank==0)
{

int arr[10]={1,2,3,4,5,6,7,8,9,10};
buf=arr;
}

printf("\ndata before scatter in process %d is:%d",rank,data);

MPI_Scatter(buf,1,MPI_INT,&data,1,MPI_INT,0,MPI_COMM_WORLD);

printf("\ndata after scatter in process %d is:%d",rank,data);

MPI_Finalize();
return 0;
}

_______________________________
gather.c
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv[])


{

int myrank,csize,localx;

int arr[4];

MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
MPI_Comm_size(MPI_COMM_WORLD,&csize);
localx=myrank*2;
MPI_Gather(&localx,1,MPI_INT,arr,1,MPI_INT,0,MPI_COMM_WORLD);

if(myrank==0)
for(int i=0;i<4;i++)
printf("%d",arr[i]);

MPI_Finalize();
return 0;

You might also like