tags:

views:

286

answers:

2

I coded a mpi matrix multification program, which use scanf("%d", &size), designate matrix size, then I defined int matrix[size*size], but when I complied it, it reported that matrix is undeclared. Please tell me why, or what my problem is!

According Ed's suggestion, I changed the matrix definition to if(myid == 0) block, but got the same err! Now I post my code, please help me find out where I made mistakes! thank you!

include "mpi.h"

include

include

include

int size;

int main(int argc, char* argv[]) {

 int myid, numprocs;
 int *p;
 MPI_Status status;

 int i,j,k;

 MPI_Init(&argc, &argv);
 MPI_Comm_rank(MPI_COMM_WORLD,&myid);
 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);




 if(myid == 0)
 {
         scanf("%d", &size);
         int matrix1[size*size];
         int matrix2[size*size];
         int matrix3[size*size];
         int section = size/numprocs;
         int tail = size % numprocs;


         srand((unsigned)time(NULL));

         for( i=0; i<size; i++)
            for( j=0; j<size; j++)
               {
                    matrix1[i*size+j]=rand()%9;
                    matrix3[i*size+j]= 0;
                    matrix2[i*size+j]=rand()%9;
               }   

         printf("Matrix1 is: \n"); 
         for( i=0; i<size; i++)
            {
            for( j=0; j<size; j++)
              {
                    printf("%3d", matrix1[i*size+j]);
              }
            printf("\n");
            }

         printf("\n");
         printf("Matrix2 is: \n");
         for( i=0; i<size; i++)
            {
            for( j=0; j<size; j++)
            {
                    printf("%3d", matrix2[i*size+j]);
            }
            printf("\n");
            }                
          //MPI_BCAST(matrix1, size*size, MPI_INT, 0, MPI_COMM_WORLD, );

          for( i=1; i<numprocs; i++)
              {
                  MPI_Send(&size, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
                  MPI_Send(&section, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
                  MPI_Send(&tail, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
                  MPI_Send(maxtrix2, size*size, MPI_INT, i, 0, MPI_COMM_WORLD); 

              }


          j = 0;

          for( i=1; i<numprocs-1; i++)
              {
                   p = &matrix1[size*section*j++];
                   MPI_Send(p, size*section, MPI_INT, i, 1, MPI_COMM_WORLD);
              }
          p = &matrix1[size*section*j];
          MPI_Send(p, size*section+size*tail, MPI_INT, numprocs-1, 1, MPI_COMM_WORLD);


          p = matrix3;
          for( i=1; i<numprocs-1; i++)
              {
                  MPI_Recv(p, size*section, MPI_INT, i, 1, MPI_COMM_WORLD, &status);
                  p = &matrix3[size*section*i];

                  }
          MPI_Recv(p, size*section+size*tail, MPI_INT, numprocs-1, 1, MPI_COMM_WORLD, &status);

         printf("\n");
         printf("Matrix3 is: \n");
         for( i=0; i<size; i++)
            {
            for( j=0; j<size; j++)
            {
                    printf("%2d ", matrix3[i*size+j]);
            }
            printf("\n");
            }
   }

   else if (myid > 0 && myid<numprocs-1 )
   {      
          MPI_Recv(&size, 1, MPI_INT, 0, 0,MPI_COMM_WORLD, &status);
          MPI_Recv(&section, 1, MPI_INT, 0, 0,MPI_COMM_WORLD, &status);
          MPI_Recv(&tail, 1, MPI_INT, 0, 0,MPI_COMM_WORLD, &status);
          int matrix1[size*size];
          int matrix2[size*size];
          int matrix3[size*size];

          MPI_Recv(matrix2, size*size, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
          MPI_Recv(matrix1, size*section, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);   

          for( i=0; i<section; i++)
              for( j=0; j<size; j++)
                for( k=0; k<size; k++)
                {
                        matrix1[i*size+j] = matrix1[i*size+k]*matrix2[k*size+j];
                }

         MPI_Send(matrix1, size*section, MPI_INT, 0, 1, MPI_COMM_WORLD);             
   }

   else if (myid > 0 && myid == numprocs-1)
   {
          MPI_Recv(&size, 1, MPI_INT, 0, 0,MPI_COMM_WORLD, &status);
          MPI_Recv(&section, 1, MPI_INT, 0, 0,MPI_COMM_WORLD, &status);
          MPI_Recv(&tail, 1, MPI_INT, 0, 0,MPI_COMM_WORLD, &status);
          int matrix1[size*size];
          int matrix2[size*size];
          int matrix3[size*size];

          MPI_Recv(matrix2, size*size, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
          MPI_Recv(matrix1, size*section+size*tail, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);

          for( i=0; i<section+tail; i++)
              for( j=0; j<size; j++)
                for( k=0; k<size; k++)
                {
                        matrix1[i*size+j] = matrix1[i*size+k]*matrix2[k*size+j];
                }

         MPI_Send(matrix1, size*section+size*tail, MPI_INT, 0, 1, MPI_COMM_WORLD);       
   }

 return 0;         
 MPI_Finalize();

}

A: 
Ed Woodcock
Thank you! your answer inspired me! I tried, but the errors are still there! So I post my code, hope you help me debug it!
Johnson
A: 

Hiii!!!

I have a problem trying to run your program =(, afther it print Matrix1 and Matrix2 the program doesn't do anything, it freeze, don't print Matrix3, I had another problem that happens the same =(, anyone knows how fix it??

Coco