并行程序的设计实验.docxVIP

  • 11
  • 0
  • 约1.18万字
  • 约 24页
  • 2018-08-04 发布于福建
  • 举报
并行程序的设计实验

并行程序设计实验1数据接力传送#include mpi.h#include stdio.hint main(int argc,char ** argv ){ int rank, value, size;MPI_Status status; MPI_Init( argc, argv ); MPI_Comm_rank( MPI_COMM_WORLD, rank ); MPI_Comm_size( MPI_COMM_WORLD, size ); /* 得到当前进程标识和总的进程个数*/ /* 循环执行 直到输入的数据为负时才退出*/ if (rank == 0) { fprintf(stderr, \nPlease give new value=\n); /*进程0读入要传递的数据*/ scanf( %d, value ); fprintf(stderr,%d read -- (%d)\n,rank,value); if (size1) { MPI_Send( value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD ); fprintf(stderr,%d send (%d)-- %d\n, rank,value,rank+1); /* 若不少于一个进程则向下一个进程传递该数据*/ } } else { MPI_Recv( value, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, status ); /* 其它进程从前一个进程接收传递过来的数据*/ fprintf(stderr,%d receive (%d)-- %d\n,rank,value,rank-1); if (rank == size - 1) { MPI_Send( value, 1, MPI_INT, 0, 0, MPI_COMM_WORLD ); fprintf(stderr,%d send (%d)-- %d\n, rank,value,0); /*若当前进程不是最后一个进程则将该数据继续向后传递*/ } if (rank size - 1) { MPI_Send( value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD ); fprintf(stderr,%d send (%d)-- %d\n, rank,value,rank+1); /*若当前进程不是最后一个进程则将该数据继续向后传递*/ } } MPI_Barrier(MPI_COMM_WORLD); /* 执行一下同步 加入它主要是为了将前后两次数据传递分开*/ MPI_Finalize( );}3任意源和任意标识#include mpi.h#include stdio.h#include stdlib.h/* 使用了随机函数srand和rand */#include time.h/* 使用了时间函数time */int main(int argc,char *argv[]){int rank,size,i,buf[1];MPI_Status status;MPI_Init(argc,argv);/* 得到当前进程标识和总的进程个数 */MPI_Comm_rank(MPI_COMM_WORLD,rank);MPI_Comm_size(MPI_COMM_WORLD,size);if(rank==0){/* 进程0接收来自其他所有进程的消息,将各消息的内容、消息来源和消息标识打印出来。 */for(i=0;i10*(size-1);i++){MPI_Recv(buf,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,status);fprintf(stderr,Msg=%d\tfrom %d with tag %d.\n,buf[0],status.MPI_SOURCE,status.MPI_TAG);}}else{/* 其

文档评论(0)

1亿VIP精品文档

相关文档