cjacobi.c
上传用户:hhyinxing
上传日期:2013-09-10
资源大小:833k
文件大小:22k
源码类别:

并行计算

开发平台:

Unix_Linux

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "mpi.h"
  4. #include "math.h"
  5. #include "string.h"
  6. #define E 0.00001
  7. #define min -1
  8. #define intsize sizeof(int)
  9. #define charsize sizeof(char)
  10. #define floatsize sizeof(float)
  11. #define A(x,y) A[x*N+y]
  12. #define I(x,y) I[x*N+y]
  13. #define a(x,y) a[x*N+y]
  14. #define e(x,y) e[x*N+y]
  15. #define b(x) b[x]
  16. #define buffer(x,y) buffer[x*N+y]
  17. #define buffee(x,y) buffee[x*N+y]
  18. int M,N;
  19. int *b;
  20. int m,p;
  21. int myid,group_size;
  22. float *A,*I;
  23. float *a,*e;
  24. float max;
  25. float sum;
  26. double starttime,time1,time2,time3,endtime;
  27. MPI_Status status;
  28. float sgn(float v)
  29. {
  30.     float f;
  31.     if (v>=0) f=1;
  32.     else f=-1;
  33.     return f;
  34. }
  35. void read_fileA()
  36. {
  37.     int i,j;
  38.     FILE *fdA;
  39.     time1=MPI_Wtime();
  40.     fdA=fopen("dataIn.txt","r");
  41.     fscanf(fdA,"%d %d", &M, &N);
  42.     if(M != N)
  43.     {
  44.         puts("The input is error!");
  45.         exit(0);
  46.     }
  47.     m=N/p; if (N%p!=0) m++;
  48.     A=(float*)malloc(floatsize*N*m*p);
  49.     I=(float*)malloc(floatsize*N*m*p);
  50.     for(i = 0; i < M; i ++)
  51.     {
  52.         for(j = 0; j < M; j ++) fscanf(fdA, "%f", A+i*M+j);
  53.     }
  54.     fclose(fdA);
  55.     printf("Input of file "dataIn.txt"n");
  56.     printf("%dt %dn",M, N);
  57.     for(i=0;i<M;i++)
  58.     {
  59.         for(j=0;j<N;j++) printf("%ft",A(i,j));
  60.         printf("n");
  61.     }
  62.     for(i=0;i<N;i++)
  63.     {
  64.         for(j=0;j<N;j++)
  65.         {
  66.             if (i==j) I(i,j)=1;
  67.             else I(i,j)=0;
  68.         }
  69.     }
  70. }
  71. void send_a()
  72. {
  73.     int i;
  74.     for(i=1;i<p;i++)
  75.     {
  76.         MPI_Send(&(A(m*i,0)),m*N,MPI_FLOAT,i,i,MPI_COMM_WORLD);
  77.         MPI_Send(&(I(m*i,0)),m*N,MPI_FLOAT,i,i,MPI_COMM_WORLD);
  78.     }
  79.     free(A);
  80.     free(I);
  81. }
  82. void get_a()
  83. {
  84.     int i,j;
  85.     if (myid==0)
  86.     {
  87.         for(i=0;i<m;i++)
  88.             for(j=0;j<N;j++)
  89.         {
  90.             a(i,j)=A(i,j);
  91.             e(i,j)=I(i,j);
  92.         }
  93.     }
  94.     else
  95.     {
  96.         MPI_Recv(a,m*N,MPI_FLOAT,0,myid,MPI_COMM_WORLD,&status);
  97.         MPI_Recv(e,m*N,MPI_FLOAT,0,myid,MPI_COMM_WORLD,&status);
  98.     }
  99. }
  100. int main(int argc,char **argv)
  101. {
  102.     float *c;
  103.     int k;
  104.     int loop;
  105.     int i,j,v,z,r,t,y;
  106.     int i1,j1;
  107.     float f,g,h;
  108.     float sin1,sin2,cos1;
  109.     float s1,c1;
  110.     float *br,*bt,*bi,*bj,*zi,*zj;
  111.     float *temp1,*temp2,*buffer,*buffee;
  112.     int counter,current;
  113.     int *buf;
  114.     int mml,mpl;
  115.     float bpp,bqq,bpq,bqp;
  116.     float lmax;
  117.     MPI_Init(&argc,&argv);
  118.     MPI_Comm_size(MPI_COMM_WORLD,&group_size);
  119.     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  120.     if (myid==0) starttime=MPI_Wtime();
  121.     p=group_size;
  122.     max=10.0;
  123.     loop=0;
  124.     if (myid==0) read_fileA();
  125.     MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
  126.     m=N/p;
  127.     if (N%p!=0) m++;
  128.     a=(float*)malloc(floatsize*m*N);
  129.     e=(float*)malloc(floatsize*m*N);
  130.     b=(int*)malloc(intsize*m);
  131.     br=(float*)malloc(floatsize*N);
  132.     bt=(float*)malloc(floatsize*N);
  133.     bi=(float*)malloc(floatsize*m);
  134.     bj=(float*)malloc(floatsize*m);
  135.     zi=(float*)malloc(floatsize*m);
  136.     zj=(float*)malloc(floatsize*m);
  137.     temp1=(float*)malloc(floatsize*4);
  138.     temp2=(float*)malloc(floatsize*4*p);
  139.     if ((myid==p-1)&&(myid%2!=0))
  140.     {
  141.         buffer=(float*)malloc(floatsize*m/2*N);
  142.         buffee=(float*)malloc(floatsize*m/2*N);
  143.         buf=(int*)malloc(intsize*m/2);            /* store i,j */
  144.     }
  145.     if ((myid%2!=0)&&(myid!=p-1))
  146.     {
  147.         buffer=(float*)malloc(floatsize*m*N);
  148.         buffee=(float*)malloc(floatsize*m*N);
  149.         buf=(int*)malloc(intsize*m);
  150.     }
  151.     if (myid==0)
  152.     {
  153.         get_a();
  154.         send_a();
  155.     }
  156.     else
  157.     {
  158.         get_a();
  159.     }
  160.     if (myid==0)                                  /* start computing now */
  161.         time2=MPI_Wtime();
  162.     for(i=0;i<m;i++)
  163.         b(i)=myid*m+i;
  164.     while (fabs(max)>E)
  165.     {
  166.         loop++;
  167.         for(i=myid*m;i<(myid+1)*m-1;i++)
  168.             for(j=i+1;j<=(myid+1)*m-1;j++)
  169.         {
  170.             r=i%m; t=j%m;                         /* to make a(r,j) zero use a(r,i) a(t,j) */
  171.             if(a(r,j)!=0)
  172.             {
  173.                 f=(-a(r,j));
  174.                 g=(a(t,j)-a(r,i))/2;
  175.                 h=sgn(g)*f/sqrt(f*f+g*g);
  176.                 sin2=h;
  177.                 sin1=h/sqrt(2*(1+sqrt(1-h*h)));
  178.                 cos1=sqrt(1-sin1*sin1);
  179.                 bpp=a(r,i)*cos1*cos1+a(t,j)*sin1*sin1+a(r,j)*sin2;
  180.                 bqq=a(r,i)*sin1*sin1+a(t,j)*cos1*cos1-a(r,j)*sin2;
  181.                 bpq=0; bqp=0;
  182.                 for(v=0;v<N;v++)                  /* compute row */
  183.                     if ((v!=i)&&(v!=j))
  184.                 {
  185.                     br[v]=a(r,v)*cos1+a(t,v)*sin1;
  186.                     a(t,v)=(-a(r,v))*sin1+a(t,v)*cos1;
  187.                 }
  188.                 for(v=0;v<N;v++)                  /* row */
  189.                     if ((v!=i)&&(v!=j))
  190.                 {
  191.                     a(r,v)=br[v];
  192.                 }
  193.                 for(v=0;v<m;v++)
  194.                     br[v]=e(v,i)*cos1+e(v,j)*sin1;
  195.                 for(v=0;v<m;v++)
  196.                     e(v,j)=e(v,i)*(-sin1)+e(v,j)*cos1;
  197.                 for(v=0;v<m;v++)
  198.                     e(v,i)=br[v];
  199.                 for(v=0;v<m;v++)                  /* compute col */
  200.                     if ((v!=r)&&(v!=t))
  201.                 {
  202.                     bi[v]=a(v,i)*cos1+a(v,j)*sin1;
  203.                     a(v,j)=(-a(v,i))*sin1+a(v,j)*cos1;
  204.                 }
  205.                 for(v=0;v<m;v++)                  /* col */
  206.                     if ((v!=r)&&(v!=t))
  207.                         a(v,i)=bi[v];
  208.                 a(r,i)=bpp;
  209.                 a(t,j)=bqq;
  210.                 a(r,j)=bpq;
  211.                 a(t,i)=bqp;
  212.                 temp1[0]=sin1;
  213.                 temp1[1]=cos1;
  214.                 temp1[2]=(float)i;
  215.                 temp1[3]=(float)j;
  216.             }
  217.             else
  218.             {
  219.                 temp1[0]=0.0;
  220.                 temp1[1]=0.0;
  221.                 temp1[2]=0.0;
  222.                 temp1[3]=0.0;
  223.             }                                     /* end of if */
  224.             MPI_Allgather(temp1,4,MPI_FLOAT,temp2,4,MPI_FLOAT,MPI_COMM_WORLD);
  225.             current=0;
  226.             for(v=1;v<=p;v++)
  227.             {
  228.                 s1=temp2[(v-1)*4+0];
  229.                 c1=temp2[(v-1)*4+1];
  230.                 i1=(int)temp2[(v-1)*4+2];
  231.                 j1=(int)temp2[(v-1)*4+3];
  232.                 if ((s1!=0.0)||(c1!=0.0)||(i1!=0)||(j1!=0))
  233.                 {
  234.                     if (myid!=current)
  235.                     {
  236.                         for(z=0;z<m;z++)
  237.                         {
  238.                             zi[z]=a(z,i1)*c1 + a(z,j1)*s1;
  239.                             a(z,j1)=-a(z,i1)*s1 + a(z,j1)*c1;
  240.                         }
  241.                         for(z=0;z<m;z++)
  242.                             a(z,i1)=zi[z];
  243.                         for(z=0;z<m;z++)
  244.                             zi[z]=e(z,i1)*c1+e(z,j1)*s1;
  245.                         for(z=0;z<m;z++)
  246.                             e(z,j1)=-e(z,i1)*s1+e(z,j1)*c1;
  247.                         for(z=0;z<m;z++)
  248.                             e(z,i1)=zi[z];
  249.                     }                             /* if myid!=current */
  250.                 }                                 /* if */
  251.                 current=current+1;
  252.             }                                     /* for v */
  253.         }                                         /* for i,j */
  254.         for(counter=1;counter<=2*p-2;counter++)
  255.         {
  256.             if (myid==0)
  257.             {
  258.                 MPI_Send(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  259.                 MPI_Send(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  260.                 MPI_Send(&b(m/2),m/2,MPI_INT,myid+1,myid+1,MPI_COMM_WORLD);
  261.                 MPI_Recv(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  262.                 MPI_Recv(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  263.                 MPI_Recv(&b(m/2),m/2,MPI_INT,myid+1,myid,MPI_COMM_WORLD,&status);
  264.             }
  265.             if ((myid==p-1)&&(myid%2!=0))
  266.             {
  267.                 for(i=m/2;i<m;i++)
  268.                     for(j=0;j<N;j++)
  269.                         buffer((i-m/2),j)=a(i,j);
  270.                 for(i=m/2;i<m;i++)
  271.                     for(j=0;j<N;j++)
  272.                         buffee((i-m/2),j)=e(i,j);
  273.                 for(i=m/2;i<m;i++)
  274.                     buf[i-m/2]=b(i);
  275.                 for(i=0;i<m/2;i++)
  276.                     for(j=0;j<N;j++)
  277.                         a((i+m/2),j)=a(i,j);
  278.                 for(i=0;i<m/2;i++)
  279.                     for(j=0;j<N;j++)
  280.                         e((i+m/2),j)=e(i,j);
  281.                 for(i=0;i<m/2;i++)
  282.                     b(m/2+i)=b(i);
  283.                 MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  284.                 MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  285.                 MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  286.                 MPI_Send(buffer,m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  287.                 MPI_Send(buffee,m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  288.                 MPI_Send(buf,m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  289.             }
  290.             if ((myid==p-1)&&(myid%2==0))
  291.             {
  292.                 MPI_Send(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  293.                 MPI_Send(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  294.                 MPI_Send(&b(m/2),m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  295.                 for(i=0;i<m/2;i++)
  296.                     for(j=0;j<N;j++)
  297.                         a((i+m/2),j)=a(i,j);
  298.                 for(i=0;i<m/2;i++)
  299.                     for(j=0;j<N;j++)
  300.                         e((i+m/2),j)=e(i,j);
  301.                 for(i=0;i<m/2;i++)
  302.                     b(i+m/2)=b(i);
  303.                 MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  304.                 MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  305.                 MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  306.             }
  307.             if ((myid!=0)&&(myid!=p-1))
  308.             {
  309.                 if(myid%2==0)
  310.                 {
  311.                     MPI_Send(&(a(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  312.                     MPI_Send(&(e(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  313.                     MPI_Send(&b(0),m/2,MPI_INT,myid+1,myid+1,MPI_COMM_WORLD);
  314.                     MPI_Send(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  315.                     MPI_Send(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  316.                     MPI_Send(&b(m/2),m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  317.                     MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  318.                     MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  319.                     MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  320.                     MPI_Recv(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  321.                     MPI_Recv(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  322.                     MPI_Recv(&b(m/2),m/2,MPI_INT,myid+1,myid,MPI_COMM_WORLD,&status);
  323.                 }
  324.                 if(myid%2!=0)
  325.                 {
  326.                     for(i=0;i<m;i++)
  327.                         for(j=0;j<N;j++)
  328.                             buffer(i,j)=a(i,j);
  329.                     for(i=0;i<m;i++)
  330.                         for(j=0;j<N;j++)
  331.                             buffee(i,j)=e(i,j);
  332.                     for(i=0;i<m;i++)
  333.                         buf[i]=b(i);
  334.                     MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  335.                     MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  336.                     MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  337.                     MPI_Recv(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  338.                     MPI_Recv(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  339.                     MPI_Recv(&b(m/2),m/2,MPI_INT,myid+1,myid,MPI_COMM_WORLD,&status);
  340.                     MPI_Send(&(buffer(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  341.                     MPI_Send(&(buffee(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  342.                     MPI_Send(&buf[0],m/2,MPI_INT,myid+1,myid+1,MPI_COMM_WORLD);
  343.                     MPI_Send(&(buffer(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  344.                     MPI_Send(&(buffee(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  345.                     MPI_Send(&buf[m/2],m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  346.                 }
  347.             }
  348.             for(i=0;i<m/2;i++)
  349.                 for(j=m/2;j<m;j++)
  350.             {
  351.                 if (a(i,b(j))!=0)
  352.                 {
  353.                     f=-a(i,b(j));
  354.                     g=(a(j,b(j))-a(i,b(i)))/2;
  355.                     h=sgn(g)*f/sqrt(f*f+g*g);
  356.                     sin2=h;
  357.                     sin1=h/sqrt(2*(1+sqrt(1-h*h)));
  358.                     cos1=sqrt(1-sin1*sin1);
  359.                     bpp=a(i,b(i))*cos1*cos1+a(j,b(j))*sin1*sin1+a(i,b(j))*sin2;
  360.                     bqq=a(i,b(i))*sin1*sin1+a(j,b(j))*cos1*cos1-a(i,b(j))*sin2;
  361.                     bpq=0; bqp=0;
  362.                     for(v=0;v<N;v++)              /* compute row */
  363.                         if ((v!=b(i))&&(v!=b(j)))
  364.                     {
  365.                         br[v]=a(i,v)*cos1+a(j,v)*sin1;
  366.                         a(j,v)=-a(i,v)*sin1+a(j,v)*cos1;
  367.                     }
  368.                     for(v=0;v<N;v++)              /* row */
  369.                         if ((v!=b(i))&&(v!=b(j)))
  370.                             a(i,v)=br[v];
  371.                     for(v=0;v<m;v++)
  372.                         br[v]=e(v,b(i))*cos1+e(v,b(j))*sin1;
  373.                     for(v=0;v<m;v++)
  374.                         e(v,b(j))=e(v,b(i))*(-sin1)+e(v,b(j))*cos1;
  375.                     for(v=0;v<m;v++)
  376.                         e(v,b(i))=br[v];
  377.                     for(v=0;v<m;v++)              /* compute col */
  378.                         if ((v!=i)&&(v!=j))
  379.                     {
  380.                         bi[v]=a(v,b(i))*cos1+a(v,b(j))*sin1;
  381.                         a(v,b(j))=-a(v,b(i))*sin1+a(v,b(j))*cos1;
  382.                     }
  383.                     for(v=0;v<m;v++)              /* col */
  384.                         if ((v!=i)&&(v!=j))
  385.                             a(v,b(i))=bi[v];
  386.                     a(i,b(i))=bpp;
  387.                     a(j,b(j))=bqq;
  388.                     a(i,b(j))=bpq;                /* 0 */
  389.                     a(j,b(i))=bqp;                /* 0 */
  390.                     temp1[0]=sin1;
  391.                     temp1[1]=cos1;
  392.                     temp1[2]=(float)b(i);
  393.                     temp1[3]=(float)b(j);
  394.                 }
  395.                 else
  396.                 {
  397.                     temp1[0]=0.0;
  398.                     temp1[1]=0.0;
  399.                     temp1[2]=0.0;
  400.                     temp1[3]=0.0;
  401.                 }
  402.                 MPI_Allgather(temp1,4,MPI_FLOAT,temp2,4,MPI_FLOAT,MPI_COMM_WORLD);
  403.                 current=0;
  404.                 for(v=1;v<=p;v++)
  405.                 {
  406.                     s1=temp2[(v-1)*4+0];
  407.                     c1=temp2[(v-1)*4+1];
  408.                     i1=(int)temp2[(v-1)*4+2];
  409.                     j1=(int)temp2[(v-1)*4+3];
  410.                     if ((s1!=0.0)||(c1!=0.0)||(i1!=0)||(j1!=0))
  411.                     {
  412.                         if (myid!=current)
  413.                         {
  414.                             for(z=0;z<m;z++)
  415.                             {
  416.                                 zi[z]=a(z,i1)*c1 + a(z,j1)*s1;
  417.                                 a(z,j1)=-a(z,i1)*s1 + a(z,j1)*c1;
  418.                             }
  419.                             for(z=0;z<m;z++)
  420.                                 a(z,i1)=zi[z];
  421.                             for(z=0;z<m;z++)
  422.                                 zi[z]=e(z,i1)*c1+e(z,j1)*s1;
  423.                             for(z=0;z<m;z++)
  424.                                 e(z,j1)=-e(z,i1)*s1+e(z,j1)*c1;
  425.                             for(z=0;z<m;z++)
  426.                                 e(z,i1)=zi[z];
  427.                         }                         /* if myid!=current */
  428.                     }                             /* if */
  429.                     current=current+1;
  430.                 }                                 /* for v */
  431.             }                                     /* for i,j */
  432.         }                                         /*  counter */
  433.         if (myid==0)
  434.         {
  435.             MPI_Send(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  436.             MPI_Send(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  437.             MPI_Send(&b(m/2),m/2,MPI_INT,myid+1,myid+1,MPI_COMM_WORLD);
  438.             MPI_Recv(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  439.             MPI_Recv(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  440.             MPI_Recv(&b(m/2),m/2,MPI_INT,myid+1,myid,MPI_COMM_WORLD,&status);
  441.         }
  442.         if ((myid==p-1)&&(myid%2!=0))
  443.         {
  444.             for(i=m/2;i<m;i++)
  445.                 for(j=0;j<N;j++)
  446.                     buffer((i-m/2),j)=a(i,j);
  447.             for(i=m/2;i<m;i++)
  448.                 for(j=0;j<N;j++)
  449.                     buffee((i-m/2),j)=e(i,j);
  450.             for(i=m/2;i<m;i++)
  451.                 buf[i-m/2]=b(i);
  452.             for(i=0;i<m/2;i++)
  453.                 for(j=0;j<N;j++)
  454.                     a((i+m/2),j)=a(i,j);
  455.             for(i=0;i<m/2;i++)
  456.                 for(j=0;j<N;j++)
  457.                     e((i+m/2),j)=e(i,j);
  458.             for(i=0;i<m/2;i++)
  459.                 b(m/2+i)=b(i);
  460.             MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  461.             MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  462.             MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  463.             MPI_Send(buffer,m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  464.             MPI_Send(buffee,m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  465.             MPI_Send(buf,m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  466.         }
  467.         if ((myid==p-1)&&(myid%2==0))
  468.         {
  469.             MPI_Send(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  470.             MPI_Send(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  471.             MPI_Send(&b(m/2),m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  472.             for(i=0;i<m/2;i++)
  473.                 for(j=0;j<N;j++)
  474.                     a((i+m/2),j)=a(i,j);
  475.             for(i=0;i<m/2;i++)
  476.                 for(j=0;j<N;j++)
  477.                     e((i+m/2),j)=e(i,j);
  478.             for(i=0;i<m/2;i++)
  479.                 b(i+m/2)=b(i);
  480.             MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  481.             MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  482.             MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  483.         }
  484.         if ((myid!=0)&&(myid!=p-1))
  485.         {
  486.             if(myid%2==0)
  487.             {
  488.                 MPI_Send(&(a(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  489.                 MPI_Send(&(e(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  490.                 MPI_Send(&b(0),m/2,MPI_INT,myid+1,myid+1,MPI_COMM_WORLD);
  491.                 MPI_Send(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  492.                 MPI_Send(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  493.                 MPI_Send(&b(m/2),m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  494.                 MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  495.                 MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  496.                 MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  497.                 MPI_Recv(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  498.                 MPI_Recv(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  499.                 MPI_Recv(&b(m/2),m/2,MPI_INT,myid+1,myid,MPI_COMM_WORLD,&status);
  500.             }
  501.             if(myid%2!=0)
  502.             {
  503.                 for(i=0;i<m;i++)
  504.                     for(j=0;j<N;j++)
  505.                         buffer(i,j)=a(i,j);
  506.                 for(i=0;i<m;i++)
  507.                     for(j=0;j<N;j++)
  508.                         buffee(i,j)=e(i,j);
  509.                 for(i=0;i<m;i++)
  510.                     buf[i]=b(i);
  511.                 MPI_Recv(&(a(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  512.                 MPI_Recv(&(e(0,0)),m/2*N,MPI_FLOAT,myid-1,myid,MPI_COMM_WORLD,&status);
  513.                 MPI_Recv(&b(0),m/2,MPI_INT,myid-1,myid,MPI_COMM_WORLD,&status);
  514.                 MPI_Recv(&(a(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  515.                 MPI_Recv(&(e(m/2,0)),m/2*N,MPI_FLOAT,myid+1,myid,MPI_COMM_WORLD,&status);
  516.                 MPI_Recv(&b(m/2),m/2,MPI_INT,myid+1,myid,MPI_COMM_WORLD,&status);
  517.                 MPI_Send(&(buffer(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  518.                 MPI_Send(&(buffee(0,0)),m/2*N,MPI_FLOAT,myid+1,myid+1,MPI_COMM_WORLD);
  519.                 MPI_Send(&buf[0],m/2,MPI_INT,myid+1,myid+1,MPI_COMM_WORLD);
  520.                 MPI_Send(&(buffer(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  521.                 MPI_Send(&(buffee(m/2,0)),m/2*N,MPI_FLOAT,myid-1,myid-1,MPI_COMM_WORLD);
  522.                 MPI_Send(&buf[m/2],m/2,MPI_INT,myid-1,myid-1,MPI_COMM_WORLD);
  523.             }
  524.         }
  525.         lmax=min;
  526.         for(i=0;i<m;i++)
  527.             for(j=0;j<N;j++)
  528.                 if ((m*myid+i)!=j)
  529.                 {
  530.                     if (fabs(a(i,j))>lmax)
  531.                         lmax=fabs(a(i,j));
  532.                 }
  533.         MPI_Allreduce(&lmax,&max,1,MPI_FLOAT,MPI_MAX,MPI_COMM_WORLD);
  534.     }                                             /* while */
  535.     if (myid==0)
  536.     {
  537.         time3=MPI_Wtime();
  538.         A=(float*)malloc(floatsize*N*m*p);
  539.         I=(float*)malloc(floatsize*N*m*p);
  540.         for(i=0;i<m;i++)
  541.             for(j=0;j<N;j++)
  542.         {
  543.             A(i,j)=a(i,j);
  544.             I(i,j)=e(i,j);
  545.         }
  546.     }
  547.     if (myid!=0)
  548.     {
  549.         MPI_Send(a,m*N,MPI_FLOAT,0,myid,MPI_COMM_WORLD);
  550.         MPI_Send(e,m*N,MPI_FLOAT,0,myid,MPI_COMM_WORLD);
  551.     }
  552.     else
  553.         for(i=1;i<p;i++)
  554.     {
  555.         MPI_Recv(a,m*N,MPI_FLOAT,i,i,MPI_COMM_WORLD,&status);
  556.         MPI_Recv(e,m*N,MPI_FLOAT,i,i,MPI_COMM_WORLD,&status);
  557.         for(j=0;j<m;j++)
  558.             for(k=0;k<N;k++)
  559.                 A((i*m+j),k)=a(j,k);
  560.         for(j=0;j<m;j++)
  561.             for(k=0;k<N;k++)
  562.                 I((i*m+j),k)=e(j,k);
  563.     }
  564.     if (myid==0)
  565.     {
  566.         for(i=0;i<N;i++)
  567.             printf("the %dst envalue:%fn",i,A(i,i));
  568.         endtime=MPI_Wtime();
  569.         printf("n");
  570.         printf("Iteration num = %dn",loop);
  571.         printf("Whole running time    = %f secondsn",endtime-starttime);
  572.         printf("Distribute data time  = %f secondsn",time2-time1);
  573.         printf("Parallel compute time = %f secondsn",time3-time2);
  574.     }
  575.     MPI_Finalize();
  576.     free(a);
  577.     free(b);
  578.     free(c);
  579.     free(br);
  580.     free(bt);
  581.     free(bi);
  582.     free(bj);
  583.     free(zi);
  584.     free(zj);
  585.     free(buffer);
  586.     free(buf);
  587.     free(buffee);
  588.     free(A);
  589.     free(I);
  590.     free(temp1);
  591.     free(temp2);
  592.     return(0);
  593. }