pp


#include<mpi.h>
#include<stdio.h>
int main(int argc,char** argv){
int rank,size,msg_send=100,msg_recv;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(size<2){
if(rank==0)printf("Run with at least 2 processes.\n");
MPI_Finalize();
return 0;}
if(rank==0){
printf("Process 0 sending to Process 1...\n");
MPI_Send(&msg_send,1,MPI_INT,1,0,MPI_COMM_WORLD);
MPI_Recv(&msg_recv,1,MPI_INT,1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
printf("Process 0 received from Process 1: %d\n",msg_recv);}
else if(rank==1){
printf("Process 1 sending to Process 0...\n");
MPI_Send(&msg_send,1,MPI_INT,0,0,MPI_COMM_WORLD);
MPI_Recv(&msg_recv,1,MPI_INT,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
printf("Process 1 received from Process 0: %d\n",msg_recv);}
MPI_Finalize();
return 0;}





#include<mpi.h>
#include<stdio.h>
int main(int argc,char** argv){
int rank,size,number;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank==0){
printf("Enter a number to broadcast:");
fflush(stdout);
scanf("%d",&number);}
MPI_Bcast(&number,1,MPI_INT,0,MPI_COMM_WORLD);
printf("Process %d received number:%d\n",rank,number);
MPI_Finalize();
return 0;}




#include<mpi.h>
#include<stdio.h>
int main(int argc,char** argv){
int rank,size,send_data[4]={10,20,30,40},recv_data;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Scatter(send_data,1,MPI_INT,&recv_data,1,MPI_INT,0,MPI_COMM_WORLD);
printf("Process %d received:%d\n",rank,recv_data);
recv_data+=1;
MPI_Gather(&recv_data,1,MPI_INT,send_data,1,MPI_INT,0,MPI_COMM_WORLD);
if(rank==0){
printf("Gathered data:");
for(int i=0;i<size;i++)printf("%d ",send_data[i]);
printf("\n");}
MPI_Finalize();
return 0;}





#include<mpi.h>
#include<stdio.h>
int main(int argc,char** argv){
int rank,size,value,sum,prod,max,min,sum_all,prod_all,max_all,min_all;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
value=rank+1;
printf("Process %d has value %d\n",rank,value);
MPI_Reduce(&value,&sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
MPI_Reduce(&value,&prod,1,MPI_INT,MPI_PROD,0,MPI_COMM_WORLD);
MPI_Reduce(&value,&max,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
MPI_Reduce(&value,&min,1,MPI_INT,MPI_MIN,0,MPI_COMM_WORLD);
if(rank==0){
printf("[Using MPI_Reduce at Root Process]\n");
printf("Sum=%d\n",sum);
printf("Prod=%d\n",prod);
printf("Max=%d\n",max);
printf("Min=%d\n",min);}
MPI_Allreduce(&value,&sum_all,1,MPI_INT,MPI_SUM,MPI_COMM_WORLD);
MPI_Allreduce(&value,&prod_all,1,MPI_INT,MPI_PROD,MPI_COMM_WORLD);
MPI_Allreduce(&value,&max_all,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD);
MPI_Allreduce(&value,&min_all,1,MPI_INT,MPI_MIN,MPI_COMM_WORLD);
printf("[Process %d] MPI_Allreduce Results:\n",rank);
printf("Sum=%d\n",sum_all);
printf("Prod=%d\n",prod_all);
printf("Max=%d\n",max_all);
printf("Min=%d\n",min_all);
MPI_Finalize();
return 0;}


Comments

Popular posts from this blog

sml