Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 209249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:53:54+00:00 2026-05-11T17:53:54+00:00

I coded a mpi matrix multification program, which use scanf(%d, &size) , designate matrix

  • 0

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

}              
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:53:54+00:00Added an answer on May 11, 2026 at 5:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 170k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The approach you have here is exactly right, in my… May 12, 2026 at 2:00 pm
  • Editorial Team
    Editorial Team added an answer read this thread https://stackoverflow.com/questions/72394/what-should-a-developer-know-before-building-a-public-web-site May 12, 2026 at 2:00 pm
  • Editorial Team
    Editorial Team added an answer You can bind the execution of each plugin to the… May 12, 2026 at 2:00 pm

Related Questions

I'm having issues with a very strange error in some code I wrote. The
I'm actually writing an MPI program. This is a basic client / server pattern.
I'm writing a parallel evolutionary algorithm library using C++, MPI and CUDA. I need
I use VS2k8 to write and compile (but not run) a program using the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.