0

I'm new in programming MPI and my difficulty is allocate dynamically a larger matrix. The problem that appeared was:

Line command:

$ mpicc teste5.c -o teste5
$ mpirun -np 2 teste5

[frontend:13283] *** Process received signal ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 13283 on node frontend exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
$

Code:

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

#define max 5000


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

int numtasks,              /* number of tasks in partition */
taskid,                /* a task identifier */
numworkers,            /* number of worker tasks */
source,                /* task id of message source */
dest,                  /* task id of message destination */
i, j, k;           /* misc */

// float a[max][max],aux_a[max][max]; /* See comments */
float **a, **aux_a;

MPI_Status status;

MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
(MPI_COMM_WORLD,&numtasks);

numworkers = numtasks-1;

if (taskid == 0){

a = (float **) malloc (max * sizeof(float *));
for ( i = 0; i < max; i++ )
    a[i] = (float*) malloc (max * sizeof(float));

aux_a = (float **) malloc (max * sizeof(float *));
for ( i = 0; i < max; i++ )
    aux_a[i] = (float*) malloc (max * sizeof(float));

for (i=0; i<max; i++)
    for (j=0; j<max; j++)
        a[i][j]= i+1;

for (dest=1; dest<=numworkers; dest++){

    MPI_Send(&a, max*max, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);

}

for (i=1; i<=numworkers; i++){

    source = i;
    MPI_Recv(&aux_a, max*max, MPI_FLOAT, source, 1, MPI_COMM_WORLD, &status);

}

printf("******************************************************\n");
printf("Result Matrix:\n");
for (i=0; i<max; i++){
    printf("\n"); 
    for (j=0; j<max; j++) 
       printf("%6.2f   ", aux_a[i][j]);
}
printf("\n******************************************************\n");
printf ("Done.\n");
}

if (taskid > 0)
{
a = (float **) malloc (max * sizeof(float *));
for ( i = 0; i < max; i++ )
    a[i] = (float*) malloc (max * sizeof(float));

aux_a = (float **) malloc (max * sizeof(float *));
for ( i = 0; i < max; i++ )
    aux_a[i] = (float*) malloc (max * sizeof(float));

MPI_Recv(&a, max*max, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &status);

for (i=0; i<max; i++)
   for (j=0; j<max; j++)
aux_a[i][j] = a[i][j];

MPI_Send(&aux_a, max*max, MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
}

MPI_Finalize();
}

Can you help me?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pedro A.
  • 149
  • 5
  • You don't need to cast the return value of `malloc` in a C program. – Carl Norum Jul 24 '13 at 20:51
  • .. does this code even compile? – Carl Norum Jul 24 '13 at 20:53
  • I'm using MPI programming parallel. – Pedro A. Jul 24 '13 at 20:56
  • Yes Carl Norum, compile. – Pedro A. Jul 24 '13 at 20:59
  • I see that, but arrays are still non-modifiabl lvalues, right? A quick test of your code here gave me several errors. – Carl Norum Jul 24 '13 at 20:59
  • Excuse, Carl Norum, replace the line: float a[max][max],aux_a[max][max]; by float **a,**aux_a; – Pedro A. Jul 24 '13 at 21:11
  • ... that makes more sense. Please show your correct code in the future. – Carl Norum Jul 24 '13 at 21:30
  • MPI does not support 2D arrays where each row is allocated separately. See [here](http://stackoverflow.com/a/5901671/1374437) for the proper way to treat 2D arrays in MPI programs. – Hristo Iliev Jul 24 '13 at 21:39
  • Thanks Hristo lliev, the link helped I solve the problem. – Pedro A. Jul 24 '13 at 23:43
  • Welcome to Stack Overflow. Please read the [About] page soon. FWIW: when preparing code for pasting into SO, (a) do not use tabs, (b) usually use 4 space indentation, and (c) paste the code into the edit box, then select it, then use the **`{}`** button above the edit box to indent it all 4 spaces so it shows as code. That should preserve your indentation. Avoid tabs; they just make life unnecessarily hard. – Jonathan Leffler Jul 24 '13 at 23:49

0 Answers0