如何分配更大的矩阵?

时间:2013-07-24 20:50:36

标签: c mpi dynamic-memory-allocation

我是编程MPI的新手,我的困难是动态分配更大的矩阵。出现的问题是:

行命令:

$ 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).
--------------------------------------------------------------------------
$

代码:

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

你能帮助我吗?

0 个答案:

没有答案