title: MPI segmenation fault when using Bcast, Scatter and Gather with Dynamic Allocation
body: <p>I am working with some code and I encountered some problem with implementating of table size for matrix mult from concole input.</p>
  
  <p>First version work on:</p>
  
  <pre><code> const int size = 1000;
   int mat_a[size][size], mat_b[size][size], mat_c[size][size];
  </code></pre>
  
  <p>To use console arguments I found that there is need to implement dynamic array allocation.
  Unfortunatelly I encounter a problem:</p>
  
  <pre><code> *** Process received signal ***
   Signal: Segmentation fault (11)
   Signal code: Address not mapped (1)
   Failing at address: 0x7ffd955237f8
  </code></pre>
  
  <p>I suppose that problem may be in MPI functions like Bcast, Scatter and Gather. I searched on similar cases in stackovf, but I cannot see it.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cmath&gt;
  #include &lt;math.h&gt;
  
  int main(int argc, char *argv[])
  {
    int taskid, ntasks, mat_start, mat_end, i, j, k;
  double start_time; //hold start time
  double end_time; // hold end time  
  
    MPI_Init (&amp;argc, &amp;argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &amp;taskid);   
    MPI_Comm_size(MPI_COMM_WORLD, &amp;ntasks); 
  
          int size = 0;
  
          if (argc != 2) {
                  printf("No arguments");
                  exit(-1);
          }
  
          size = atoi(argv[1]);
  
          if (size &lt; 0 ) {
                  printf("SIZE: %2d \n", size);
                  exit(-1);
          }
  
  int **mat_a = (int **)malloc(sizeof(int *)*size);
  int **mat_b = (int **)malloc(sizeof(int *)*size);  
  int **mat_c = (int **)malloc(sizeof(int *)*size);
  
  for (int z = 0 ; z &lt; size ; z++){
       mat_a[z] = (int *)malloc(sizeof(int)*size);
       mat_b[z] = (int *)malloc(sizeof(int)*size);
       mat_c[z] = (int *)malloc(sizeof(int)*size);
  }
  
    mat_start = taskid * size/ntasks;
    mat_end = (taskid+1) * size/ntasks;
  
    if (taskid==0) {
      for (i = 0; i &lt; size; i++) {
          for (j = 0; j &lt; size; j++) {
              mat_a[i][j] = (int)(sin(i) * i * j) % 10;
          }
      }
      for (i = 0; i &lt; size; i++) {
          for (j = 0; j &lt; size; j++) {
              mat_b[i][j] = (int)(cos(j) * (i + j)) % 10;
          }
      }
    }
  
  start_time = MPI_Wtime();
  
    MPI_Bcast (&amp;mat_b, size*size, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Scatter (&amp;mat_a, size*size/ntasks, MPI_INT, mat_a[mat_start], size*size/ntasks, MPI_INT, 0, MPI_COMM_WORLD);
  
    printf("computing slice %d (from row %d to %d)\n", taskid, mat_start, mat_end-1);
    for (i=mat_start; i&lt;mat_end; i++) 
      for (j=0; j&lt;size; j++) {
        mat_c[i][j]=0;
        for (k=0; k&lt;size; k++)
      mat_c[i][j] += mat_a[i][k]*mat_b[k][j];
      }
  
    MPI_Gather (mat_c[mat_start], size*size/ntasks, MPI_INT, mat_c, size*size/ntasks, MPI_INT, 0, MPI_COMM_WORLD);
  
          end_time = MPI_Wtime();
          printf("\nRunning Time = %f\n\n", end_time - start_time);
  
    MPI_Finalize();
    return 0;
  }
  </code></pre>
  
  <p>Can anyone tell me what is wrong?</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Thanks for answer. I tried to implement your proposal of solution but with no good results. I changed some part of code to look like this:</p>
  
  <pre><code>int **mat_a=(int **)malloc(size*sizeof(int *));
          int **mat_b=(int **)malloc(size*sizeof(int *));
          int **mat_c=(int **)malloc(size*sizeof(int *));
  
      if(mat_a==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
      if(mat_b==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
      if(mat_c==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
  
      mat_a[0]=(int*)malloc(size*size*sizeof(int));
   mat_b[0]=(int*)malloc(size*size*sizeof(int));
  mat_c[0]=(int*)malloc(size*size*sizeof(int));
  
     if(mat_a[0]==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
  if(mat_b[0]==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
  if(mat_c[0]==NULL){fprintf(stderr,"malloc failed\n");exit(1);}
  
    int ti;
      for(ti=1;ti&lt;size;ti++){
          mat_a[ti]=&amp;mat_a[0][size*ti];
  mat_b[ti]=&amp;mat_a[0][size*ti];
  mat_c[ti]=&amp;mat_a[0][size*ti];
  
   }
  
    mat_start = taskid * size/ntasks;
    mat_end = (taskid+1) * size/ntasks;
  
  //populating the array ......
  
  start_time = MPI_Wtime();
  
    MPI_Bcast(mat_a[0],size*size, MPI_INT,0,MPI_COMM_WORLD); 
   //MPI_Bcast (&amp;mat_b, size*size, MPI_INT, 0, MPI_COMM_WORLD);
  //  MPI_Scatter (&amp;mat_b, size*size/ntasks, MPI_INT, mat_a[mat_start], size*size/ntasks, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Scatter (mat_b[0], size*size/ntasks, MPI_INT, mat_a[mat_start], size*size/ntasks, MPI_INT, 0, MPI_COMM_WORLD);
  
    printf("computing slice %d (from row %d to %d)\n", taskid, mat_start, mat_end-1);
    for (i=mat_start; i&lt;mat_end; i++) 
      for (j=0; j&lt;size; j++) {
        mat_c[i][j]=0;
        for (k=0; k&lt;size; k++)
      mat_c[i][j] += mat_a[i][k]*mat_b[k][j];
      }
  
    MPI_Gather (mat_c[mat_start], size*size/ntasks, MPI_INT, mat_c, size*size/ntasks, MPI_INT, 0, MPI_COMM_WORLD);
  
          end_time = MPI_Wtime();
          printf("\nRunning Time = %f\n\n", end_time - start_time);
  
    MPI_Finalize();
    return 0;
  }
  </code></pre>
  
  <p>and program starts to run and even print mat_a (when added printing), but after some delay I got this:</p>
  
  <pre><code>[cuda:05167] *** Process received signal ***
  [cuda:05167] Signal: Segmentation fault (11)
  [cuda:05167] Signal code:  (128)
  [cuda:05167] Failing at address: (nil)
  </code></pre>
  
  <p>Scatter and Gather may be roblem? How to change, to make it finally work?</p>