资源描述:
《cannon矩阵乘法mpi实现及性能的分析》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、...页眉#include#include#include#include#include#includeMPI_Statusstatus;double**A,**B,**C;//C=A*Bdouble*a,*b,*c;//各个进程的缓冲区intn;//矩阵的行列数intnp;//每个进程控制的小矩阵的行列数intp,rank;//进程个个数、当前进程的编号,笛卡尔进程编号double*tempa,*tempb;voidProduceABC();//在根处理器中生成矩阵AB,初始化矩阵Cvo
2、idPrintABC();//输出结果voidScatterAB();//分发矩阵AB中的元素到各个进程中voidMainProcess();//cannon算法的主过程voidcollectC();//收集结果矩阵CvoidMutiply();//矩阵相乘voidPrintab();voidPrintc();intmain(intargc,char*argv[]){inti;doublestarttime,endtime;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&p);MPI_Comm_rank(MPI_COMM_WORLD,&r
3、ank);if(rank==0){....页脚...页眉printf("请输入矩阵的行列数n=");fflush(stdout);scanf_s("%d",&n);printf("");}MPI_Bcast(&n,1,MPI_DOUBLE,0,MPI_COMM_WORLD);//n=atoi(argv[1]);np=n/(int)sqrt(p);a=(double*)malloc(np*np*sizeof(double));b=(double*)malloc(np*np*sizeof(double));c=(double*)malloc(np*np*sizeof(double));mems
4、et(c,0,np*np*sizeof(double));tempa=(double*)malloc(np*np*sizeof(double));tempb=(double*)malloc(np*np*sizeof(double));if(rank==0){//在根处理器中为矩阵ABC分配空间A=(double**)malloc(n*sizeof(double*));B=(double**)malloc(n*sizeof(double*));C=(double**)malloc(n*sizeof(double*));for(i=0;i5、*sizeof(double));B[i]=(double*)malloc(n*sizeof(double));C[i]=(double*)malloc(n*sizeof(double));}ProduceABC();//在根处理器中随机生成矩阵AB,初始化矩阵CScatterAB();//分发矩阵AB中的元素到各个进程中}....页脚...页眉else{MPI_Recv(a,np*np,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);MPI_Recv(b,np*np,MPI_DOUBLE,0,2,MPI_COMM_WORLD,&status);}starttim
6、e=MPI_Wtime();//开始时间MainProcess();//cannon算法的主过程if(rank==0){collectC();//收集结果矩阵CPrintABC();//输出结果endtime=MPI_Wtime();printf("timeused:%lf",endtime-starttime);for(i=0;i7、ee(b);free(c);....页脚...页眉free(tempa);free(tempb);MPI_Finalize();return0;}voidProduceABC()//在根处理器中生成矩阵AB{inti,j;for(i=0;i