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

  • SEARCH
  • Home
  • 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 3694102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:27:23+00:00 2026-05-19T04:27:23+00:00

I tried scattering a matrix by row to all the processors but it resulted

  • 0

I tried scattering a matrix by row to all the processors but it resulted in a segmentation fault.. I don’t know what i am doing wrong.. Here is my code

      if(rank == 0) {
                    A_row = 10;
                    A_col = 10;
                    /* calculate the strip size */
                    strip_size = A_row / size;

                    /* genarate Matrix A */
                    A = (double **)malloc(sizeof(double*) * 10);
                    int k = 0;
                    for(i = 0; i < 10; i++) {
                            A[i] = (double*)malloc(sizeof(double) * 10);
                            for(j = 0; j < 10; j++) {
                                    A[i][j] = k;
                                    k++;
                                    printf("%lf  ", A[i][j]);
                            }
                            printf("\n");
                    }
            }

            /* Broadcasting the row, column size of Matrix A as well as strip size and Matrix B*/
            MPI_Bcast(&A_row, 1, MPI_INT, 0, MPI_COMM_WORLD);
            MPI_Bcast(&A_col, 1, MPI_INT, 0, MPI_COMM_WORLD);
            MPI_Bcast(&strip_size, 1, MPI_INT, 0, MPI_COMM_WORLD);

            /* defining a datatype for sub-matrix */
            MPI_Type_vector(strip_size, A_col, A_col, MPI_DOUBLE, &strip);
            MPI_Type_commit(&strip);

            strip_A = (double **)malloc(sizeof(double*)*strip_size);
            for(i= 0; i< strip_size; i++) {
                    strip_A[i] = (double*)malloc(sizeof(double)*A_col);
            }

            MPI_Scatter(&A[0][0], 1, strip, &strip_A[0][0], 1, strip, 0, MPI_COMM_WORLD);

            for(i = 0; i < strip_size; i++) {
                    if(i == 0) {
                            printf("rank = %d\n", rank);
                    }
                    for(j = 0; j < A_col; j++) {
                            printf("%lf  ", strip_A[i][j]);
                    }
                    printf("\n");
            }

Can anyone tell me what is wrong…

here is my error when I run

mpirun -np 2 ./a.out



 0.000000  1.000000  2.000000  3.000000  4.000000  5.000000  6.000000  7.000000  8.000000  9.000000
 10.000000  11.000000  12.000000  13.000000  14.000000  15.000000  16.000000  17.000000  18.000000  19.000000
 20.000000  21.000000  22.000000  23.000000  24.000000  25.000000  26.000000  27.000000  28.000000  29.000000
 30.000000  31.000000  32.000000  33.000000  34.000000  35.000000  36.000000  37.000000  38.000000  39.000000
 40.000000  41.000000  42.000000  43.000000  44.000000  45.000000  46.000000  47.000000  48.000000  49.000000
 50.000000  51.000000  52.000000  53.000000  54.000000  55.000000  56.000000  57.000000  58.000000  59.000000
 60.000000  61.000000  62.000000  63.000000  64.000000  65.000000  66.000000  67.000000  68.000000  69.000000
 70.000000  71.000000  72.000000  73.000000  74.000000  75.000000  76.000000  77.000000  78.000000  79.000000
 80.000000  81.000000  82.000000  83.000000  84.000000  85.000000  86.000000  87.000000  88.000000  89.000000
 90.000000  91.000000  92.000000  93.000000  94.000000  95.000000  96.000000  97.000000  98.000000  99.000000 

 rank = 1
 42.000000  43.000000  44.000000  45.000000  46.000000  47.000000  48.000000  49.000000  0.000000  0.000000
 52.000000  53.000000  54.000000  55.000000  56.000000  57.000000  58.000000  59.000000  0.000000  0.000000
 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000
 0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 
[seismicmstm:07338] *** Process received signal *** [seismicmstm:07338] Signal:
 Segmentation fault (11)
 [seismicmstm:07338] Signal code: 
 (128) [seismicmstm:07338] Failing at
 address: (nil)
 -------------------------------------------------------------------------- mpirun noticed that process rank 1 with PID 7338 on node seismicmstm.cluster exited on signal 11 (Segmentation fault).
 --------------------------------------------------------------------------
  • 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-19T04:27:24+00:00Added an answer on May 19, 2026 at 4:27 am

    There’s a couple things going on here. The good news is that the hardest stuff — getting the mpi data type created, and the basic structure of the MPI_Scatter call — are correct.

    The first issue is that the MPI_Scatter line uses &(A[0][0]) — but in all but rank zero, you haven’t set A to point to anything! So you dereference a random pointer twice, and there’s your segfault.

    A more subtle issue, as suggested by suszterpatt, is that there’s no guarantee that your rows of allocated memory are contiguous, so your scatter operation may not work even if you fix the above. You are trying to send strip_size * A_col doubles from somewhere in A to strip_A, but strip_A may not consist of that many doubles contiguously – it could be A_col doubles, and then some padding, then A_col doubles — or indeed, the various rows could be scattered all over memory. The three ways of fixing that are, in order of ease (IMHO): (a) make the data contiguous in memory by creating the entire array and then creating the two-d C arrays to point to the right places; (b) to just send one row at a time; or (c) to create an MPI data type that actually reflects how your data is mapped (perhaps randomly) in memory.

    An approach using (a) that seems to work (for A_row evenly divided by size, anyway) looks like below:

    #include <stdio.h>
    #include <mpi.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv) {
      int rank, size;
      int strip_size, A_row, A_col;
      double **A, **strip_A, *Adata, *stripdata;
      MPI_Datatype strip;
      int i,j;
    
      MPI_Init(&argc,&argv) ;
    
      MPI_Comm_rank(MPI_COMM_WORLD,&rank) ;
      MPI_Comm_size(MPI_COMM_WORLD,&size) ;
    
          if(rank == 0) {
                        A_row = 10;
                        A_col = 10;
                        /* calculate the strip size */
                        strip_size = A_row / size;
    
                        /* genarate Matrix A */
                        Adata = (double *)malloc(sizeof(double)*A_row*A_col);
                        A = (double **)malloc(sizeof(double*) * A_row);
                        for(i = 0; i < A_row; i++) {
                                A[i] = &(Adata[i*A_col]);
                        }
                        int k = 0;
                        for(i = 0; i < A_row; i++) {
                                for(j = 0; j < A_col; j++) {
                                        A[i][j] = k;
                                        k++;
                                }
                        }
                }
    
                /* Broadcasting the row, column size of Matrix A as well as strip size and Matrix B*/
                MPI_Bcast(&A_row, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(&A_col, 1, MPI_INT, 0, MPI_COMM_WORLD);
                MPI_Bcast(&strip_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
    
                /* defining a datatype for sub-matrix */
                MPI_Type_vector(strip_size, A_col, A_col, MPI_DOUBLE, &strip);
                MPI_Type_commit(&strip);
    
                stripdata = (double *)malloc(sizeof(double)*strip_size*A_col);
                strip_A = (double **)malloc(sizeof(double*)*strip_size);
                for(i= 0; i< strip_size; i++) {
                        strip_A[i] = &(stripdata[i*A_col]);
                }
    
                MPI_Scatter(Adata, 1, strip, &(strip_A[0][0]), 1, strip, 0, MPI_COMM_WORLD);
                //MPI_Scatter(Adata, A_col*strip_size, MPI_DOUBLE, &(strip_A[0][0]), A_col*strip_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    
                for(i = 0; i < strip_size; i++) {
                        if(i == 0) {
                                printf("rank = %d\n", rank);
                        }
                        for(j = 0; j < A_col; j++) {
                                printf("%lf  ", strip_A[i][j]);
                        }
                        printf("\n");
                }
    
        MPI_Type_free(&strip);
        free(strip_A);
        free(stripdata);
        free(Adata);
        free(A);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
Tried with : IList<IList<string>> matrix = new List<new List<string>()>(); but I can't. How can
Tried : var a=10.3; var b=2.3; alert(a+b); but I get 12.600000000000001 . I know
Tried many ways based on other questions asked here but could not get a
Tried to find answer here but, no luck so, here goes; I would like
Tried to use what's here , but that doesn't solve things for me. I've
Tried with: listPacchetti[2] but it doesnt works at all. In fact I should do
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried following the instructions here: How to use Google app engine with my own
Tried a bunch of things but I can't get it to work consistently amid

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.