.矩阵求逆的并行算法:#includestdio.h#includestdlib.h#includempi.h#defineintsizesizeof(int)#definefloatsizesizeof(float)#defineA(x,y)A[x*N+y]#defineQ(x,y)Q[x*N+y]#definea(x,y)a[x*N+y]#definef(x)f[x]float*A,*Q;float*a,*f;intN,v,m;intp;intmyid;MPI_Statusstatus;FILE*dataFile;doublestarttime,endtime,time1;voidreadData(){inti,j;starttime=MPI_Wtime();dataFile=fopen(dataIn.txt,r);fscanf(dataFile,%d,&N);A=(float*)malloc(floatsize*N*N);for(i=0;iN;i++){for(j=0;jN;j++){fscanf(dataFile,%f,&A(i,j));}}fclose(dataFile);printf(Inputoffile\dataIn.txt\\n);printf(%d\n,N);for(i=0;iN;i++){for(j=0;jN;j++){printf(%f\t,A(i,j));}printf(\n);}Q=(float*)malloc(floatsize*N*N);}voidprintResult(){inti,j;printf(\nOutputofMatrixQ\n);for(i=0;iN;i++){for(j=0;jN;j++){printf(%f\t,Q(i,j));}printf(\n);}endtime=MPI_Wtime();printf(\n);printf(Wholerunningtime=%fseconds\n,endtime-starttime);printf(Distributedatatime=%fseconds\n,time1-starttime);printf(Parallelcomputetime=%fseconds\n,endtime-time1);dataFile=fopen(dataOut.txt,w);fprintf(dataFile,Inputoffile\dataIn.txt\\n);fprintf(dataFile,%d\n,N);for(i=0;iN;i++){for(j=0;jN;j++){fprintf(dataFile,%f\t,A(i,j));}fprintf(dataFile,\n);}fprintf(dataFile,\nOutputofMatrixA'sinversion\n);for(i=0;iN;i++){for(j=0;jN;j++){fprintf(dataFile,%f\t,Q(i,j));}fprintf(dataFile,\n);}/*0号进程将时间统计写入目标文件*/fprintf(dataFile,\n);fprintf(dataFile,Wholerunningtime=%fseconds\n,endtime-starttime);fprintf(dataFile,Distributedatatime=%fseconds\n,time1-starttime);fprintf(dataFile,Parallelcomputetime=%fseconds\n,endtime-time1);fprintf(dataFile,ParallelProcessnumber=%d\n,p);fclose(dataFile);}voidbroadcast(inti,intj,intv){intk;if(myid==j){a(i,v)=(float)(1/a(i,v));for(k=0;kN;k++){if(k!=v){a(i,k)=a(i,k)*a(i,v);}}for(k=0;kN;k++){f(k)=a(i,k);}MPI_Bcast(&f(0),N,MPI_FLOAT,myid,MPI_COMM_WORLD);}else{MPI_Bcast(f,N,MPI_FLOAT,j,MPI_COMM_WORLD);}}voidtransform(inti,intj,intv){intk,w;if(myid!=j){for(k=0;km;k++){for(w=0;wN;w++){if(w!=v){a(k,w)=a(k,w)-f(w)*a(k,v);}}}for(k=0;km;k++){a(k,v)=-f(v)*a(k,v);}}if(myid==j){for(k=0;km;k++){if(k!=i){for(w=0;wN;w++){if(w!=v){a(k,w)=a(k,w)-f(w)*a(k,v);}}}}for(k=0;km;k++){if(k!=i){a(k,v)=-f(v)*a(k,v);}}}}intmain(intargc,char**argv){inti,j,k,w,group_size;MPI_Init(&argc,&argv);MPI_Comm_size(MPI_COMM_WORLD,&group_size);MPI_Comm_rank(MPI_COMM_WORLD,&myid);p=group_size;if(myid==0){readData();}m=N/p;if(myid==0){for(i=1;ip;i++){MPI_Send(&N,1,MPI_INT,i,i,MPI_COMM_WORLD);MPI_Send(&m,1,MPI_INT,i,i,MPI_COMM_WORLD);}}if(myid!=0){MPI_Recv(&N,1,MPI_INT,0,myid,MPI_COMM_WORLD,&status);MPI_Recv(&m,1,MPI_INT,0,myid,MPI_COMM_WORLD,&status);}a=(float*)malloc(floatsize*m*N);f=(float*)malloc(floatsize*N);if(a==NULL||f==NULL){printf(Allocatespaceforaorffail!);}if(myid==0){for(i=0;im;i++){for(j=0;jN;j++){a(i,j)=A(p*i,j);}}for(k=1;kp;k++){for(i=0;im;i++){MPI_Send(&A(p*i+k,0),N,MPI_FLOAT,k,k,MPI_COMM_WORLD);}}}if((myid0)&&(myidp)){MPI_Recv(&a(0,0),m*N,MPI_FLOAT,0,myid,MPI_COMM_WORLD,&status);}if(myidp){for(i=0;im;i++){for(j=0;jp;j++){v=i*p+j;broadcast(i,j,v);transform(i,j,v);}}}if((myid0)&&(myidp)){MPI_Send(a,m*N,MPI_FLOAT,0,myid,MPI_COMM_WORLD);}if(myid==0){for(i=0;im;i++){for(j=0;jN;j++){Q(p*i,j)=a(i,j);}}for(k=1;kp;k++){for(i=0;im;i++){MPI_Recv(&Q(p*i+k,0),N,MPI_FLOAT,k,k,MPI_COMM_WORLD,&status);}}printResult();free(Q);}MPI_Finalize();if(myidp){free(a);free(f);}return(0);}