資源描述:
《cannon矩陣乘法的mpi實現(xiàn)及性能分析》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、#include#include#include#include#include#includeMPI_Statusstatus;double**A,**B,**C;//C=A*Bdouble*a,*b,*c;//各個進(jìn)程的緩沖區(qū)intn;//矩陣的行列數(shù)intnp;//每個進(jìn)程控制的小矩陣的行列數(shù)intp,rank;//進(jìn)程個個數(shù)、當(dāng)前進(jìn)程的編號,笛卡爾進(jìn)程編號double*tempa,*tempb;voidProduceABC();//在根處理器中生成矩陣AB,初始
2、化矩陣CvoidPrintABC();//輸出結(jié)果voidScatterAB();//分發(fā)矩陣AB中的元素到各個進(jìn)程中voidMainProcess();//cannon算法的主過程voidcollectC();//收集結(jié)果矩陣CvoidMutiply();//矩陣相乘voidPrintab();voidPrintc();intmain(intargc,char*argv[]){inti;doublestarttime,endtime;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&p);MPI_Comm_rank(
3、MPI_COMM_WORLD,&rank);if(rank==0){printf("請輸入矩陣的行列數(shù)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*siz
4、eof(double));memset(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、;i++){A[i]=(double*)malloc(n*sizeof(double));B[i]=(double*)malloc(n*sizeof(double));C[i]=(double*)malloc(n*sizeof(double));}ProduceABC();//在根處理器中隨機生成矩陣AB,初始化矩陣CScatterAB();//分發(fā)矩陣AB中的元素到各個進(jìn)程中}else{MPI_Recv(a,np*np,MPI_DOUBLE,0,1,MPI_COMM_WORLD,&status);MPI_Recv(b,np*np,MPI_DOUBLE,0,2,MPI_C
6、OMM_WORLD,&status);}starttime=MPI_Wtime();//開始時間MainProcess();//cannon算法的主過程if(rank==0){collectC();//收集結(jié)果矩陣CPrintABC();//輸出結(jié)果endtime=MPI_Wtime();printf("timeused:%lf",endtime-starttime);for(i=0;i7、_DOUBLE,0,1,MPI_COMM_WORLD);}free(a);free(b);free(c);free(tempa);free(tempb);MPI_Finalize();return0;}voidProduceABC()//在根處理器中生成矩陣AB{inti,j;for(i=0;i