Question

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 <stdio.h>
#include <math.h>
#include <time.h>

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();     

}              
Was it helpful?

Solution

It may be that you are using scanf() on one machine before you set the size of the matrix, however if the size of the matrix is stored on all the machines the scanf() will not be run on them all.

If that is the case, you will have to scanf() the size of the matrix on the main process before you begin with the MPI functionality, and then send the size of the matrix (via COMM_WORLD.Bcast() or some other method) to each process in order for the matrix to be defined correctly.

Of course, this is just a guess because you've provided far too little information to make an informed answer, so I'm going for the most likely explanation.

EDIT

Ok, here's some changes that will make it compile (Some of them may be done anyways, your code came out a bit funny when you pasted it in, and there may be others I've missed, again code is formatted a bit funny)

MPI_Send(maxtrix2, size*size, MPI_INT, i, 0, MPI_COMM_WORLD); 
should be
MPI_Send(&matrix2, size*size, MPI_INT, i, 0, MPI_COMM_WORLD); 

         int section = size/numprocs;
         int tail = size % numprocs;
These need to be defined before the first if statement in order for them to work further in, so just define them straight after the main without assigning them. (Otherwise they don't exist when your other processes try to use them)

Sorry but I don't have time to figure out the code and actually get it to do what you want, but that should at least get you runnable code you can debug.

OTHER TIPS

The value of "size" is not known at compile time. Hence the error.

It may seem logical, if you are new to coding, that you are reading the value of size and trying to allocate it. This will, in fact, work for interpreted languages like Python. But your code is in C. C programs need to be compiled to work. When the compiler looks at your code, it doesnt know what is the value of the variable "size". And in the next statement, you are using the variable "size". So, you are attempting to use a variable whose value is not yet known. That is what the compiler is complaining about.

Two ways to solve this: 1) Declare a sufficiently large matrix, say, 1000 X 1000. And during run time, you decide how much size you want to use. But dont give values more than what you hard coded in the source, i.e. 1000 X 1000. What you are doing here is telling the compiler to allocate memory for 1000 X 1000 items, but you may or may not use the entire space. You will be wasting memory and this is not an efficient way to do this.

2) Use dynamic allocation. However, given the nature of this question, this may be too advanced for you at the moment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top